Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.select.function;
package org.mybatis.dynamic.sql;

import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;

public class Constant<T> implements BasicColumn {
public class Constant implements BasicColumn {

private String alias;
private T value;
private String value;

private Constant(T value) {
private Constant(String value) {
this.value = Objects.requireNonNull(value);
}

Expand All @@ -37,17 +36,17 @@ public Optional<String> alias() {

@Override
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
return value.toString();
return value;
}

@Override
public Constant<T> as(String alias) {
Constant<T> copy = new Constant<>(value);
public Constant as(String alias) {
Constant copy = new Constant(value);
copy.alias = alias;
return copy;
}

public static <T> Constant<T> of(T value) {
return new Constant<>(value);
public static Constant of(String value) {
return new Constant(value);
}
}
9 changes: 4 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
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.Constant;
import org.mybatis.dynamic.sql.select.function.Divide;
import org.mybatis.dynamic.sql.select.function.Lower;
import org.mybatis.dynamic.sql.select.function.Multiply;
Expand Down Expand Up @@ -192,12 +191,12 @@ static Sum sum(BasicColumn column) {
}

// constants
static <T extends Number> Constant<T> constant(T number) {
return Constant.of(number);
static Constant constant(String constant) {
return Constant.of(constant);
}

static Constant<String> constant(String string) {
return Constant.of("'" + string + "'"); //$NON-NLS-1$ //$NON-NLS-2$
static StringConstant stringConstant(String constant) {
return StringConstant.of(constant);
}

// functions
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/StringConstant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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;

import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.TableAliasCalculator;

public class StringConstant implements BasicColumn {

private String alias;
private String value;

private StringConstant(String value) {
this.value = Objects.requireNonNull(value);
}

@Override
public Optional<String> alias() {
return Optional.ofNullable(alias);
}

@Override
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
return "'" + value + "'"; //$NON-NLS-1$ //$NON-NLS-2$
}

@Override
public StringConstant as(String alias) {
StringConstant copy = new StringConstant(value);
copy.alias = alias;
return copy;
}

public static StringConstant of(String value) {
return new StringConstant(value);
}
}
45 changes: 7 additions & 38 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
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.ArithmeticConstantMapping;
import org.mybatis.dynamic.sql.util.ArithmeticOperation;
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;
Expand Down Expand Up @@ -126,6 +126,11 @@ public UpdateDSL<R> equalTo(Buildable<SelectModel> buildable) {
return UpdateDSL.this;
}

public UpdateDSL<R> equalTo(BasicColumn rightColumn) {
columnMappings.add(ColumnMapping.of(column, rightColumn));
return UpdateDSL.this;
}

public UpdateDSL<R> equalToWhenPresent(T value) {
return equalToWhenPresent(() -> value);
}
Expand All @@ -136,42 +141,6 @@ public UpdateDSL<R> equalToWhenPresent(Supplier<T> valueSupplier) {
}
return UpdateDSL.this;
}

public UpdateDSL<R> incrementBy(T value) {
return incrementBy(() -> value);
}

public UpdateDSL<R> incrementBy(Supplier<T> valueSupplier) {
columnMappings.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.ADD, valueSupplier));
return UpdateDSL.this;
}

public UpdateDSL<R> decrementBy(T value) {
return decrementBy(() -> value);
}

public UpdateDSL<R> decrementBy(Supplier<T> valueSupplier) {
columnMappings.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.SUBTRACT, valueSupplier));
return UpdateDSL.this;
}

public UpdateDSL<R> multiplyBy(T value) {
return multiplyBy(() -> value);
}

public UpdateDSL<R> multiplyBy(Supplier<T> valueSupplier) {
columnMappings.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.MULTIPLY, valueSupplier));
return UpdateDSL.this;
}

public UpdateDSL<R> divideBy(T value) {
return divideBy(() -> value);
}

public UpdateDSL<R> divideBy(Supplier<T> valueSupplier) {
columnMappings.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.DIVIDE, valueSupplier));
return UpdateDSL.this;
}
}

public class UpdateWhereBuilder extends AbstractWhereDSL<UpdateWhereBuilder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.util.ArithmeticConstantMapping;
import org.mybatis.dynamic.sql.util.ColumnMapping;
import org.mybatis.dynamic.sql.util.ConstantMapping;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
import org.mybatis.dynamic.sql.util.NullMapping;
Expand Down Expand Up @@ -80,19 +81,6 @@ public <T> FragmentAndParameters visit(ValueMapping<T> mapping) {
.build();
}

