Skip to content

Add Support for Fetch First Based Paging #96

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 pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.1</version>
<version>2.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.7.RELEASE</version>
<version>5.1.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/mybatis/dynamic/sql/SqlTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ protected SqlTable(Supplier<Optional<String>> schemaSupplier, String tableName)
this(Optional::empty, schemaSupplier, tableName);
}

protected SqlTable(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier, String tableName) {
protected SqlTable(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier,
String tableName) {
Objects.requireNonNull(catalogSupplier);
Objects.requireNonNull(schemaSupplier);
Objects.requireNonNull(tableName);

this.nameSupplier = () -> compose(catalogSupplier, schemaSupplier, tableName);
}

private String compose(Supplier<Optional<String>> catalogSupplier, Supplier<Optional<String>> schemaSupplier, String tableName) {
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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select;

import java.util.Optional;

public class FetchFirstPagingModel implements PagingModel {

private Long fetchFirstRows;
private Long offset;

private FetchFirstPagingModel(Builder builder) {
this.fetchFirstRows = builder.fetchFirstRows;
this.offset = builder.offset;
}

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

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

@Override
public <R> R accept(PagingModelVisitor<R> visitor) {
return visitor.visit(this);
}

public static class Builder {
private Long fetchFirstRows;
private Long offset;

public Builder withFetchFirstRows(Long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
return this;
}

public Builder withOffset(Long offset) {
this.offset = offset;
return this;
}

public FetchFirstPagingModel build() {
return new FetchFirstPagingModel(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select;

import java.util.Optional;

public class LimitAndOffsetPagingModel implements PagingModel {

private Long limit;
private Long offset;

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

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

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

@Override
public <R> R accept(PagingModelVisitor<R> visitor) {
return visitor.visit(this);
}

public static class Builder {
private Long limit;
private Long offset;

public Builder withLimit(Long limit) {
this.limit = limit;
return this;
}

public Builder withOffset(Long offset) {
this.offset = offset;
return this;
}

public LimitAndOffsetPagingModel build() {
return new LimitAndOffsetPagingModel(this);
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/select/PagingModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select;

public interface PagingModel {
<R> R accept(PagingModelVisitor<R> visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright 2016-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select;

public interface PagingModelVisitor<R> {
R visit(LimitAndOffsetPagingModel pagingModel);

R visit(FetchFirstPagingModel pagingModel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ public SelectDSL<R>.LimitFinisher limit(long limit) {
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFinisher offset(long offset) {
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}

public static class FromGatherer<R> {
private FromGathererBuilder<R> builder;
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
Expand Down Expand Up @@ -250,12 +255,18 @@ public SelectDSL<R>.LimitFinisher limit(long limit) {
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFinisher offset(long offset) {
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}

@Override
public R build() {
whereModel = buildWhereModel();
Expand Down Expand Up @@ -413,7 +424,7 @@ public SelectDSL<R>.LimitFinisher limit(long limit) {
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFinisher offset(long offset) {
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
Expand All @@ -435,7 +446,7 @@ public SelectDSL<R>.LimitFinisher limit(long limit) {
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFinisher offset(long offset) {
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
return selectDSL.offset(offset);
}
}
Expand Down
98 changes: 87 additions & 11 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public class SelectDSL<R> implements Buildable<R> {
private Function<SelectModel, R> adapterFunction;
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
private OrderByModel orderByModel;
private Long limit;
private Long offset;
private PagingModel pagingModel;

private SelectDSL(Function<SelectModel, R> adapterFunction) {
this.adapterFunction = Objects.requireNonNull(adapterFunction);
Expand Down Expand Up @@ -100,39 +99,116 @@ void setOrderByModel(OrderByModel orderByModel) {
}

public LimitFinisher limit(long limit) {
this.limit = limit;
return new LimitFinisher();
return new LimitFinisher(limit);
}

public OffsetFinisher offset(long offset) {
this.offset = offset;
return new OffsetFinisher();
public OffsetFirstFinisher offset(long offset) {
return new OffsetFirstFinisher(offset);
}

public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return new FetchFirstFinisher(fetchFirstRows);
}

@Override
public R build() {
SelectModel selectModel = SelectModel.withQueryExpressions(queryExpressions)
.withOrderByModel(orderByModel)
.withLimit(limit)
.withOffset(offset)
.withPagingModel(pagingModel)
.build();
return adapterFunction.apply(selectModel);
}

public class LimitFinisher implements Buildable<R> {
public OffsetFinisher offset(int offset) {
return SelectDSL.this.offset(offset);
private long limit;

public LimitFinisher(long limit) {
this.limit = limit;
}

public OffsetFinisher offset(long offset) {
return new OffsetFinisher(limit, offset);
}

@Override
public R build() {
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
.withLimit(limit)
.build();
return SelectDSL.this.build();
}
}

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

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

@Override
public R build() {
SelectDSL.this.pagingModel = pagingModel;
return SelectDSL.this.build();
}
}

public class OffsetFirstFinisher implements Buildable<R> {
private long offset;

public OffsetFirstFinisher(long offset) {
this.offset = offset;
}

public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
return new FetchFirstFinisher(offset, fetchFirstRows);
}

@Override
public R build() {
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
.withOffset(offset)
.build();
return SelectDSL.this.build();
}
}

public class FetchFirstFinisher {
private Long offset;
private Long fetchFirstRows;

public FetchFirstFinisher(long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
}

public FetchFirstFinisher(long offset, long fetchFirstRows) {
this.offset = offset;
this.fetchFirstRows = fetchFirstRows;
}

public RowsOnlyFinisher rowsOnly() {
return new RowsOnlyFinisher(offset, fetchFirstRows);
}
}

public class RowsOnlyFinisher implements Buildable<R> {
private Long offset;
private Long fetchFirstRows;

public RowsOnlyFinisher(Long offset, Long fetchFirstRows) {
this.offset = offset;
this.fetchFirstRows = fetchFirstRows;
}

@Override
public R build() {
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
return SelectDSL.this.build();
}
}
Expand Down
Loading