Skip to content

Commit

Permalink
upgrade mybatis dynamic sql
Browse files Browse the repository at this point in the history
  • Loading branch information
ningpp committed May 1, 2024
1 parent 5a41d41 commit 786f33c
Showing 1 changed file with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import org.mybatis.dynamic.sql.common.OrderByModel;
import org.mybatis.dynamic.sql.common.OrderByRenderer;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
import org.mybatis.dynamic.sql.render.RenderingContext;
import org.mybatis.dynamic.sql.render.RenderingStrategies;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
Expand All @@ -32,14 +34,17 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* render SelectModel with custom PaginationModelRender
* <p>copy source code from SelectRenderer</p>
* @see org.mybatis.dynamic.sql.select.render.SelectRenderer
*/
public class PaginationSelectRenderer {
private static final StatementConfiguration DEFAULT_STATEMENT_CONFIGURATION = new StatementConfiguration();
private final SelectModel selectModel;
private final LimitOffset limitOffset;
private final PaginationModelRenderer paginationModelRender;
Expand All @@ -61,12 +66,16 @@ public PaginationSelectRenderer(SelectModel selectModel, LimitOffset limitOffset
} else {
this.sequence = sequence;
}
this.parentTableAliasCalculator = parentTableAliasCalculator;
this.parentTableAliasCalculator = parentTableAliasCalculator == null
? TableAliasCalculator.empty() : parentTableAliasCalculator;
}

private <R> Stream<R> mapQueryExpressions(Function<QueryExpressionModel, R> mapper) {
return selectModel.queryExpressions().map(mapper);
}

public SelectStatementProvider render() {
FragmentCollector fragmentCollector = selectModel
.mapQueryExpressions(this::renderQueryExpression)
FragmentCollector fragmentCollector = mapQueryExpressions(this::renderQueryExpression)
.collect(FragmentCollector.collect());

Optional<FragmentAndParameters> orderByFragmentAndParam = renderOrderBy();
Expand All @@ -86,16 +95,21 @@ public SelectStatementProvider render() {

private SelectStatementProvider toSelectStatementProvider(FragmentCollector fragmentCollector) {
return DefaultSelectStatementProvider
.withSelectStatement(fragmentCollector.fragments().collect(Collectors.joining(" ")))
.withSelectStatement(fragmentCollector.collectFragments(Collectors.joining(" ")))
.withParameters(fragmentCollector.parameters())
.build();
}

private FragmentAndParameters renderQueryExpression(QueryExpressionModel queryExpressionModel) {
return QueryExpressionRenderer.withQueryExpression(queryExpressionModel)
.withRenderingStrategy(renderingStrategy)
.withSequence(sequence)
.withParentTableAliasCalculator(parentTableAliasCalculator)
.withRenderingContext(
RenderingContext
.withRenderingStrategy(renderingStrategy)
.withSequence(sequence)
.withTableAliasCalculator(parentTableAliasCalculator)
.withStatementConfiguration(DEFAULT_STATEMENT_CONFIGURATION)
.build()
)
.build()
.render();
}
Expand Down

0 comments on commit 786f33c

Please sign in to comment.