From 13128e0a9e8d1dbaaca3e9838a20aaa1b34cfa3e Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 4 Nov 2022 16:19:35 +0800 Subject: [PATCH] =?UTF-8?q?CrispDbKit=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/db/crisp/BatchResult.java | 80 ++++++ .../neutrino/core/db/crisp/CrispDbKit.java | 78 +----- .../{CrispDbConfig.java => DbConfig.java} | 28 +- .../neutrino/core/db/crisp/DbContext.java | 41 +++ .../{CrispDbExecutor.java => DbExecutor.java} | 34 +-- .../{CrispDbOperator.java => DbOperator.java} | 8 +- ...bUpdateResult.java => DbUpdateResult.java} | 2 +- .../db/crisp/DefaultSqlSessionFactory.java | 85 ++++++ .../neutrino/core/db/crisp/JdbcManager.java | 100 +++++++ ...ispDbContext.java => MappedStatement.java} | 8 +- .../neutrino/core/db/crisp/RowBounds.java | 54 ++++ .../core/db/crisp/SqlExecutorType.java | 30 ++ .../neutrino/core/db/crisp/SqlSession.java | 259 ++++++++++++++++++ .../core/db/crisp/SqlSessionFactory.java | 49 ++++ .../db/crisp/TransactionIsolationLevel.java | 43 +++ .../{LicenseMapper.xml => DbOperator.xml} | 0 16 files changed, 788 insertions(+), 111 deletions(-) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/BatchResult.java rename neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/{CrispDbConfig.java => DbConfig.java} (74%) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbContext.java rename neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/{CrispDbExecutor.java => DbExecutor.java} (64%) rename neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/{CrispDbOperator.java => DbOperator.java} (90%) rename neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/{CrispDbUpdateResult.java => DbUpdateResult.java} (97%) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcManager.java rename neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/{CrispDbContext.java => MappedStatement.java} (88%) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/RowBounds.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlExecutorType.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSession.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactory.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/TransactionIsolationLevel.java rename neutrino-proxy-server/src/main/resources/mapper/{LicenseMapper.xml => DbOperator.xml} (100%) diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/BatchResult.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/BatchResult.java new file mode 100644 index 00000000..74665b9c --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/BatchResult.java @@ -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 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 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); + } + +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbKit.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbKit.java index 3a702ebb..b189ea9c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbKit.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbKit.java @@ -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 crispDbConfigMap = new HashMap<>(); - /** - * 数据库操作实例 - */ - private static Map 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); - } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbConfig.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbConfig.java similarity index 74% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbConfig.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbConfig.java index 350d5576..7d1529ff 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbConfig.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbConfig.java @@ -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; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbContext.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbContext.java new file mode 100644 index 00000000..36c378ce --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbContext.java @@ -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 connectionHolder = new ThreadLocal<>(); + + public DbContext(DbConfig dbConfig) { + this.dbConfig = dbConfig; + } + public SqlSessionFactory getSqlSessionFactory() { + // TODO + return null; + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbExecutor.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbExecutor.java similarity index 64% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbExecutor.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbExecutor.java index 984b13f9..11b344d4 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbExecutor.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbExecutor.java @@ -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; } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbOperator.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbOperator.java similarity index 90% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbOperator.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbOperator.java index 651df9dd..e2775d39 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbOperator.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbOperator.java @@ -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); } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbUpdateResult.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbUpdateResult.java similarity index 97% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbUpdateResult.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbUpdateResult.java index ea761a65..41d4e32e 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbUpdateResult.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DbUpdateResult.java @@ -25,6 +25,6 @@ package fun.asgc.neutrino.core.db.crisp; * @author: aoshiguchen * @date: 2022/11/3 */ -public class CrispDbUpdateResult { +public class DbUpdateResult { } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java new file mode 100644 index 00000000..e57ba3b2 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java @@ -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; + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcManager.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcManager.java new file mode 100644 index 00000000..c29cb768 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcManager.java @@ -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 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); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbContext.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/MappedStatement.java similarity index 88% rename from neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbContext.java rename to neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/MappedStatement.java index 21648b2e..6c68cc6b 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/CrispDbContext.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/MappedStatement.java @@ -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 connectionHolder = new ThreadLocal<>(); +public class MappedStatement { + // TODO } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/RowBounds.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/RowBounds.java new file mode 100644 index 00000000..dddbdc58 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/RowBounds.java @@ -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; + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlExecutorType.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlExecutorType.java new file mode 100644 index 00000000..19fe0ae7 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlExecutorType.java @@ -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 +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSession.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSession.java new file mode 100644 index 00000000..94e98e08 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSession.java @@ -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 the returned object type + * @param statement + * @return Mapped object + */ + T selectOne(String statement); + + /** + * Retrieve a single row mapped from the statement key and parameter. + * @param 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 selectOne(String statement, Object parameter); + + /** + * Retrieve a list of mapped objects from the statement key and parameter. + * @param the returned list element type + * @param statement Unique identifier matching the statement to use. + * @return List of mapped object + */ + List selectList(String statement); + + /** + * Retrieve a list of mapped objects from the statement key and parameter. + * @param 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 + */ + List selectList(String statement, Object parameter); + + /** + * Retrieve a list of mapped objects from the statement key and parameter, + * within the specified row bounds. + * @param 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 + */ + List 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 the returned Map keys type + * @param 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. + */ + Map 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 the returned Map keys type + * @param 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. + */ + Map 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 the returned Map keys type + * @param 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. + */ + Map 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 flushStatements(); + + /** + * Closes the session. + */ + @Override + void close(); + + /** + * Clears local session cache. + */ + void clearCache(); + + /** + * Retrieves current configuration. + * @return DbConfig + */ + DbConfig getDbConfig(); + + /** + * Retrieves a mapper. + * @param the mapper type + * @param type Mapper interface class + * @return a mapper bound to this SqlSession + */ + T getMapper(Class type); + + /** + * Retrieves inner database connection. + * @return Connection + */ + Connection getConnection(); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactory.java new file mode 100644 index 00000000..26897c15 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactory.java @@ -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(); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/TransactionIsolationLevel.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/TransactionIsolationLevel.java new file mode 100644 index 00000000..553cc039 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/TransactionIsolationLevel.java @@ -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; +} diff --git a/neutrino-proxy-server/src/main/resources/mapper/LicenseMapper.xml b/neutrino-proxy-server/src/main/resources/mapper/DbOperator.xml similarity index 100% rename from neutrino-proxy-server/src/main/resources/mapper/LicenseMapper.xml rename to neutrino-proxy-server/src/main/resources/mapper/DbOperator.xml