Skip to content

Commit e174138

Browse files
committed
Implementing the ability to use add, substract, multiply and divide by a number for the set clause of update query
1 parent a3ffea4 commit e174138

File tree

6 files changed

+158
-4
lines changed

6 files changed

+158
-4
lines changed

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@
2727
import org.mybatis.dynamic.sql.SqlTable;
2828
import org.mybatis.dynamic.sql.VisitableCondition;
2929
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
30+
import org.mybatis.dynamic.sql.util.ArithmeticConstantMapping;
31+
import org.mybatis.dynamic.sql.util.ArithmeticOperation;
3032
import org.mybatis.dynamic.sql.util.ConstantMapping;
3133
import org.mybatis.dynamic.sql.util.NullMapping;
3234
import org.mybatis.dynamic.sql.util.StringConstantMapping;
@@ -126,6 +128,42 @@ public UpdateDSL<R> equalToWhenPresent(Supplier<T> valueSupplier) {
126128
}
127129
return UpdateDSL.this;
128130
}
131+
132+
public UpdateDSL<R> incrementBy(T value) {
133+
return incrementBy(() -> value);
134+
}
135+
136+
public UpdateDSL<R> incrementBy(Supplier<T> valueSupplier) {
137+
columnsAndValues.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.add, valueSupplier));
138+
return UpdateDSL.this;
139+
}
140+
141+
public UpdateDSL<R> decrementBy(T value) {
142+
return decrementBy(() -> value);
143+
}
144+
145+
public UpdateDSL<R> decrementBy(Supplier<T> valueSupplier) {
146+
columnsAndValues.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.substract, valueSupplier));
147+
return UpdateDSL.this;
148+
}
149+
150+
public UpdateDSL<R> multiplyBy(T value) {
151+
return multiplyBy(() -> value);
152+
}
153+
154+
public UpdateDSL<R> multiplyBy(Supplier<T> valueSupplier) {
155+
columnsAndValues.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.multiply, valueSupplier));
156+
return UpdateDSL.this;
157+
}
158+
159+
public UpdateDSL<R> divideBy(T value) {
160+
return divideBy(() -> value);
161+
}
162+
163+
public UpdateDSL<R> divideBy(Supplier<T> valueSupplier) {
164+
columnsAndValues.add(ArithmeticConstantMapping.of(column, ArithmeticOperation.divide, valueSupplier));
165+
return UpdateDSL.this;
166+
}
129167
}
130168

131169
public class UpdateWhereBuilder extends AbstractWhereDSL<UpdateWhereBuilder> {

src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import org.mybatis.dynamic.sql.SqlColumn;
2222
import org.mybatis.dynamic.sql.render.RenderingStrategy;
23+
import org.mybatis.dynamic.sql.util.ArithmeticConstantMapping;
2324
import org.mybatis.dynamic.sql.util.ConstantMapping;
2425
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
2526
import org.mybatis.dynamic.sql.util.NullMapping;
@@ -78,4 +79,11 @@ private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
7879
return column -> renderingStrategy
7980
.getFormattedJdbcPlaceholder(column, "parameters", parameterName); //$NON-NLS-1$
8081
}
82+
83+
@Override
84+
public <S> FragmentAndParameters visit(ArithmeticConstantMapping<S> mapping) {
85+
String fragment = mapping.mapColumn(SqlColumn::name) + " = " + mapping.mapColumn(SqlColumn::name) + " " + mapping.operation().getOperator() + " " + mapping.valueSupplier().get(); //$NON-NLS-1$
86+
return FragmentAndParameters.withFragment(fragment)
87+
.build();
88+
}
8189
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2016-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
import java.util.function.Supplier;
19+
20+
import org.mybatis.dynamic.sql.SqlColumn;
21+
22+
public class ArithmeticConstantMapping<T> extends AbstractColumnMapping implements UpdateMapping {
23+
private Supplier<T> valueSupplier;
24+
private ArithmeticOperation operation;
25+
26+
private ArithmeticConstantMapping(SqlColumn<?> column, ArithmeticOperation operation, Supplier<T> valueSupplier) {
27+
super(column);
28+
this.operation = operation;
29+
this.valueSupplier = valueSupplier;
30+
}
31+
32+
public Supplier<T> valueSupplier() {
33+
return valueSupplier;
34+
}
35+
36+
public ArithmeticOperation operation() {
37+
return operation;
38+
}
39+
40+
public static <T> ArithmeticConstantMapping<T> of(SqlColumn<?> column, ArithmeticOperation operation, Supplier<T> valueSupplier) {
41+
ArithmeticConstantMapping<T> mapping = new ArithmeticConstantMapping<>(column, operation, valueSupplier);
42+
return mapping;
43+
}
44+
45+
@Override
46+
public <R> R accept(UpdateMappingVisitor<R> visitor) {
47+
return visitor.visit(this);
48+
}
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright 2016-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.util;
17+
18+
public enum ArithmeticOperation {
19+
add("+"),
20+
substract("-"),
21+
multiply("*"),
22+
divide("/");
23+
24+
private String operator;
25+
26+
private ArithmeticOperation(String operator) {
27+
this.operator = operator;
28+
}
29+
30+
public String getOperator() {
31+
return operator;
32+
}
33+
}

src/main/java/org/mybatis/dynamic/sql/util/UpdateMappingVisitor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,4 +23,6 @@ public interface UpdateMappingVisitor<T> {
2323
T visit(StringConstantMapping mapping);
2424

2525
<S> T visit(ValueMapping<S> mapping);
26+
27+
<S> T visit(ArithmeticConstantMapping<S> mapping);
2628
}

src/test/java/org/mybatis/dynamic/sql/update/UpdateStatementTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -170,4 +170,28 @@ public void testFullUpdateStatementNoWhere() {
170170
softly.assertThat(updateStatement.getParameters().get("up2")).isEqualTo("jones");
171171
});
172172
}
173+
174+
@Test
175+
public void testUpdateStatementArithmeticOperation() {
176+
UpdateStatementProvider updateStatement = update(foo)
177+
.set(id).incrementBy(1)
178+
.set(id).decrementBy(2)
179+
.set(id).multiplyBy(3)
180+
.set(id).divideBy(4)
181+
.build()
182+
.render(RenderingStrategy.MYBATIS3);
183+
184+
String expectedStatement = "update foo "
185+
+ "set id = id + 1, "
186+
+ "id = id - 2, "
187+
+ "id = id * 3, "
188+
+ "id = id / 4";
189+
190+
SoftAssertions.assertSoftly(softly -> {
191+
softly.assertThat(updateStatement.getUpdateStatement()).isEqualTo(expectedStatement);
192+
193+
softly.assertThat(updateStatement.getParameters().size()).isEqualTo(0);
194+
});
195+
}
173196
}
197+

0 commit comments

Comments
 (0)