From 128f502c64df5c48c140b5233ad0925fc2b29ce6 Mon Sep 17 00:00:00 2001 From: "j.x" Date: Sun, 21 Jul 2024 17:59:33 +0800 Subject: [PATCH] fix[data]: verification postgresql and solve type problem and fix DbCommandExecutor about bool type error etc. --- uno-bom/pom.xml | 4 +- .../java/cc/allio/uno/core/api/Single.java | 7 +- .../concurrent/MultiCheckedException.java | 5 +- .../cc/allio/uno/core/util/JsonUtils.java | 2 + .../cc/allio/uno/data/orm/dsl/DSLName.java | 54 +++++- .../data/orm/dsl/ddl/ShowColumnsOperator.java | 1 + .../data/orm/dsl/ddl/ShowTablesOperator.java | 1 + .../data/orm/dsl/type/BooleanJavaType.java | 10 +- .../uno/data/orm/dsl/type/ByteJavaType.java | 10 +- .../allio/uno/data/orm/dsl/type/DSLType.java | 12 +- .../uno/data/orm/dsl/type/EnumJavaType.java | 1 + .../uno/data/orm/dsl/type/FloatJavaType.java | 9 +- .../data/orm/dsl/type/IntegerJavaType.java | 9 +- .../uno/data/orm/dsl/type/LongJavaType.java | 9 +- .../uno/data/orm/dsl/type/ShortJavaType.java | 9 +- .../uno/data/orm/dsl/type/TypeRegistry.java | 154 ++++++++++++------ .../data/orm/executor/DMLCommandExecutor.java | 6 +- .../handler/BeanResultSetHandler.java | 5 + .../executor/handler/BoolResultHandler.java | 5 + .../handler/CohesionListResultSetHandler.java | 38 +++++ .../ColumnDefListResultSetHandler.java | 14 +- .../handler/DefaultListResultSetHandler.java | 5 + .../handler/DefaultResultSetHandler.java | 5 + .../handler/ListBeanResultSetHandler.java | 5 + .../handler/ListMapResultHandler.java | 5 + .../handler/ListResultSetHandler.java | 8 + .../executor/handler/MapResultSetHandler.java | 6 + .../executor/handler/ResultSetHandler.java | 10 ++ .../handler/TableListResultSetHandler.java | 7 + .../orm/executor/db/DbCommandExecutor.java | 83 +++++++--- .../executor/db/DbStatementSetHandler.java | 30 +++- .../dsl/sql/ddl/SQLShowColumnsOperator.java | 27 ++- .../dsl/sql/ddl/SQLShowTablesOperator.java | 35 ++-- .../dsl/sql/dialect/type/DSLTypeDelegate.java | 5 + .../sql/dialect/type/H2SQLTypeDelegate.java | 6 + .../dialect/type/PostgreSQLTypeDelegate.java | 10 +- 36 files changed, 489 insertions(+), 123 deletions(-) create mode 100644 uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/CohesionListResultSetHandler.java diff --git a/uno-bom/pom.xml b/uno-bom/pom.xml index 1aea8705..bd080646 100644 --- a/uno-bom/pom.xml +++ b/uno-bom/pom.xml @@ -46,7 +46,7 @@ 1.2.20 8.0.22 12.2.0.1 - 42.2.22 + 42.7.3 8.4.1.jre8 4.3.0 @@ -417,4 +417,4 @@ https://packages.aliyun.com/maven/repository/2300285-snapshot-JAW5GT/ - \ No newline at end of file + diff --git a/uno-core/src/main/java/cc/allio/uno/core/api/Single.java b/uno-core/src/main/java/cc/allio/uno/core/api/Single.java index 1241aaf0..cdc0fc0e 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/api/Single.java +++ b/uno-core/src/main/java/cc/allio/uno/core/api/Single.java @@ -1,7 +1,6 @@ package cc.allio.uno.core.api; import cc.allio.uno.core.exception.Exceptions; -import cc.allio.uno.core.util.CollectionUtils; import java.util.List; import java.util.Map; @@ -28,7 +27,7 @@ public interface Single { * @return the {@link Single} instance */ static Single from(List list) { - if (CollectionUtils.isEmpty(list)) { + if (list == null) { throw Exceptions.unNull("list"); } return new InternalSingle<>(list.stream()); @@ -41,7 +40,7 @@ static Single from(List list) { * @return the {@link Single} instance */ static Single from(Set set) { - if (CollectionUtils.isEmpty(set)) { + if (set == null) { throw Exceptions.unNull("set"); } return new InternalSingle<>(set.stream()); @@ -54,7 +53,7 @@ static Single from(Set set) { * @return the {@link Single} instance */ static Single from(Map map) { - if (CollectionUtils.isEmpty(map)) { + if (map == null) { throw Exceptions.unNull("map"); } return (Single) new InternalSingle<>(map.entrySet().stream()); diff --git a/uno-core/src/main/java/cc/allio/uno/core/concurrent/MultiCheckedException.java b/uno-core/src/main/java/cc/allio/uno/core/concurrent/MultiCheckedException.java index 64d52948..306240f2 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/concurrent/MultiCheckedException.java +++ b/uno-core/src/main/java/cc/allio/uno/core/concurrent/MultiCheckedException.java @@ -44,9 +44,10 @@ public T findUncheckedException(Class errType) { return (T) unchecked.get(errType); } var err = Single.from(unchecked.entrySet()) - .forReturn(errEntry -> errEntry.getKey().isAssignableFrom(errType)) + .forReturn(errEntry -> errType.isAssignableFrom(errEntry.getKey())) .map(Map.Entry::getValue) .orElse(null); + return (T) err; } @@ -62,7 +63,7 @@ public T findCheckedException(Class errType) { return (T) checked.get(errType); } var err = Single.from(checked.entrySet()) - .forReturn(errEntry -> errEntry.getKey().isAssignableFrom(errType)) + .forReturn(errEntry -> errType.isAssignableFrom(errEntry.getKey())) .map(Map.Entry::getValue) .orElse(null); diff --git a/uno-core/src/main/java/cc/allio/uno/core/util/JsonUtils.java b/uno-core/src/main/java/cc/allio/uno/core/util/JsonUtils.java index 4663ccfc..e7461f3e 100644 --- a/uno-core/src/main/java/cc/allio/uno/core/util/JsonUtils.java +++ b/uno-core/src/main/java/cc/allio/uno/core/util/JsonUtils.java @@ -1,6 +1,7 @@ package cc.allio.uno.core.util; import cc.allio.uno.core.exception.Exceptions; +import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.json.JsonReadFeature; @@ -445,6 +446,7 @@ public static ObjectMapper newJsonMapper() { // 失败处理 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 反序列化时,属性不存在的兼容处理s mapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 单引号处理 diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java index 1d4c74f8..e00bffce 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/DSLName.java @@ -7,7 +7,6 @@ import lombok.Getter; import lombok.Setter; -import java.util.Objects; import java.util.regex.Pattern; /** @@ -47,12 +46,15 @@ public class DSLName implements Comparable, EqualsTo, Meta> extends P String TABLE_SCHEMA_FILED = "TABLE_SCHEMA"; String TABLE_NAME_FILED = "TABLE_NAME"; String COLUMN_NAME_FIELD = "COLUMN_NAME"; + String COLUMN_COMMENT_FIELD = "COLUMN_COMMENT"; String ORDINAL_POSITION_FIELD = "ORDINAL_POSITION"; String COLUMN_DEFAULT_FIELD = "COLUMN_DEFAULT"; String IS_NULLABLE_FIELD = "IS_NULLABLE"; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java index 5e4343cc..1f10a03e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/ddl/ShowTablesOperator.java @@ -18,6 +18,7 @@ public interface ShowTablesOperator> extends Tab String TABLE_SCHEMA_FILED = "TABLE_SCHEMA"; String TABLE_NAME_FILED = "TABLE_NAME"; String TABLE_TYPE_FILED = "TABLE_TYPE"; + String TABLE_COMMENT_FILED = "TABLE_COMMENT"; /** * 转换为{@link QueryOperator} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/BooleanJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/BooleanJavaType.java index 17fb9499..1a62212d 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/BooleanJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/BooleanJavaType.java @@ -10,6 +10,7 @@ * @since 1.1.4 */ public class BooleanJavaType extends JavaTypeImpl { + @Override public Class getJavaType() { return Types.BOOLEAN; @@ -17,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Boolean.class.isAssignableFrom(otherJavaType) - || boolean.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return boolean.class.isAssignableFrom(otherJavaType); + } + return Boolean.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ByteJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ByteJavaType.java index 157a3fe7..3a8719a9 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ByteJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ByteJavaType.java @@ -10,6 +10,7 @@ * @since 1.1.4 */ public class ByteJavaType extends JavaTypeImpl { + @Override public Class getJavaType() { return Types.BYTE; @@ -17,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Byte.class.isAssignableFrom(otherJavaType) - || byte.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return byte.class.isAssignableFrom(otherJavaType); + } + return Byte.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DSLType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DSLType.java index b2ca9f87..4c2bdaf7 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DSLType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/DSLType.java @@ -22,6 +22,7 @@ public interface DSLType extends EqualsTo { DSLType BIGINT = DefaultDSLType.BIGINT; DSLType SMALLINT = DefaultDSLType.SMALLINT; + DSLType INT = DefaultDSLType.INT; DSLType INTEGER = DefaultDSLType.INTEGER; DSLType BIT = DefaultDSLType.BIT; DSLType TINYINT = DefaultDSLType.TINYINT; @@ -44,6 +45,9 @@ public interface DSLType extends EqualsTo { DSLType VARBINARY = DefaultDSLType.VARBINARY; DSLType LONGVARBINARY = DefaultDSLType.LONGVARBINARY; + // ====================== 其他类型 ====================== + DSLType BOOLEAN = DefaultDSLType.BOOLEAN; + // ====================== 高级类型 ====================== DSLType OBJECT = DefaultDSLType.OBJECT; DSLType ARRAY = DefaultDSLType.ARRAY; @@ -102,7 +106,7 @@ static DSLType getByJdbcCode(int jdbcCode) { */ @Override default boolean equalsTo(DSLType other) { - return java.util.Objects.equals(this.getName(), other.getName()); + return other != null && this.getJdbcType() == other.getJdbcType(); } /** @@ -167,7 +171,8 @@ enum DefaultDSLType implements DSLType { // ====================== 数字型 ====================== BIGINT("bigint", Types.BIGINT, 64, null), SMALLINT("smallint", Types.SMALLINT, 32, null), - INTEGER("int", Types.INTEGER, 64, null), + INT("int", Types.INTEGER, 64, null), + INTEGER("integer", Types.INTEGER, 64, null), BIT("bit", Types.BIT, 4, null), TINYINT("tinyint", Types.TINYINT, 16, null), NUMBER("number", Types.NUMERIC, 12, 2), @@ -190,6 +195,9 @@ enum DefaultDSLType implements DSLType { VARBINARY("varbinary", Types.VARBINARY, 1024, null), LONGVARBINARY("longvarchar", Types.LONGVARBINARY, 2048, null), + // ====================== 其他类型 ====================== + BOOLEAN("char", Types.BOOLEAN, 0, null), + // ====================== 高级类型 ====================== OBJECT("object", Types.JAVA_OBJECT, null, null), ARRAY("array", Types.ARRAY, null, null); diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/EnumJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/EnumJavaType.java index 61d3c411..0c3169e3 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/EnumJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/EnumJavaType.java @@ -8,6 +8,7 @@ * @since 1.1.4 */ public class EnumJavaType extends JavaTypeImpl { + @Override public Class getJavaType() { return Enum.class; diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/FloatJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/FloatJavaType.java index 902507c2..fd393ca1 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/FloatJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/FloatJavaType.java @@ -18,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Float.class.isAssignableFrom(otherJavaType) - || float.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return float.class.isAssignableFrom(otherJavaType); + } + return Float.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/IntegerJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/IntegerJavaType.java index f4d1f16e..0ede2375 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/IntegerJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/IntegerJavaType.java @@ -18,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Integer.class.isAssignableFrom(otherJavaType) - || int.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return int.class.isAssignableFrom(otherJavaType); + } + return Integer.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/LongJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/LongJavaType.java index 3a7b8edb..e17f2c66 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/LongJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/LongJavaType.java @@ -18,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Long.class.isAssignableFrom(otherJavaType) - || long.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return long.class.isAssignableFrom(otherJavaType); + } + return Long.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ShortJavaType.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ShortJavaType.java index 3e9b3edf..adba05b0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ShortJavaType.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/ShortJavaType.java @@ -18,7 +18,12 @@ public Class getJavaType() { @Override public boolean equalsTo(Class otherJavaType) { - return Short.class.isAssignableFrom(otherJavaType) - || short.class.isAssignableFrom(otherJavaType); + if (otherJavaType == null) { + return false; + } + if (otherJavaType.isPrimitive()) { + return short.class.isAssignableFrom(otherJavaType); + } + return Short.class.isAssignableFrom(otherJavaType); } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/TypeRegistry.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/TypeRegistry.java index 35d5565b..b6d60303 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/TypeRegistry.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/dsl/type/TypeRegistry.java @@ -5,14 +5,21 @@ import java.sql.Types; import java.util.*; -import java.util.stream.Collectors; /** - * type register + * Type Registry is about of {@link JavaType} and {@link JdbcType} registry. it description relation of {@link JavaType} and {@link JdbcType} + *

