@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),})
public class SqlParser implements Interceptor {
public static int limit=1000;
public static boolean openInterceptor = false;
public SqlParser() {
//标识使用了该插件
setOpenInterceptor(true);
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameterObject = args[1];
BoundSql boundSql = ms.getBoundSql(parameterObject);
String sql = boundSql.getSql();
MySqlStatementParser mySqlStatementParser = new MySqlStatementParser(sql);
SQLStatement statement = mySqlStatementParser.parseStatement();
SQLSelect selectQuery = ((SQLSelectStatement)statement).getSelect();
MySqlSelectQueryBlock sqlSelectQuery = (MySqlSelectQueryBlock)selectQuery.getQuery();
Object limits = sqlSelectQuery.getLimit();
if(null == limits){
StringBuilder sb = new StringBuilder(sqlSelectQuery.toString());
sb.append(" limit 0,").append(limit);
BoundSql bs = new BoundSql(ms.getConfiguration(),sb.toString(),boundSql.getParameterMappings(),parameterObject);
MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(bs));
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
bs.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
args[0] = newMs;
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public boolean isOpenInterceptor() {
return openInterceptor;
}
public void setOpenInterceptor(boolean openInterceptor) {
this.openInterceptor = openInterceptor;
}
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
public static class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
}