Skip to content

Commit d7106d2

Browse files
committed
Add column comparison conditions
Allows writing queries that compare columns. For example "where column1 = column2"
1 parent e76118d commit d7106d2

15 files changed

+665
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
17+
18+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
19+
20+
public abstract class AbstractColumnComparisonCondition<T> implements VisitableCondition<T> {
21+
22+
protected BasicColumn column;
23+
24+
protected AbstractColumnComparisonCondition(BasicColumn column) {
25+
this.column = column;
26+
}
27+
28+
@Override
29+
public <R> R accept(ConditionVisitor<T,R> visitor) {
30+
return visitor.visit(this);
31+
}
32+
33+
public String renderCondition(String columnName, TableAliasCalculator tableAliasCalculator) {
34+
return renderCondition(columnName, column.renderWithTableAlias(tableAliasCalculator));
35+
}
36+
37+
protected abstract String renderCondition(String leftColumn, String rightColumn);
38+
}

src/main/java/org/mybatis/dynamic/sql/ConditionVisitor.java

+3-1
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.
@@ -25,4 +25,6 @@ public interface ConditionVisitor<T, R> {
2525
R visit(AbstractTwoValueCondition<T> condition);
2626

2727
R visit(AbstractSubselectCondition<T> condition);
28+
29+
R visit(AbstractColumnComparisonCondition<T> condition);
2830
}

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

