CrispDbKit相关代码结构调整
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public class BatchResult {
|
||||
|
||||
private final MappedStatement mappedStatement;
|
||||
private final String sql;
|
||||
private final List<Object> parameterObjects;
|
||||
|
||||
private int[] updateCounts;
|
||||
|
||||
public BatchResult(MappedStatement mappedStatement, String sql) {
|
||||
super();
|
||||
this.mappedStatement = mappedStatement;
|
||||
this.sql = sql;
|
||||
this.parameterObjects = new ArrayList<>();
|
||||
}
|
||||
|
||||
public BatchResult(MappedStatement mappedStatement, String sql, Object parameterObject) {
|
||||
this(mappedStatement, sql);
|
||||
addParameterObject(parameterObject);
|
||||
}
|
||||
|
||||
public MappedStatement getMappedStatement() {
|
||||
return mappedStatement;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Object getParameterObject() {
|
||||
return parameterObjects.get(0);
|
||||
}
|
||||
|
||||
public List<Object> getParameterObjects() {
|
||||
return parameterObjects;
|
||||
}
|
||||
|
||||
public int[] getUpdateCounts() {
|
||||
return updateCounts;
|
||||
}
|
||||
|
||||
public void setUpdateCounts(int[] updateCounts) {
|
||||
this.updateCounts = updateCounts;
|
||||
}
|
||||
|
||||
public void addParameterObject(Object parameterObject) {
|
||||
this.parameterObjects.add(parameterObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,89 +21,13 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import fun.asgc.neutrino.core.util.LockUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 清爽的数据库工具
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
public class CrispDbKit {
|
||||
/**
|
||||
* 数据源缓存
|
||||
*/
|
||||
private static Map<String, CrispDbConfig> crispDbConfigMap = new HashMap<>();
|
||||
/**
|
||||
* 数据库操作实例
|
||||
*/
|
||||
private static Map<String, CrispDbExecutor> crispDbExecutorMap = new HashMap<>();
|
||||
/**
|
||||
* 默认数据库的key
|
||||
*/
|
||||
private static final String DEFAULT_KEY = "default";
|
||||
private static final JdbcManager manager = new JdbcManager();
|
||||
|
||||
/**
|
||||
* 设置默认数据库配置
|
||||
* @param config 数据库配置
|
||||
*/
|
||||
public static void setConfig(CrispDbConfig config) {
|
||||
setConfig(DEFAULT_KEY, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据库配置
|
||||
* @param key 键
|
||||
* @param config 数据库配置
|
||||
*/
|
||||
public static void setConfig(String key, CrispDbConfig config) {
|
||||
crispDbConfigMap.put(key, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名称获取数据库配置
|
||||
* @param key 键
|
||||
* @return 数据库配置
|
||||
*/
|
||||
private static CrispDbConfig getConfig(String key) {
|
||||
return crispDbConfigMap.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认数据库操作实例
|
||||
* @return
|
||||
*/
|
||||
private static CrispDbExecutor getDbExecutor() {
|
||||
return getDbExecutor(DEFAULT_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库操作实例
|
||||
* @param key 键
|
||||
* @return 数据库操作实例
|
||||
*/
|
||||
private static CrispDbExecutor getDbExecutor(String key) {
|
||||
return LockUtil.doubleCheckProcessForNoException(
|
||||
() -> !crispDbExecutorMap.containsKey(key),
|
||||
key,
|
||||
() -> {
|
||||
CrispDbConfig config = getConfig(key);
|
||||
if (null != config) {
|
||||
crispDbExecutorMap.put(key, new CrispDbExecutor(config));
|
||||
}
|
||||
},
|
||||
() -> crispDbExecutorMap.get(crispDbExecutorMap)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换数据源
|
||||
* @param name 数据源名称
|
||||
* @return 数据库操作实例
|
||||
*/
|
||||
public static CrispDbExecutor use(String name) {
|
||||
return getDbExecutor(name);
|
||||
}
|
||||
}
|
||||
|
||||
+23
-5
@@ -27,7 +27,7 @@ import javax.sql.DataSource;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
public class CrispDbConfig {
|
||||
public class DbConfig {
|
||||
/**
|
||||
* 数据库名称
|
||||
*/
|
||||
@@ -44,23 +44,41 @@ public class CrispDbConfig {
|
||||
* 是否开启调试模式
|
||||
*/
|
||||
private Boolean debugMode;
|
||||
/**
|
||||
* 上下文
|
||||
*/
|
||||
private DbContext context;
|
||||
/**
|
||||
* 数据库执行器
|
||||
*/
|
||||
private DbExecutor dbExecutor;
|
||||
|
||||
public CrispDbConfig(String name, IDataSourceProvider dataSourceProvider) {
|
||||
public DbConfig(String name, IDataSourceProvider dataSourceProvider) {
|
||||
this.name = name;
|
||||
this.dataSourceProvider = dataSourceProvider;
|
||||
this.showSql = Boolean.FALSE;
|
||||
this.debugMode = Boolean.FALSE;
|
||||
this.context = new DbContext(this);
|
||||
this.dbExecutor = new DbExecutor(this);
|
||||
}
|
||||
|
||||
public CrispDbConfig(String name, DataSource dataSource) {
|
||||
public DbConfig(String name, DataSource dataSource) {
|
||||
this(name, new DefaultDataSourceProvider(dataSource));
|
||||
}
|
||||
|
||||
public CrispDbConfig(IDataSourceProvider dataSourceProvider) {
|
||||
public DbConfig(IDataSourceProvider dataSourceProvider) {
|
||||
this("unknown", dataSourceProvider);
|
||||
}
|
||||
|
||||
public CrispDbConfig(DataSource dataSource) {
|
||||
public DbConfig(DataSource dataSource) {
|
||||
this(new DefaultDataSourceProvider(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库操作实例
|
||||
* @return 数据库操作实例
|
||||
*/
|
||||
public DbExecutor getDbExecutor() {
|
||||
return dbExecutor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
public class DbContext {
|
||||
private DbConfig dbConfig;
|
||||
private final ThreadLocal<Connection> connectionHolder = new ThreadLocal<>();
|
||||
|
||||
public DbContext(DbConfig dbConfig) {
|
||||
this.dbConfig = dbConfig;
|
||||
}
|
||||
public SqlSessionFactory getSqlSessionFactory() {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+15
-19
@@ -28,35 +28,31 @@ import java.util.List;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
class CrispDbExecutor implements CrispDbOperator {
|
||||
class DbExecutor implements DbOperator {
|
||||
/**
|
||||
* 配置
|
||||
*/
|
||||
private CrispDbConfig config;
|
||||
/**
|
||||
* 上下文
|
||||
*/
|
||||
private CrispDbContext context;
|
||||
private DbConfig config;
|
||||
|
||||
public CrispDbExecutor(CrispDbConfig config) {
|
||||
|
||||
public DbExecutor(DbConfig config) {
|
||||
this.config = config;
|
||||
this.context = new CrispDbContext();
|
||||
}
|
||||
|
||||
public CrispDbExecutor(IDataSourceProvider dataSourceProvider) {
|
||||
this(new CrispDbConfig(dataSourceProvider));
|
||||
public DbExecutor(IDataSourceProvider dataSourceProvider) {
|
||||
this(new DbConfig(dataSourceProvider));
|
||||
}
|
||||
|
||||
public CrispDbExecutor(String name, IDataSourceProvider dataSourceProvider) {
|
||||
this(new CrispDbConfig(name, dataSourceProvider));
|
||||
public DbExecutor(String name, IDataSourceProvider dataSourceProvider) {
|
||||
this(new DbConfig(name, dataSourceProvider));
|
||||
}
|
||||
|
||||
public CrispDbExecutor(DataSource dataSource) {
|
||||
this(new CrispDbConfig(dataSource));
|
||||
public DbExecutor(DataSource dataSource) {
|
||||
this(new DbConfig(dataSource));
|
||||
}
|
||||
|
||||
public CrispDbExecutor(String name, DataSource dataSource) {
|
||||
this(new CrispDbConfig(name, dataSource));
|
||||
public DbExecutor(String name, DataSource dataSource) {
|
||||
this(new DbConfig(name, dataSource));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -70,17 +66,17 @@ class CrispDbExecutor implements CrispDbOperator {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrispDbUpdateResult update(String sql, Object... params) {
|
||||
public DbUpdateResult update(String sql, Object... params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrispDbUpdateResult insert(String sql, Object... params) {
|
||||
public DbUpdateResult insert(String sql, Object... params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrispDbUpdateResult delete(String sql, Object... params) {
|
||||
public DbUpdateResult delete(String sql, Object... params) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -27,7 +27,7 @@ import java.util.List;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
public interface CrispDbOperator {
|
||||
public interface DbOperator {
|
||||
/**
|
||||
* 查询数据列表
|
||||
* @param resultType 结果类型
|
||||
@@ -54,7 +54,7 @@ public interface CrispDbOperator {
|
||||
* @param params 参数列表
|
||||
* @return 更新结果
|
||||
*/
|
||||
CrispDbUpdateResult update(String sql, Object... params);
|
||||
DbUpdateResult update(String sql, Object... params);
|
||||
|
||||
/**
|
||||
* 新增
|
||||
@@ -62,7 +62,7 @@ public interface CrispDbOperator {
|
||||
* @param params 参数列表
|
||||
* @return 新增结果
|
||||
*/
|
||||
CrispDbUpdateResult insert(String sql, Object... params);
|
||||
DbUpdateResult insert(String sql, Object... params);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
@@ -70,5 +70,5 @@ public interface CrispDbOperator {
|
||||
* @param params 参数列表
|
||||
* @return 删除结果
|
||||
*/
|
||||
CrispDbUpdateResult delete(String sql, Object... params);
|
||||
DbUpdateResult delete(String sql, Object... params);
|
||||
}
|
||||
+1
-1
@@ -25,6 +25,6 @@ package fun.asgc.neutrino.core.db.crisp;
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
*/
|
||||
public class CrispDbUpdateResult {
|
||||
public class DbUpdateResult {
|
||||
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public class DefaultSqlSessionFactory implements SqlSessionFactory {
|
||||
|
||||
@Override
|
||||
public SqlSession openSession() {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(boolean autoCommit) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(Connection connection) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(TransactionIsolationLevel level) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(SqlExecutorType execType) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(SqlExecutorType execType, boolean autoCommit) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(SqlExecutorType execType, TransactionIsolationLevel level) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SqlSession openSession(SqlExecutorType execType, Connection connection) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DbConfig getDbConfig() {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public class JdbcManager {
|
||||
/**
|
||||
* 数据源缓存
|
||||
*/
|
||||
private Map<String, DbConfig> dbConfigMap = new HashMap<>();
|
||||
/**
|
||||
* 默认数据库的key
|
||||
*/
|
||||
private final String DEFAULT_KEY = "default";
|
||||
|
||||
/**
|
||||
* 设置默认数据库配置
|
||||
* @param config 数据库配置
|
||||
*/
|
||||
public void setConfig(DbConfig config) {
|
||||
setConfig(DEFAULT_KEY, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据库配置
|
||||
* @param key 键
|
||||
* @param config 数据库配置
|
||||
*/
|
||||
public void setConfig(String key, DbConfig config) {
|
||||
dbConfigMap.put(key, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据名称获取数据库配置
|
||||
* @param key 键
|
||||
* @return 数据库配置
|
||||
*/
|
||||
public DbConfig getConfig(String key) {
|
||||
return dbConfigMap.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认数据库配置
|
||||
* @return 数据库配置
|
||||
*/
|
||||
public DbConfig getConfig() {
|
||||
return getConfig(DEFAULT_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认数据库操作实例
|
||||
* @return
|
||||
*/
|
||||
private DbExecutor getDbExecutor(String key) {
|
||||
DbConfig dbConfig = getConfig(key);
|
||||
return null == dbConfig ? null : dbConfig.getDbExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换数据源
|
||||
* @param name 数据源名称
|
||||
* @return 数据库操作实例
|
||||
*/
|
||||
public DbExecutor use(String name) {
|
||||
return getDbExecutor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换为默认数据源
|
||||
* @return 默认数据库操作实例
|
||||
*/
|
||||
public DbExecutor useDefault() {
|
||||
return getDbExecutor(DEFAULT_KEY);
|
||||
}
|
||||
}
|
||||
+3
-5
@@ -21,12 +21,10 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/3
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public class CrispDbContext {
|
||||
private final ThreadLocal<Connection> connectionHolder = new ThreadLocal<>();
|
||||
public class MappedStatement {
|
||||
// TODO
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public class RowBounds {
|
||||
|
||||
public static final int NO_ROW_OFFSET = 0;
|
||||
public static final int NO_ROW_LIMIT = Integer.MAX_VALUE;
|
||||
public static final RowBounds DEFAULT = new RowBounds();
|
||||
|
||||
private final int offset;
|
||||
private final int limit;
|
||||
|
||||
public RowBounds() {
|
||||
this.offset = NO_ROW_OFFSET;
|
||||
this.limit = NO_ROW_LIMIT;
|
||||
}
|
||||
|
||||
public RowBounds(int offset, int limit) {
|
||||
this.offset = offset;
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
public int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public enum SqlExecutorType {
|
||||
SIMPLE, REUSE, BATCH
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import sun.plugin2.main.server.ResultHandler;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public interface SqlSession extends Closeable {
|
||||
|
||||
/**
|
||||
* Retrieve a single row mapped from the statement key.
|
||||
* @param <T> the returned object type
|
||||
* @param statement
|
||||
* @return Mapped object
|
||||
*/
|
||||
<T> T selectOne(String statement);
|
||||
|
||||
/**
|
||||
* Retrieve a single row mapped from the statement key and parameter.
|
||||
* @param <T> the returned object type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @return Mapped object
|
||||
*/
|
||||
<T> T selectOne(String statement, Object parameter);
|
||||
|
||||
/**
|
||||
* Retrieve a list of mapped objects from the statement key and parameter.
|
||||
* @param <E> the returned list element type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @return List of mapped object
|
||||
*/
|
||||
<E> List<E> selectList(String statement);
|
||||
|
||||
/**
|
||||
* Retrieve a list of mapped objects from the statement key and parameter.
|
||||
* @param <E> the returned list element type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @return List of mapped object
|
||||
*/
|
||||
<E> List<E> selectList(String statement, Object parameter);
|
||||
|
||||
/**
|
||||
* Retrieve a list of mapped objects from the statement key and parameter,
|
||||
* within the specified row bounds.
|
||||
* @param <E> the returned list element type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @param rowBounds Bounds to limit object retrieval
|
||||
* @return List of mapped object
|
||||
*/
|
||||
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
|
||||
|
||||
/**
|
||||
* The selectMap is a special case in that it is designed to convert a list
|
||||
* of results into a Map based on one of the properties in the resulting
|
||||
* objects.
|
||||
* Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
|
||||
* @param <K> the returned Map keys type
|
||||
* @param <V> the returned Map values type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param mapKey The property to use as key for each value in the list.
|
||||
* @return Map containing key pair data.
|
||||
*/
|
||||
<K, V> Map<K, V> selectMap(String statement, String mapKey);
|
||||
|
||||
/**
|
||||
* The selectMap is a special case in that it is designed to convert a list
|
||||
* of results into a Map based on one of the properties in the resulting
|
||||
* objects.
|
||||
* @param <K> the returned Map keys type
|
||||
* @param <V> the returned Map values type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @param mapKey The property to use as key for each value in the list.
|
||||
* @return Map containing key pair data.
|
||||
*/
|
||||
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
|
||||
|
||||
/**
|
||||
* The selectMap is a special case in that it is designed to convert a list
|
||||
* of results into a Map based on one of the properties in the resulting
|
||||
* objects.
|
||||
* @param <K> the returned Map keys type
|
||||
* @param <V> the returned Map values type
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @param mapKey The property to use as key for each value in the list.
|
||||
* @param rowBounds Bounds to limit object retrieval
|
||||
* @return Map containing key pair data.
|
||||
*/
|
||||
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
|
||||
|
||||
/**
|
||||
* Retrieve a single row mapped from the statement key and parameter
|
||||
* using a {@code ResultHandler}.
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @param handler ResultHandler that will handle each retrieved row
|
||||
*/
|
||||
void select(String statement, Object parameter, ResultHandler handler);
|
||||
|
||||
/**
|
||||
* Retrieve a single row mapped from the statement
|
||||
* using a {@code ResultHandler}.
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param handler ResultHandler that will handle each retrieved row
|
||||
*/
|
||||
void select(String statement, ResultHandler handler);
|
||||
|
||||
/**
|
||||
* Retrieve a single row mapped from the statement key and parameter
|
||||
* using a {@code ResultHandler} and {@code RowBounds}.
|
||||
* @param statement Unique identifier matching the statement to use.
|
||||
* @param rowBounds RowBound instance to limit the query results
|
||||
* @param handler ResultHandler that will handle each retrieved row
|
||||
*/
|
||||
void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
|
||||
|
||||
/**
|
||||
* Execute an insert statement.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @return int The number of rows affected by the insert.
|
||||
*/
|
||||
int insert(String statement);
|
||||
|
||||
/**
|
||||
* Execute an insert statement with the given parameter object. Any generated
|
||||
* autoincrement values or selectKey entries will modify the given parameter
|
||||
* object properties. Only the number of rows affected will be returned.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @return int The number of rows affected by the insert.
|
||||
*/
|
||||
int insert(String statement, Object parameter);
|
||||
|
||||
/**
|
||||
* Execute an update statement. The number of rows affected will be returned.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @return int The number of rows affected by the update.
|
||||
*/
|
||||
int update(String statement);
|
||||
|
||||
/**
|
||||
* Execute an update statement. The number of rows affected will be returned.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @return int The number of rows affected by the update.
|
||||
*/
|
||||
int update(String statement, Object parameter);
|
||||
|
||||
/**
|
||||
* Execute a delete statement. The number of rows affected will be returned.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @return int The number of rows affected by the delete.
|
||||
*/
|
||||
int delete(String statement);
|
||||
|
||||
/**
|
||||
* Execute a delete statement. The number of rows affected will be returned.
|
||||
* @param statement Unique identifier matching the statement to execute.
|
||||
* @param parameter A parameter object to pass to the statement.
|
||||
* @return int The number of rows affected by the delete.
|
||||
*/
|
||||
int delete(String statement, Object parameter);
|
||||
|
||||
/**
|
||||
* Flushes batch statements and commits database connection.
|
||||
* Note that database connection will not be committed if no updates/deletes/inserts were called.
|
||||
* To force the commit call {@link SqlSession#commit(boolean)}
|
||||
*/
|
||||
void commit();
|
||||
|
||||
/**
|
||||
* Flushes batch statements and commits database connection.
|
||||
* @param force forces connection commit
|
||||
*/
|
||||
void commit(boolean force);
|
||||
|
||||
/**
|
||||
* Discards pending batch statements and rolls database connection back.
|
||||
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
|
||||
* To force the rollback call {@link SqlSession#rollback(boolean)}
|
||||
*/
|
||||
void rollback();
|
||||
|
||||
/**
|
||||
* Discards pending batch statements and rolls database connection back.
|
||||
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
|
||||
* @param force forces connection rollback
|
||||
*/
|
||||
void rollback(boolean force);
|
||||
|
||||
/**
|
||||
* Flushes batch statements.
|
||||
* @return BatchResult list of updated records
|
||||
* @since 3.0.6
|
||||
*/
|
||||
List<BatchResult> flushStatements();
|
||||
|
||||
/**
|
||||
* Closes the session.
|
||||
*/
|
||||
@Override
|
||||
void close();
|
||||
|
||||
/**
|
||||
* Clears local session cache.
|
||||
*/
|
||||
void clearCache();
|
||||
|
||||
/**
|
||||
* Retrieves current configuration.
|
||||
* @return DbConfig
|
||||
*/
|
||||
DbConfig getDbConfig();
|
||||
|
||||
/**
|
||||
* Retrieves a mapper.
|
||||
* @param <T> the mapper type
|
||||
* @param type Mapper interface class
|
||||
* @return a mapper bound to this SqlSession
|
||||
*/
|
||||
<T> T getMapper(Class<T> type);
|
||||
|
||||
/**
|
||||
* Retrieves inner database connection.
|
||||
* @return Connection
|
||||
*/
|
||||
Connection getConnection();
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
public interface SqlSessionFactory {
|
||||
|
||||
SqlSession openSession();
|
||||
|
||||
SqlSession openSession(boolean autoCommit);
|
||||
|
||||
SqlSession openSession(Connection connection);
|
||||
|
||||
SqlSession openSession(TransactionIsolationLevel level);
|
||||
|
||||
SqlSession openSession(SqlExecutorType execType);
|
||||
|
||||
SqlSession openSession(SqlExecutorType execType, boolean autoCommit);
|
||||
|
||||
SqlSession openSession(SqlExecutorType execType, TransactionIsolationLevel level);
|
||||
|
||||
SqlSession openSession(SqlExecutorType execType, Connection connection);
|
||||
|
||||
DbConfig getDbConfig();
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright (c) 2022 aoshiguchen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package fun.asgc.neutrino.core.db.crisp;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
/**
|
||||
* @author: aoshiguchen
|
||||
* @date: 2022/11/4
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum TransactionIsolationLevel {
|
||||
NONE(Connection.TRANSACTION_NONE),
|
||||
READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED),
|
||||
READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
|
||||
REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ),
|
||||
SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE);
|
||||
|
||||
private final int level;
|
||||
}
|
||||
Reference in New Issue
Block a user