Skip to content

Commit

Permalink
fix:修复not条件查询忽略null值问题
Browse files Browse the repository at this point in the history
  • Loading branch information
goten7 committed May 10, 2024
1 parent f6ca1ac commit 978b57d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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判断不等于条件时,添加空值条件.
* 判断不等于条件时,添加空值条件.
* <p>
* 例如 !=、 not like、 not in等,会将null值过滤掉。需要添加一个 isnull的或条件
* <p>
* 示例:
* <p>
* 通用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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 978b57d

Please sign in to comment.