+30
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,28 @@
5050
import org.mybatis.dynamic.sql.where.WhereDSL;
5151
import org.mybatis.dynamic.sql.where.condition.IsBetween;
5252
import org.mybatis.dynamic.sql.where.condition.IsEqualTo;
53+
import org.mybatis.dynamic.sql.where.condition.IsEqualToColumn;
5354
import org.mybatis.dynamic.sql.where.condition.IsEqualToWithSubselect;
5455
import org.mybatis.dynamic.sql.where.condition.IsGreaterThan;
56+
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanColumn;
5557
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualTo;
58+
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToColumn;
5659
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanOrEqualToWithSubselect;
5760
import org.mybatis.dynamic.sql.where.condition.IsGreaterThanWithSubselect;
5861
import org.mybatis.dynamic.sql.where.condition.IsIn;
5962
import org.mybatis.dynamic.sql.where.condition.IsInCaseInsensitive;
6063
import org.mybatis.dynamic.sql.where.condition.IsInWithSubselect;
6164
import org.mybatis.dynamic.sql.where.condition.IsLessThan;
65+
import org.mybatis.dynamic.sql.where.condition.IsLessThanColumn;
6266
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualTo;
67+
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToColumn;
6368
import org.mybatis.dynamic.sql.where.condition.IsLessThanOrEqualToWithSubselect;
6469
import org.mybatis.dynamic.sql.where.condition.IsLessThanWithSubselect;
6570
import org.mybatis.dynamic.sql.where.condition.IsLike;
6671
import org.mybatis.dynamic.sql.where.condition.IsLikeCaseInsensitive;
6772
import org.mybatis.dynamic.sql.where.condition.IsNotBetween;
6873
import org.mybatis.dynamic.sql.where.condition.IsNotEqualTo;
74+
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToColumn;
6975
import org.mybatis.dynamic.sql.where.condition.IsNotEqualToWithSubselect;
7076
import org.mybatis.dynamic.sql.where.condition.IsNotIn;
7177
import org.mybatis.dynamic.sql.where.condition.IsNotInCaseInsensitive;
@@ -256,6 +262,10 @@ static <T> IsEqualTo<T> isEqualTo(Supplier<T> valueSupplier) {
256262
static <T> IsEqualToWithSubselect<T> isEqualTo(Buildable<SelectModel> selectModelBuilder) {
257263
return IsEqualToWithSubselect.of(selectModelBuilder);
258264
}
265+
266+
static <T> IsEqualToColumn<T> isEqualTo(BasicColumn column) {
267+
return IsEqualToColumn.of(column);
268+
}
259269

260270
static <T> IsNotEqualTo<T> isNotEqualTo(T value) {
261271
return isNotEqualTo(() -> value);
@@ -269,6 +279,10 @@ static <T> IsNotEqualToWithSubselect<T> isNotEqualTo(Buildable<SelectModel> sele
269279
return IsNotEqualToWithSubselect.of(selectModelBuilder);
270280
}
271281

282+
static <T> IsNotEqualToColumn<T> isNotEqualTo(BasicColumn column) {
283+
return IsNotEqualToColumn.of(column);
284+
}
285+
272286
static <T> IsGreaterThan<T> isGreaterThan(T value) {
273287
return isGreaterThan(() -> value);
274288
}
@@ -281,6 +295,10 @@ static <T> IsGreaterThanWithSubselect<T> isGreaterThan(Buildable<SelectModel> se
281295
return IsGreaterThanWithSubselect.of(selectModelBuilder);
282296
}
283297

298+
static <T> IsGreaterThanColumn<T> isGreaterThan(BasicColumn column) {
299+
return IsGreaterThanColumn.of(column);
300+
}
301+
284302
static <T> IsGreaterThanOrEqualTo<T> isGreaterThanOrEqualTo(T value) {
285303
return isGreaterThanOrEqualTo(() -> value);
286304
}
@@ -294,6 +312,10 @@ static <T> IsGreaterThanOrEqualToWithSubselect<T> isGreaterThanOrEqualTo(
294312
return IsGreaterThanOrEqualToWithSubselect.of(selectModelBuilder);
295313
}
296314

315+
static <T> IsGreaterThanOrEqualToColumn<T> isGreaterThanOrEqualTo(BasicColumn column) {
316+
return IsGreaterThanOrEqualToColumn.of(column);
317+
}
318+
297319
static <T> IsLessThan<T> isLessThan(T value) {
298320
return isLessThan(() -> value);
299321
}
@@ -306,6 +328,10 @@ static <T> IsLessThanWithSubselect<T> isLessThan(Buildable<SelectModel> selectMo
306328
return IsLessThanWithSubselect.of(selectModelBuilder);
307329
}
308330

331+
static <T> IsLessThanColumn<T> isLessThan(BasicColumn column) {
332+
return IsLessThanColumn.of(column);
333+
}
334+
309335
static <T> IsLessThanOrEqualTo<T> isLessThanOrEqualTo(T value) {
310336
return isLessThanOrEqualTo(() -> value);
311337
}
@@ -318,6 +344,10 @@ static <T> IsLessThanOrEqualToWithSubselect<T> isLessThanOrEqualTo(Buildable<Sel
318344
return IsLessThanOrEqualToWithSubselect.of(selectModelBuilder);
319345
}
320346

347+
static <T> IsLessThanOrEqualToColumn<T> isLessThanOrEqualTo(BasicColumn column) {
348+
return IsLessThanOrEqualToColumn.of(column);
349+
}
350+
321351
@SafeVarargs
322352
static <T> IsIn<T> isIn(T...values) {
323353
return isIn(Arrays.asList(values));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsEqualToColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsEqualToColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " = " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsEqualToColumn<T> of(BasicColumn column) {
33+
return new IsEqualToColumn<>(column);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsGreaterThanColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsGreaterThanColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " > " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsGreaterThanColumn<T> of(BasicColumn column) {
33+
return new IsGreaterThanColumn<>(column);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsGreaterThanOrEqualToColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsGreaterThanOrEqualToColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " >= " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsGreaterThanOrEqualToColumn<T> of(BasicColumn column) {
33+
return new IsGreaterThanOrEqualToColumn<>(column);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsLessThanColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsLessThanColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " < " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsLessThanColumn<T> of(BasicColumn column) {
33+
return new IsLessThanColumn<>(column);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsLessThanOrEqualToColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsLessThanOrEqualToColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " <= " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsLessThanOrEqualToColumn<T> of(BasicColumn column) {
33+
return new IsLessThanOrEqualToColumn<>(column);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.where.condition;
17+
18+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
19+
import org.mybatis.dynamic.sql.BasicColumn;
20+
21+
public class IsNotEqualToColumn<T> extends AbstractColumnComparisonCondition<T> {
22+
23+
protected IsNotEqualToColumn(BasicColumn column) {
24+
super(column);
25+
}
26+
27+
@Override
28+
protected String renderCondition(String leftColumn, String rightColumn) {
29+
return leftColumn + " <> " + rightColumn; //$NON-NLS-1$
30+
}
31+
32+
public static <T> IsNotEqualToColumn<T> of(BasicColumn column) {
33+
return new IsNotEqualToColumn<>(column);
34+
}
35+
}

src/main/java/org/mybatis/dynamic/sql/where/render/WhereConditionVisitor.java

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Optional;
2020
import java.util.concurrent.atomic.AtomicInteger;
2121

22+
import org.mybatis.dynamic.sql.AbstractColumnComparisonCondition;
2223
import org.mybatis.dynamic.sql.AbstractListValueCondition;
2324
import org.mybatis.dynamic.sql.AbstractNoValueCondition;
2425
import org.mybatis.dynamic.sql.AbstractSingleValueCondition;
@@ -106,6 +107,12 @@ public FragmentAndParameters visit(AbstractSubselectCondition<T> condition) {
106107
.build();
107108
}
108109

110+
@Override
111+
public FragmentAndParameters visit(AbstractColumnComparisonCondition<T> condition) {
112+
String fragment = condition.renderCondition(columnName(), tableAliasCalculator);
113+
return FragmentAndParameters.withFragment(fragment).build();
114+
}
115+
109116
private FragmentAndParameters toFragmentAndParameters(Object value) {
110117
String mapKey = formatParameterMapKey(sequence.getAndIncrement());
111118

0 commit comments

Comments
 (0)