Skip to content

Commit

Permalink
refactor: 优化表结构解析
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 21, 2023
1 parent fabfe64 commit 4d72022
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ private void handleColumnAnnotation(PropertyDescriptor descriptor, Set<Annotatio
}
customColumn(descriptor, field, metadata, annotations);

if(metadata.getType().isNumber() || !metadata.getType().isLengthSupport()){
if (metadata.getType().sqlTypeIsNumber()) {
metadata.setLength(metadata.getPrecision());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ default boolean isScaleSupport() {
default boolean isLengthSupport() {
return isScaleSupport() ||
getSqlType() == JDBCType.VARCHAR ||
getSqlType() == JDBCType.CHAR||
getSqlType() == JDBCType.CHAR ||
getSqlType() == JDBCType.NVARCHAR
;
}

default boolean isNumber(){
default boolean isNumber() {
return DataTypeUtils.typeIsNumber(this);
}

default boolean sqlTypeIsNumber() {
return DataTypeUtils.sqlTypeIsNumber(this.getSqlType());
}

static DataType custom(String id, String name, SQLType sqlType, Class<?> javaType) {
return CustomDataType.of(id, name, sqlType, javaType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,19 @@ public void setJdbcType(SQLType jdbcType, Class<?> javaType) {
setType(JdbcDataType.of(jdbcType, javaType));
}

public int getLength(int length) {
if (this.length <= 0) {
return length;
}
return this.length;
}

public int getPrecision(int defaultPrecision) {
if (precision <= 0) {
if (length <= 0) {
return defaultPrecision;
} else {
return length;
return Math.min(length, defaultPrecision);
}
}
return precision;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public DefaultDialect() {
defaultDataTypeBuilder = (meta) -> meta.getType().getName().toLowerCase();

registerDataType("varchar", DataType.builder(DataType.jdbc(JDBCType.VARCHAR, String.class),
column -> "varchar(" + column.getLength() + ")"));
column -> "varchar(" + column.getLength(255) + ")"));

registerDataType("nvarchar", DataType.builder(DataType.jdbc(JDBCType.NVARCHAR, String.class),
column -> "nvarchar(" + column.getLength() + ")"));
column -> "nvarchar(" + column.getLength(255) + ")"));

registerDataType("decimal", DataType.builder(DataType.jdbc(JDBCType.DECIMAL, BigDecimal.class),
column -> "decimal(" + column.getPrecision(32) + "," + column.getScale() + ")"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ protected String getTableMetaSql(String tname) {

@Override
protected String getTableCommentSql(String tname) {
return "select cast(p.value as varchar(500)) as comment " +
"from sys.extended_properties p " +
" where p.major_id=object_id(#{table}) and p.minor_id=0";
return "select cast(ep.value as nvarchar(500)) as [comment],t.name as [table_name]" +
" from sys.tables t" +
" LEFT JOIN"+
" sys.extended_properties ep ON t.object_id = ep.major_id AND ep.name = 'MS_Description'"+
" where ep.minor_id=0 and t.schema_id = SCHEMA_ID(#{schema}) and t.name like #{table}";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class PostgresqlDialect extends DefaultDialect {

public PostgresqlDialect() {

addDataTypeBuilder(JDBCType.CHAR, (meta) -> StringUtils.concat("char(", meta.getLength(), ")"));
addDataTypeBuilder(JDBCType.VARCHAR, (meta) -> StringUtils.concat("varchar(", meta.getLength(), ")"));
addDataTypeBuilder(JDBCType.CHAR, (meta) -> StringUtils.concat("char(", meta.getLength(255), ")"));
addDataTypeBuilder(JDBCType.VARCHAR, (meta) -> StringUtils.concat("varchar(", meta.getLength(255), ")"));
addDataTypeBuilder(JDBCType.TIMESTAMP, (meta) -> "timestamp");
addDataTypeBuilder(JDBCType.TIME, (meta) -> "time");
addDataTypeBuilder(JDBCType.DATE, (meta) -> "date");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,14 @@ public class PostgresqlTableMetadataParser extends RDBTableMetadataParser {
" and table_name like #{table}"
);

private static final String TABLE_COMMENT_SQL = String.join(" ",
"select"
, "relname::varchar as \"table_name\","
, "cast(obj_description(relfilenode,'pg_class') as varchar) as \"comment\" "
, "from pg_class c",
"where relname like #{table} and relkind = 'r' and relname not like 'pg_%'",
"and relname not like 'sql_%'"
private static final String TABLE_COMMENT_SQL = String.join(" "
, "SELECT DISTINCT \"table_name\", cast(obj_description(c.oid,'pg_class') as varchar) AS \"comment\""
, "FROM information_schema.tables t"
, "JOIN pg_class c ON t.table_name = c.relname"
, "JOIN pg_description d ON c.oid = d.objoid"
, "WHERE t.table_schema = #{schema}"
, "AND d.objsubid = 0"
, "AND t.table_name LIKE #{table}"
);

private static final String ALL_TABLE_SQL = "select table_name::varchar as \"name\" from information_schema.TABLES where table_schema=#{schema}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public static boolean typeIsDate(DataType type) {

}

public static boolean typeIsNumber(DataType type) {
if (type == null) {
return false;
}

SQLType sqlType = type.getSqlType();
public static boolean sqlTypeIsNumber(SQLType sqlType) {
if (sqlType != null) {
if (sqlType instanceof JDBCType)
switch (((JDBCType) sqlType)) {
Expand All @@ -71,6 +66,17 @@ public static boolean typeIsNumber(DataType type) {
return true;
}
}
return false;
}

public static boolean typeIsNumber(DataType type) {
if (type == null) {
return false;
}

if (sqlTypeIsNumber(type.getSqlType())) {
return true;
}

if (type.getJavaType() != null) {
Class<?> clazz = type.getJavaType();
Expand Down

0 comments on commit 4d72022

Please sign in to comment.