+ * first, it will be registration abound of {@link JavaType} and {@link JdbcType} instance to current class. + * then build relation of {@link JavaType} and {@link JdbcType}. + *

+ *

+ * {@link TypeRegistry} supply abundant method about of find {@link JavaType} and {@link JdbcType}, like as {@link #findJdbcType(Integer)}, {@link #findJavaType(Integer)} etc. + *

* * @author j.x * @date 2023/1/13 16:00 * @since 1.1.4 + * @modify 1.1.9 */ public final class TypeRegistry { @@ -42,6 +49,7 @@ private TypeRegistry() { initNumericTypes(); initCharTypes(); initDateTypes(); + initOtherTypes(); initComplexTypes(); initUnknownTypes(); } @@ -345,10 +353,6 @@ private void initJavaType() { */ private void initNumericTypes() { // ----------- numeric types ----------- - registerRelation( - getJdbcType(Types.BOOLEAN), - getJavaType(ByteJavaType.class) - ); // smallint registerRelation( getJdbcType(Types.SMALLINT), @@ -475,6 +479,16 @@ private void initCharTypes() { ); } + /** + * init other types, like boolean + */ + private void initOtherTypes() { + registerRelation( + getJdbcType(Types.BOOLEAN), + getJavaType(BooleanJavaType.class) + ); + } + /** * 初始化复杂类型,如blob、clob */ @@ -549,6 +563,36 @@ private void initUnknownTypes() { } + /** + * 注册java type + * + * @param javaType javaType INSTANCE + */ + void registerJavaType(JavaType javaType) { + javaTypes.put((Class>) javaType.getClass(), javaType); + } + + /** + * 注册 jdbc type + * + * @param jdbcType jdbcType INSTANCE + */ + void registerJdbcType(JdbcType jdbcType) { + jdbcTypes.put(jdbcType.getJdbcCode(), jdbcType); + } + + /** + * key = jdbcType + * value = javaType + * + * @param jdbcType jdbc type + * @param javaType java type + */ + void registerRelation(JdbcType jdbcType, JavaType javaType) { + jdbcTypesMappings.put(jdbcType.getJdbcCode(), javaType); + javaTypeMappings.put(javaType, jdbcType.getJdbcCode()); + } + /** * 获取jdbc types * @@ -638,7 +682,7 @@ public JdbcType findJdbcType(Integer jdbcCode) { public JavaType findJavaType(Integer jdbcCode) { Collection> types = jdbcTypesMappings.get(jdbcCode); if (CollectionUtils.isNotEmpty(types)) { - return Lists.newArrayList(types).get(0); + return Lists.newArrayList(types).getFirst(); } return null; } @@ -651,39 +695,7 @@ public JavaType findJavaType(Integer jdbcCode) { */ public Collection findJdbcTypeFindJavaType(JavaType javaType) { Collection jdbcCodes = javaTypeMappings.get(javaType); - return Collections.unmodifiableCollection( - jdbcCodes.stream().map(jdbcTypes::get).collect(Collectors.toList()) - ); - } - - /** - * 注册java type - * - * @param javaType javaType INSTANCE - */ - public void registerJavaType(JavaType javaType) { - javaTypes.put((Class>) javaType.getClass(), javaType); - } - - /** - * 注册 jdbc type - * - * @param jdbcType jdbcType INSTANCE - */ - public void registerJdbcType(JdbcType jdbcType) { - jdbcTypes.put(jdbcType.getJdbcCode(), jdbcType); - } - - /** - * key = jdbcType - * value = javaType - * - * @param jdbcType jdbc type - * @param javaType java type - */ - public void registerRelation(JdbcType jdbcType, JavaType javaType) { - jdbcTypesMappings.put(jdbcType.getJdbcCode(), javaType); - javaTypeMappings.put(javaType, jdbcType.getJdbcCode()); + return jdbcCodes.stream().map(jdbcTypes::get).toList(); } /** @@ -739,10 +751,7 @@ public JavaType guessJavaType(Class valueType) { */ public Collection guessJdbcType(Object value) { JavaType javaType = guessJavaType(value); - if (javaType != null) { - return findJdbcTypeFindJavaType(javaType); - } - return Collections.emptyList(); + return findJdbcTypeFindJavaType(javaType); } /** @@ -753,10 +762,7 @@ public Collection guessJdbcType(Object value) { */ public Collection guessJdbcType(Class valueType) { JavaType javaType = guessJavaType(valueType); - if (javaType != null) { - return findJdbcTypeFindJavaType(javaType); - } - return Collections.emptyList(); + return findJdbcTypeFindJavaType(javaType); } /** @@ -768,4 +774,54 @@ public static TypeRegistry getInstance() { return INSTANCE; } -} \ No newline at end of file + // ----------------------------- static method ----------------------------- + + /** + * @see #getJdbcTypes() + */ + public static Collection obtainJdbcTypes() { + return getInstance().getJdbcTypes(); + } + + /** + * @see #getJdbcType(Integer) + */ + public static JdbcType obtainJdbcType(Integer code) { + return getInstance().getJdbcType(code); + } + + /** + * @see #getJavaTypes() + */ + public static Collection> obtainJavaTypes() { + return getInstance().getJavaTypes(); + } + + /** + * @see #getJavaTypeByClassType(Class) + */ + public static JavaType obtainJavaTypeByClassType(Class classType) { + return getInstance().getJavaTypeByClassType(classType); + } + + /** + * @see #getJavaType(Class) + */ + public static JavaType obtainJavaType(Class> javaTypeClass) { + return getInstance().getJavaType(javaTypeClass); + } + + /** + * @see #findJavaType(Integer) + */ + public static JavaType obtainJavaType(Integer jdbcCode) { + return getInstance().findJavaType(jdbcCode); + } + + /** + * @see #findJdbcTypeFindJavaType(JavaType) + */ + public static Collection obtainJdbcTypeFindJavaType(JavaType javaType) { + return getInstance().findJdbcTypeFindJavaType(javaType); + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java index 4d7271da..c3afee1b 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/DMLCommandExecutor.java @@ -12,6 +12,7 @@ import cc.allio.uno.data.orm.dsl.exception.DSLException; import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; +import cc.allio.uno.data.orm.executor.handler.CohesionListResultSetHandler; import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; import com.google.common.collect.Lists; @@ -526,9 +527,8 @@ default R queryOne(UnaryOperator> func, ResultSetHandler * @throws DSLException 当结果集大于1时抛出 */ default R queryOne(QueryOperator queryOperator, ResultSetHandler resultSetHandler) { - List resultGroups = queryList(queryOperator); - ResultGroup resultGroup = checkCollectionIsOneAndGet(resultGroups); - return Optional.ofNullable(resultGroup).map(resultSetHandler).orElse(null); + List resultGroups = queryList(queryOperator, new CohesionListResultSetHandler<>(resultSetHandler)); + return checkCollectionIsOneAndGet(resultGroups); } /** diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BeanResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BeanResultSetHandler.java index f90849b6..87cdd0e0 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BeanResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BeanResultSetHandler.java @@ -26,4 +26,9 @@ public R apply(ResultGroup resultGroup) { public Class getBeanType() { return beanClass; } + + @Override + public Class getResultType() { + return getBeanType(); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BoolResultHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BoolResultHandler.java index 7dbf6146..7f06b5f7 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BoolResultHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/BoolResultHandler.java @@ -37,4 +37,9 @@ public Boolean apply(ResultGroup resultGroup) { } return Boolean.FALSE; } + + @Override + public Class getResultType() { + return Boolean.class; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/CohesionListResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/CohesionListResultSetHandler.java new file mode 100644 index 00000000..a566e319 --- /dev/null +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/CohesionListResultSetHandler.java @@ -0,0 +1,38 @@ +package cc.allio.uno.data.orm.executor.handler; + +import cc.allio.uno.data.orm.executor.ResultGroup; +import cc.allio.uno.data.orm.executor.ResultSet; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * decorate {@link ListBeanResultSetHandler} from {@link ResultSetHandler} + * + * @author j.x + * @date 2024/7/21 17:33 + * @since 0.1.1 + */ +public class CohesionListResultSetHandler implements ListResultSetHandler { + + private final ResultSetHandler resultSetHandler; + + public CohesionListResultSetHandler(ResultSetHandler resultSetHandler) { + this.resultSetHandler = resultSetHandler; + } + + @Override + public Class getResultType() { + return resultSetHandler.getResultType(); + } + + @Override + public List apply(ResultSet resultGroups) { + List results = Lists.newArrayList(); + for (ResultGroup resultGroup : resultGroups) { + R r = resultSetHandler.apply(resultGroup); + results.add(r); + } + return results; + } +} diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ColumnDefListResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ColumnDefListResultSetHandler.java index b469e97e..d2494574 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ColumnDefListResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ColumnDefListResultSetHandler.java @@ -39,8 +39,15 @@ public List apply(ResultSet resultSet) { protected ColumnDef.ColumnDefBuilder commonFieldBuilder(ResultGroup r) { ColumnDef.ColumnDefBuilder builder = ColumnDef.builder(); // column name - DSLName dslName = r.getStringValue(ShowColumnsOperator.COLUMN_NAME_FIELD, () -> StringPool.EMPTY, columnName -> DSLName.of(columnName, DSLName.PLAIN_FEATURE)); + DSLName dslName = r.getStringValue( + ShowColumnsOperator.COLUMN_NAME_FIELD, + () -> StringPool.EMPTY, + columnName -> DSLName.of(columnName, DSLName.PLAIN_FEATURE)); builder.dslName(dslName); + + String comment = r.getStringValue(ShowColumnsOperator.COLUMN_COMMENT_FIELD, () -> StringPool.EMPTY); + builder.comment(comment); + // column type ExecutorOptions executorOptions = obtainExecutorOptions(); DBType dbType = executorOptions.getDbType(); @@ -76,4 +83,9 @@ protected DataType buildDataType(DSLType dslType, ResultGroup r) { } return dataType; } + + @Override + public Class getResultType() { + return ColumnDef.class; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultListResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultListResultSetHandler.java index d94eb32f..9a1bda16 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultListResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultListResultSetHandler.java @@ -23,4 +23,9 @@ public List apply(ResultSet resultSet) { } return r; } + + @Override + public Class getResultType() { + return ResultGroup.class; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultResultSetHandler.java index b62c8cd3..20074506 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/DefaultResultSetHandler.java @@ -15,4 +15,9 @@ public class DefaultResultSetHandler extends ExecutorOptionsAwareImpl implements public ResultGroup apply(ResultGroup resultGroup) { return resultGroup; } + + @Override + public Class getResultType() { + return ResultGroup.class; + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListBeanResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListBeanResultSetHandler.java index b1d8ff3b..40a3246e 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListBeanResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListBeanResultSetHandler.java @@ -35,4 +35,9 @@ public List apply(ResultSet resultSet) { public Class getBeanType() { return handler.getBeanType(); } + + @Override + public Class getResultType() { + return handler.getBeanType(); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListMapResultHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListMapResultHandler.java index b8fb5899..f7474eeb 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListMapResultHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListMapResultHandler.java @@ -30,4 +30,9 @@ public List> apply(ResultSet resultSet) { } return r; } + + @Override + public Class> getResultType() { + return handler.getResultType(); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListResultSetHandler.java index 37a5f4cc..e2417c1b 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ListResultSetHandler.java @@ -11,7 +11,15 @@ * @author j.x * @date 2023/4/18 13:29 * @since 1.1.4 + * @see ResultSetHandler */ public interface ListResultSetHandler extends Function>, ResultHandler { + /** + * get result type + * + * @since 1.1.9 + * @return the result type + */ + Class getResultType(); } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/MapResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/MapResultSetHandler.java index 751110d5..092d2334 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/MapResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/MapResultSetHandler.java @@ -2,6 +2,7 @@ import cc.allio.uno.data.orm.executor.ResultGroup; +import java.util.Collections; import java.util.Map; /** @@ -17,4 +18,9 @@ public class MapResultSetHandler extends ExecutorOptionsAwareImpl implements Res public Map apply(ResultGroup resultGroup) { return resultGroup.toMap(); } + + @Override + public Class> getResultType() { + return (Class>) Collections.EMPTY_MAP.getClass(); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ResultSetHandler.java index ac717f48..bcf55256 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/ResultSetHandler.java @@ -1,5 +1,6 @@ package cc.allio.uno.data.orm.executor.handler; +import cc.allio.uno.core.reflect.ReflectTools; import cc.allio.uno.data.orm.executor.ResultGroup; import java.util.function.Function; @@ -13,4 +14,13 @@ */ public interface ResultSetHandler extends Function, ResultHandler { + /** + * get result type + * + * @since 1.1.9 + * @return the result type + */ + default Class getResultType() { + return (Class) ReflectTools.getGenericType(this, ResultSetHandler.class); + } } diff --git a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/TableListResultSetHandler.java b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/TableListResultSetHandler.java index 60180cc6..910cb591 100644 --- a/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/TableListResultSetHandler.java +++ b/uno-data/uno-data-api/src/main/java/cc/allio/uno/data/orm/executor/handler/TableListResultSetHandler.java @@ -21,6 +21,7 @@ public class TableListResultSetHandler extends ExecutorOptionsAwareImpl implemen public static final DSLName TABLE_SCHEMA_DSL_NAME = DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.HUMP_FEATURE); public static final DSLName TABLE_NAME_DSL_NAME = DSLName.of(ShowTablesOperator.TABLE_NAME_FILED, DSLName.HUMP_FEATURE); public static final DSLName TABLE_TYPE_DSL_NAME = DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.HUMP_FEATURE); + public static final DSLName TABLE_COMMENT_DSL_NAME = DSLName.of(ShowTablesOperator.TABLE_COMMENT_FILED, DSLName.HUMP_FEATURE); @Override public List apply(ResultSet resultSet) { @@ -32,8 +33,14 @@ public List
apply(ResultSet resultSet) { r.getOptionStringValue(TABLE_SCHEMA_DSL_NAME, table::setSchema); r.getOptionStringValue(TABLE_NAME_DSL_NAME, name -> table.setName(DSLName.of(name, DSLName.PLAIN_FEATURE))); r.getOptionStringValue(TABLE_TYPE_DSL_NAME, table::setType); + r.getOptionStringValue(TABLE_COMMENT_DSL_NAME, table::setComment); return table; }) .toList(); } + + @Override + public Class
getResultType() { + return Table.class; + } } diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java index 967246b4..d88a74cb 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbCommandExecutor.java @@ -8,10 +8,11 @@ import cc.allio.uno.data.orm.dsl.ddl.ShowTablesOperator; import cc.allio.uno.data.orm.dsl.dml.QueryOperator; import cc.allio.uno.data.orm.dsl.exception.DSLException; +import cc.allio.uno.data.orm.dsl.helper.PojoWrapper; import cc.allio.uno.data.orm.dsl.opeartorgroup.OperatorGroup; +import cc.allio.uno.data.orm.dsl.type.*; import cc.allio.uno.data.orm.executor.*; import cc.allio.uno.data.orm.dsl.*; -import cc.allio.uno.data.orm.dsl.type.IntegerJavaType; import cc.allio.uno.data.orm.executor.handler.BoolResultHandler; import cc.allio.uno.data.orm.executor.handler.ListResultSetHandler; import cc.allio.uno.data.orm.executor.handler.ResultSetHandler; @@ -19,6 +20,7 @@ import cc.allio.uno.data.orm.executor.options.ExecutorKey; import cc.allio.uno.data.orm.executor.options.ExecutorOptions; import cc.allio.uno.data.orm.executor.options.ExecutorOptionsImpl; +import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.SimpleExecutor; @@ -30,6 +32,7 @@ import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.transaction.Transaction; import org.apache.ibatis.transaction.TransactionFactory; +import org.apache.ibatis.type.JdbcType; import java.lang.reflect.Field; import java.net.SocketTimeoutException; @@ -152,26 +155,23 @@ protected boolean doBool(Operator operator, CommandType commandType, ResultSe @Override protected List doQueryList(Operator operator, CommandType commandType, ListResultSetHandler resultSetHandler) { + Class resultType = resultSetHandler.getResultType(); + if (resultType == null) { + resultType = ResultGroup.class; + } + // get sql QueryOperator queryOperator; - if (commandType == CommandType.SELECT) { - queryOperator = (QueryOperator) operator; - } else if (commandType == CommandType.SHOW_TABLES) { - queryOperator = ((ShowTablesOperator) operator).toQueryOperator(); - } else if (commandType == CommandType.SHOW_COLUMNS) { - queryOperator = ((ShowColumnsOperator) operator).toQueryOperator(); - } else { - throw new DSLException(String.format("un accept command to Db query list %s", commandType)); + switch (commandType) { + case CommandType.SELECT -> queryOperator = (QueryOperator) operator; + case CommandType.SHOW_TABLES -> queryOperator = ((ShowTablesOperator) operator).toQueryOperator(); + case CommandType.SHOW_COLUMNS -> queryOperator = ((ShowColumnsOperator) operator).toQueryOperator(); + default -> throw new DSLException(String.format("un accept command to Db query list %s", commandType)); } String querySQL = queryOperator.getPrepareDSL(); SqlSource sqlSource = languageDriver.createSqlSource(configuration, querySQL, null); - // 构建ResultMap对象 - ResultMap resultMap = - new ResultMap.Builder( - configuration, - IdGenerator.defaultGenerator().getNextIdAsString(), - ResultGroup.class, - Collections.emptyList()).build(); + + ResultMap resultMap = getResultMap(configuration, resultType); ParameterMap parameterMap = getParameterMap(queryOperator); String cacheId = getCacheId(); MappedStatement statement = @@ -230,11 +230,11 @@ public void destroy() { /** * getParameterMap * - * @param sqlOperator sqlOperator + * @param operator sqlOperator * @return ParameterMap */ - private ParameterMap getParameterMap(Operator sqlOperator) { - if (sqlOperator instanceof PrepareOperator prepareOperator) { + private ParameterMap getParameterMap(Operator operator) { + if (operator instanceof PrepareOperator prepareOperator) { List prepareValues = prepareOperator.getPrepareValues(); List parameterMappings = prepareValues.stream() .map(prepareValue -> { @@ -257,6 +257,51 @@ private ParameterMap getParameterMap(Operator sqlOperator) { return new ParameterMap.Builder(configuration, "defaultParameterMap", null, new ArrayList<>()).build(); } + /** + * get Mybatis {@link ResultMap} instance + * + * @param configuration the mybatis {@link Configuration} instance + * @param resultType return type + * @return the {@link ResultMap} instance + */ + private ResultMap getResultMap(Configuration configuration, Class resultType) { + List resultMappings = Lists.newArrayList(); + Class mapperType; + if (ResultGroup.class.isAssignableFrom(resultType) + || Table.class.isAssignableFrom(resultType) + || ColumnDef.class.isAssignableFrom(resultType)) { + mapperType = ResultGroup.class; + } else { + mapperType = resultType; + List columnDefs = PojoWrapper.getInstance(mapperType).getColumnDefs(); + for (ColumnDef columnDef : columnDefs) { + var property = columnDef.getDslName().formatHump(); + ResultMapping.Builder mb = new ResultMapping.Builder(configuration, property); + // build column + var column = columnDef.getDslName().formatUnderline(); + mb.column(column); + // build java type and jdbc + DataType dataType = columnDef.getDataType(); + DSLType dslType = dataType.getDslType(); + int jdbcType = dslType.getJdbcType(); + JavaType javaType = TypeRegistry.obtainJavaType(jdbcType); + if (javaType != null) { + mb.javaType(javaType.getJavaType()); + } + mb.jdbcType(JdbcType.forCode(jdbcType)); + mb.lazy(false); + ResultMapping mapping = mb.build(); + resultMappings.add(mapping); + } + } + + return new ResultMap.Builder( + configuration, + IdGenerator.defaultGenerator().getNextIdAsString(), + mapperType, + resultMappings).build(); + } + /** * 检查当前{@link Executor}的Connection.检查关闭后,重置连接 */ diff --git a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbStatementSetHandler.java b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbStatementSetHandler.java index 222d0aa6..6482d447 100644 --- a/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbStatementSetHandler.java +++ b/uno-data/uno-data-db/src/main/java/cc/allio/uno/data/orm/executor/db/DbStatementSetHandler.java @@ -25,6 +25,7 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; +import java.util.stream.Collectors; /** * 解决值无法映射到一个抽象对象上的问题 @@ -47,6 +48,9 @@ public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHan DefaultResultContext resultContext = new DefaultResultContext<>(); ResultSet resultSet = rsw.getResultSet(); skipRows(resultSet, rowBounds); + List resultMappings = resultMap.getResultMappings(); + var propertyMappings = resultMappings.stream().collect(Collectors.toMap(ResultMapping::getProperty, v -> v)); + while (shouldProcessMoreRows(resultContext, rowBounds) && !resultSet.isClosed() && resultSet.next()) { ResultGroup resultGroup = new ResultGroup(); List columnNames = rsw.getColumnNames(); @@ -55,13 +59,29 @@ public void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHan resultRowBuilder.index(i); String columnName = columnNames.get(i); // 字段名称自动为驼峰 - resultRowBuilder.column(DSLName.of(columnName, DSLName.HUMP_FEATURE)); - JdbcType mybatisJdbcType = rsw.getJdbcType(columnName); - JDBCType jdbcType = JDBCType.valueOf(mybatisJdbcType.TYPE_CODE); + DSLName column = DSLName.of(columnName, DSLName.HUMP_FEATURE); + resultRowBuilder.column(column); + + JDBCType jdbcType; + JavaType javaType; + TypeHandler typeHandler; + // if column contains mappings, then use mappings description. + // ensure data type is consistency (because like as pg bool type transfer to java type is bit, it will be failed to bool value 't' or 'f' get bit type.) + if (propertyMappings.containsKey(column.format())) { + ResultMapping mapping = propertyMappings.get(column.format()); + JdbcType mbJdbcType = mapping.getJdbcType(); + jdbcType = JDBCType.valueOf(mbJdbcType.TYPE_CODE); + Class type = mapping.getJavaType(); + javaType = TypeRegistry.obtainJavaTypeByClassType(type); + typeHandler = rsw.getTypeHandler(type, columnName); + } else { + JdbcType mybatisJdbcType = rsw.getJdbcType(columnName); + jdbcType = JDBCType.valueOf(mybatisJdbcType.TYPE_CODE); + javaType = TypeRegistry.obtainJavaType(jdbcType.getVendorTypeNumber()); + typeHandler = rsw.getTypeHandler(javaType.getJavaType(), columnName); + } resultRowBuilder.jdbcType(jdbcType); - JavaType javaType = TypeRegistry.getInstance().findJavaType(jdbcType.getVendorTypeNumber()); resultRowBuilder.javaType(javaType); - TypeHandler typeHandler = rsw.getTypeHandler(javaType.getJavaType(), columnName); Object value = typeHandler.getResult(rsw.getResultSet(), columnName); resultRowBuilder.value(value); resultGroup.addRow(resultRowBuilder.build()); diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java index fedaebd1..fda97651 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowColumnsOperator.java @@ -31,6 +31,27 @@ public class SQLShowColumnsOperator extends PrepareOperatorImpl { + case DbType.h2 -> { Table systemTable = Table.of(DSLName.of("INFORMATION_SCHEMA.COLUMNS", DSLName.PLAIN_FEATURE)).setSchema(null); queryOperator .from(systemTable) .eq(DSLName.of("TABLE_NAME", DSLName.PLAIN_FEATURE), table.getName().format()); } + case postgresql -> { + queryOperator.customize(PG_SHOW_COLUMN_SQL) + .eq(DSLName.of("TABLE_NAME", DSLName.PLAIN_FEATURE), table.getName().format()); + } case DbType.db2 -> { Table systemTable = Table.of(DSLName.of("SYSCAT.COLUMNS", DSLName.PLAIN_FEATURE)).setSchema(null); queryOperator diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java index bfda06d3..7ecdc6e6 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/ddl/SQLShowTablesOperator.java @@ -27,6 +27,18 @@ public class SQLShowTablesOperator extends PrepareOperatorImpl tables; private SQLQueryOperator queryOperator; + public static final String PG_SHOW_TABLES = "SELECT\n" + + "\tTABLE_CATALOG,\n" + + "\tTABLE_SCHEMA,\n" + + "\tTABLE_NAME,\n" + + "\tTABLE_TYPE,\n" + + "\td.description AS TABLE_COMMENT \n" + + "FROM\n" + + "\tINFORMATION_SCHEMA.TABLES\n" + + "\tT LEFT JOIN PG_CLASS C ON C.relname = T.\n" + + "\tTABLE_NAME LEFT JOIN PG_DESCRIPTION d ON d.objoid = C.oid \n" + + "\tAND d.objsubid = 0 "; + public SQLShowTablesOperator() { this(DBType.getSystemDbType()); } @@ -106,19 +118,16 @@ private void trigger() { .eq(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE), database.getName().format()) .and() .eq(DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.PLAIN_FEATURE), "BASE TABLE"); - case DbType.h2, DbType.postgresql -> { - queryOperator.select(DSLName.of(ShowTablesOperator.TABLE_CATALOG_FILED, DSLName.PLAIN_FEATURE)) - .select(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE)) - .select(DSLName.of(ShowTablesOperator.TABLE_NAME_FILED, DSLName.PLAIN_FEATURE)) - .select(DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.PLAIN_FEATURE)) - .from(formTable) - .eq(DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.PLAIN_FEATURE), "BASE TABLE"); - if (DbType.h2 == druidDbType) { - queryOperator.eq(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE), schema); - } else if (DbType.postgresql == druidDbType) { - queryOperator.eq(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE), schema.toLowerCase()); - } - } + case DbType.h2 -> + queryOperator.select(DSLName.of(ShowTablesOperator.TABLE_CATALOG_FILED, DSLName.PLAIN_FEATURE)) + .select(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE)) + .select(DSLName.of(ShowTablesOperator.TABLE_NAME_FILED, DSLName.PLAIN_FEATURE)) + .select(DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.PLAIN_FEATURE)) + .from(formTable) + .eq(DSLName.of(ShowTablesOperator.TABLE_TYPE_FILED, DSLName.PLAIN_FEATURE), "BASE TABLE") + .eq(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE), schema); + case DbType.postgresql -> + queryOperator.customize(PG_SHOW_TABLES).eq(DSLName.of(ShowTablesOperator.TABLE_SCHEMA_FILED, DSLName.PLAIN_FEATURE), schema.toLowerCase()); } if (CollectionUtils.isNotEmpty(tables)) { if (tables.size() == 1) { diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/DSLTypeDelegate.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/DSLTypeDelegate.java index d2bc6058..2ca8920f 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/DSLTypeDelegate.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/DSLTypeDelegate.java @@ -57,6 +57,11 @@ public Integer getScale() { return sqlType.getScale(); } + @Override + public boolean equalsTo(DSLType other) { + return this.sqlType.equalsTo(other); + } + /** * 子类实现,获取{@link DSLLinkType}数组,得到某个{@link DefaultDSLType}关联到不同数据实例的数据类型 * diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/H2SQLTypeDelegate.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/H2SQLTypeDelegate.java index cd0a15cc..30dd0ad7 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/H2SQLTypeDelegate.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/H2SQLTypeDelegate.java @@ -43,5 +43,11 @@ public enum H2LinkType implements DSLLinkType { private final Integer precision; private final Integer scale; private final List parent; + + + @Override + public boolean equalsTo(DSLType other) { + return this.parent.contains((other)); + } } } diff --git a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/PostgreSQLTypeDelegate.java b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/PostgreSQLTypeDelegate.java index 02547218..2f339c38 100644 --- a/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/PostgreSQLTypeDelegate.java +++ b/uno-data/uno-data-sql/src/main/java/cc/allio/uno/data/orm/dsl/sql/dialect/type/PostgreSQLTypeDelegate.java @@ -32,16 +32,24 @@ protected DSLLinkType[] getDSLLinkValues() { @Getter @AllArgsConstructor public enum PostgreSQLLinkType implements DSLLinkType { + INT8("int8", Types.BIGINT, null, null, List.of(DSLType.BIGINT)), FLOAT("float8", Types.FLOAT, 12, 2, List.of(DSLType.FLOAT)), NUMERIC("numeric", Types.DOUBLE, 12, 2, List.of(DSLType.DOUBLE, DSLType.NUMBER)), INT4("int4", Types.INTEGER, null, null, List.of(DSLType.INTEGER)), - INT2("int2", Types.INTEGER, null, null, List.of(DSLType.SMALLINT)); + INT2("int2", Types.INTEGER, null, null, List.of(DSLType.SMALLINT)), + TEXT("text", Types.LONGVARCHAR, null, null, List.of(DSLType.LONGVARCHAR)), + BOOL("bool", Types.BOOLEAN, null, null, List.of(DSLType.BOOLEAN)); private final String name; private final int jdbcType; private final Integer precision; private final Integer scale; private final List parent; + + @Override + public boolean equalsTo(DSLType other) { + return parent.contains(other); + } } }