mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-09 20:57:23 +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>
|
@@ -1,93 +0,0 @@
|
||||
/*
|
||||
* 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.core.util.db;
|
||||
|
||||
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 javax.sql.DataSource;
|
||||
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 表信息列表
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
* 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.core.util.db;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user