org.mybatis.dynamic.sql.update.UpdateDSL Maven / Gradle / Ivy
/**
* Copyright 2016-2018 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.update;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlCriterion;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.VisitableCondition;
import org.mybatis.dynamic.sql.select.SelectModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.Buildable;
import org.mybatis.dynamic.sql.util.ColumnMapping;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.NullMapping;
import org.mybatis.dynamic.sql.util.SelectMapping;
import org.mybatis.dynamic.sql.util.StringConstantMapping;
import org.mybatis.dynamic.sql.util.UpdateMapping;
import org.mybatis.dynamic.sql.util.ValueMapping;
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
public class UpdateDSL {
private Function adapterFunction;
private List columnMappings = new ArrayList<>();
private SqlTable table;
private UpdateDSL(SqlTable table, Function adapterFunction) {
this.table = Objects.requireNonNull(table);
this.adapterFunction = Objects.requireNonNull(adapterFunction);
}
public SetClauseFinisher set(SqlColumn column) {
return new SetClauseFinisher<>(column);
}
public UpdateWhereBuilder where(BindableColumn column, VisitableCondition condition) {
return new UpdateWhereBuilder(column, condition);
}
public UpdateWhereBuilder where(BindableColumn column, VisitableCondition condition,
SqlCriterion>...subCriteria) {
return new UpdateWhereBuilder(column, condition, subCriteria);
}
/**
* WARNING! Calling this method could result in an update statement that updates
* all rows in a table.
*
* @return the update model
*/
public R build() {
UpdateModel updateModel = UpdateModel.withTable(table)
.withColumnMappings(columnMappings)
.build();
return adapterFunction.apply(updateModel);
}
public static UpdateDSL update(Function adapterFunction, SqlTable table) {
return new UpdateDSL<>(table, adapterFunction);
}
public static UpdateDSL update(SqlTable table) {
return update(Function.identity(), table);
}
public static UpdateDSL> updateWithMapper(
Function mapperMethod, SqlTable table) {
return update(updateModel -> MyBatis3UpdateModelAdapter.of(updateModel, mapperMethod), table);
}
public class SetClauseFinisher {
private SqlColumn column;
public SetClauseFinisher(SqlColumn column) {
this.column = column;
}
public UpdateDSL equalToNull() {
columnMappings.add(NullMapping.of(column));
return UpdateDSL.this;
}
public UpdateDSL equalToConstant(String constant) {
columnMappings.add(ConstantMapping.of(column, constant));
return UpdateDSL.this;
}
public UpdateDSL equalToStringConstant(String constant) {
columnMappings.add(StringConstantMapping.of(column, constant));
return UpdateDSL.this;
}
public UpdateDSL equalTo(T value) {
return equalTo(() -> value);
}
public UpdateDSL equalTo(Supplier valueSupplier) {
columnMappings.add(ValueMapping.of(column, valueSupplier));
return UpdateDSL.this;
}
public UpdateDSL equalTo(Buildable buildable) {
columnMappings.add(SelectMapping.of(column, buildable));
return UpdateDSL.this;
}
public UpdateDSL equalTo(BasicColumn rightColumn) {
columnMappings.add(ColumnMapping.of(column, rightColumn));
return UpdateDSL.this;
}
public UpdateDSL equalToWhenPresent(T value) {
return equalToWhenPresent(() -> value);
}
public UpdateDSL equalToWhenPresent(Supplier valueSupplier) {
if (valueSupplier.get() != null) {
columnMappings.add(ValueMapping.of(column, valueSupplier));
}
return UpdateDSL.this;
}
}
public class UpdateWhereBuilder extends AbstractWhereDSL {
public UpdateWhereBuilder(BindableColumn column, VisitableCondition condition) {
super(column, condition);
}
public UpdateWhereBuilder(BindableColumn column, VisitableCondition condition,
SqlCriterion>...subCriteria) {
super(column, condition, subCriteria);
}
public R build() {
UpdateModel updateModel = UpdateModel.withTable(table)
.withColumnMappings(columnMappings)
.withWhereModel(buildWhereModel())
.build();
return adapterFunction.apply(updateModel);
}
@Override
protected UpdateWhereBuilder getThis() {
return this;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy