Skip to content

Commit 6b16765

Browse files
authored
Merge pull request #78 from jeffgbutler/limit-offset
Add Limit and Offset Support
2 parents 97e7837 + c23d62a commit 6b16765

18 files changed

+665
-28
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Change Log
2+
3+
## Release 1.1.1 (Unreleased)
4+
5+
### Added
6+
7+
- Limit and offset support in the select statement
8+
9+
10+
## Release 1.1.0

pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@
6060
</plugin>
6161
</plugins>
6262
</pluginManagement>
63+
<plugins>
64+
<!-- Copy the changelog into the generated site -->
65+
<plugin>
66+
<artifactId>maven-resources-plugin</artifactId>
67+
<executions>
68+
<execution>
69+
<id>copy-changelog</id>
70+
<phase>pre-site</phase>
71+
<goals>
72+
<goal>copy-resources</goal>
73+
</goals>
74+
<configuration>
75+
<outputDirectory>${project.build.directory}/generated-site/markdown/docs</outputDirectory>
76+
<resources>
77+
<resource>
78+
<directory>${basedir}</directory>
79+
<include>CHANGELOG.md</include>
80+
</resource>
81+
</resources>
82+
</configuration>
83+
</execution>
84+
</executions>
85+
</plugin>
86+
</plugins>
6387
</build>
6488

6589
<reporting>

src/main/java/org/mybatis/dynamic/sql/SqlColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

src/main/java/org/mybatis/dynamic/sql/render/MyBatis3RenderingStrategy.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,13 @@
1515
*/
1616
package org.mybatis.dynamic.sql.render;
1717

18+
import java.util.Optional;
19+
1820
import org.mybatis.dynamic.sql.BindableColumn;
1921

2022
public class MyBatis3RenderingStrategy extends RenderingStrategy {
2123
@Override
22-
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
24+
public String getFormattedJdbcPlaceholder(Optional<BindableColumn<?>> column, String prefix, String parameterName) {
2325
return "#{" //$NON-NLS-1$
2426
+ prefix
2527
+ "." //$NON-NLS-1$
@@ -29,13 +31,17 @@ public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefi
2931
+ "}"; //$NON-NLS-1$
3032
}
3133

32-
private String renderTypeHandler(BindableColumn<?> column) {
33-
return column.typeHandler().map(th -> ",typeHandler=" + th) //$NON-NLS-1$
34+
private String renderTypeHandler(Optional<BindableColumn<?>> column) {
35+
return column
36+
.flatMap(BindableColumn::typeHandler)
37+
.map(th -> ",typeHandler=" + th) //$NON-NLS-1$
3438
.orElse(""); //$NON-NLS-1$
3539
}
3640

37-
private String renderJdbcType(BindableColumn<?> column) {
38-
return column.jdbcType().map(jt -> ",jdbcType=" + jt.getName()) //$NON-NLS-1$
41+
private String renderJdbcType(Optional<BindableColumn<?>> column) {
42+
return column
43+
.flatMap(BindableColumn::jdbcType)
44+
.map(jt -> ",jdbcType=" + jt.getName()) //$NON-NLS-1$
3945
.orElse(""); //$NON-NLS-1$
4046
}
4147
}

src/main/java/org/mybatis/dynamic/sql/render/RenderingStrategy.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,11 +15,22 @@
1515
*/
1616
package org.mybatis.dynamic.sql.render;
1717

18+
import java.util.Optional;
19+
1820
import org.mybatis.dynamic.sql.BindableColumn;
1921

2022
public abstract class RenderingStrategy {
2123
public static final RenderingStrategy MYBATIS3 = new MyBatis3RenderingStrategy();
2224
public static final RenderingStrategy SPRING_NAMED_PARAMETER = new SpringNamedParameterRenderingStrategy();
25+
public static final String DEFAULT_PARAMETER_PREFIX = "parameters"; //$NON-NLS-1$
2326

24-
public abstract String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName);
27+
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
28+
return getFormattedJdbcPlaceholder(Optional.of(column), prefix, parameterName);
29+
}
30+
31+
public String getFormattedJdbcPlaceholder(String prefix, String parameterName) {
32+
return getFormattedJdbcPlaceholder(Optional.empty(), prefix, parameterName);
33+
}
34+
35+
public abstract String getFormattedJdbcPlaceholder(Optional<BindableColumn<?>> column, String prefix, String parameterName);
2536
}

src/main/java/org/mybatis/dynamic/sql/render/SpringNamedParameterRenderingStrategy.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,13 +15,14 @@
1515
*/
1616
package org.mybatis.dynamic.sql.render;
1717

18+
import java.util.Optional;
19+
1820
import org.mybatis.dynamic.sql.BindableColumn;
1921

2022
public class SpringNamedParameterRenderingStrategy extends RenderingStrategy {
2123

2224
@Override
23-
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
25+
public String getFormattedJdbcPlaceholder(Optional<BindableColumn<?>> column, String prefix, String parameterName) {
2426
return ":" + parameterName; //$NON-NLS-1$
2527
}
26-
2728
}

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -144,6 +144,16 @@ protected QueryExpressionModel buildModel() {
144144
.build();
145145
}
146146

147+
public SelectDSL<R>.LimitFinisher limit(long limit) {
148+
selectDSL.addQueryExpression(buildModel());
149+
return selectDSL.limit(limit);
150+
}
151+
152+
public SelectDSL<R>.OffsetFinisher offset(long offset) {
153+
selectDSL.addQueryExpression(buildModel());
154+
return selectDSL.offset(offset);
155+
}
156+
147157
public static class FromGatherer<R> {
148158
private FromGathererBuilder<R> builder;
149159
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
@@ -234,6 +244,18 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
234244
return new GroupByFinisher();
235245
}
236246

