From 978b57d4e69c123940038f381e89dc7228ca8466 Mon Sep 17 00:00:00 2001 From: gyl <1554863190@qq.com> Date: Fri, 10 May 2024 10:01:18 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8Dnot=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E6=9F=A5=E8=AF=A2=E5=BF=BD=E7=95=A5null=E5=80=BC?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ezorm/rdb/metadata/RDBSchemaMetadata.java | 13 +++-- .../NotFillOrNullFragmentBuilder.java} | 12 ++-- .../postgres/PostgresqlSchemaMetadata.java | 6 -- .../rdb/supports/BasicReactiveTests.java | 58 +++++++++++++++++++ 4 files changed, 71 insertions(+), 18 deletions(-) rename hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/{supports/postgres/PostgresqlNotFragmentBuilder.java => operator/builder/fragments/NotFillOrNullFragmentBuilder.java} (77%) diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBSchemaMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBSchemaMetadata.java index 3bf0ee2e..66ebf3b3 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBSchemaMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/metadata/RDBSchemaMetadata.java @@ -5,6 +5,7 @@ import org.hswebframework.ezorm.core.meta.ObjectType; import org.hswebframework.ezorm.rdb.metadata.dialect.Dialect; import org.hswebframework.ezorm.rdb.operator.builder.DefaultQuerySqlBuilder; +import org.hswebframework.ezorm.rdb.operator.builder.fragments.NotFillOrNullFragmentBuilder; import org.hswebframework.ezorm.rdb.operator.builder.fragments.ddl.CommonAlterTableSqlBuilder; import org.hswebframework.ezorm.rdb.operator.builder.fragments.ddl.CommonCreateIndexSqlBuilder; import org.hswebframework.ezorm.rdb.operator.builder.fragments.ddl.CommonCreateTableSqlBuilder; @@ -33,25 +34,27 @@ public RDBSchemaMetadata(String name) { /* 通用查询条件 */ addFeature(RDBFeatures.eq); addFeature(RDBFeatures.is); - addFeature(RDBFeatures.not); - addFeature(RDBFeatures.notEq); addFeature(RDBFeatures.gt); addFeature(RDBFeatures.gte); addFeature(RDBFeatures.lt); addFeature(RDBFeatures.lte); addFeature(RDBFeatures.like); - addFeature(RDBFeatures.nlike); addFeature(RDBFeatures.in); - addFeature(RDBFeatures.notIn); addFeature(RDBFeatures.between); - addFeature(RDBFeatures.notBetween); addFeature(RDBFeatures.isNull); addFeature(RDBFeatures.notNull); addFeature(RDBFeatures.isEmpty); addFeature(RDBFeatures.notEmpty); addFeature(RDBFeatures.nEmpty); + //不匹配查询支持包含空值 + addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.not)); + addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notEq)); + addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.nlike)); + addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notIn)); + addFeature(new NotFillOrNullFragmentBuilder(RDBFeatures.notBetween)); + //自动关联外键条件 addFeature(DefaultForeignKeyTermFragmentBuilder.INSTANCE); diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlNotFragmentBuilder.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/NotFillOrNullFragmentBuilder.java similarity index 77% rename from hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlNotFragmentBuilder.java rename to hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/NotFillOrNullFragmentBuilder.java index 6535edef..ec435b49 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlNotFragmentBuilder.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/operator/builder/fragments/NotFillOrNullFragmentBuilder.java @@ -1,31 +1,29 @@ -package org.hswebframework.ezorm.rdb.supports.postgres; +package org.hswebframework.ezorm.rdb.operator.builder.fragments; import org.hswebframework.ezorm.core.param.Term; import org.hswebframework.ezorm.rdb.metadata.RDBColumnMetadata; import org.hswebframework.ezorm.rdb.metadata.RDBFeatures; -import org.hswebframework.ezorm.rdb.operator.builder.fragments.PrepareSqlFragments; -import org.hswebframework.ezorm.rdb.operator.builder.fragments.SqlFragments; import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.AbstractTermFragmentBuilder; import org.hswebframework.ezorm.rdb.operator.builder.fragments.term.NullTermFragmentBuilder; /** - * postgresql判断不等于条件时,添加空值条件. + * 判断不等于条件时,添加空值条件. *

* 例如 !=、 not like、 not in等,会将null值过滤掉。需要添加一个 isnull的或条件 *

* 示例: *

* 通用sql: where name != 'a' - * postgresql: where (name isnull or name != 'a') + * 填充后sql: where (name isnull or name != 'a') * * @author zhangji 2023/3/22 */ -public class PostgresqlNotFragmentBuilder extends AbstractTermFragmentBuilder { +public class NotFillOrNullFragmentBuilder extends AbstractTermFragmentBuilder { private final NullTermFragmentBuilder nullBuilder; private final AbstractTermFragmentBuilder notBuilder; - public PostgresqlNotFragmentBuilder(AbstractTermFragmentBuilder notBuilder) { + public NotFillOrNullFragmentBuilder(AbstractTermFragmentBuilder notBuilder) { super(notBuilder.getTermType(), notBuilder.getName()); this.notBuilder = notBuilder; this.nullBuilder = RDBFeatures.isNull; diff --git a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlSchemaMetadata.java b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlSchemaMetadata.java index 9c713c3d..199388dc 100644 --- a/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlSchemaMetadata.java +++ b/hsweb-easy-orm-rdb/src/main/java/org/hswebframework/ezorm/rdb/supports/postgres/PostgresqlSchemaMetadata.java @@ -23,12 +23,6 @@ public PostgresqlSchemaMetadata(String name) { addFeature(new CompositeExceptionTranslation() .add(FeatureUtils.r2dbcIsAlive(), () -> PostgresqlR2DBCExceptionTranslation.of(this)) ); - - // 覆盖通用的基本条件 - addFeature(new PostgresqlNotFragmentBuilder(RDBFeatures.not)); - addFeature(new PostgresqlNotFragmentBuilder(RDBFeatures.notIn)); - addFeature(new PostgresqlNotFragmentBuilder(RDBFeatures.nlike)); - addFeature(new PostgresqlNotFragmentBuilder(RDBFeatures.notBetween)); } @Override diff --git a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java index 14d75f03..e5bd5617 100644 --- a/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java +++ b/hsweb-easy-orm-rdb/src/test/java/org/hswebframework/ezorm/rdb/supports/BasicReactiveTests.java @@ -34,6 +34,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.testcontainers.shaded.com.google.common.collect.Lists; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -574,4 +575,61 @@ public void testEnums() { } + @Test + public void testNotOrNULL() { + BasicTestEntity entity = BasicTestEntity + .builder() + .id("nullAddressId") + .name("name") + .state((byte) 1) + .build(); + + BasicTestEntity entity2 = BasicTestEntity + .builder() + .id("hasAddressId") + .name("name") + .state((byte) 1) + .addressId("0") + .build(); + + repository + .insert(Lists.newArrayList(entity, entity2)) + .as(StepVerifier::create) + .expectNext(2) + .verifyComplete(); + + + repository + .createQuery() + .notLike(BasicTestEntity::getAddressId, "no") + .count() + .as(StepVerifier::create) + .expectNext(2) + .verifyComplete(); + + repository + .createQuery() + .not(BasicTestEntity::getAddressId, "no") + .count() + .as(StepVerifier::create) + .expectNext(2) + .verifyComplete(); + + repository + .createQuery() + .notIn(BasicTestEntity::getAddressId, "no") + .count() + .as(StepVerifier::create) + .expectNext(2) + .verifyComplete(); + + repository + .createQuery() + .notBetween(BasicTestEntity::getAddressId, "1", "3") + .count() + .as(StepVerifier::create) + .expectNext(2) + .verifyComplete(); + } + }