feat(data): 新增 continew-starter-data-core 模块

1.移动 MetaUtils core => data-core
2.新增 SQL 函数接口及数据库类型枚举
This commit is contained in:
2024-02-19 21:00:07 +08:00
parent d42585cb4d
commit 4ffc5dc1d4
9 changed files with 170 additions and 8 deletions

View 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>

View File

@@ -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;
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,114 @@
/*
* 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.util;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
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;
import java.util.List;
/**
* 数据库元数据信息工具类
*
* @author Charles7c
* @since 1.0.0
*/
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);
}
}
/**
* 获取所有表信息
*
* @param dataSource 数据源
* @return 表信息列表
*/
public static List<Table> getTables(DataSource dataSource) throws SQLException {
return getTables(dataSource, null);
}
/**
* 获取所有表信息
*
* @param dataSource 数据源
* @param tableName 表名称
* @return 表信息列表
*/
public static List<Table> getTables(DataSource dataSource, String tableName) throws SQLException {
String querySql = "SHOW TABLE STATUS";
List<Entity> tableEntityList;
Db db = Db.use(dataSource);
if (CharSequenceUtil.isNotBlank(tableName)) {
tableEntityList = db.query("%s WHERE NAME = ?".formatted(querySql), tableName);
} else {
tableEntityList = db.query(querySql);
}
List<Table> tableList = new ArrayList<>(tableEntityList.size());
for (Entity tableEntity : tableEntityList) {
Table table = new Table(tableEntity.getStr("NAME"));
table.setComment(tableEntity.getStr("COMMENT"));
table.setEngine(tableEntity.getStr("ENGINE"));
table.setCharset(tableEntity.getStr("COLLATION"));
table.setCreateTime(DateUtil.toLocalDateTime(tableEntity.getDate("CREATE_TIME")));
table.setUpdateTime(DateUtil.toLocalDateTime(tableEntity.getDate("UPDATE_TIME")));
tableList.add(table);
}
return tableList;
}
/**
* 获取所有列信息
*
* @param dataSource 数据源
* @param tableName 表名称
* @return 列信息列表
*/
public static Collection<Column> getColumns(DataSource dataSource, String tableName) {
cn.hutool.db.meta.Table table = MetaUtil.getTableMeta(dataSource, tableName);
return table.getColumns();
}
}

View File

@@ -0,0 +1,115 @@
/*
* 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.util;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 数据库表信息
*
* @author Charles7c
* @since 1.0.0
*/
public class Table implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 表名称
*/
private String tableName;
/**
* 注释
*/
private String comment;
/**
* 存储引擎
*/
private String engine;
/**
* 字符集
*/
private String charset;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 修改时间
*/
private LocalDateTime updateTime;
public Table(String tableName) {
this.tableName = tableName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public LocalDateTime getUpdateTime() {
return updateTime;
}
public void setUpdateTime(LocalDateTime updateTime) {
this.updateTime = updateTime;
}
}