use of org.apache.ibatis.executor.statement.RoutingStatementHandler in project mybatis-3 by mybatis.
the class Configuration method newStatementHandler.
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
return statementHandler;
}
use of org.apache.ibatis.executor.statement.RoutingStatementHandler in project mybatis.flying by limeng32.
the class AutoMapperInterceptor method intercept.
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = getRealObj(statementHandler);
String originalSql = (String) metaStatementHandler.getValue(DELEGATE_BOUNDSQL_SQL);
Configuration configuration = (Configuration) metaStatementHandler.getValue(DELEGATE_CONFIGURATION);
Object parameterObject = metaStatementHandler.getValue(DELEGATE_BOUNDSQL_PARAMETEROBJECT);
MappedStatement mappedStatement = (MappedStatement) metaStatementHandler.getValue(DELEGATE_MAPPEDSTATEMENT);
FlyingModel flyingModel = FlyingManager.fetchFlyingFeatureNew(originalSql, configuration, mappedStatement);
if (flyingModel.isHasFlyingFeature()) {
boolean needHandleLimiterAndSorter = false;
String newSql = null;
switch(flyingModel.getActionType()) {
case COUNT:
newSql = SqlBuilder.buildCountSql(parameterObject, flyingModel);
break;
case DELETE:
newSql = SqlBuilder.buildDeleteSql(parameterObject);
break;
case INSERT:
newSql = SqlBuilder.buildInsertSql(parameterObject, flyingModel);
break;
case INSERT_BATCH:
newSql = SqlBuilder.buildInsertBatchSql(parameterObject, flyingModel);
break;
case SELECT:
newSql = SqlBuilder.buildSelectSql(mappedStatement.getResultMaps().get(0).getType(), flyingModel);
break;
case SELECT_ALL:
newSql = SqlBuilder.buildSelectAllSql(parameterObject, flyingModel);
needHandleLimiterAndSorter = true;
break;
case SELECT_ONE:
newSql = SqlBuilder.buildSelectOneSql(parameterObject, flyingModel);
needHandleLimiterAndSorter = true;
break;
case UPDATE:
newSql = SqlBuilder.buildUpdateSql(parameterObject, flyingModel);
break;
case UPDATE_BATCH:
newSql = SqlBuilder.buildUpdateBatchSql(parameterObject, flyingModel);
break;
case UPDATE_PERSISTENT:
newSql = SqlBuilder.buildUpdatePersistentSql(parameterObject, flyingModel);
break;
default:
break;
}
if (!LogLevel.NONE.equals(logLevel)) {
log(logger, logLevel, new StringBuilder("Auto generated sql: ").append(newSql).toString());
}
SqlSource sqlSource = buildSqlSource(configuration, newSql, parameterObject.getClass());
List<ParameterMapping> parameterMappings = sqlSource.getBoundSql(parameterObject).getParameterMappings();
String sqlToexecute = sqlSource.getBoundSql(parameterObject).getSql();
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_PARAMETERMAPPINGS, parameterMappings);
/* Start dealing with paging */
if (needHandleLimiterAndSorter && (parameterObject instanceof Conditionable) && (invocation.getTarget() instanceof RoutingStatementHandler)) {
Conditionable condition = (Conditionable) parameterObject;
if (condition.getLimiter() != null) {
BoundSql boundSql = statementHandler.getBoundSql();
// The total count needs to be identified when encountering "selectAll"
if (ActionType.SELECT_ALL.equals(flyingModel.getActionType())) {
Connection connection = (Connection) invocation.getArgs()[0];
String countSql = new StringBuilder("select count(0) from (").append(sqlToexecute).append(") myCount").toString();
PreparedStatement countStmt = connection.prepareStatement(countSql);
BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(), countSql, boundSql.getParameterMappings(), parameterObject);
setParameters(countStmt, mappedStatement, countBS, parameterObject);
ResultSet rs = countStmt.executeQuery();
try {
int count = 0;
if (rs.next()) {
count = rs.getInt(1);
}
condition.getLimiter().setTotalCount(count);
} finally {
rs.close();
countStmt.close();
}
}
sqlToexecute = generatePageSql(sqlToexecute, condition);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
} else if (condition.getSorter() != null) {
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
sqlToexecute = generatePageSql(sql, condition);
metaStatementHandler.setValue(DELEGATE_BOUNDSQL_SQL, sqlToexecute);
}
}
if (loggerDescriptionHandler != null) {
LogLevel loggerLevel = loggerDescriptionHandler.getLogLevel(flyingModel.getId());
if (!LogLevel.NONE.equals(loggerLevel)) {
BoundSql boundSqlTemp = statementHandler.getBoundSql();
String sqlTemp = boundSqlTemp.getSql();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Method: ").append(flyingModel.getId()).append("\r\n");
stringBuilder.append("Bound sql: ").append(sqlTemp).append("\r\n");
if (ActionType.SELECT.equals(flyingModel.getActionType())) {
stringBuilder.append("Bound value: {").append(parameterMappings.get(0).getProperty()).append("=").append(parameterObject).append("};");
} else {
MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject);
stringBuilder.append("Bound value: {");
boolean b = true;
for (ParameterMapping parameterMapping : parameterMappings) {
if (parameterMapping.getMode() != ParameterMode.OUT) {
Object value;
String propertyName = parameterMapping.getProperty();
value = metaObject == null ? null : metaObject.getValue(propertyName);
if (b) {
b = false;
} else {
stringBuilder.append(", ");
}
stringBuilder.append(propertyName).append("=").append(value);
}
}
stringBuilder.append("};");
}
log(logger, loggerLevel, stringBuilder.toString());
}
}
} else if (loggerDescriptionHandler != null) {
LogLevel loggerLevel = loggerDescriptionHandler.getLogLevel(mappedStatement.getId());
if (!LogLevel.NONE.equals(loggerLevel)) {
String sqlTemp = statementHandler.getBoundSql().getSql();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Method: ").append(mappedStatement.getId()).append("\r\n");
stringBuilder.append("Bound sql: ").append(sqlTemp).append("\r\n");
stringBuilder.append("Bound value: ").append(parameterObject).append(";");
log(logger, loggerLevel, stringBuilder.toString());
}
}
/* Pass to the next interceptor. */
return invocation.proceed();
}
Aggregations