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 35d5565..b6d6030 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 extends JavaType>>) 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 extends JavaType>>) 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 extends JavaType>> 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 4d7271d..c3afee1 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 f90849b..87cdd0e 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 7dbf614..7f06b5f 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 0000000..a566e31
--- /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 b469e97..d249457 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 d94eb32..9a1bda1 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 b62c8cd..2007450 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 b1d8ff3..40a3246 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 b8fb589..f7474ee 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 37a5f4c..e2417c1 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 751110d..092d233 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 ac717f4..bcf5525 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 60180cc..910cb59 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 967246b..d88a74c 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 222d0aa..6482d44 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 fedaebd..fda9765 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 bfda06d..7ecdc6e 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 d2bc605..2ca8920 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 cd0a15c..30dd0ad 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 0254721..2f339c3 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);
+ }
}
}