@Override
public <S> FragmentAndParameters visit(ArithmeticConstantMapping<S> mapping) {
String fragment = mapping.mapColumn(SqlColumn::name)
+ " = " //$NON-NLS-1$
+ mapping.mapColumn(SqlColumn::name)
+ " " //$NON-NLS-1$
+ mapping.operation().getOperator()
+ " " //$NON-NLS-1$
+ mapping.valueSupplier().get();
return FragmentAndParameters.withFragment(fragment)
.build();
}

@Override
public FragmentAndParameters visit(SelectMapping mapping) {
SelectStatementProvider selectStatement = SelectRenderer.withSelectModel(mapping.selectModel())
Expand All @@ -111,6 +99,16 @@ public FragmentAndParameters visit(SelectMapping mapping) {
.build();
}

@Override
public FragmentAndParameters visit(ColumnMapping mapping) {
String setPhrase = mapping.mapColumn(SqlColumn::name)
+ " = " //$NON-NLS-1$
+ mapping.rightColumn().renderWithTableAlias(TableAliasCalculator.empty());

return FragmentAndParameters.withFragment(setPhrase)
.build();
}

private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
return column -> renderingStrategy
.getFormattedJdbcPlaceholder(column, "parameters", parameterName); //$NON-NLS-1$
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,28 @@
*/
package org.mybatis.dynamic.sql.util;

public enum ArithmeticOperation {
ADD("+"), //$NON-NLS-1$
SUBTRACT("-"), //$NON-NLS-1$
MULTIPLY("*"), //$NON-NLS-1$
DIVIDE("/"); //$NON-NLS-1$
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlColumn;

public class ColumnMapping extends AbstractColumnMapping implements UpdateMapping {

private BasicColumn rightColumn;

private String operator;
private ColumnMapping(SqlColumn<?> column, BasicColumn rightColumn) {
super(column);
this.rightColumn = rightColumn;
}

private ArithmeticOperation(String operator) {
this.operator = operator;
public BasicColumn rightColumn() {
return rightColumn;
}

public String getOperator() {
return operator;
@Override
public <R> R accept(UpdateMappingVisitor<R> visitor) {
return visitor.visit(this);
}

public static ColumnMapping of(SqlColumn<?> column, BasicColumn rightColumn) {
return new ColumnMapping(column, rightColumn);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface UpdateMappingVisitor<T> {

<S> T visit(ValueMapping<S> mapping);

<S> T visit(ArithmeticConstantMapping<S> mapping);

T visit(SelectMapping mapping);

T visit(ColumnMapping columnMapping);
}
1 change: 1 addition & 0 deletions src/site/markdown/docs/update.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Notice the `set` method. It is used to set the value of a database column. Ther
6. `set(column).equalToWhenPresent(T value)` will set a value into a column if the value is non-null. The value of the property will be bound to the SQL statement as a prepared statement parameter. This is used to generate a "selective" update as defined in MyBatis Generator.
7. `set(column).equalToWhenPresent(Supplier<T> valueSupplier)` will set a value into a column if the value is non-null. The value of the property will be bound to the SQL statement as a prepared statement parameter. This is used to generate a "selective" update as defined in MyBatis Generator.
8. `set(column).equalTo(Buildable<SelectModel> selectModelBuilder)` will set the result of a sub-query into a column. The query should only have one column and the type of the returned column must be able to be converted by the database if it is not the same type. These constraints are NOT validated by the library.
9. `set(column).equalTo(BasicColumn rightColumn)` will set the value of a column the be the value of another column. This is also useful for specifying a function such as add, subtract, etc.

You can also build an update statement without a where clause. This will update every row in a table.
For example:
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/examples/animal/data/AnimalDataMapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2017 the original author or authors.
* 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.
Expand Down Expand Up @@ -46,6 +46,10 @@ public interface AnimalDataMapper {
})
List<AnimalData> selectMany(SelectStatementProvider selectStatement);

@SelectProvider(type=SqlProviderAdapter.class, method="select")
@ResultMap("AnimalDataResult")
AnimalData selectOne(SelectStatementProvider selectStatement);

@SelectProvider(type=SqlProviderAdapter.class, method="select")
List<Map<String, Object>> generalSelect(SelectStatementProvider selectStatement);

Expand Down
Loading