
org.mybatis.dynamic.sql.SqlBuilder 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;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import org.mybatis.dynamic.sql.delete.DeleteDSL;
import org.mybatis.dynamic.sql.delete.DeleteModel;
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
import org.mybatis.dynamic.sql.insert.GeneralInsertDSL;
import org.mybatis.dynamic.sql.insert.InsertDSL;
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
import org.mybatis.dynamic.sql.insert.MultiRowInsertDSL;
import org.mybatis.dynamic.sql.select.ColumnSortSpecification;
import org.mybatis.dynamic.sql.select.CountDSL;
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
import org.mybatis.dynamic.sql.select.SelectDSL;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.select.SimpleSortSpecification;
import org.mybatis.dynamic.sql.select.aggregate.Avg;
import org.mybatis.dynamic.sql.select.aggregate.Count;
import org.mybatis.dynamic.sql.select.aggregate.CountAll;
import org.mybatis.dynamic.sql.select.aggregate.CountDistinct;
import org.mybatis.dynamic.sql.select.aggregate.Max;
import org.mybatis.dynamic.sql.select.aggregate.Min;
import org.mybatis.dynamic.sql.select.aggregate.Sum;
import org.mybatis.dynamic.sql.select.function.Add;
import org.mybatis.dynamic.sql.select.function.Concatenate;
import org.mybatis.dynamic.sql.select.function.Divide;
import org.mybatis.dynamic.sql.select.function.Lower;
import org.mybatis.dynamic.sql.select.function.Multiply;
import org.mybatis.dynamic.sql.select.function.OperatorFunction;
import org.mybatis.dynamic.sql.select.function.Substring;
import org.mybatis.dynamic.sql.select.function.Subtract;
import org.mybatis.dynamic.sql.select.function.Upper;
import org.mybatis.dynamic.sql.select.join.EqualTo;
import org.mybatis.dynamic.sql.select.join.JoinCondition;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.where.WhereDSL;
import org.mybatis.dynamic.sql.where.condition.IsBetween;
import org.mybatis.dynamic.sql.where.condition.IsEqualTo;
import org.mybatis.dynamic.sql.where.condition.IsEqualToColumn;
import org.mybatis.dynamic.sql.where.condition.IsEqualToWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThan;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanColumn;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualTo;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToColumn;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsIn;
import org.mybatis.dynamic.sql.where.condition.IsInCaseInsensitive;
import org.mybatis.dynamic.sql.where.condition.IsInWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsLessThan;
import org.mybatis.dynamic.sql.where.condition.IsLessThanColumn;
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualTo;
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToColumn;
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsLessThanWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsLike;
import org.mybatis.dynamic.sql.where.condition.IsLikeCaseInsensitive;
import org.mybatis.dynamic.sql.where.condition.IsNotBetween;
import org.mybatis.dynamic.sql.where.condition.IsNotEqualTo;
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToColumn;
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
import org.mybatis.dynamic.sql.where.condition.IsNotInCaseInsensitive;
import org.mybatis.dynamic.sql.where.condition.IsNotInWithSubselect;
import org.mybatis.dynamic.sql.where.condition.IsNotLike;
import org.mybatis.dynamic.sql.where.condition.IsNotLikeCaseInsensitive;
import org.mybatis.dynamic.sql.where.condition.IsNotNull;
import org.mybatis.dynamic.sql.where.condition.IsNull;
public interface SqlBuilder {
// statements
/**
* Renders as select count(distinct column) from table...
*
* @param column the column to count
* @return the next step in the DSL
*/
static CountDSL.FromGatherer countDistinctColumn(BasicColumn column) {
return CountDSL.countDistinct(column);
}
/**
* Renders as select count(column) from table...
*
* @param column the column to count
* @return the next step in the DSL
*/
static CountDSL.FromGatherer countColumn(BasicColumn column) {
return CountDSL.count(column);
}
/**
* Renders as select count(*) from table...
*
* @param table the table to count
* @return the next step in the DSL
*/
static CountDSL countFrom(SqlTable table) {
return CountDSL.countFrom(table);
}
static DeleteDSL deleteFrom(SqlTable table) {
return DeleteDSL.deleteFrom(table);
}
static InsertDSL.IntoGatherer insert(T row) {
return InsertDSL.insert(row);
}
/**
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
*
* @param records records to insert
* @param the type of record to insert
* @return the next step in the DSL
*/
@SafeVarargs
static BatchInsertDSL.IntoGatherer insertBatch(T...records) {
return BatchInsertDSL.insert(records);
}
/**
* Insert a Batch of records. The model object is structured to support bulk inserts with JDBC batch support.
*
* @param records records to insert
* @param the type of record to insert
* @return the next step in the DSL
*/
static BatchInsertDSL.IntoGatherer insertBatch(Collection records) {
return BatchInsertDSL.insert(records);
}
/**
* Insert multiple records in a single statement. The model object is structured as a single insert statement with
* multiple values clauses. This statement is suitable for use with a small number of records. It is not suitable
* for large bulk inserts as it is possible to exceed the limit of parameter markers in a prepared statement.
*
* For large bulk inserts, see {@link SqlBuilder#insertBatch(Object[])}
*
* @param records records to insert
* @param the type of record to insert
* @return the next step in the DSL
*/
@SafeVarargs
static MultiRowInsertDSL.IntoGatherer insertMultiple(T...records) {
return MultiRowInsertDSL.insert(records);
}
/**
* Insert multiple records in a single statement. The model object is structured as a single insert statement with
* multiple values clauses. This statement is suitable for use with a small number of records. It is not suitable
* for large bulk inserts as it is possible to exceed the limit of parameter markers in a prepared statement.
*
* For large bulk inserts, see {@link SqlBuilder#insertBatch(Collection)}
*
* @param records records to insert
* @param the type of record to insert
* @return the next step in the DSL
*/
static MultiRowInsertDSL.IntoGatherer insertMultiple(Collection records) {
return MultiRowInsertDSL.insert(records);
}
static InsertIntoNextStep insertInto(SqlTable table) {
return new InsertIntoNextStep(table);
}
static FromGatherer select(BasicColumn...selectList) {
return SelectDSL.select(selectList);
}
static FromGatherer select(Collection selectList) {
return SelectDSL.select(selectList);
}
static FromGatherer selectDistinct(BasicColumn...selectList) {
return SelectDSL.selectDistinct(selectList);
}
static FromGatherer selectDistinct(Collection selectList) {
return SelectDSL.selectDistinct(selectList);
}
static UpdateDSL update(SqlTable table) {
return UpdateDSL.update(table);
}
static WhereDSL where() {
return WhereDSL.where();
}
static WhereDSL where(BindableColumn column, VisitableCondition condition) {
return WhereDSL.where().where(column, condition);
}
static WhereDSL where(BindableColumn column, VisitableCondition condition,
SqlCriterion... subCriteria) {
return WhereDSL.where().where(column, condition, subCriteria);
}
static WhereDSL where(ExistsPredicate existsPredicate) {
return WhereDSL.where().where(existsPredicate);
}
static WhereDSL where(ExistsPredicate existsPredicate, SqlCriterion... subCriteria) {
return WhereDSL.where().where(existsPredicate, subCriteria);
}
// where condition connectors
static SqlCriterion or(BindableColumn column, VisitableCondition condition) {
return ColumnAndConditionCriterion.withColumn(column)
.withConnector("or") //$NON-NLS-1$
.withCondition(condition)
.build();
}
static SqlCriterion or(BindableColumn column, VisitableCondition condition,
SqlCriterion...subCriteria) {
return ColumnAndConditionCriterion.withColumn(column)
.withConnector("or") //$NON-NLS-1$
.withCondition(condition)
.withSubCriteria(Arrays.asList(subCriteria))
.build();
}
static SqlCriterion or(ExistsPredicate existsPredicate) {
return new ExistsCriterion.Builder()
.withConnector("or") //$NON-NLS-1$
.withExistsPredicate(existsPredicate)
.build();
}
static SqlCriterion or(ExistsPredicate existsPredicate, SqlCriterion...subCriteria) {
return new ExistsCriterion.Builder()
.withConnector("or") //$NON-NLS-1$
.withExistsPredicate(existsPredicate)
.withSubCriteria(Arrays.asList(subCriteria))
.build();
}
static SqlCriterion and(BindableColumn column, VisitableCondition condition) {
return ColumnAndConditionCriterion.withColumn(column)
.withConnector("and") //$NON-NLS-1$
.withCondition(condition)
.build();
}
static SqlCriterion and(BindableColumn column, VisitableCondition condition,
SqlCriterion...subCriteria) {
return ColumnAndConditionCriterion.withColumn(column)
.withConnector("and") //$NON-NLS-1$
.withCondition(condition)
.withSubCriteria(Arrays.asList(subCriteria))
.build();
}
static SqlCriterion and(ExistsPredicate existsPredicate) {
return new ExistsCriterion.Builder()
.withConnector("and") //$NON-NLS-1$
.withExistsPredicate(existsPredicate)
.build();
}
static SqlCriterion and(ExistsPredicate existsPredicate, SqlCriterion...subCriteria) {
return new ExistsCriterion.Builder()
.withConnector("and") //$NON-NLS-1$
.withExistsPredicate(existsPredicate)
.withSubCriteria(Arrays.asList(subCriteria))
.build();
}
// join support
static JoinCriterion and(BasicColumn joinColumn, JoinCondition joinCondition) {
return new JoinCriterion.Builder()
.withConnector("and") //$NON-NLS-1$
.withJoinColumn(joinColumn)
.withJoinCondition(joinCondition)
.build();
}
static JoinCriterion on(BasicColumn joinColumn, JoinCondition joinCondition) {
return new JoinCriterion.Builder()
.withConnector("on") //$NON-NLS-1$
.withJoinColumn(joinColumn)
.withJoinCondition(joinCondition)
.build();
}
static EqualTo equalTo(BasicColumn column) {
return new EqualTo(column);
}
// aggregate support
static CountAll count() {
return new CountAll();
}
static Count count(BasicColumn column) {
return Count.of(column);
}
static CountDistinct countDistinct(BasicColumn column) {
return CountDistinct.of(column);
}
static Max max(BindableColumn column) {
return Max.of(column);
}
static Min min(BindableColumn column) {
return Min.of(column);
}
static Avg avg(BindableColumn column) {
return Avg.of(column);
}
static Sum sum(BindableColumn column) {
return Sum.of(column);
}
// constants
static Constant constant(String constant) {
return Constant.of(constant);
}
static StringConstant stringConstant(String constant) {
return StringConstant.of(constant);
}
// functions
static Add add(BindableColumn firstColumn, BasicColumn secondColumn,
BasicColumn... subsequentColumns) {
return Add.of(firstColumn, secondColumn, subsequentColumns);
}
static Divide divide(BindableColumn firstColumn, BasicColumn secondColumn,
BasicColumn... subsequentColumns) {
return Divide.of(firstColumn, secondColumn, subsequentColumns);
}
static Multiply multiply(BindableColumn firstColumn, BasicColumn secondColumn,
BasicColumn... subsequentColumns) {
return Multiply.of(firstColumn, secondColumn, subsequentColumns);
}
static Subtract subtract(BindableColumn firstColumn, BasicColumn secondColumn,
BasicColumn... subsequentColumns) {
return Subtract.of(firstColumn, secondColumn, subsequentColumns);
}
static Concatenate concatenate(BindableColumn firstColumn, BasicColumn secondColumn,
BasicColumn... subsequentColumns) {
return Concatenate.concatenate(firstColumn, secondColumn, subsequentColumns);
}
static OperatorFunction applyOperator(String operator, BindableColumn firstColumn,
BasicColumn secondColumn, BasicColumn... subsequentColumns) {
return OperatorFunction.of(operator, firstColumn, secondColumn, subsequentColumns);
}
static Lower lower(BindableColumn column) {
return Lower.of(column);
}
static Substring substring(BindableColumn column, int offset, int length) {
return Substring.of(column, offset, length);
}
static Upper upper(BindableColumn column) {
return Upper.of(column);
}
// conditions for all data types
static ExistsPredicate exists(Buildable selectModelBuilder) {
return ExistsPredicate.exists(selectModelBuilder);
}
static ExistsPredicate notExists(Buildable selectModelBuilder) {
return ExistsPredicate.notExists(selectModelBuilder);
}
static IsNull isNull() {
return new IsNull<>();
}
static IsNotNull isNotNull() {
return new IsNotNull<>();
}
static IsEqualTo isEqualTo(T value) {
return IsEqualTo.of(value);
}
static IsEqualTo isEqualTo(Supplier valueSupplier) {
return isEqualTo(valueSupplier.get());
}
static IsEqualToWithSubselect isEqualTo(Buildable selectModelBuilder) {
return IsEqualToWithSubselect.of(selectModelBuilder);
}
static IsEqualToColumn isEqualTo(BasicColumn column) {
return IsEqualToColumn.of(column);
}
static IsEqualTo isEqualToWhenPresent(T value) {
return IsEqualTo.of(value).filter(Objects::nonNull);
}
static IsEqualTo isEqualToWhenPresent(Supplier valueSupplier) {
return isEqualToWhenPresent(valueSupplier.get());
}
static IsNotEqualTo isNotEqualTo(T value) {
return IsNotEqualTo.of(value);
}
static IsNotEqualTo isNotEqualTo(Supplier valueSupplier) {
return isNotEqualTo(valueSupplier.get());
}
static IsNotEqualToWithSubselect isNotEqualTo(Buildable selectModelBuilder) {
return IsNotEqualToWithSubselect.of(selectModelBuilder);
}
static IsNotEqualToColumn isNotEqualTo(BasicColumn column) {
return IsNotEqualToColumn.of(column);
}
static IsNotEqualTo isNotEqualToWhenPresent(T value) {
return IsNotEqualTo.of(value).filter(Objects::nonNull);
}
static IsNotEqualTo isNotEqualToWhenPresent(Supplier valueSupplier) {
return isNotEqualToWhenPresent(valueSupplier.get());
}
static IsGreaterThan isGreaterThan(T value) {
return IsGreaterThan.of(value);
}
static IsGreaterThan isGreaterThan(Supplier valueSupplier) {
return isGreaterThan(valueSupplier.get());
}
static IsGreaterThanWithSubselect isGreaterThan(Buildable selectModelBuilder) {
return IsGreaterThanWithSubselect.of(selectModelBuilder);
}
static IsGreaterThanColumn isGreaterThan(BasicColumn column) {
return IsGreaterThanColumn.of(column);
}
static IsGreaterThan isGreaterThanWhenPresent(T value) {
return IsGreaterThan.of(value).filter(Objects::nonNull);
}
static IsGreaterThan isGreaterThanWhenPresent(Supplier valueSupplier) {
return isGreaterThanWhenPresent(valueSupplier.get());
}
static IsGreaterThanOrEqualTo isGreaterThanOrEqualTo(T value) {
return IsGreaterThanOrEqualTo.of(value);
}
static IsGreaterThanOrEqualTo isGreaterThanOrEqualTo(Supplier valueSupplier) {
return isGreaterThanOrEqualTo(valueSupplier.get());
}
static IsGreaterThanOrEqualToWithSubselect isGreaterThanOrEqualTo(
Buildable selectModelBuilder) {
return IsGreaterThanOrEqualToWithSubselect.of(selectModelBuilder);
}
static IsGreaterThanOrEqualToColumn isGreaterThanOrEqualTo(BasicColumn column) {
return IsGreaterThanOrEqualToColumn.of(column);
}
static IsGreaterThanOrEqualTo isGreaterThanOrEqualToWhenPresent(T value) {
return IsGreaterThanOrEqualTo.of(value).filter(Objects::nonNull);
}
static IsGreaterThanOrEqualTo isGreaterThanOrEqualToWhenPresent(Supplier valueSupplier) {
return isGreaterThanOrEqualToWhenPresent(valueSupplier.get());
}
static IsLessThan isLessThan(T value) {
return IsLessThan.of(value);
}
static IsLessThan isLessThan(Supplier valueSupplier) {
return isLessThan(valueSupplier.get());
}
static IsLessThanWithSubselect isLessThan(Buildable selectModelBuilder) {
return IsLessThanWithSubselect.of(selectModelBuilder);
}
static IsLessThanColumn isLessThan(BasicColumn column) {
return IsLessThanColumn.of(column);
}
static IsLessThan isLessThanWhenPresent(T value) {
return IsLessThan.of(value).filter(Objects::nonNull);
}
static IsLessThan isLessThanWhenPresent(Supplier valueSupplier) {
return isLessThanWhenPresent(valueSupplier.get());
}
static IsLessThanOrEqualTo isLessThanOrEqualTo(T value) {
return IsLessThanOrEqualTo.of(value);
}
static IsLessThanOrEqualTo isLessThanOrEqualTo(Supplier valueSupplier) {
return isLessThanOrEqualTo(valueSupplier.get());
}
static IsLessThanOrEqualToWithSubselect isLessThanOrEqualTo(Buildable selectModelBuilder) {
return IsLessThanOrEqualToWithSubselect.of(selectModelBuilder);
}
static IsLessThanOrEqualToColumn isLessThanOrEqualTo(BasicColumn column) {
return IsLessThanOrEqualToColumn.of(column);
}
static IsLessThanOrEqualTo isLessThanOrEqualToWhenPresent(T value) {
return IsLessThanOrEqualTo.of(value).filter(Objects::nonNull);
}
static IsLessThanOrEqualTo isLessThanOrEqualToWhenPresent(Supplier valueSupplier) {
return isLessThanOrEqualToWhenPresent(valueSupplier.get());
}
@SafeVarargs
static IsIn isIn(T...values) {
return IsIn.of(values);
}
static IsIn isIn(Collection values) {
return IsIn.of(values);
}
static IsInWithSubselect isIn(Buildable selectModelBuilder) {
return IsInWithSubselect.of(selectModelBuilder);
}
@SafeVarargs
static IsIn isInWhenPresent(T...values) {
return IsIn.of(values).filter(Objects::nonNull);
}
static IsIn isInWhenPresent(Collection values) {
return values == null ? IsIn.empty() : IsIn.of(values).filter(Objects::nonNull);
}
@SafeVarargs
static IsNotIn isNotIn(T...values) {
return IsNotIn.of(values);
}
static IsNotIn isNotIn(Collection values) {
return IsNotIn.of(values);
}
static IsNotInWithSubselect isNotIn(Buildable selectModelBuilder) {
return IsNotInWithSubselect.of(selectModelBuilder);
}
@SafeVarargs
static IsNotIn isNotInWhenPresent(T...values) {
return IsNotIn.of(values).filter(Objects::nonNull);
}
static IsNotIn isNotInWhenPresent(Collection values) {
return values == null ? IsNotIn.empty() : IsNotIn.of(values).filter(Objects::nonNull);
}
static IsBetween.Builder isBetween(T value1) {
return IsBetween.isBetween(value1);
}
static IsBetween.Builder isBetween(Supplier valueSupplier1) {
return isBetween(valueSupplier1.get());
}
static IsBetween.WhenPresentBuilder isBetweenWhenPresent(T value1) {
return IsBetween.isBetweenWhenPresent(value1);
}
static IsBetween.WhenPresentBuilder isBetweenWhenPresent(Supplier valueSupplier1) {
return isBetweenWhenPresent(valueSupplier1.get());
}
static IsNotBetween.Builder isNotBetween(T value1) {
return IsNotBetween.isNotBetween(value1);
}
static IsNotBetween.Builder isNotBetween(Supplier valueSupplier1) {
return isNotBetween(valueSupplier1.get());
}
static IsNotBetween.WhenPresentBuilder isNotBetweenWhenPresent(T value1) {
return IsNotBetween.isNotBetweenWhenPresent(value1);
}
static IsNotBetween.WhenPresentBuilder isNotBetweenWhenPresent(Supplier valueSupplier1) {
return isNotBetweenWhenPresent(valueSupplier1.get());
}
// for string columns, but generic for columns with type handlers
static IsLike isLike(T value) {
return IsLike.of(value);
}
static IsLike isLike(Supplier valueSupplier) {
return isLike(valueSupplier.get());
}
static IsLike isLikeWhenPresent(T value) {
return IsLike.of(value).filter(Objects::nonNull);
}
static IsLike isLikeWhenPresent(Supplier valueSupplier) {
return isLikeWhenPresent(valueSupplier.get());
}
static IsNotLike isNotLike(T value) {
return IsNotLike.of(value);
}
static IsNotLike isNotLike(Supplier valueSupplier) {
return isNotLike(valueSupplier.get());
}
static IsNotLike isNotLikeWhenPresent(T value) {
return IsNotLike.of(value).filter(Objects::nonNull);
}
static IsNotLike isNotLikeWhenPresent(Supplier valueSupplier) {
return isNotLikeWhenPresent(valueSupplier.get());
}
// shortcuts for booleans
static IsEqualTo