From 82c59d7d99f21ebd4fa9ff9aa34c8d046ea10a94 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Wed, 27 Mar 2024 12:53:15 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E4=B8=8D=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=20null=20=E5=80=BC=20SQL=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coretest/DynamicConditionTest.java | 48 +++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java index 481c6eb7..1cd5557a 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java @@ -18,7 +18,13 @@ package com.mybatisflex.coretest; import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.constant.SqlOperator; -import com.mybatisflex.core.query.*; +import com.mybatisflex.core.query.CPI; +import com.mybatisflex.core.query.If; +import com.mybatisflex.core.query.QueryColumn; +import com.mybatisflex.core.query.QueryColumnBehavior; +import com.mybatisflex.core.query.QueryCondition; +import com.mybatisflex.core.query.QueryTable; +import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.util.StringUtil; import org.junit.Assert; import org.junit.Test; @@ -33,7 +39,11 @@ import static com.mybatisflex.core.query.QueryColumnBehavior.getConditionCaster; import static com.mybatisflex.core.query.QueryMethods.bracket; import static com.mybatisflex.core.query.QueryMethods.raw; import static com.mybatisflex.coretest.table.AccountTableDef.ACCOUNT; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; /** * 动态条件测试。 @@ -225,11 +235,13 @@ public class DynamicConditionTest { @Test public void test12() { - QueryWrapper qw = QueryWrapper.create() + QueryWrapper queryWrapper = QueryWrapper.create() .select().from(ACCOUNT) .where(ACCOUNT.IS_DELETE.eq(0)) .or(raw("1 = 1").or(ACCOUNT.ID.eq(123))); - System.out.println(qw.toSQL()); + String sql = queryWrapper.toSQL(); + System.out.println(sql); + assertEquals("SELECT * FROM `tb_account` WHERE `is_delete` = 0 OR ( 1 = 1 OR `id` = 123)", sql); } @@ -346,4 +358,32 @@ public class DynamicConditionTest { assertTrue(queryWrapper.hasCondition()); } + @Test + public void testNull() { + QueryColumnBehavior.setIgnoreFunction(QueryColumnBehavior.IGNORE_NONE); + QueryWrapper queryWrapper = QueryWrapper.create() + .from(ACCOUNT) + .where(ACCOUNT.ID.eq(null)) + .and(ACCOUNT.USER_NAME.eq("QAQ", false)) + .and(ACCOUNT.AGE.ne(null)) + .and(QueryCondition.createEmpty()); + String sql1 = queryWrapper.toSQL(); + System.out.println(sql1); + + assertThrows(Exception.class, () -> QueryWrapper.create() + .from(ACCOUNT) + .where(ACCOUNT.AGE.in((Object[]) null))); + + QueryColumnBehavior.setIgnoreFunction(QueryColumnBehavior.IGNORE_NULL); + queryWrapper = QueryWrapper.create() + .from(ACCOUNT) + .where(ACCOUNT.ID.eq(null)) + .and(ACCOUNT.USER_NAME.eq("QAQ", false)) + .and(ACCOUNT.AGE.ne(null)); + String sql2 = queryWrapper.toSQL(); + System.out.println(sql2); + + assertNotEquals(sql1, sql2); + } + }