diff --git a/continew-starter-core/pom.xml b/continew-starter-core/pom.xml index 73bbd303..ebe827b4 100644 --- a/continew-starter-core/pom.xml +++ b/continew-starter-core/pom.xml @@ -52,11 +52,5 @@ cn.hutool hutool-http - - - - cn.hutool - hutool-db - \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-core/pom.xml b/continew-starter-data/continew-starter-data-core/pom.xml new file mode 100644 index 00000000..78cce692 --- /dev/null +++ b/continew-starter-data/continew-starter-data-core/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + top.charles7c.continew + continew-starter-data + ${revision} + + + continew-starter-data-core + ContiNew Starter 数据访问模块 - 核心模块 + + + + + cn.hutool + hutool-db + + + \ No newline at end of file diff --git a/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/enums/DatabaseType.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/enums/DatabaseType.java new file mode 100644 index 00000000..993cc94f --- /dev/null +++ b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/enums/DatabaseType.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.data.core.enums; + +import top.charles7c.continew.starter.data.core.function.ISqlFunction; + +import java.io.Serializable; + +/** + * 数据库类型枚举 + * + * @author Charles7c + * @since 1.4.1 + */ +public enum DatabaseType implements ISqlFunction { + + /** + * MySQL + */ + MYSQL("MySQL") { + @Override + public String findInSet(Serializable value, String set) { + return "find_in_set('%s', %s) <> 0".formatted(value, set); + } + }, + + /** + * PostgreSQL + */ + POSTGRE_SQL("PostgreSQL") { + @Override + public String findInSet(Serializable value, String set) { + return "(select position(',%s,' in ','||%s||',')) <> 0".formatted(value, set); + } + },; + + private final String database; + + DatabaseType(String database) { + this.database = database; + } + + /** + * 获取数据库类型 + * + * @param database 数据库 + */ + public static DatabaseType get(String database) { + for (DatabaseType databaseType : DatabaseType.values()) { + if (databaseType.database.equalsIgnoreCase(database)) { + return databaseType; + } + } + return null; + } + + public String getDatabase() { + return database; + } +} diff --git a/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/function/ISqlFunction.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/function/ISqlFunction.java new file mode 100644 index 00000000..af958538 --- /dev/null +++ b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/function/ISqlFunction.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.data.core.function; + +import java.io.Serializable; + +/** + * SQL 函数接口 + * + * @author Charles7c + * @since 1.4.1 + */ +public interface ISqlFunction { + + /** + * find_in_set 函数 + * + * @param value 值 + * @param set 集合 + * @return 函数实现 + */ + String findInSet(Serializable value, String set); +} diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/MetaUtils.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/MetaUtils.java similarity index 78% rename from continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/MetaUtils.java rename to continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/MetaUtils.java index cf41516a..d988f3bd 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/MetaUtils.java +++ b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/MetaUtils.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.charles7c.continew.starter.core.util.db; +package top.charles7c.continew.starter.data.core.util; import cn.hutool.core.date.DateUtil; import cn.hutool.core.text.CharSequenceUtil; @@ -22,8 +22,12 @@ import cn.hutool.db.Db; import cn.hutool.db.Entity; import cn.hutool.db.meta.Column; import cn.hutool.db.meta.MetaUtil; +import top.charles7c.continew.starter.core.exception.BusinessException; +import top.charles7c.continew.starter.data.core.enums.DatabaseType; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.DatabaseMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -40,6 +44,23 @@ public class MetaUtils { private MetaUtils() { } + /** + * 获取数据库类型 + * + * @param dataSource 数据源 + * @return 数据库类型 + * @since 1.4.1 + */ + public static DatabaseType getDatabaseType(DataSource dataSource) { + try (Connection conn = dataSource.getConnection()) { + DatabaseMetaData metaData = conn.getMetaData(); + String databaseProductName = metaData.getDatabaseProductName(); + return DatabaseType.get(databaseProductName); + } catch (SQLException e) { + throw new BusinessException(e); + } + } + /** * 获取所有表信息 * diff --git a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/Table.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/Table.java similarity index 97% rename from continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/Table.java rename to continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/Table.java index 6e91929f..84b56e07 100644 --- a/continew-starter-core/src/main/java/top/charles7c/continew/starter/core/util/db/Table.java +++ b/continew-starter-data/continew-starter-data-core/src/main/java/top/charles7c/continew/starter/data/core/util/Table.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.charles7c.continew.starter.core.util.db; +package top.charles7c.continew.starter.data.core.util; import java.io.Serial; import java.io.Serializable; diff --git a/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml index df5dcd4f..5370cfe9 100644 --- a/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml +++ b/continew-starter-data/continew-starter-data-mybatis-plus/pom.xml @@ -31,6 +31,12 @@ p6spy + + + top.charles7c.continew + continew-starter-data-core + + me.ahoo.cosid diff --git a/continew-starter-data/pom.xml b/continew-starter-data/pom.xml index fc934a1f..71420d5a 100644 --- a/continew-starter-data/pom.xml +++ b/continew-starter-data/pom.xml @@ -14,6 +14,7 @@ ContiNew Starter 数据访问模块 + continew-starter-data-core continew-starter-data-mybatis-plus diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 940aca3e..061e74bd 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -321,6 +321,13 @@ ${revision} + + + top.charles7c.continew + continew-starter-data-core + ${revision} + + top.charles7c.continew