Skip to content

Misc. Optimizations #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/mybatis/dynamic/sql/SqlTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ protected SqlTable(Supplier<Optional<String>> catalogSupplier, Supplier<Optional
private String compose(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier,
String tableName) {
return catalogSupplier.get().map(c -> compose(c, schemaSupplier, tableName))
.orElse(compose(schemaSupplier, tableName));
.orElseGet(() -> compose(schemaSupplier, tableName));
}

private String compose(String catalog, Supplier<Optional<String>> schemaSupplier, String tableName) {
return schemaSupplier.get().map(s -> composeCatalogSchemaAndAndTable(catalog, s, tableName))
.orElse(composeCatalogAndTable(catalog, tableName));
.orElseGet(() -> composeCatalogAndTable(catalog, tableName));
}

private String compose(Supplier<Optional<String>> schemaSupplier, String tableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private GuaranteedTableAliasCalculator(Map<SqlTable, String> aliases) {
public Optional<String> aliasForColumn(SqlTable table) {
return super.aliasForColumn(table)
.map(Optional::of)
.orElse(Optional.of(table.tableNameAtRuntime()));
.orElseGet(() -> Optional.of(table.tableNameAtRuntime()));
}

public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.mybatis.dynamic.sql.select;

import java.util.Objects;
import java.util.Optional;

public class LimitAndOffsetPagingModel implements PagingModel {
Expand All @@ -23,12 +24,12 @@ public class LimitAndOffsetPagingModel implements PagingModel {
private Long offset;

private LimitAndOffsetPagingModel(Builder builder) {
this.limit = builder.limit;
this.limit = Objects.requireNonNull(builder.limit);
this.offset = builder.offset;
}

public Optional<Long> limit() {
return Optional.ofNullable(limit);
public Long limit() {
return limit;
}

public Optional<Long> offset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private QueryExpressionModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
joinModel = builder.joinModel;
tableAliasCalculator = joinModel().map(jm -> GuaranteedTableAliasCalculator.of(builder.tableAliases))
.orElse(TableAliasCalculator.of(builder.tableAliases));
.orElseGet(() -> TableAliasCalculator.of(builder.tableAliases));
whereModel = builder.whereModel;
groupByModel = builder.groupByModel;
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,15 @@ public R build() {
}

public class OffsetFinisher implements Buildable<R> {
private LimitAndOffsetPagingModel pagingModel;

public OffsetFinisher(long limit, long offset) {
pagingModel = new LimitAndOffsetPagingModel.Builder()
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
.withLimit(limit)
.withOffset(offset)
.build();
}

@Override
public R build() {
SelectDSL.this.pagingModel = pagingModel;
return SelectDSL.this.build();
}
}
Expand All @@ -169,7 +166,7 @@ public FetchFirstFinisher fetchFirst(long fetchFirstRows) {

@Override
public R build() {
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
.withOffset(offset)
.build();
return SelectDSL.this.build();
Expand All @@ -196,7 +193,6 @@ public RowsOnlyFinisher rowsOnly() {
}

public class RowsOnlyFinisher implements Buildable<R> {

@Override
public R build() {
return SelectDSL.this.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public FetchFirstPagingModelRenderer(RenderingStrategy renderingStrategy,
public Optional<FragmentAndParameters> render() {
return pagingModel.offset()
.map(this::renderWithOffset)
.orElse(renderFetchFirstRowsOnly());
.orElseGet(this::renderFetchFirstRowsOnly);
}

private Optional<FragmentAndParameters> renderWithOffset(Long offset) {
return pagingModel.fetchFirstRows()
.map(ffr -> renderOffsetAndFetchFirstRows(offset, ffr))
.orElse(renderOffsetOnly(offset));
.orElseGet(() -> renderOffsetOnly(offset));
}

private Optional<FragmentAndParameters> renderFetchFirstRowsOnly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,21 @@ public LimitAndOffsetPagingModelRenderer(RenderingStrategy renderingStrategy,
}

public Optional<FragmentAndParameters> render() {
return pagingModel.limit()
.map(this::renderWithLimit)
.orElse(renderOffsetOnly());
}

private Optional<FragmentAndParameters> renderWithLimit(Long limit) {
return pagingModel.offset()
.map(o -> renderLimitAndOffset(limit, o))
.orElse(renderLimitOnly(limit));
}

private Optional<FragmentAndParameters> renderOffsetOnly() {
return pagingModel.offset().flatMap(this::renderOffsetOnly);
}

private Optional<FragmentAndParameters> renderOffsetOnly(Long offset) {
return FragmentAndParameters
.withFragment("offset " + renderPlaceholder(OFFSET_PARAMETER)) //$NON-NLS-1$
.withParameter(OFFSET_PARAMETER, offset)
.buildOptional();
.map(this::renderLimitAndOffset)
.orElseGet(this::renderLimitOnly);
}
private Optional<FragmentAndParameters> renderLimitOnly(Long limit) {

private Optional<FragmentAndParameters> renderLimitOnly() {
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(LIMIT_PARAMETER)) //$NON-NLS-1$
.withParameter(LIMIT_PARAMETER, limit)
.withParameter(LIMIT_PARAMETER, pagingModel.limit())
.buildOptional();
}

private Optional<FragmentAndParameters> renderLimitAndOffset(Long limit, Long offset) {
private Optional<FragmentAndParameters> renderLimitAndOffset(Long offset) {
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(LIMIT_PARAMETER) //$NON-NLS-1$
+ " offset " + renderPlaceholder(OFFSET_PARAMETER)) //$NON-NLS-1$
.withParameter(LIMIT_PARAMETER, limit)
.withParameter(LIMIT_PARAMETER, pagingModel.limit())
.withParameter(OFFSET_PARAMETER, offset)
.buildOptional();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SelectRenderer {
private SelectRenderer(Builder builder) {
selectModel = Objects.requireNonNull(builder.selectModel);
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
sequence = builder.sequence().orElse(new AtomicInteger(1));
sequence = builder.sequence().orElseGet(() -> new AtomicInteger(1));
}

public SelectStatementProvider render() {
Expand Down