|
@@ -2,7 +2,6 @@ package org.ssssssss.magicapi.modules;
|
|
|
|
|
|
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
|
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
|
import org.springframework.jdbc.core.RowMapper;
|
|
import org.springframework.jdbc.core.RowMapper;
|
|
-import org.springframework.jdbc.datasource.DataSourceUtils;
|
|
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
|
import org.springframework.jdbc.support.KeyHolder;
|
|
import org.springframework.jdbc.support.KeyHolder;
|
|
import org.ssssssss.magicapi.adapter.ColumnMapperAdapter;
|
|
import org.ssssssss.magicapi.adapter.ColumnMapperAdapter;
|
|
@@ -12,7 +11,6 @@ import org.ssssssss.magicapi.config.MagicDynamicDataSource;
|
|
import org.ssssssss.magicapi.config.MagicDynamicDataSource.DataSourceNode;
|
|
import org.ssssssss.magicapi.config.MagicDynamicDataSource.DataSourceNode;
|
|
import org.ssssssss.magicapi.config.MagicModule;
|
|
import org.ssssssss.magicapi.config.MagicModule;
|
|
import org.ssssssss.magicapi.dialect.Dialect;
|
|
import org.ssssssss.magicapi.dialect.Dialect;
|
|
-import org.ssssssss.magicapi.exception.MagicAPIException;
|
|
|
|
import org.ssssssss.magicapi.model.Page;
|
|
import org.ssssssss.magicapi.model.Page;
|
|
import org.ssssssss.magicapi.provider.PageProvider;
|
|
import org.ssssssss.magicapi.provider.PageProvider;
|
|
import org.ssssssss.magicapi.provider.ResultProvider;
|
|
import org.ssssssss.magicapi.provider.ResultProvider;
|
|
@@ -20,7 +18,6 @@ import org.ssssssss.script.MagicScriptContext;
|
|
import org.ssssssss.script.annotation.Comment;
|
|
import org.ssssssss.script.annotation.Comment;
|
|
import org.ssssssss.script.annotation.UnableCall;
|
|
import org.ssssssss.script.annotation.UnableCall;
|
|
|
|
|
|
-import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.Statement;
|
|
import java.sql.Statement;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
@@ -171,19 +168,6 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
return new Transaction(this.dataSourceNode.getDataSourceTransactionManager());
|
|
return new Transaction(this.dataSourceNode.getDataSourceTransactionManager());
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- * 添加至缓存
|
|
|
|
- *
|
|
|
|
- * @param value 缓存名
|
|
|
|
- */
|
|
|
|
- @UnableCall
|
|
|
|
- private <T> T putCacheValue(T value, BoundSql boundSql) {
|
|
|
|
- if (this.cacheName != null) {
|
|
|
|
- this.sqlCache.put(this.cacheName, boundSql.getCacheKey(this.sqlCache), value, this.ttl);
|
|
|
|
- }
|
|
|
|
- return value;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
|
|
* 使用缓存
|
|
* 使用缓存
|
|
*
|
|
*
|
|
@@ -266,9 +250,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
*/
|
|
*/
|
|
@Comment("查询SQL,返回List类型结果")
|
|
@Comment("查询SQL,返回List类型结果")
|
|
public List<Map<String, Object>> select(@Comment("`SQL`语句") String sql) {
|
|
public List<Map<String, Object>> select(@Comment("`SQL`语句") String sql) {
|
|
- BoundSql boundSql = new BoundSql(sql);
|
|
+ BoundSql boundSql = new BoundSql(sql, this.sqlCache, this.cacheName, this.ttl);
|
|
- return (List<Map<String, Object>>) boundSql.getCacheValue(this.sqlCache, this.cacheName)
|
|
+ return boundSql.getCacheValue(() -> dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters()));
|
|
- .orElseGet(() -> putCacheValue(dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters()), boundSql));
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -284,70 +267,6 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
return value;
|
|
return value;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- * 如果已存在就修改,否则增加
|
|
|
|
- */
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
|
|
* 插入并返回主键
|
|
* 插入并返回主键
|
|
*/
|
|
*/
|
|
@@ -384,27 +303,14 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
*/
|
|
*/
|
|
@Comment("执行分页查询,分页条件手动传入")
|
|
@Comment("执行分页查询,分页条件手动传入")
|
|
public Object page(@Comment("`SQL`语句") String sql, @Comment("限制条数") long limit, @Comment("跳过条数") long offset) {
|
|
public Object page(@Comment("`SQL`语句") String sql, @Comment("限制条数") long limit, @Comment("跳过条数") long offset) {
|
|
- BoundSql boundSql = new BoundSql(sql);
|
|
+ BoundSql boundSql = new BoundSql(sql, this.sqlCache, this.cacheName, this.ttl);
|
|
- Connection connection = null;
|
|
+ Dialect dialect = dataSourceNode.getDialect(dialectAdapter);
|
|
- Dialect dialect;
|
|
+ String countSql = dialect.getCountSql(boundSql.getSql());
|
|
- try {
|
|
+ int count = boundSql.getCacheValue(countSql, boundSql.getParameters(), () -> dataSourceNode.getJdbcTemplate().queryForObject(countSql, Integer.class, boundSql.getParameters()));
|
|
- connection = dataSourceNode.getJdbcTemplate().getDataSource().getConnection();
|
|
+ List<Map<String, Object>> list = null;
|
|
- dialect = dialectAdapter.getDialectFromUrl(connection.getMetaData().getURL());
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- throw new MagicAPIException("自动获取数据库方言失败", e);
|
|
|
|
- } finally {
|
|
|
|
- DataSourceUtils.releaseConnection(connection, dataSourceNode.getJdbcTemplate().getDataSource());
|
|
|
|
- }
|
|
|
|
- if (dialect == null) {
|
|
|
|
- throw new MagicAPIException("自动获取数据库方言失败");
|
|
|
|
- }
|
|
|
|
- int count = (int) boundSql.getCacheValue(this.sqlCache, this.cacheName)
|
|
|
|
- .orElseGet(() -> putCacheValue(dataSourceNode.getJdbcTemplate().queryForObject(dialect.getCountSql(boundSql.getSql()), Integer.class, boundSql.getParameters()), boundSql));
|
|
|
|
- List<Object> list = null;
|
|
|
|
if (count > 0) {
|
|
if (count > 0) {
|
|
String pageSql = dialect.getPageSql(boundSql.getSql(), boundSql, offset, limit);
|
|
String pageSql = dialect.getPageSql(boundSql.getSql(), boundSql, offset, limit);
|
|
- list = (List<Object>) boundSql.removeCacheKey().getCacheValue(this.sqlCache, this.cacheName)
|
|
+ list = boundSql.getCacheValue(pageSql, boundSql.getParameters(), () -> dataSourceNode.getJdbcTemplate().query(pageSql, this.columnMapRowMapper, boundSql.getParameters()));
|
|
- .orElseGet(() -> putCacheValue(dataSourceNode.getJdbcTemplate().query(pageSql, this.columnMapRowMapper, boundSql.getParameters()), boundSql));
|
|
|
|
}
|
|
}
|
|
return resultProvider.buildPageResult(count, list);
|
|
return resultProvider.buildPageResult(count, list);
|
|
}
|
|
}
|
|
@@ -414,9 +320,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
*/
|
|
*/
|
|
@Comment("查询int值,适合单行单列int的结果")
|
|
@Comment("查询int值,适合单行单列int的结果")
|
|
public Integer selectInt(@Comment("`SQL`语句") String sql) {
|
|
public Integer selectInt(@Comment("`SQL`语句") String sql) {
|
|
- BoundSql boundSql = new BoundSql(sql);
|
|
+ BoundSql boundSql = new BoundSql(sql, this.sqlCache, this.cacheName, this.ttl);
|
|
- return (Integer) boundSql.getCacheValue(this.sqlCache, this.cacheName)
|
|
+ return boundSql.getCacheValue(() -> dataSourceNode.getJdbcTemplate().queryForObject(boundSql.getSql(), boundSql.getParameters(), Integer.class));
|
|
- .orElseGet(() -> putCacheValue(dataSourceNode.getJdbcTemplate().queryForObject(boundSql.getSql(), boundSql.getParameters(), Integer.class), boundSql));
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -424,12 +329,11 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
*/
|
|
*/
|
|
@Comment("查询单条结果,查不到返回null")
|
|
@Comment("查询单条结果,查不到返回null")
|
|
public Map<String, Object> selectOne(@Comment("`SQL`语句") String sql) {
|
|
public Map<String, Object> selectOne(@Comment("`SQL`语句") String sql) {
|
|
- BoundSql boundSql = new BoundSql(sql);
|
|
+ BoundSql boundSql = new BoundSql(sql, this.sqlCache, this.cacheName, this.ttl);
|
|
- return (Map<String, Object>) boundSql.getCacheValue(this.sqlCache, this.cacheName)
|
|
+ return boundSql.getCacheValue(() -> {
|
|
- .orElseGet(() -> {
|
|
+ List<Map<String, Object>> list = dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters());
|
|
- List<Map<String, Object>> list = dataSourceNode.getJdbcTemplate().query(boundSql.getSql(), this.columnMapRowMapper, boundSql.getParameters());
|
|
+ return list != null && list.size() > 0 ? list.get(0) : null;
|
|
- return list != null && list.size() > 0 ? list.get(0) : null;
|
|
+ });
|
|
- });
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -437,9 +341,8 @@ public class SQLModule extends HashMap<String, SQLModule> implements MagicModule
|
|
*/
|
|
*/
|
|
@Comment("查询单行单列的值")
|
|
@Comment("查询单行单列的值")
|
|
public Object selectValue(@Comment("`SQL`语句") String sql) {
|
|
public Object selectValue(@Comment("`SQL`语句") String sql) {
|
|
- BoundSql boundSql = new BoundSql(sql);
|
|
+ BoundSql boundSql = new BoundSql(sql, this.sqlCache, this.cacheName, this.ttl);
|
|
- return boundSql.getCacheValue(this.sqlCache, this.cacheName)
|
|
+ return boundSql.getCacheValue(() -> dataSourceNode.getJdbcTemplate().queryForObject(boundSql.getSql(), boundSql.getParameters(), Object.class));
|
|
- .orElseGet(() -> putCacheValue(dataSourceNode.getJdbcTemplate().queryForObject(boundSql.getSql(), boundSql.getParameters(), Object.class), boundSql));
|
|
|
|
}
|
|
}
|
|
|
|
|
|
@UnableCall
|
|
@UnableCall
|