feat: 优化代码
This commit is contained in:
parent
e2ec8874f5
commit
9a6fbb6c1a
@ -18,6 +18,7 @@ import org.springframework.data.relational.core.dialect.RenderContextFactory;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
|
||||
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
|
||||
import org.springframework.data.relational.core.sql.SqlIdentifier;
|
||||
import org.springframework.data.relational.core.sql.render.RenderContext;
|
||||
import org.springframework.data.relational.domain.RowDocument;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.r2dbc.core.Parameter;
|
||||
@ -55,11 +56,13 @@ public class FullReactiveDataAccessStrategy implements ReactiveDataAccessStrateg
|
||||
Assert.notNull(dialect, "Dialect must not be null");
|
||||
Assert.notNull(converter, "RelationalConverter must not be null");
|
||||
this.delegated = new DefaultReactiveDataAccessStrategy(dialect, converter);
|
||||
RenderContextFactory factory = new RenderContextFactory(dialect);
|
||||
RenderContext renderContext = factory.createRenderContext();
|
||||
|
||||
this.converter = converter;
|
||||
this.updateMapper = new FullUpdateMapper(dialect, converter);
|
||||
RenderContextFactory factory = new RenderContextFactory(dialect);
|
||||
this.statementMapper = new PredefinedStatementMapper(dialect, factory.createRenderContext(), this.updateMapper,
|
||||
this.updateMapper = new FullUpdateMapper(dialect, converter, renderContext);
|
||||
|
||||
this.statementMapper = new PredefinedStatementMapper(dialect, renderContext, this.updateMapper,
|
||||
delegated.getMappingContext());
|
||||
}
|
||||
|
||||
|
@ -14,15 +14,8 @@ import org.springframework.data.relational.core.sql.render.RenderContext;
|
||||
*/
|
||||
public class PredefinedStatementMapper extends DefaultStatementMapper {
|
||||
|
||||
private static RenderContext RENDER_CONTEXT;
|
||||
|
||||
public PredefinedStatementMapper(R2dbcDialect dialect, RenderContext renderContext, UpdateMapper updateMapper,
|
||||
MappingContext<RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> mappingContext) {
|
||||
super(dialect, renderContext, updateMapper, mappingContext);
|
||||
RENDER_CONTEXT = renderContext;
|
||||
}
|
||||
|
||||
public static RenderContext sharedRenderContext() {
|
||||
return RENDER_CONTEXT;
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import org.springframework.data.relational.core.mapping.RelationalPersistentProp
|
||||
import org.springframework.data.relational.core.query.CriteriaDefinition;
|
||||
import org.springframework.data.relational.core.query.ValueFunction;
|
||||
import org.springframework.data.relational.core.sql.*;
|
||||
import org.springframework.data.relational.core.sql.render.RenderContext;
|
||||
import org.springframework.data.util.Pair;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
import org.springframework.lang.NonNull;
|
||||
@ -32,12 +33,14 @@ import java.util.*;
|
||||
public class FullUpdateMapper extends UpdateMapper {
|
||||
|
||||
private final R2dbcDialect dialect;
|
||||
private final RenderContext renderContext;
|
||||
private final MappingContext<? extends RelationalPersistentEntity<?>, RelationalPersistentProperty> mappingContext;
|
||||
|
||||
public FullUpdateMapper(R2dbcDialect dialect, R2dbcConverter converter) {
|
||||
public FullUpdateMapper(R2dbcDialect dialect, R2dbcConverter converter, RenderContext renderContext) {
|
||||
super(dialect, converter);
|
||||
this.dialect = dialect;
|
||||
this.mappingContext = (MappingContext) converter.getMappingContext();
|
||||
this.renderContext = renderContext;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -246,13 +249,13 @@ public class FullUpdateMapper extends UpdateMapper {
|
||||
BindMarker bindMarker = bindings.nextMarker(column.getName().getReference());
|
||||
expressions.add(bind(o, valueType, bindings, bindMarker));
|
||||
}
|
||||
return FunctionCondition.of(criteria.getFunctionName(), expressions);
|
||||
return FunctionCondition.of(renderContext, criteria.getFunctionName(), expressions);
|
||||
} else {
|
||||
// 单值绑定
|
||||
BindMarker bindMarker = bindings.nextMarker(column.getName().getReference());
|
||||
Expression expression = bind(mappedValue, valueType, bindings, bindMarker);
|
||||
|
||||
return FunctionCondition.of(criteria.getFunctionName(), column, expression);
|
||||
return FunctionCondition.of(renderContext, criteria.getFunctionName(), column, expression);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package org.springframework.data.relational.core.sql;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.data.relational.core.sql.render.FunctionVisitor;
|
||||
import org.springframework.data.relational.core.sql.render.RenderContext;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -14,22 +15,26 @@ import java.util.List;
|
||||
*/
|
||||
public class FunctionCondition extends ConstantCondition implements Condition {
|
||||
|
||||
@Getter
|
||||
private final RenderContext renderContext;
|
||||
|
||||
@Getter
|
||||
private final SimpleFunction function;
|
||||
|
||||
private final StringBuilder builder = new StringBuilder();
|
||||
|
||||
private FunctionCondition(SimpleFunction function) {
|
||||
private FunctionCondition(RenderContext context, SimpleFunction function) {
|
||||
super("");
|
||||
this.renderContext = context;
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public static Condition of(String functionName, List<Expression> args) {
|
||||
return new FunctionCondition(SimpleFunction.create(functionName, args));
|
||||
public static Condition of(RenderContext context, String functionName, List<Expression> args) {
|
||||
return new FunctionCondition(context, SimpleFunction.create(functionName, args));
|
||||
}
|
||||
|
||||
public static Condition of(String functionName, Expression... args) {
|
||||
return of(functionName, Arrays.asList(args));
|
||||
public static Condition of(RenderContext context, String functionName, Expression... args) {
|
||||
return of(context, functionName, Arrays.asList(args));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -43,7 +48,7 @@ public class FunctionCondition extends ConstantCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
FunctionVisitor visitor = new FunctionVisitor(this, builder::append);
|
||||
FunctionVisitor visitor = new FunctionVisitor(this, builder::append);
|
||||
this.visit(visitor);
|
||||
return builder.toString();
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package org.springframework.data.relational.core.sql.render;
|
||||
|
||||
import org.springframework.data.r2dbc.core.PredefinedStatementMapper;
|
||||
import org.springframework.data.relational.core.sql.FunctionCondition;
|
||||
import org.springframework.data.relational.core.sql.SimpleFunction;
|
||||
import org.springframework.data.relational.core.sql.Visitable;
|
||||
@ -8,6 +7,12 @@ import org.springframework.lang.Nullable;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 很高级的自定义函数访问器
|
||||
*
|
||||
* @author wangyu
|
||||
* 完全使用官方行为渲染,准确无误
|
||||
*/
|
||||
public class FunctionVisitor extends FilteredSubtreeVisitor {
|
||||
|
||||
private final RenderContext context;
|
||||
@ -17,7 +22,7 @@ public class FunctionVisitor extends FilteredSubtreeVisitor {
|
||||
|
||||
public FunctionVisitor(FunctionCondition condition, Consumer<CharSequence> target) {
|
||||
super(it -> it == condition);
|
||||
this.context = PredefinedStatementMapper.sharedRenderContext();
|
||||
this.context = condition.getRenderContext();
|
||||
this.target = target::accept;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user