use of indi.mybatis.flying.models.Conditionable in project mybatis.flying by limeng32.
the class EnhancedCachingInterceptor method createCacheKey.
private CacheKey createCacheKey(MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, BoundSql boundSql, Executor executor) {
MetaObject metaObject = MetaObject.forObject(parameter, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
/* 当需要分页查询时,将page参数里的当前页和每页数加到cachekey里 */
if (metaObject.getOriginalObject() instanceof Conditionable) {
CacheKey cacheKey = new CacheKey();
cacheKey.update(mappedStatement.getId());
cacheKey.update(rowBounds.getOffset());
cacheKey.update(rowBounds.getLimit());
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
cacheKey.update(boundSql.getSql());
if (parameterMappings.size() > 0 && parameter != null) {
TypeHandlerRegistry typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameter.getClass())) {
cacheKey.update(parameter);
} else {
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
cacheKey.update(metaObject.getValue(propertyName));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
cacheKey.update(boundSql.getAdditionalParameter(propertyName));
}
}
}
}
Sortable sorter = ((Conditionable) metaObject.getOriginalObject()).getSorter();
if (sorter != null) {
cacheKey.update(sorter.toSql());
}
Limitable limiter = ((Conditionable) metaObject.getOriginalObject()).getLimiter();
if (limiter != null) {
cacheKey.update(limiter.getPageNo());
cacheKey.update(limiter.getPageSize());
}
return cacheKey;
} else {
return executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
}
}
use of indi.mybatis.flying.models.Conditionable 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