feat: 完善r2dbc场景
This commit is contained in:
parent
2bf84f0c27
commit
0ec52fd90a
@ -2,7 +2,12 @@ package com.flyfish.framework.query.chain;
|
||||
|
||||
import com.flyfish.framework.query.holder.QueryChainHolder;
|
||||
import com.flyfish.framework.query.spi.adaptor.CriteriaAdaptor;
|
||||
import com.flyfish.framework.utils.Assert;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
|
||||
@ -13,6 +18,10 @@ import java.util.function.Function;
|
||||
*/
|
||||
class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
|
||||
private Function<CriteriaAdaptor<C>, BinaryOperator<C>> operator;
|
||||
|
||||
private final List<BiFunction<C, CriteriaAdaptor<C>, C>> builders = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 等待下一步消费
|
||||
* 如果不调用with,该状态将一直保持,可被其他link调用替换
|
||||
@ -23,7 +32,8 @@ class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
*/
|
||||
@Override
|
||||
public QueryChainHolder<C> link(Function<CriteriaAdaptor<C>, BinaryOperator<C>> operator) {
|
||||
return null;
|
||||
this.operator = operator;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -33,10 +43,19 @@ class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
*/
|
||||
@Override
|
||||
public void with(Function<CriteriaAdaptor<C>, C> criteria) {
|
||||
|
||||
Assert.notNull(operator, "连接操作不可为空");
|
||||
BiFunction<C, CriteriaAdaptor<C>, C> builder = (previous, adaptor) -> {
|
||||
// 连接逻辑
|
||||
BinaryOperator<C> linker = operator.apply(adaptor);
|
||||
// 具体条件
|
||||
C next = criteria.apply(adaptor);
|
||||
// 之前的条件
|
||||
return linker.apply(previous, next);
|
||||
};
|
||||
builders.add(builder);
|
||||
this.operator = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 触发动作,得到最终的
|
||||
*
|
||||
@ -44,7 +63,11 @@ class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
*/
|
||||
@Override
|
||||
public C get(CriteriaAdaptor<C> adaptor) {
|
||||
return null;
|
||||
if (CollectionUtils.isNotEmpty(builders)) {
|
||||
return builders.stream()
|
||||
.reduce(adaptor.empty(), (result, builder) -> builder.apply(result, adaptor), (a, b) -> a);
|
||||
}
|
||||
return adaptor.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,6 +77,6 @@ class DefaultQueryChainHolder<C> implements QueryChainHolder<C> {
|
||||
*/
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
return builders.isEmpty();
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,13 @@ import java.util.List;
|
||||
*/
|
||||
public interface CriteriaAdaptor<C> {
|
||||
|
||||
/**
|
||||
* 一个空的查询,用于起手
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
C empty();
|
||||
|
||||
/**
|
||||
* 且的关系进行拼接
|
||||
*
|
||||
|
@ -10,6 +10,11 @@ import java.util.List;
|
||||
|
||||
public class MongoCriteriaAdaptor implements CriteriaAdaptor<Criteria> {
|
||||
|
||||
@Override
|
||||
public Criteria empty() {
|
||||
return new Criteria();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Criteria and(Criteria first, Criteria other) {
|
||||
return first.andOperator(other);
|
||||
|
@ -6,7 +6,8 @@ import com.flyfish.framework.r2dbc.repository.factory.DefaultReactiveRepositoryF
|
||||
import com.flyfish.framework.r2dbc.repository.impl.DefaultReactiveRepositoryImpl;
|
||||
import com.flyfish.framework.repository.ReactiveEntityOperations;
|
||||
import io.r2dbc.spi.ConnectionFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.data.mapping.callback.EntityCallback;
|
||||
@ -31,6 +32,7 @@ import java.util.Optional;
|
||||
basePackages = "com.flyfish.framework"
|
||||
)
|
||||
@EnableR2dbcAuditing
|
||||
@AutoConfiguration(before = R2dbcAutoConfiguration.class)
|
||||
public class R2dbcDataConfig {
|
||||
|
||||
@Bean
|
||||
@ -39,7 +41,6 @@ public class R2dbcDataConfig {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(R2dbcEntityOperations.class)
|
||||
public ReactiveEntityOperations r2dbcReactiveEntityOperations(R2dbcEntityOperations r2dbcEntityOperations) {
|
||||
return new R2dbcReactiveEntityOperations(r2dbcEntityOperations);
|
||||
}
|
||||
|
@ -15,6 +15,11 @@ import java.util.List;
|
||||
*/
|
||||
public class R2dbcCriteriaAdaptor implements CriteriaAdaptor<Criteria> {
|
||||
|
||||
@Override
|
||||
public Criteria empty() {
|
||||
return Criteria.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Criteria and(Criteria first, Criteria other) {
|
||||
return first.and(other);
|
||||
|
Loading…
x
Reference in New Issue
Block a user