247+
public SelectDSL<R>.LimitFinisher limit(long limit) {
248+
whereModel = buildWhereModel();
249+
selectDSL.addQueryExpression(buildModel());
250+
return selectDSL.limit(limit);
251+
}
252+
253+
public SelectDSL<R>.OffsetFinisher offset(long offset) {
254+
whereModel = buildWhereModel();
255+
selectDSL.addQueryExpression(buildModel());
256+
return selectDSL.offset(offset);
257+
}
258+
237259
@Override
238260
public R build() {
239261
whereModel = buildWhereModel();
@@ -384,6 +406,18 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
384406
selectDSL.setOrderByModel(OrderByModel.of(columns));
385407
return selectDSL;
386408
}
409+
410+
public SelectDSL<R>.LimitFinisher limit(long limit) {
411+
joinModel = buildJoinModel();
412+
selectDSL.addQueryExpression(buildModel());
413+
return selectDSL.limit(limit);
414+
}
415+
416+
public SelectDSL<R>.OffsetFinisher offset(long offset) {
417+
joinModel = buildJoinModel();
418+
selectDSL.addQueryExpression(buildModel());
419+
return selectDSL.offset(offset);
420+
}
387421
}
388422

389423
public class GroupByFinisher implements Buildable<R> {
@@ -396,6 +430,14 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
396430
public R build() {
397431
return selectDSL.build();
398432
}
433+
434+
public SelectDSL<R>.LimitFinisher limit(long limit) {
435+
return selectDSL.limit(limit);
436+
}
437+
438+
public SelectDSL<R>.OffsetFinisher offset(long offset) {
439+
return selectDSL.offset(offset);
440+
}
399441
}
400442

401443
public class UnionBuilder {

src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
2525
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGathererBuilder;
2626
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
27+
import org.mybatis.dynamic.sql.util.Buildable;
2728

2829
/**
2930
* Implements a standard SQL dialect for building model classes.
@@ -32,11 +33,13 @@
3233
*
3334
* @param <R> the type of model produced by this builder
3435
*/
35-
public class SelectDSL<R> {
36+
public class SelectDSL<R> implements Buildable<R> {
3637

3738
private Function<SelectModel, R> adapterFunction;
3839
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
3940
private OrderByModel orderByModel;
41+
private Long limit;
42+
private Long offset;
4043

4144
private SelectDSL(Function<SelectModel, R> adapterFunction) {
4245
this.adapterFunction = Objects.requireNonNull(adapterFunction);
@@ -96,10 +99,41 @@ void setOrderByModel(OrderByModel orderByModel) {
9699
this.orderByModel = orderByModel;
97100
}
98101

102+
public LimitFinisher limit(long limit) {
103+
this.limit = limit;
104+
return new LimitFinisher();
105+
}
106+
107+
public OffsetFinisher offset(long offset) {
108+
this.offset = offset;
109+
return new OffsetFinisher();
110+
}
111+
112+
@Override
99113
public R build() {
100114
SelectModel selectModel = SelectModel.withQueryExpressions(queryExpressions)
101115
.withOrderByModel(orderByModel)
116+
.withLimit(limit)
117+
.withOffset(offset)
102118
.build();
103119
return adapterFunction.apply(selectModel);
104120
}
121+
122+
public class LimitFinisher implements Buildable<R> {
123+
public OffsetFinisher offset(int offset) {
124+
return SelectDSL.this.offset(offset);
125+
}
126+
127+
@Override
128+
public R build() {
129+
return SelectDSL.this.build();
130+
}
131+
}
132+
133+
public class OffsetFinisher implements Buildable<R> {
134+
@Override
135+
public R build() {
136+
return SelectDSL.this.build();
137+
}
138+
}
105139
}

src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,10 +29,14 @@
2929
public class SelectModel {
3030
private List<QueryExpressionModel> queryExpressions;
3131
private OrderByModel orderByModel;
32+
private Long limit;
33+
private Long offset;
3234

3335
private SelectModel(Builder builder) {
3436
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
3537
orderByModel = builder.orderByModel;
38+
limit = builder.limit;
39+
offset = builder.offset;
3640
}
3741

3842
public <R> Stream<R> mapQueryExpressions(Function<QueryExpressionModel, R> mapper) {
@@ -43,6 +47,14 @@ public Optional<OrderByModel> orderByModel() {
4347
return Optional.ofNullable(orderByModel);
4448
}
4549

50+
public Optional<Long> limit() {
51+
return Optional.ofNullable(limit);
52+
}
53+
54+
public Optional<Long> offset() {
55+
return Optional.ofNullable(offset);
56+
}
57+
4658
public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
4759
return SelectRenderer.withSelectModel(this)
4860
.withRenderingStrategy(renderingStrategy)
@@ -57,6 +69,8 @@ public static Builder withQueryExpressions(List<QueryExpressionModel> queryExpre
5769
public static class Builder {
5870
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
5971
private OrderByModel orderByModel;
72+
private Long limit;
73+
private Long offset;
6074

6175
public Builder withQueryExpressions(List<QueryExpressionModel> queryExpressions) {
6276
this.queryExpressions.addAll(queryExpressions);
@@ -68,6 +82,16 @@ public Builder withOrderByModel(OrderByModel orderByModel) {
6882
return this;
6983
}
7084

85+
public Builder withLimit(Long limit) {
86+
this.limit = limit;
87+
return this;
88+
}
89+
90+
public Builder withOffset(Long offset) {
91+
this.offset = offset;
92+
return this;
93+
}
94+
7195
public SelectModel build() {
7296
return new SelectModel(this);
7397
}

0 commit comments

Comments
 (0)