mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
feat(data): 新增 continew-starter-data-core 模块
1.移动 MetaUtils core => data-core 2.新增 SQL 函数接口及数据库类型枚举
This commit is contained in:
@@ -52,11 +52,5 @@
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-http</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Hutool 数据库模块(在 JDBC 基础上封装的数据库操作工具类,通过包装,使用 ActiveRecord 思想操作数据库) -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-db</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
22
continew-starter-data/continew-starter-data-core/pom.xml
Normal file
22
continew-starter-data/continew-starter-data-core/pom.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
<artifactId>continew-starter-data</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>continew-starter-data-core</artifactId>
|
||||
<description>ContiNew Starter 数据访问模块 - 核心模块</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- Hutool 数据库模块(在 JDBC 基础上封装的数据库操作工具类,通过包装,使用 ActiveRecord 思想操作数据库) -->
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-db</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有表信息
|
||||
*
|
@@ -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;
|
@@ -31,6 +31,12 @@
|
||||
<artifactId>p6spy</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 数据访问模块 - 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
<artifactId>continew-starter-data-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- CosId(通用、灵活、高性能的分布式 ID 生成器) -->
|
||||
<dependency>
|
||||
<groupId>me.ahoo.cosid</groupId>
|
||||
|
@@ -14,6 +14,7 @@
|
||||
<description>ContiNew Starter 数据访问模块</description>
|
||||
|
||||
<modules>
|
||||
<module>continew-starter-data-core</module>
|
||||
<module>continew-starter-data-mybatis-plus</module>
|
||||
</modules>
|
||||
|
||||
|
@@ -321,6 +321,13 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 数据访问模块 - 核心模块 -->
|
||||
<dependency>
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
<artifactId>continew-starter-data-core</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 缓存模块 - JetCache -->
|
||||
<dependency>
|
||||
<groupId>top.charles7c.continew</groupId>
|
||||
|
Reference in New Issue
Block a user