
org.mybatis.dynamic.sql.select.SelectDSL Maven / Gradle / Ivy
/*
* Copyright ${license.git.copyrightYears} 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.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SortSpecification;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.TableExpression;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.util.Buildable;
/**
* Implements a SQL DSL for building select statements.
*
* @author Jeff Butler
*
* @param the type of model produced by this builder, typically SelectModel
*/
public class SelectDSL implements Buildable {
private final Function adapterFunction;
private final List> queryExpressions = new ArrayList<>();
private OrderByModel orderByModel;
private Long limit;
private Long offset;
private Long fetchFirstRows;
private SelectDSL(Function adapterFunction) {
this.adapterFunction = Objects.requireNonNull(adapterFunction);
}
public static QueryExpressionDSL.FromGatherer select(BasicColumn...selectList) {
return select(Arrays.asList(selectList));
}
public static QueryExpressionDSL.FromGatherer select(Collection selectList) {
return select(Function.identity(), selectList);
}
public static QueryExpressionDSL.FromGatherer select(Function adapterFunction,
BasicColumn...selectList) {
return select(adapterFunction, Arrays.asList(selectList));
}
public static QueryExpressionDSL.FromGatherer select(Function adapterFunction,
Collection selectList) {
return new FromGatherer.Builder()
.withSelectList(selectList)
.withSelectDSL(new SelectDSL<>(adapterFunction))
.build();
}
public static QueryExpressionDSL.FromGatherer selectDistinct(BasicColumn...selectList) {
return selectDistinct(Function.identity(), selectList);
}
public static QueryExpressionDSL.FromGatherer selectDistinct(Collection selectList) {
return selectDistinct(Function.identity(), selectList);
}
public static QueryExpressionDSL.FromGatherer selectDistinct(Function adapterFunction,
BasicColumn...selectList) {
return selectDistinct(adapterFunction, Arrays.asList(selectList));
}
public static QueryExpressionDSL.FromGatherer selectDistinct(Function adapterFunction,
Collection selectList) {
return new FromGatherer.Builder()
.withSelectList(selectList)
.withSelectDSL(new SelectDSL<>(adapterFunction))
.isDistinct()
.build();
}
QueryExpressionDSL newQueryExpression(FromGatherer fromGatherer, TableExpression table) {
QueryExpressionDSL queryExpression = new QueryExpressionDSL<>(fromGatherer, table);
queryExpressions.add(queryExpression);
return queryExpression;
}
QueryExpressionDSL newQueryExpression(FromGatherer fromGatherer, SqlTable table, String tableAlias) {
QueryExpressionDSL queryExpression = new QueryExpressionDSL<>(fromGatherer, table, tableAlias);
queryExpressions.add(queryExpression);
return queryExpression;
}
void orderBy(Collection columns) {
orderByModel = OrderByModel.of(columns);
}
public LimitFinisher limit(long limit) {
this.limit = limit;
return new LimitFinisher();
}
public OffsetFirstFinisher offset(long offset) {
this.offset = offset;
return new OffsetFirstFinisher();
}
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
@NotNull
@Override
public R build() {
SelectModel selectModel = SelectModel.withQueryExpressions(buildModels())
.withOrderByModel(orderByModel)
.withPagingModel(buildPagingModel())
.build();
return adapterFunction.apply(selectModel);
}
private List buildModels() {
return queryExpressions.stream()
.map(QueryExpressionDSL::buildModel)
.collect(Collectors.toList());
}
private PagingModel buildPagingModel() {
return new PagingModel.Builder()
.withLimit(limit)
.withOffset(offset)
.withFetchFirstRows(fetchFirstRows)
.build();
}
public class LimitFinisher implements Buildable {
public OffsetFinisher offset(long offset) {
SelectDSL.this.offset = offset;
return new OffsetFinisher();
}
@NotNull
@Override
public R build() {
return SelectDSL.this.build();
}
}
public class OffsetFinisher implements Buildable {
@NotNull
@Override
public R build() {
return SelectDSL.this.build();
}
}
public class OffsetFirstFinisher implements Buildable {
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
SelectDSL.this.fetchFirstRows = fetchFirstRows;
return new FetchFirstFinisher();
}
@NotNull
@Override
public R build() {
return SelectDSL.this.build();
}
}
public class FetchFirstFinisher {
public RowsOnlyFinisher rowsOnly() {
return new RowsOnlyFinisher();
}
}
public class RowsOnlyFinisher implements Buildable {
@NotNull
@Override
public R build() {
return SelectDSL.this.build();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy