|
@@ -1,5 +1,6 @@
|
|
|
package org.ssssssss.executor;
|
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
|
@@ -9,6 +10,7 @@ import org.springframework.jdbc.datasource.DataSourceUtils;
|
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
|
import org.springframework.jdbc.support.JdbcUtils;
|
|
|
import org.springframework.jdbc.support.KeyHolder;
|
|
|
+import org.ssssssss.cache.SqlCache;
|
|
|
import org.ssssssss.context.RequestContext;
|
|
|
import org.ssssssss.dialect.Dialect;
|
|
|
import org.ssssssss.dialect.DialectUtils;
|
|
@@ -17,6 +19,7 @@ import org.ssssssss.exception.S8Exception;
|
|
|
import org.ssssssss.provider.KeyProvider;
|
|
|
import org.ssssssss.scripts.SqlNode;
|
|
|
import org.ssssssss.session.DynamicDataSource;
|
|
|
+import org.ssssssss.session.ExecuteSqlStatement;
|
|
|
import org.ssssssss.session.SqlStatement;
|
|
|
import org.ssssssss.utils.Assert;
|
|
|
import org.ssssssss.utils.DomUtils;
|
|
@@ -38,16 +41,21 @@ public class SqlExecutor {
|
|
|
private DynamicDataSource dynamicDataSource;
|
|
|
|
|
|
private ColumnMapRowMapper columnMapRowMapper = new ColumnMapRowMapper();
|
|
|
- ;
|
|
|
|
|
|
private Map<String, KeyProvider> keyProviders = new HashMap<>();
|
|
|
|
|
|
private Map<String, Dialect> cachedDialects = new ConcurrentHashMap<>();
|
|
|
|
|
|
+ private SqlCache sqlCache;
|
|
|
+
|
|
|
public SqlExecutor(DynamicDataSource dynamicDataSource) {
|
|
|
this.dynamicDataSource = dynamicDataSource;
|
|
|
}
|
|
|
|
|
|
+ public void setSqlCache(SqlCache sqlCache) {
|
|
|
+ this.sqlCache = sqlCache;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 设置是否是驼峰命名
|
|
|
*
|
|
@@ -86,41 +94,70 @@ public class SqlExecutor {
|
|
|
|
|
|
/**
|
|
|
* 执行SQL
|
|
|
- *
|
|
|
- * @param mode SQL模式
|
|
|
- * @param sql SQL
|
|
|
- * @param parameters SQL参数
|
|
|
- * @param returnType 返回值类型
|
|
|
- * @return
|
|
|
*/
|
|
|
- public Object execute(String dataSourceName, SqlMode mode, String sql, Object[] parameters, Class<?> returnType) {
|
|
|
- JdbcTemplate jdbcTemplate = getJdbcTemplate(dataSourceName);
|
|
|
- printLog(dataSourceName, sql, parameters);
|
|
|
- if (SqlMode.SELECT_LIST == mode) {
|
|
|
+ public Object execute(ExecuteSqlStatement statement) {
|
|
|
+ // 获取SQL
|
|
|
+ String sql = statement.getSql();
|
|
|
+ // 获取参数
|
|
|
+ Object[] parameters = statement.getParameters();
|
|
|
+ // 获取SQL模式
|
|
|
+ SqlMode mode = statement.getSqlMode();
|
|
|
+ // 获取返回值类型
|
|
|
+ Class<?> returnType = statement.getReturnType();
|
|
|
+ // 缓存Key
|
|
|
+ String sqlCacheKey = null;
|
|
|
+ // 返回值
|
|
|
+ Object value;
|
|
|
+ // 判断是否使用缓存
|
|
|
+ if (this.sqlCache != null && StringUtils.isNotBlank(statement.getUseCache())) {
|
|
|
+ // 构建key
|
|
|
+ sqlCacheKey = this.sqlCache.buildSqlCacheKey(sql, parameters);
|
|
|
+ // 查询缓存
|
|
|
+ value = this.sqlCache.get(statement.getUseCache(), sqlCacheKey);
|
|
|
+ if (value != null) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ JdbcTemplate jdbcTemplate = getJdbcTemplate(statement.getDataSourceName());
|
|
|
+ // 打印SQL日志
|
|
|
+ printLog(statement.getDataSourceName(), sql, parameters);
|
|
|
+ if (SqlMode.SELECT_LIST == mode) { //查询List
|
|
|
if (returnType == null || returnType == Map.class) {
|
|
|
- return jdbcTemplate.query(sql, parameters, columnMapRowMapper);
|
|
|
+ value = jdbcTemplate.query(sql, parameters, columnMapRowMapper);
|
|
|
+ } else {
|
|
|
+ value = jdbcTemplate.queryForList(sql, parameters, returnType);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else if (SqlMode.UPDATE == mode || SqlMode.INSERT == mode || SqlMode.DELETE == mode) { //增删改
|
|
|
+ int retVal = jdbcTemplate.update(sql, parameters);
|
|
|
+ // 删除缓存
|
|
|
+ if (retVal > 0 && this.sqlCache != null && StringUtils.isNotBlank(statement.getDeleteCache())) {
|
|
|
+ this.sqlCache.remove(statement.getDeleteCache());
|
|
|
}
|
|
|
- return jdbcTemplate.queryForList(sql, parameters, returnType);
|
|
|
- } else if (SqlMode.UPDATE == mode || SqlMode.INSERT == mode || SqlMode.DELETE == mode) {
|
|
|
- int value = jdbcTemplate.update(sql, parameters);
|
|
|
// 当设置返回值是boolean类型时,做>0比较
|
|
|
if (returnType == Boolean.class) {
|
|
|
- return value > 0;
|
|
|
+ return retVal > 0;
|
|
|
}
|
|
|
- return value;
|
|
|
- } else if (SqlMode.SELECT_ONE == mode) {
|
|
|
+ return retVal;
|
|
|
+ } else if (SqlMode.SELECT_ONE == mode) { //查询一条
|
|
|
Collection collection;
|
|
|
if (returnType == null || returnType == Map.class) {
|
|
|
collection = jdbcTemplate.query(sql, columnMapRowMapper, parameters);
|
|
|
} else {
|
|
|
collection = jdbcTemplate.queryForList(sql, returnType, parameters);
|
|
|
}
|
|
|
- return collection != null && collection.size() >= 1 ? collection.iterator().next() : null;
|
|
|
+ value = collection != null && collection.size() >= 1 ? collection.iterator().next() : null;
|
|
|
} else {
|
|
|
throw new S8Exception("暂时不支持[" + mode + "]模式");
|
|
|
}
|
|
|
+ // 判断是否使用了缓存
|
|
|
+ if (sqlCacheKey != null && value != null) {
|
|
|
+ this.sqlCache.put(statement.getUseCache(), sqlCacheKey, value);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
public Object executeInsertWithPk(SqlStatement statement, RequestContext requestContext) throws SQLException {
|
|
|
String dataSourceName = statement.getDataSourceName();
|
|
|
JdbcTemplate jdbcTemplate = getJdbcTemplate(dataSourceName);
|
|
@@ -148,7 +185,8 @@ public class SqlExecutor {
|
|
|
// 获取插入SQL
|
|
|
String insertSQL = statement.getSqlNode().getSql(requestContext);
|
|
|
// 执行插入
|
|
|
- executeUpdate(dataSourceName, connection, insertSQL, requestContext.getParameters());
|
|
|
+ executeUpdate(dataSourceName, connection, insertSQL, requestContext.getParameters(),statement.getDeleteCache());
|
|
|
+
|
|
|
// 清空参数
|
|
|
requestContext.getParameters().clear();
|
|
|
if (!before) {
|
|
@@ -166,7 +204,7 @@ public class SqlExecutor {
|
|
|
// 获取插入SQL
|
|
|
String insertSQL = statement.getSqlNode().getSql(requestContext);
|
|
|
// 执行插入
|
|
|
- executeUpdate(dataSourceName, connection, insertSQL, requestContext.getParameters());
|
|
|
+ executeUpdate(dataSourceName, connection, insertSQL, requestContext.getParameters(),statement.getDeleteCache());
|
|
|
}
|
|
|
return value;
|
|
|
} finally {
|
|
@@ -188,13 +226,17 @@ public class SqlExecutor {
|
|
|
/**
|
|
|
* 执行插入
|
|
|
*/
|
|
|
- private int executeUpdate(String dataSourceName, Connection connection, String sql, List<Object> parameters) throws SQLException {
|
|
|
+ private int executeUpdate(String dataSourceName, Connection connection, String sql, List<Object> parameters,String deleteCache) throws SQLException {
|
|
|
PreparedStatement ps = null;
|
|
|
try {
|
|
|
printLog(dataSourceName, sql, parameters);
|
|
|
ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
|
|
new ArgumentPreparedStatementSetter(parameters.toArray()).setValues(ps);
|
|
|
- return ps.executeUpdate();
|
|
|
+ int val = ps.executeUpdate();
|
|
|
+ if (this.sqlCache != null && StringUtils.isNotBlank(deleteCache)) {
|
|
|
+ this.sqlCache.remove(deleteCache);
|
|
|
+ }
|
|
|
+ return val;
|
|
|
} finally {
|
|
|
JdbcUtils.closeStatement(ps);
|
|
|
}
|
|
@@ -254,7 +296,7 @@ public class SqlExecutor {
|
|
|
* 获取数据库方言
|
|
|
*/
|
|
|
public Dialect getDialect(String dataSourceName) throws SQLException {
|
|
|
- Dialect dialect = cachedDialects.get(cachedDialects);
|
|
|
+ Dialect dialect = cachedDialects.get(dataSourceName);
|
|
|
if (dialect == null && !cachedDialects.containsKey(dataSourceName)) {
|
|
|
JdbcTemplate jdbcTemplate = getJdbcTemplate(dataSourceName);
|
|
|
Connection connection = jdbcTemplate.getDataSource().getConnection();
|