From 21c759ae4f254fcf69636e0899ef4c750c9bbe2a Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 6 Sep 2024 10:50:00 +0800 Subject: [PATCH] add ColumnType --- .../org/dromara/hutool/db/meta/Column.java | 93 +------------ ...{ColumnIndexInfo.java => ColumnIndex.java} | 14 +- .../dromara/hutool/db/meta/ColumnType.java | 130 ++++++++++++++++++ .../db/meta/DatabaseMetaDataWrapper.java | 2 +- .../org/dromara/hutool/db/meta/IndexInfo.java | 18 +-- 5 files changed, 154 insertions(+), 103 deletions(-) rename hutool-db/src/main/java/org/dromara/hutool/db/meta/{ColumnIndexInfo.java => ColumnIndex.java} (85%) create mode 100644 hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnType.java diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/Column.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/Column.java index c9f6f15e5..64f43aeae 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/meta/Column.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/Column.java @@ -37,23 +37,15 @@ public class Column implements Serializable, Cloneable { * 表名 */ private String tableName; - /** * 列名 */ private String name; /** - * 类型,对应java.sql.Types中的类型 + * 数据库字段类型,包括长度 */ - private int type; - /** - * 类型名称 - */ - private String typeName; - /** - * 大小或数据长度 - */ - private long size; + private ColumnType type; + /** * 保留小数位数 */ @@ -134,14 +126,13 @@ public class Column implements Serializable, Cloneable { this.name = columnMetaRs.getString("COLUMN_NAME"); this.isPk = table.isPk(this.name); - this.type = columnMetaRs.getInt("DATA_TYPE"); - + final int type = columnMetaRs.getInt("DATA_TYPE"); String typeName = columnMetaRs.getString("TYPE_NAME"); //issue#2201@Gitee typeName = ReUtil.delLast("\\(\\d+\\)", typeName); - this.typeName = typeName; + final long size = columnMetaRs.getLong("COLUMN_SIZE"); + this.type = new ColumnType(type, typeName, size); - this.size = columnMetaRs.getLong("COLUMN_SIZE"); this.isNullable = columnMetaRs.getBoolean("NULLABLE"); this.remarks = columnMetaRs.getString("REMARKS"); this.columnDef = columnMetaRs.getString("COLUMN_DEF"); @@ -206,76 +197,6 @@ public class Column implements Serializable, Cloneable { return this; } - /** - * 获取字段类型的枚举 - * - * @return 阻断类型枚举 - * @since 4.5.8 - */ - public JdbcType getTypeEnum() { - return JdbcType.valueOf(this.type); - } - - /** - * 获取类型,对应{@link java.sql.Types}中的类型 - * - * @return 类型 - */ - public int getType() { - return type; - } - - /** - * 设置类型,对应java.sql.Types中的类型 - * - * @param type 类型 - * @return this - */ - public Column setType(final int type) { - this.type = type; - return this; - } - - /** - * 获取类型名称 - * - * @return 类型名称 - */ - public String getTypeName() { - return typeName; - } - - /** - * 设置类型名称 - * - * @param typeName 类型名称 - * @return this - */ - public Column setTypeName(final String typeName) { - this.typeName = typeName; - return this; - } - - /** - * 获取大小或数据长度 - * - * @return 大小或数据长度 - */ - public long getSize() { - return size; - } - - /** - * 设置大小或数据长度 - * - * @param size 大小或数据长度 - * @return this - */ - public Column setSize(final long size) { - this.size = size; - return this; - } - /** * 获取小数位数 * @@ -424,7 +345,7 @@ public class Column implements Serializable, Cloneable { @Override public String toString() { - return "Column [tableName=" + tableName + ", name=" + name + ", type=" + type + ", size=" + size + ", isNullable=" + isNullable + ", order=" + order + "]"; + return "Column [tableName=" + tableName + ", name=" + name + ", type=" + type + ", isNullable=" + isNullable + ", order=" + order + "]"; } @Override diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndexInfo.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndex.java similarity index 85% rename from hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndexInfo.java rename to hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndex.java index ad9e7ecbe..d345bd115 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndexInfo.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnIndex.java @@ -23,12 +23,12 @@ import java.sql.ResultSet; import java.sql.SQLException; /** - * 索引中的列信息 + * 索引中的列索引信息 * * @author huzhongying * @since 5.7.23 */ -public class ColumnIndexInfo implements Serializable, Cloneable { +public class ColumnIndex implements Serializable, Cloneable { private static final long serialVersionUID = 1L; /** @@ -37,9 +37,9 @@ public class ColumnIndexInfo implements Serializable, Cloneable { * @param rs 结果集,通过DatabaseMetaData#getIndexInfo获取 * @return ColumnIndexInfo */ - public static ColumnIndexInfo of(final ResultSet rs) { + public static ColumnIndex of(final ResultSet rs) { try { - return new ColumnIndexInfo( + return new ColumnIndex( rs.getString("COLUMN_NAME"), rs.getString("ASC_OR_DESC")); } catch (final SQLException e) { @@ -62,7 +62,7 @@ public class ColumnIndexInfo implements Serializable, Cloneable { * @param columnName 索引列名 * @param ascOrDesc 正序或反序,null表示无顺序表示 */ - public ColumnIndexInfo(final String columnName, final String ascOrDesc) { + public ColumnIndex(final String columnName, final String ascOrDesc) { this.columnName = columnName; this.ascOrDesc = ascOrDesc; } @@ -84,8 +84,8 @@ public class ColumnIndexInfo implements Serializable, Cloneable { } @Override - public ColumnIndexInfo clone() throws CloneNotSupportedException { - return (ColumnIndexInfo) super.clone(); + public ColumnIndex clone() throws CloneNotSupportedException { + return (ColumnIndex) super.clone(); } @Override diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnType.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnType.java new file mode 100644 index 000000000..a0b60539d --- /dev/null +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/ColumnType.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2024 Hutool Team and hutool.cn + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.dromara.hutool.db.meta; + +import org.dromara.hutool.core.text.StrUtil; + +/** + * 数据库字段类型 + * + * @author Looly + * @since 6.0.0 + */ +public class ColumnType { + /** + * 类型,对应java.sql.Types中的类型 + */ + private int type; + /** + * 类型名称 + */ + private String typeName; + + /** + * 构造 + * + * @param type 类型,对应java.sql.Types中的类型 + * @param typeName 类型名称 + * @param size 大小或数据长度 + */ + public ColumnType(final int type, final String typeName, final long size) { + this.type = type; + this.typeName = typeName; + this.size = size; + } + + /** + * 大小或数据长度 + */ + private long size; + + + /** + * 获取字段类型的枚举 + * + * @return 阻断类型枚举 + * @since 4.5.8 + */ + public JdbcType getTypeEnum() { + return JdbcType.valueOf(this.type); + } + + /** + * 获取类型,对应{@link java.sql.Types}中的类型 + * + * @return 类型 + */ + public int getType() { + return type; + } + + /** + * 设置类型,对应java.sql.Types中的类型 + * + * @param type 类型 + * @return this + */ + public ColumnType setType(final int type) { + this.type = type; + return this; + } + + /** + * 获取类型名称 + * + * @return 类型名称 + */ + public String getTypeName() { + return typeName; + } + + /** + * 设置类型名称 + * + * @param typeName 类型名称 + * @return this + */ + public ColumnType setTypeName(final String typeName) { + this.typeName = typeName; + return this; + } + + /** + * 获取大小或数据长度 + * + * @return 大小或数据长度 + */ + public long getSize() { + return size; + } + + /** + * 设置大小或数据长度 + * + * @param size 大小或数据长度 + * @return this + */ + public ColumnType setSize(final long size) { + this.size = size; + return this; + } + + @Override + public String toString() { + return StrUtil.format("{}-{}({})", this.type, this.typeName, this.size); + } +} diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/DatabaseMetaDataWrapper.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/DatabaseMetaDataWrapper.java index dfd1de3e1..32583f9da 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/meta/DatabaseMetaDataWrapper.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/DatabaseMetaDataWrapper.java @@ -231,7 +231,7 @@ public class DatabaseMetaDataWrapper extends SimpleWrapper { indexInfo = new IndexInfo(rs.getBoolean("NON_UNIQUE"), indexName, tableName, schema, catalog); indexInfoMap.put(key, indexInfo); } - indexInfo.getColumnIndexInfoList().add(ColumnIndexInfo.of(rs)); + indexInfo.getColumnIndexInfoList().add(ColumnIndex.of(rs)); } } } catch (final SQLException e) { diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/meta/IndexInfo.java b/hutool-db/src/main/java/org/dromara/hutool/db/meta/IndexInfo.java index 449a49089..beeba1203 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/meta/IndexInfo.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/meta/IndexInfo.java @@ -26,7 +26,7 @@ import java.util.Objects; /** * 数据库表的索引信息
- * 如果时单列索引,只有一个{@link ColumnIndexInfo},联合索引则拥有多个{@link ColumnIndexInfo} + * 如果时单列索引,只有一个{@link ColumnIndex},联合索引则拥有多个{@link ColumnIndex} * * @author huzhongying */ @@ -60,7 +60,7 @@ public class IndexInfo implements Serializable, Cloneable { /** * 索引中的列信息,按索引顺序排列 */ - private List columnIndexInfoList; + private List columnIndexList; /** * 构造 @@ -77,7 +77,7 @@ public class IndexInfo implements Serializable, Cloneable { this.tableName = tableName; this.schema = schema; this.catalog = catalog; - this.columnIndexInfoList = new ArrayList<>(); + this.columnIndexList = new ArrayList<>(); } /** @@ -185,18 +185,18 @@ public class IndexInfo implements Serializable, Cloneable { * * @return 列索引信息列表 */ - public List getColumnIndexInfoList() { - return columnIndexInfoList; + public List getColumnIndexInfoList() { + return columnIndexList; } /** * 设置列索引信息列表 * - * @param columnIndexInfoList 列索引信息列表 + * @param columnIndexList 列索引信息列表 * @return 当前的IndexInfo对象,以支持链式调用 */ - public IndexInfo setColumnIndexInfoList(final List columnIndexInfoList) { - this.columnIndexInfoList = columnIndexInfoList; + public IndexInfo setColumnIndexInfoList(final List columnIndexList) { + this.columnIndexList = columnIndexList; return this; } @@ -231,7 +231,7 @@ public class IndexInfo implements Serializable, Cloneable { ", tableName='" + tableName + '\'' + ", schema='" + schema + '\'' + ", catalog='" + catalog + '\'' + - ", columnIndexInfoList=" + columnIndexInfoList + + ", columnIndexInfoList=" + columnIndexList + '}'; } }