Skip to content

Add Limit and Offset Support #78

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 5 commits into from
Apr 2, 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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Change Log

## Release 1.1.1 (Unreleased)

### Added

- Limit and offset support in the select statement


## Release 1.1.0
24 changes: 24 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,30 @@
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- Copy the changelog into the generated site -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-changelog</id>
<phase>pre-site</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-site/markdown/docs</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<include>CHANGELOG.md</include>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<reporting>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mybatis/dynamic/sql/SqlColumn.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* 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.
Expand All @@ -15,11 +15,13 @@
*/
package org.mybatis.dynamic.sql.render;

import java.util.Optional;

import org.mybatis.dynamic.sql.BindableColumn;

public class MyBatis3RenderingStrategy extends RenderingStrategy {
@Override
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
public String getFormattedJdbcPlaceholder(Optional<BindableColumn<?>> column, String prefix, String parameterName) {
return "#{" //$NON-NLS-1$
+ prefix
+ "." //$NON-NLS-1$
Expand All @@ -29,13 +31,17 @@ public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefi
+ "}"; //$NON-NLS-1$
}

private String renderTypeHandler(BindableColumn<?> column) {
return column.typeHandler().map(th -> ",typeHandler=" + th) //$NON-NLS-1$
private String renderTypeHandler(Optional<BindableColumn<?>> column) {
return column
.flatMap(BindableColumn::typeHandler)
.map(th -> ",typeHandler=" + th) //$NON-NLS-1$
.orElse(""); //$NON-NLS-1$
}

private String renderJdbcType(BindableColumn<?> column) {
return column.jdbcType().map(jt -> ",jdbcType=" + jt.getName()) //$NON-NLS-1$
private String renderJdbcType(Optional<BindableColumn<?>> column) {
return column
.flatMap(BindableColumn::jdbcType)
.map(jt -> ",jdbcType=" + jt.getName()) //$NON-NLS-1$
.orElse(""); //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* 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.
Expand All @@ -15,11 +15,22 @@
*/
package org.mybatis.dynamic.sql.render;

import java.util.Optional;

import org.mybatis.dynamic.sql.BindableColumn;

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

public abstract String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName);
public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
return getFormattedJdbcPlaceholder(Optional.of(column), prefix, parameterName);
}

public String getFormattedJdbcPlaceholder(String prefix, String parameterName) {
return getFormattedJdbcPlaceholder(Optional.empty(), prefix, parameterName);
}

public abstract String getFormattedJdbcPlaceholder(Optional<BindableColumn<?>> column, String prefix, String parameterName);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* 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.
Expand All @@ -15,13 +15,14 @@
*/
package org.mybatis.dynamic.sql.render;

import java.util.Optional;

import org.mybatis.dynamic.sql.BindableColumn;

public class SpringNamedParameterRenderingStrategy extends RenderingStrategy {

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* 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.
Expand Down Expand Up @@ -144,6 +144,16 @@ protected QueryExpressionModel buildModel() {
.build();
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

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

public static class FromGatherer<R> {
private FromGathererBuilder<R> builder;
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
Expand Down Expand Up @@ -234,6 +244,18 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
return new GroupByFinisher();
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

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

@Override
public R build() {
whereModel = buildWhereModel();
Expand Down Expand Up @@ -384,6 +406,18 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
selectDSL.setOrderByModel(OrderByModel.of(columns));
return selectDSL;
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

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

public class GroupByFinisher implements Buildable<R> {
Expand All @@ -396,6 +430,14 @@ public SelectDSL<R> orderBy(SortSpecification...columns) {
public R build() {
return selectDSL.build();
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
return selectDSL.limit(limit);
}

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

public class UnionBuilder {
Expand Down
38 changes: 36 additions & 2 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* 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.
Expand All @@ -24,6 +24,7 @@
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGathererBuilder;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.Buildable;

/**
* Implements a standard SQL dialect for building model classes.
Expand All @@ -32,11 +33,13 @@
*
* @param <R> the type of model produced by this builder
*/
public class SelectDSL<R> {
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 SelectDSL(Function<SelectModel, R> adapterFunction) {
this.adapterFunction = Objects.requireNonNull(adapterFunction);
Expand Down Expand Up @@ -96,10 +99,41 @@ void setOrderByModel(OrderByModel orderByModel) {
this.orderByModel = orderByModel;
}

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

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

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

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

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

public class OffsetFinisher implements Buildable<R> {
@Override
public R build() {
return SelectDSL.this.build();
}
}
}
26 changes: 25 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/select/SelectModel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2018 the original author or authors.
* 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.
Expand Down Expand Up @@ -29,10 +29,14 @@
public class SelectModel {
private List<QueryExpressionModel> queryExpressions;
private OrderByModel orderByModel;
private Long limit;
private Long offset;

private SelectModel(Builder builder) {
queryExpressions = Objects.requireNonNull(builder.queryExpressions);
orderByModel = builder.orderByModel;
limit = builder.limit;
offset = builder.offset;
}

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

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

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

public SelectStatementProvider render(RenderingStrategy renderingStrategy) {
return SelectRenderer.withSelectModel(this)
.withRenderingStrategy(renderingStrategy)
Expand All @@ -57,6 +69,8 @@ public static Builder withQueryExpressions(List<QueryExpressionModel> queryExpre
public static class Builder {
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
private OrderByModel orderByModel;
private Long limit;
private Long offset;

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

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

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

public SelectModel build() {
return new SelectModel(this);
}
Expand Down
Loading