mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add field
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package cn.hutool.db.dialect.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
@@ -10,7 +10,11 @@ import cn.hutool.db.Page;
|
||||
import cn.hutool.db.StatementUtil;
|
||||
import cn.hutool.db.dialect.Dialect;
|
||||
import cn.hutool.db.dialect.DialectName;
|
||||
import cn.hutool.db.sql.*;
|
||||
import cn.hutool.db.sql.Condition;
|
||||
import cn.hutool.db.sql.LogicalOperator;
|
||||
import cn.hutool.db.sql.Query;
|
||||
import cn.hutool.db.sql.SqlBuilder;
|
||||
import cn.hutool.db.sql.Wrapper;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@@ -129,7 +133,7 @@ public class AnsiSqlDialect implements Dialect {
|
||||
|
||||
@Override
|
||||
public PreparedStatement psForCount(Connection conn, Query query) throws SQLException {
|
||||
query.setFields(CollectionUtil.newArrayList("count(1)"));
|
||||
query.setFields(ListUtil.toList("count(1)"));
|
||||
return psForFind(conn, query);
|
||||
}
|
||||
|
||||
|
@@ -1,51 +1,88 @@
|
||||
package cn.hutool.db.meta;
|
||||
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.db.DbRuntimeException;
|
||||
|
||||
/**
|
||||
* 数据库表的列信息
|
||||
*
|
||||
* @author loolly
|
||||
*
|
||||
* @author loolly
|
||||
*/
|
||||
public class Column implements Serializable, Cloneable {
|
||||
private static final long serialVersionUID = 577527740359719367L;
|
||||
|
||||
// ----------------------------------------------------- Fields start
|
||||
/** 表名 */
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String tableName;
|
||||
|
||||
/** 列名 */
|
||||
/**
|
||||
* 列名
|
||||
*/
|
||||
private String name;
|
||||
/** 类型,对应java.sql.Types中的类型 */
|
||||
/**
|
||||
* 类型,对应java.sql.Types中的类型
|
||||
*/
|
||||
private int type;
|
||||
/** 类型名称 */
|
||||
/**
|
||||
* 类型名称
|
||||
*/
|
||||
private String typeName;
|
||||
/** 大小或数据长度 */
|
||||
/**
|
||||
* 大小或数据长度
|
||||
*/
|
||||
private int size;
|
||||
/** 是否为可空 */
|
||||
private Integer digit;
|
||||
/**
|
||||
* 是否为可空
|
||||
*/
|
||||
private boolean isNullable;
|
||||
/** 注释 */
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
private String comment;
|
||||
/**
|
||||
* 是否自增
|
||||
*/
|
||||
private boolean autoIncrement;
|
||||
/**
|
||||
* 是否为主键
|
||||
*/
|
||||
private boolean isPk;
|
||||
// ----------------------------------------------------- Fields end
|
||||
|
||||
/**
|
||||
* 创建列对象
|
||||
*
|
||||
* @param tableName 表名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param columnMetaRs 列元信息的ResultSet
|
||||
* @return 列对象
|
||||
* @deprecated 请使用 {@link #create(Table, ResultSet)}
|
||||
*/
|
||||
public static Column create(String tableName, ResultSet columnMetaRs) {
|
||||
return new Column(tableName, columnMetaRs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建列对象
|
||||
*
|
||||
* @param columnMetaRs 列元信息的ResultSet
|
||||
* @param table 表信息
|
||||
* @return 列对象
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public static Column create(Table table, ResultSet columnMetaRs) {
|
||||
return new Column(table, columnMetaRs);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*/
|
||||
@@ -54,10 +91,12 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param tableName 表名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param columnMetaRs Meta信息的ResultSet
|
||||
* @deprecated 请使用 {@link #Column(Table, ResultSet)}
|
||||
*/
|
||||
@Deprecated
|
||||
public Column(String tableName, ResultSet columnMetaRs) {
|
||||
try {
|
||||
init(tableName, columnMetaRs);
|
||||
@@ -65,30 +104,78 @@ public class Column implements Serializable, Cloneable {
|
||||
throw new DbRuntimeException(StrUtil.format("Get table [{}] meta info error!", tableName));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param table 表信息
|
||||
* @param columnMetaRs Meta信息的ResultSet
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public Column(Table table, ResultSet columnMetaRs) {
|
||||
try {
|
||||
init(table, columnMetaRs);
|
||||
} catch (SQLException e) {
|
||||
throw new DbRuntimeException(StrUtil.format("Get table [{}] meta info error!", tableName));
|
||||
}
|
||||
}
|
||||
// ----------------------------------------------------- Constructor end
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param tableName 表名
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @param columnMetaRs 列的meta ResultSet
|
||||
* @throws SQLException SQL执行异常
|
||||
* @deprecated 请使用 {@link #init(Table, ResultSet)}
|
||||
*/
|
||||
@Deprecated
|
||||
public void init(String tableName, ResultSet columnMetaRs) throws SQLException {
|
||||
init(Table.create(tableName), columnMetaRs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化
|
||||
*
|
||||
* @param table 表信息
|
||||
* @param columnMetaRs 列的meta ResultSet
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public void init(String tableName, ResultSet columnMetaRs) throws SQLException {
|
||||
this.tableName = tableName;
|
||||
public void init(Table table, ResultSet columnMetaRs) throws SQLException {
|
||||
this.tableName = table.getTableName();
|
||||
|
||||
this.name = columnMetaRs.getString("COLUMN_NAME");
|
||||
this.isPk = table.isPk(this.name);
|
||||
|
||||
this.type = columnMetaRs.getInt("DATA_TYPE");
|
||||
this.typeName = columnMetaRs.getString("TYPE_NAME");
|
||||
this.size = columnMetaRs.getInt("COLUMN_SIZE");
|
||||
this.isNullable = columnMetaRs.getBoolean("NULLABLE");
|
||||
this.comment = columnMetaRs.getString("REMARKS");
|
||||
|
||||
// 保留小数位数
|
||||
try {
|
||||
this.digit = columnMetaRs.getInt("DECIMAL_DIGITS");
|
||||
} catch (SQLException ignore) {
|
||||
//某些驱动可能不支持,跳过
|
||||
}
|
||||
|
||||
// 是否自增
|
||||
try {
|
||||
String auto = columnMetaRs.getString("IS_AUTOINCREMENT");
|
||||
if (BooleanUtil.toBoolean(auto)) {
|
||||
this.autoIncrement = true;
|
||||
}
|
||||
} catch (SQLException ignore) {
|
||||
//某些驱动可能不支持,跳过
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- Getters and Setters start
|
||||
|
||||
/**
|
||||
* 获取表名
|
||||
*
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
public String getTableName() {
|
||||
@@ -97,7 +184,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置表名
|
||||
*
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return this
|
||||
*/
|
||||
@@ -108,7 +195,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取列名
|
||||
*
|
||||
*
|
||||
* @return 列名
|
||||
*/
|
||||
public String getName() {
|
||||
@@ -117,7 +204,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置列名
|
||||
*
|
||||
*
|
||||
* @param name 列名
|
||||
* @return this
|
||||
*/
|
||||
@@ -128,17 +215,17 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取字段类型的枚举
|
||||
*
|
||||
*
|
||||
* @return 阻断类型枚举
|
||||
* @since 4.5.8
|
||||
*/
|
||||
public JdbcType getTypeEnum() {
|
||||
return JdbcType.valueOf(this.type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取类型,对应{@link java.sql.Types}中的类型
|
||||
*
|
||||
*
|
||||
* @return 类型
|
||||
*/
|
||||
public int getType() {
|
||||
@@ -147,7 +234,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置类型,对应java.sql.Types中的类型
|
||||
*
|
||||
*
|
||||
* @param type 类型
|
||||
* @return this
|
||||
*/
|
||||
@@ -155,19 +242,19 @@ public class Column implements Serializable, Cloneable {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取类型名称
|
||||
*
|
||||
*
|
||||
* @return 类型名称
|
||||
*/
|
||||
public String getTypeName() {
|
||||
return typeName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 设置类型名称
|
||||
*
|
||||
*
|
||||
* @param typeName 类型名称
|
||||
* @return this
|
||||
*/
|
||||
@@ -178,7 +265,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取大小或数据长度
|
||||
*
|
||||
*
|
||||
* @return 大小或数据长度
|
||||
*/
|
||||
public int getSize() {
|
||||
@@ -187,7 +274,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置大小或数据长度
|
||||
*
|
||||
*
|
||||
* @param size 大小或数据长度
|
||||
* @return this
|
||||
*/
|
||||
@@ -196,9 +283,29 @@ public class Column implements Serializable, Cloneable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取小数位数
|
||||
*
|
||||
* @return 大小或数据长度
|
||||
*/
|
||||
public int getDigit() {
|
||||
return digit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置小数位数
|
||||
*
|
||||
* @param digit 小数位数
|
||||
* @return this
|
||||
*/
|
||||
public Column setDigit(int digit) {
|
||||
this.digit = digit;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为可空
|
||||
*
|
||||
*
|
||||
* @return 是否为可空
|
||||
*/
|
||||
public boolean isNullable() {
|
||||
@@ -207,7 +314,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置是否为可空
|
||||
*
|
||||
*
|
||||
* @param isNullable 是否为可空
|
||||
* @return this
|
||||
*/
|
||||
@@ -218,7 +325,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取注释
|
||||
*
|
||||
*
|
||||
* @return 注释
|
||||
*/
|
||||
public String getComment() {
|
||||
@@ -227,7 +334,7 @@ public class Column implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置注释
|
||||
*
|
||||
*
|
||||
* @param comment 注释
|
||||
* @return this
|
||||
*/
|
||||
@@ -235,6 +342,50 @@ public class Column implements Serializable, Cloneable {
|
||||
this.comment = comment;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否自增
|
||||
*
|
||||
* @return 是否自增
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public boolean isAutoIncrement() {
|
||||
return autoIncrement;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否自增
|
||||
*
|
||||
* @param autoIncrement 是否自增
|
||||
* @return this
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public Column setAutoIncrement(boolean autoIncrement) {
|
||||
this.autoIncrement = autoIncrement;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否主键
|
||||
*
|
||||
* @return 是否主键
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public boolean isPk() {
|
||||
return isPk;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否主键
|
||||
*
|
||||
* @param isPk 是否主键
|
||||
* @return this
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public Column setPk(boolean isPk) {
|
||||
this.isPk = isPk;
|
||||
return this;
|
||||
}
|
||||
// ----------------------------------------------------- Getters and Setters end
|
||||
|
||||
@Override
|
||||
|
@@ -186,8 +186,10 @@ public class MetaUtil {
|
||||
conn = ds.getConnection();
|
||||
|
||||
// catalog和schema获取失败默认使用null代替
|
||||
String catalog = getCataLog(conn);
|
||||
String schema = getSchema(conn);
|
||||
final String catalog = getCataLog(conn);
|
||||
table.setCatalog(catalog);
|
||||
final String schema = getSchema(conn);
|
||||
table.setSchema(schema);
|
||||
|
||||
final DatabaseMetaData metaData = conn.getMetaData();
|
||||
|
||||
@@ -213,7 +215,7 @@ public class MetaUtil {
|
||||
try (ResultSet rs = metaData.getColumns(catalog, schema, tableName, null)) {
|
||||
if (null != rs) {
|
||||
while (rs.next()) {
|
||||
table.setColumn(Column.create(tableName, rs));
|
||||
table.setColumn(Column.create(table, rs));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -9,18 +9,31 @@ import java.util.Set;
|
||||
|
||||
/**
|
||||
* 数据库表信息
|
||||
*
|
||||
* @author loolly
|
||||
*
|
||||
* @author loolly
|
||||
*/
|
||||
public class Table implements Serializable, Cloneable {
|
||||
private static final long serialVersionUID = -810699625961392983L;
|
||||
|
||||
/** 表名 */
|
||||
/**
|
||||
* table所在的schema
|
||||
*/
|
||||
private String schema;
|
||||
/**
|
||||
* tables所在的catalog
|
||||
*/
|
||||
private String catalog;
|
||||
/**
|
||||
* 表名
|
||||
*/
|
||||
private String tableName;
|
||||
/** 注释 */
|
||||
/**
|
||||
* 注释
|
||||
*/
|
||||
private String comment;
|
||||
/** 主键字段名列表 */
|
||||
/**
|
||||
* 主键字段名列表
|
||||
*/
|
||||
private Set<String> pkNames = new LinkedHashSet<>();
|
||||
private final Map<String, Column> columns = new LinkedHashMap<>();
|
||||
|
||||
@@ -29,9 +42,10 @@ public class Table implements Serializable, Cloneable {
|
||||
}
|
||||
|
||||
// ----------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public Table(String tableName) {
|
||||
@@ -40,9 +54,54 @@ public class Table implements Serializable, Cloneable {
|
||||
// ----------------------------------------------------- Constructor end
|
||||
|
||||
// ----------------------------------------------------- Getters and Setters start
|
||||
|
||||
/**
|
||||
* 获取 schema
|
||||
*
|
||||
* @return schema
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public String getSchema() {
|
||||
return schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置schema
|
||||
*
|
||||
* @param schema schema
|
||||
* @return this
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public Table setSchema(String schema) {
|
||||
this.schema = schema;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取catalog
|
||||
*
|
||||
* @return catalog
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public String getCatalog() {
|
||||
return catalog;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置catalog
|
||||
*
|
||||
* @param catalog catalog
|
||||
* @return this
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public Table setCatalog(String catalog) {
|
||||
this.catalog = catalog;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取表名
|
||||
*
|
||||
*
|
||||
* @return 表名
|
||||
*/
|
||||
public String getTableName() {
|
||||
@@ -51,7 +110,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置表名
|
||||
*
|
||||
*
|
||||
* @param tableName 表名
|
||||
*/
|
||||
public void setTableName(String tableName) {
|
||||
@@ -60,7 +119,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取注释
|
||||
*
|
||||
*
|
||||
* @return 注释
|
||||
*/
|
||||
public String getComment() {
|
||||
@@ -69,7 +128,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置注释
|
||||
*
|
||||
*
|
||||
* @param comment 注释
|
||||
* @return this
|
||||
*/
|
||||
@@ -80,16 +139,27 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取主键列表
|
||||
*
|
||||
*
|
||||
* @return 主键列表
|
||||
*/
|
||||
public Set<String> getPkNames() {
|
||||
return pkNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定列名是否为主键
|
||||
*
|
||||
* @param columnName 列名
|
||||
* @return 是否为主键
|
||||
* @since 5.4.3
|
||||
*/
|
||||
public boolean isPk(String columnName){
|
||||
return getPkNames().contains(columnName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置主键列表
|
||||
*
|
||||
*
|
||||
* @param pkNames 主键列表
|
||||
*/
|
||||
public void setPkNames(Set<String> pkNames) {
|
||||
@@ -99,7 +169,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 设置列对象
|
||||
*
|
||||
*
|
||||
* @param column 列对象
|
||||
* @return 自己
|
||||
*/
|
||||
@@ -110,7 +180,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取某列信息
|
||||
*
|
||||
*
|
||||
* @param name 列名
|
||||
* @return 列对象
|
||||
* @since 4.2.2
|
||||
@@ -121,7 +191,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 获取所有字段元信息
|
||||
*
|
||||
*
|
||||
* @return 字段元信息集合
|
||||
* @since 4.5.8
|
||||
*/
|
||||
@@ -131,7 +201,7 @@ public class Table implements Serializable, Cloneable {
|
||||
|
||||
/**
|
||||
* 添加主键
|
||||
*
|
||||
*
|
||||
* @param pkColumnName 主键的列名
|
||||
* @return 自己
|
||||
*/
|
||||
|
Reference in New Issue
Block a user