add ColumnType

This commit is contained in:
Looly 2024-09-06 10:50:00 +08:00
parent af1f14b439
commit 21c759ae4f
5 changed files with 154 additions and 103 deletions

View File

@ -37,23 +37,15 @@ public class Column implements Serializable, Cloneable {
* 表名 * 表名
*/ */
private String tableName; private String tableName;
/** /**
* 列名 * 列名
*/ */
private String name; private String name;
/** /**
* 类型对应java.sql.Types中的类型 * 数据库字段类型包括长度
*/ */
private int type; private ColumnType type;
/**
* 类型名称
*/
private String typeName;
/**
* 大小或数据长度
*/
private long size;
/** /**
* 保留小数位数 * 保留小数位数
*/ */
@ -134,14 +126,13 @@ public class Column implements Serializable, Cloneable {
this.name = columnMetaRs.getString("COLUMN_NAME"); this.name = columnMetaRs.getString("COLUMN_NAME");
this.isPk = table.isPk(this.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"); String typeName = columnMetaRs.getString("TYPE_NAME");
//issue#2201@Gitee //issue#2201@Gitee
typeName = ReUtil.delLast("\\(\\d+\\)", typeName); 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.isNullable = columnMetaRs.getBoolean("NULLABLE");
this.remarks = columnMetaRs.getString("REMARKS"); this.remarks = columnMetaRs.getString("REMARKS");
this.columnDef = columnMetaRs.getString("COLUMN_DEF"); this.columnDef = columnMetaRs.getString("COLUMN_DEF");
@ -206,76 +197,6 @@ public class Column implements Serializable, Cloneable {
return this; 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 @Override
public String toString() { 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 @Override

View File

@ -23,12 +23,12 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
/** /**
* 索引中的列信息 * 索引中的列索引信息
* *
* @author huzhongying * @author huzhongying
* @since 5.7.23 * @since 5.7.23
*/ */
public class ColumnIndexInfo implements Serializable, Cloneable { public class ColumnIndex implements Serializable, Cloneable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/** /**
@ -37,9 +37,9 @@ public class ColumnIndexInfo implements Serializable, Cloneable {
* @param rs 结果集通过DatabaseMetaData#getIndexInfo获取 * @param rs 结果集通过DatabaseMetaData#getIndexInfo获取
* @return ColumnIndexInfo * @return ColumnIndexInfo
*/ */
public static ColumnIndexInfo of(final ResultSet rs) { public static ColumnIndex of(final ResultSet rs) {
try { try {
return new ColumnIndexInfo( return new ColumnIndex(
rs.getString("COLUMN_NAME"), rs.getString("COLUMN_NAME"),
rs.getString("ASC_OR_DESC")); rs.getString("ASC_OR_DESC"));
} catch (final SQLException e) { } catch (final SQLException e) {
@ -62,7 +62,7 @@ public class ColumnIndexInfo implements Serializable, Cloneable {
* @param columnName 索引列名 * @param columnName 索引列名
* @param ascOrDesc 正序或反序null表示无顺序表示 * @param ascOrDesc 正序或反序null表示无顺序表示
*/ */
public ColumnIndexInfo(final String columnName, final String ascOrDesc) { public ColumnIndex(final String columnName, final String ascOrDesc) {
this.columnName = columnName; this.columnName = columnName;
this.ascOrDesc = ascOrDesc; this.ascOrDesc = ascOrDesc;
} }
@ -84,8 +84,8 @@ public class ColumnIndexInfo implements Serializable, Cloneable {
} }
@Override @Override
public ColumnIndexInfo clone() throws CloneNotSupportedException { public ColumnIndex clone() throws CloneNotSupportedException {
return (ColumnIndexInfo) super.clone(); return (ColumnIndex) super.clone();
} }
@Override @Override

View File

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

View File

@ -231,7 +231,7 @@ public class DatabaseMetaDataWrapper extends SimpleWrapper<DatabaseMetaData> {
indexInfo = new IndexInfo(rs.getBoolean("NON_UNIQUE"), indexName, tableName, schema, catalog); indexInfo = new IndexInfo(rs.getBoolean("NON_UNIQUE"), indexName, tableName, schema, catalog);
indexInfoMap.put(key, indexInfo); indexInfoMap.put(key, indexInfo);
} }
indexInfo.getColumnIndexInfoList().add(ColumnIndexInfo.of(rs)); indexInfo.getColumnIndexInfoList().add(ColumnIndex.of(rs));
} }
} }
} catch (final SQLException e) { } catch (final SQLException e) {

View File

@ -26,7 +26,7 @@ import java.util.Objects;
/** /**
* 数据库表的索引信息<br> * 数据库表的索引信息<br>
* 如果时单列索引只有一个{@link ColumnIndexInfo}联合索引则拥有多个{@link ColumnIndexInfo} * 如果时单列索引只有一个{@link ColumnIndex}联合索引则拥有多个{@link ColumnIndex}
* *
* @author huzhongying * @author huzhongying
*/ */
@ -60,7 +60,7 @@ public class IndexInfo implements Serializable, Cloneable {
/** /**
* 索引中的列信息,按索引顺序排列 * 索引中的列信息,按索引顺序排列
*/ */
private List<ColumnIndexInfo> columnIndexInfoList; private List<ColumnIndex> columnIndexList;
/** /**
* 构造 * 构造
@ -77,7 +77,7 @@ public class IndexInfo implements Serializable, Cloneable {
this.tableName = tableName; this.tableName = tableName;
this.schema = schema; this.schema = schema;
this.catalog = catalog; this.catalog = catalog;
this.columnIndexInfoList = new ArrayList<>(); this.columnIndexList = new ArrayList<>();
} }
/** /**
@ -185,18 +185,18 @@ public class IndexInfo implements Serializable, Cloneable {
* *
* @return 列索引信息列表 * @return 列索引信息列表
*/ */
public List<ColumnIndexInfo> getColumnIndexInfoList() { public List<ColumnIndex> getColumnIndexInfoList() {
return columnIndexInfoList; return columnIndexList;
} }
/** /**
* 设置列索引信息列表 * 设置列索引信息列表
* *
* @param columnIndexInfoList 列索引信息列表 * @param columnIndexList 列索引信息列表
* @return 当前的IndexInfo对象以支持链式调用 * @return 当前的IndexInfo对象以支持链式调用
*/ */
public IndexInfo setColumnIndexInfoList(final List<ColumnIndexInfo> columnIndexInfoList) { public IndexInfo setColumnIndexInfoList(final List<ColumnIndex> columnIndexList) {
this.columnIndexInfoList = columnIndexInfoList; this.columnIndexList = columnIndexList;
return this; return this;
} }
@ -231,7 +231,7 @@ public class IndexInfo implements Serializable, Cloneable {
", tableName='" + tableName + '\'' + ", tableName='" + tableName + '\'' +
", schema='" + schema + '\'' + ", schema='" + schema + '\'' +
", catalog='" + catalog + '\'' + ", catalog='" + catalog + '\'' +
", columnIndexInfoList=" + columnIndexInfoList + ", columnIndexInfoList=" + columnIndexList +
'}'; '}';
} }
} }