Skip to content

Commit 8a7bed9

Browse files
committed
New Hierarchy for Functions
1 parent 8ad566f commit 8a7bed9

File tree

14 files changed

+248
-49
lines changed

14 files changed

+248
-49
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
import org.mybatis.dynamic.sql.select.aggregate.Min;
4141
import org.mybatis.dynamic.sql.select.aggregate.Sum;
4242
import org.mybatis.dynamic.sql.select.function.Add;
43+
import org.mybatis.dynamic.sql.select.function.Concatenate;
4344
import org.mybatis.dynamic.sql.select.function.Divide;
4445
import org.mybatis.dynamic.sql.select.function.Lower;
4546
import org.mybatis.dynamic.sql.select.function.Multiply;
47+
import org.mybatis.dynamic.sql.select.function.OperatorFunction;
4648
import org.mybatis.dynamic.sql.select.function.Substring;
4749
import org.mybatis.dynamic.sql.select.function.Subtract;
4850
import org.mybatis.dynamic.sql.select.function.Upper;
@@ -285,6 +287,16 @@ static <T extends Number> Subtract<T> subtract(BindableColumn<T> firstColumn, Ba
285287
return Subtract.of(firstColumn, secondColumn, Arrays.asList(subsequentColumns));
286288
}
287289

290+
static <T> Concatenate<T> concatenate(BindableColumn<T> firstColumn, BasicColumn secondColumn,
291+
BasicColumn... subsequentColumns) {
292+
return Concatenate.concatenate(firstColumn, secondColumn, subsequentColumns);
293+
}
294+
295+
static <T> OperatorFunction<T> operatorFunction(String operator, BindableColumn<T> firstColumn, BasicColumn secondColumn,
296+
BasicColumn... subsequentColumns) {
297+
return OperatorFunction.of(operator, firstColumn, secondColumn, subsequentColumns);
298+
}
299+
288300
static Lower lower(BindableColumn<String> column) {
289301
return Lower.of(column);
290302
}

src/main/java/org/mybatis/dynamic/sql/select/function/AbstractFunction.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,38 @@
1616
package org.mybatis.dynamic.sql.select.function;
1717

1818
import java.sql.JDBCType;
19+
import java.util.Objects;
1920
import java.util.Optional;
2021

2122
import org.mybatis.dynamic.sql.BindableColumn;
2223

