From d9673c5f1a38655a2d56d0ea6154c7d81755cdee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 29 Jul 2023 10:32:03 +0800 Subject: [PATCH] test: add MainSqlTest.java --- .../mybatisflex/core/query/QueryChain.java | 8 ++++ .../mybatisflex/core/update/UpdateChain.java | 13 +++++ .../coretest/LocaleFormatTest.java | 4 +- .../com/mybatisflex/test/MainSqlTest.java | 48 +++++++++++++++++++ 4 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MainSqlTest.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryChain.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryChain.java index f1d4be83..ded14920 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryChain.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryChain.java @@ -18,6 +18,8 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.BaseMapper; import com.mybatisflex.core.paginate.Page; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.SqlUtil; import java.util.List; @@ -137,4 +139,10 @@ public class QueryChain extends QueryWrapperAdapter> { return baseMapper.paginateWithRelationsAs(page, this, asType); } + @Override + public String toSQL() { + TableInfo tableInfo = TableInfoFactory.ofMapperClass(baseMapper.getClass()); + CPI.setFromIfNecessary(this, tableInfo.getSchema(), tableInfo.getTableName()); + return super.toSQL(); + } } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java index afc9e232..efaf64e4 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/update/UpdateChain.java @@ -17,10 +17,14 @@ package com.mybatisflex.core.update; import com.mybatisflex.core.BaseMapper; +import com.mybatisflex.core.dialect.DialectFactory; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.mybatis.Mappers; +import com.mybatisflex.core.query.CPI; import com.mybatisflex.core.query.QueryColumn; import com.mybatisflex.core.query.QueryWrapperAdapter; +import com.mybatisflex.core.table.TableInfo; +import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.ClassUtil; import com.mybatisflex.core.util.LambdaGetter; import com.mybatisflex.core.util.SqlUtil; @@ -124,4 +128,13 @@ public class UpdateChain extends QueryWrapperAdapter> { return SqlUtil.toBool(baseMapper.updateByQuery(entity, this)); } + + @Override + public String toSQL() { + TableInfo tableInfo = TableInfoFactory.ofMapperClass(baseMapper.getClass()); + CPI.setFromIfNecessary(this, tableInfo.getSchema(), tableInfo.getTableName()); + String sql = DialectFactory.getDialect().forUpdateEntityByQuery(tableInfo,entity,true,this); + return SqlUtil.replaceSqlParams(sql, CPI.getValueArray(this)); + } + } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LocaleFormatTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LocaleFormatTest.java index 11ef72c4..77da99fa 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LocaleFormatTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LocaleFormatTest.java @@ -16,8 +16,6 @@ package com.mybatisflex.coretest; -import com.mybatisflex.core.exception.FlexExceptions; -import com.mybatisflex.core.exception.locale.LocalizedFormats; import org.junit.Test; /** @@ -28,7 +26,7 @@ public class LocaleFormatTest { @Test public void test() { - throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL, "primaryValues"); +// throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL, "primaryValues"); } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MainSqlTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MainSqlTest.java new file mode 100644 index 00000000..0482590a --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MainSqlTest.java @@ -0,0 +1,48 @@ +package com.mybatisflex.test; + +import com.mybatisflex.core.FlexGlobalConfig; +import com.mybatisflex.core.mybatis.FlexConfiguration; +import com.mybatisflex.core.update.UpdateChain; +import com.mybatisflex.mapper.ArticleMapper; +import com.zaxxer.hikari.HikariDataSource; +import org.apache.ibatis.mapping.Environment; +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; + +public class MainSqlTest { + + public static void main(String[] args) { + + Environment environment = new Environment("test", new JdbcTransactionFactory(), new HikariDataSource()); + + FlexConfiguration configuration = new FlexConfiguration(environment); + FlexGlobalConfig globalConfig = FlexGlobalConfig.getDefaultConfig(); + globalConfig.setConfiguration(configuration); + FlexGlobalConfig.setConfig("test", globalConfig, true); + + configuration.addMapper(ArticleMapper.class); + + +// ArticleMapper mapper = (ArticleMapper) Proxy.newProxyInstance(MainSqlTest.class.getClassLoader(), +// new Class[]{ArticleMapper.class}, new InvocationHandler() { +// @Override +// public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { +// return null; +// } +// }); +// +// +// String sql1 = QueryChain.of(mapper) +// .where(Article::getId).eq(100) +// .toSQL(); +// +// System.out.println(sql1); + + + String sql2 = UpdateChain.of(Article.class) + .set("xxxx", "xxxx") + .where(Article::getId).ge(100) + .toSQL(); + + System.out.println(sql2); + } +}