From 1ce5eb3b734b13ccd47e3848117daf3c2d7d0afa Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 12 Nov 2024 21:40:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E9=87=8D=E6=9E=84=20MetaUtil?= =?UTF-8?q?s=20=E8=8E=B7=E5=8F=96=E8=A1=A8=E4=BF=A1=E6=81=AF=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/data/core/util/MetaUtils.java | 63 ++++++---- .../starter/data/core/util/Table.java | 115 ------------------ 2 files changed, 39 insertions(+), 139 deletions(-) delete mode 100644 continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/Table.java diff --git a/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/MetaUtils.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/MetaUtils.java index 91a085c7..d895ee40 100644 --- a/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/MetaUtils.java +++ b/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/MetaUtils.java @@ -16,18 +16,21 @@ package top.continew.starter.data.core.util; -import cn.hutool.core.date.DateUtil; +import cn.hutool.core.convert.Convert; import cn.hutool.core.text.CharSequenceUtil; -import cn.hutool.db.Db; -import cn.hutool.db.Entity; +import cn.hutool.db.DbRuntimeException; +import cn.hutool.db.DbUtil; import cn.hutool.db.meta.Column; import cn.hutool.db.meta.MetaUtil; +import cn.hutool.db.meta.Table; +import cn.hutool.db.meta.TableType; import top.continew.starter.core.exception.BusinessException; import top.continew.starter.data.core.enums.DatabaseType; import javax.sql.DataSource; import java.sql.Connection; import java.sql.DatabaseMetaData; +import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; @@ -80,7 +83,7 @@ public class MetaUtils { * @param dataSource 数据源 * @return 表信息列表 */ - public static List getTables(DataSource dataSource) throws SQLException { + public static List
getTables(DataSource dataSource) { return getTables(dataSource, null); } @@ -90,27 +93,39 @@ public class MetaUtils { * @param dataSource 数据源 * @param tableName 表名称 * @return 表信息列表 + * @author looly + * @since 2.7.2 */ - public static List
getTables(DataSource dataSource, String tableName) throws SQLException { - String querySql = "SHOW TABLE STATUS"; - List tableEntityList; - Db db = Db.use(dataSource); - if (CharSequenceUtil.isNotBlank(tableName)) { - tableEntityList = db.query("%s WHERE NAME = ?".formatted(querySql), tableName); - } else { - tableEntityList = db.query(querySql); + public static List
getTables(DataSource dataSource, String tableName) { + List
tables = new ArrayList<>(); + Connection conn = null; + try { + conn = dataSource.getConnection(); + String catalog = MetaUtil.getCatalog(conn); + String schema = MetaUtil.getSchema(conn); + final DatabaseMetaData metaData = conn.getMetaData(); + try (final ResultSet rs = metaData.getTables(catalog, schema, tableName, Convert + .toStrArray(TableType.TABLE))) { + if (null != rs) { + String name; + while (rs.next()) { + name = rs.getString("TABLE_NAME"); + if (CharSequenceUtil.isNotBlank(name)) { + final Table table = Table.create(name); + table.setCatalog(catalog); + table.setSchema(schema); + table.setComment(MetaUtil.getRemarks(metaData, catalog, schema, name)); + tables.add(table); + } + } + } + } + return tables; + } catch (Exception e) { + throw new DbRuntimeException("Get tables error!", e); + } finally { + DbUtil.close(conn); } - List
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; } /** @@ -121,7 +136,7 @@ public class MetaUtils { * @return 列信息列表 */ public static Collection getColumns(DataSource dataSource, String tableName) { - cn.hutool.db.meta.Table table = MetaUtil.getTableMeta(dataSource, tableName); + Table table = MetaUtil.getTableMeta(dataSource, tableName); return table.getColumns(); } } diff --git a/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/Table.java b/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/Table.java deleted file mode 100644 index 2b412111..00000000 --- a/continew-starter-data/continew-starter-data-core/src/main/java/top/continew/starter/data/core/util/Table.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.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; - } -}