23-
public abstract class AbstractFunction<T, U extends AbstractFunction<T, U>>
24-
extends AbstractTypeConvertingFunction<T, T, U> {
24+
/**
25+
* @deprecated in favor of {@link AbstractUniTypeFunction}
26+
*
27+
* @author Jeff Butler
28+
*/
29+
@Deprecated
30+
public abstract class AbstractFunction<T, U extends AbstractFunction<T, U>> implements BindableColumn<T> {
31+
32+
protected BindableColumn<T> column;
33+
protected String alias;
2534

2635
protected AbstractFunction(BindableColumn<T> column) {
27-
super(column);
36+
this.column = Objects.requireNonNull(column);
2837
}
2938

39+
@Override
40+
public Optional<String> alias() {
41+
return Optional.ofNullable(alias);
42+
}
43+
44+
@Override
45+
public U as(String alias) {
46+
U newThing = copy();
47+
newThing.alias = alias;
48+
return newThing;
49+
}
50+
3051
@Override
3152
public Optional<JDBCType> jdbcType() {
3253
return column.jdbcType();
@@ -36,4 +57,6 @@ public Optional<JDBCType> jdbcType() {
3657
public Optional<String> typeHandler() {
3758
return column.typeHandler();
3859
}
60+
61+
protected abstract U copy();
3962
}

src/main/java/org/mybatis/dynamic/sql/select/function/AbstractMultipleColumnArithmeticFunction.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -26,13 +26,19 @@
2626
import org.mybatis.dynamic.sql.BindableColumn;
2727
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2828

29+
/**
30+
* @deprecated in favor of {@link OperatorFunction}.
31+
*
32+
* @author Jeff Butler
33+
*/
34+
@Deprecated
2935
public abstract class AbstractMultipleColumnArithmeticFunction<T extends Number,
3036
U extends AbstractMultipleColumnArithmeticFunction<T, U>>
31-
extends AbstractFunction<T, AbstractMultipleColumnArithmeticFunction<T,U>> {
32-
37+
extends AbstractFunction<T, AbstractMultipleColumnArithmeticFunction<T, U>> {
38+
3339
protected BasicColumn secondColumn;
3440
protected List<BasicColumn> subsequentColumns = new ArrayList<>();
35-
41+
3642
protected AbstractMultipleColumnArithmeticFunction(BindableColumn<T> firstColumn, BasicColumn secondColumn,
3743
List<BasicColumn> subsequentColumns) {
3844
super(firstColumn);
@@ -42,13 +48,13 @@ protected AbstractMultipleColumnArithmeticFunction(BindableColumn<T> firstColumn
4248

4349
@Override
4450
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
45-
// note - the cast below is added for a type inference bug in the Java9 compiler.
51+
// note - the cast below is added for a type inference bug in the Java9
52+
// compiler.
4653
return Stream.of(Stream.of((BasicColumn) column), Stream.of(secondColumn), subsequentColumns.stream())
47-
.flatMap(Function.identity())
48-
.map(column -> column.renderWithTableAlias(tableAliasCalculator))
54+
.flatMap(Function.identity()).map(column -> column.renderWithTableAlias(tableAliasCalculator))
4955
.collect(Collectors.joining(padOperator(), "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$
5056
}
51-
57+
5258
private String padOperator() {
5359
return " " + operator() + " "; //$NON-NLS-1$ //$NON-NLS-2$
5460
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2016-2020 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.select.function;
17+
18+
import java.sql.JDBCType;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.BindableColumn;
22+
23+
/**
24+
* Represents a function that does not change the underlying data type.
25+
*
26+
* @author Jeff Butler
27+
*
28+
* @param <T> The type of the underlying column
29+
* @param <U> the specific subtype that implements the function
30+
*/
31+
public abstract class AbstractUniTypeFunction<T, U extends AbstractUniTypeFunction<T, U>>
32+
extends AbstractTypeConvertingFunction<T, T, U> {
33+
34+
protected AbstractUniTypeFunction(BindableColumn<T> column) {
35+
super(column);
36+
}
37+
38+
@Override
39+
public Optional<JDBCType> jdbcType() {
40+
return column.jdbcType();
41+
}
42+
43+
@Override
44+
public Optional<String> typeHandler() {
45+
return column.typeHandler();
46+
}
47+
}

src/main/java/org/mybatis/dynamic/sql/select/function/Add.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 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,23 +20,18 @@
2020
import org.mybatis.dynamic.sql.BasicColumn;
2121
import org.mybatis.dynamic.sql.BindableColumn;
2222

23-
public class Add<T extends Number> extends AbstractMultipleColumnArithmeticFunction<T, Add<T>> {
23+
public class Add<T extends Number> extends OperatorFunction<T> {
2424

2525
private Add(BindableColumn<T> firstColumn, BasicColumn secondColumn,
2626
List<BasicColumn> subsequentColumns) {
27-
super(firstColumn, secondColumn, subsequentColumns);
27+
super("+", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
2828
}
2929

3030
@Override
3131
protected Add<T> copy() {
3232
return new Add<>(column, secondColumn, subsequentColumns);
3333
}
3434

35-
@Override
36-
protected String operator() {
37-
return "+"; //$NON-NLS-1$
38-
}
39-
4035
public static <T extends Number> Add<T> of(BindableColumn<T> firstColumn, BasicColumn secondColumn,
4136
List<BasicColumn> subsequentColumns) {
4237
return new Add<>(firstColumn, secondColumn, subsequentColumns);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2016-2020 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.select.function;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.mybatis.dynamic.sql.BasicColumn;
22+
import org.mybatis.dynamic.sql.BindableColumn;
23+
24+
public class Concatenate<T> extends OperatorFunction<T> {
25+
26+
protected Concatenate(BindableColumn<T> firstColumn, BasicColumn secondColumn,
27+
List<BasicColumn> subsequentColumns) {
28+
super("||", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
29+
}
30+
31+
@Override
32+
protected Concatenate<T> copy() {
33+
return new Concatenate<>(column, secondColumn, subsequentColumns);
34+
}
35+
36+
public static <T> Concatenate<T> concatenate(BindableColumn<T> firstColumn, BasicColumn secondColumn,
37+
BasicColumn... subsequentColumns) {
38+
return new Concatenate<>(firstColumn, secondColumn, Arrays.asList(subsequentColumns));
39+
}
40+
}

src/main/java/org/mybatis/dynamic/sql/select/function/Divide.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 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,23 +20,18 @@
2020
import org.mybatis.dynamic.sql.BasicColumn;
2121
import org.mybatis.dynamic.sql.BindableColumn;
2222

23-
public class Divide<T extends Number> extends AbstractMultipleColumnArithmeticFunction<T, Divide<T>> {
23+
public class Divide<T extends Number> extends OperatorFunction<T> {
2424

2525
private Divide(BindableColumn<T> firstColumn, BasicColumn secondColumn,
2626
List<BasicColumn> subsequentColumns) {
27-
super(firstColumn, secondColumn, subsequentColumns);
27+
super("/", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
2828
}
2929

3030
@Override
3131
protected Divide<T> copy() {
3232
return new Divide<>(column, secondColumn, subsequentColumns);
3333
}
3434

35-
@Override
36-
protected String operator() {
37-
return "/"; //$NON-NLS-1$
38-
}
39-
4035
public static <T extends Number> Divide<T> of(BindableColumn<T> firstColumn, BasicColumn secondColumn,
4136
List<BasicColumn> subsequentColumns) {
4237
return new Divide<>(firstColumn, secondColumn, subsequentColumns);

src/main/java/org/mybatis/dynamic/sql/select/function/Lower.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 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.
@@ -18,7 +18,7 @@
1818
import org.mybatis.dynamic.sql.BindableColumn;
1919
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2020

21-
public class Lower extends AbstractFunction<String, Lower> {
21+
public class Lower extends AbstractUniTypeFunction<String, Lower> {
2222

2323
private Lower(BindableColumn<String> column) {
2424
super(column);

src/main/java/org/mybatis/dynamic/sql/select/function/Multiply.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2018 the original author or authors.
2+
* Copyright 2016-2020 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,23 +20,18 @@
2020
import org.mybatis.dynamic.sql.BasicColumn;
2121
import org.mybatis.dynamic.sql.BindableColumn;
2222

23-
public class Multiply<T extends Number> extends AbstractMultipleColumnArithmeticFunction<T, Multiply<T>> {
23+
public class Multiply<T extends Number> extends OperatorFunction<T> {
2424

2525
private Multiply(BindableColumn<T> firstColumn, BasicColumn secondColumn,
2626
List<BasicColumn> subsequentColumns) {
27-
super(firstColumn, secondColumn, subsequentColumns);
27+
super("*", firstColumn, secondColumn, subsequentColumns); //$NON-NLS-1$
2828
}
2929

3030
@Override
3131
protected Multiply<T> copy() {
3232
return new Multiply<>(column, secondColumn, subsequentColumns);
3333
}
3434

35-
@Override
36-
protected String operator() {
37-
return "*"; //$NON-NLS-1$
38-
}
39-
4035
public static <T extends Number> Multiply<T> of(BindableColumn<T> firstColumn, BasicColumn secondColumn,
4136
List<BasicColumn> subsequentColumns) {
4237
return new Multiply<>(firstColumn, secondColumn, subsequentColumns);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright 2016-2020 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.select.function;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import java.util.Objects;
22+
import java.util.function.Function;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
25+
26+
import org.mybatis.dynamic.sql.BasicColumn;
27+
import org.mybatis.dynamic.sql.BindableColumn;
28+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
29+
30+
public class OperatorFunction<T> extends AbstractUniTypeFunction<T, OperatorFunction<T>> {
31+
32+
protected BasicColumn secondColumn;
33+
protected List<BasicColumn> subsequentColumns = new ArrayList<>();
34+
private String operator;
35+
36+
protected OperatorFunction(String operator, BindableColumn<T> firstColumn, BasicColumn secondColumn,
37+
List<BasicColumn> subsequentColumns) {
38+
super(firstColumn);
39+
this.secondColumn = Objects.requireNonNull(secondColumn);
40+
this.subsequentColumns.addAll(subsequentColumns);
41+
this.operator = Objects.requireNonNull(operator);
42+
}
43+
44+
@Override
45+
protected OperatorFunction<T> copy() {
46+
return new OperatorFunction<>(operator, column, secondColumn, subsequentColumns);
47+
}
48+
49+
@Override
50+
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
51+
String paddedOperator = " " + operator + " "; //$NON-NLS-1$ //$NON-NLS-2$
52+
53+
// note - the cast below is added for a type inference bug in the Java9 compiler.
54+
return Stream.of(Stream.of((BasicColumn) column), Stream.of(secondColumn), subsequentColumns.stream())
55+
.flatMap(Function.identity())
56+
.map(column -> column.renderWithTableAlias(tableAliasCalculator))
57+
.collect(Collectors.joining(paddedOperator, "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$
58+
}
59+
60+
public static <T> OperatorFunction<T> of(String operator, BindableColumn<T> firstColumn, BasicColumn secondColumn,
61+
BasicColumn... subsequentColumns) {
62+
return new OperatorFunction<>(operator, firstColumn, secondColumn, Arrays.asList(subsequentColumns));
63+
}
64+
}

0 commit comments

Comments
 (0)