Skip to content

Commit 8c72895

Browse files
committed
Add an abstract function that can change they column data type
Implementation of #197 Thanks @Endinc!
1 parent ba7dc5d commit 8c72895

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed
Lines changed: 4 additions & 20 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.
@@ -16,31 +16,17 @@
1616
package org.mybatis.dynamic.sql.select.function;
1717

1818
import java.sql.JDBCType;
19-
import java.util.Objects;
2019
import java.util.Optional;
2120

2221
import org.mybatis.dynamic.sql.BindableColumn;
2322

24-
public abstract class AbstractFunction<T, U extends AbstractFunction<T, U>> implements BindableColumn<T> {
25-
protected BindableColumn<T> column;
26-
protected String alias;
23+
public abstract class AbstractFunction<T, U extends AbstractFunction<T, U>>
24+
extends AbstractTypeConvertingFunction<T, T, U> {
2725

2826
protected AbstractFunction(BindableColumn<T> column) {
29-
this.column = Objects.requireNonNull(column);
27+
super(column);
3028
}
3129

32-
@Override
33-
public Optional<String> alias() {
34-
return Optional.ofNullable(alias);
35-
}
36-
37-
@Override
38-
public U as(String alias) {
39-
U newThing = copy();
40-
newThing.alias = alias;
41-
return newThing;
42-
}
43-
4430
@Override
4531
public Optional<JDBCType> jdbcType() {
4632
return column.jdbcType();
@@ -50,6 +36,4 @@ public Optional<JDBCType> jdbcType() {
5036
public Optional<String> typeHandler() {
5137
return column.typeHandler();
5238
}
53-
54-
protected abstract U copy();
5539
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.Objects;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.BindableColumn;
22+
23+
/**
24+
* Represents a function that can change the underlying type. For example, converting a binary field for a base64
25+
* string, or an integer to a string, etc.
26+
*
27+
* <p>Thanks to @endink for the idea.
28+
*
29+
* @author Jeff Butler
30+
*
31+
* @param <T> The type of the underlying column. For example, if a function converts a VARCHR to an INT, then the
32+
* underlying type will be a String
33+
* @param <R> The type of the column after the conversion. For example, if a function converts a VARCHR to an INT, then
34+
* the converted type will be Integer
35+
* @param <U> the specific subtype that implements the function
36+
*/
37+
public abstract class AbstractTypeConvertingFunction<T, R, U extends AbstractTypeConvertingFunction<T, R, U>>
38+
implements BindableColumn<R> {
39+
protected BindableColumn<T> column;
40+
protected String alias;
41+
42+
protected AbstractTypeConvertingFunction(BindableColumn<T> column) {
43+
this.column = Objects.requireNonNull(column);
44+
}
45+
46+
@Override
47+
public Optional<String> alias() {
48+
return Optional.ofNullable(alias);
49+
}
50+
51+
@Override
52+
public U as(String alias) {
53+
U newThing = copy();
54+
newThing.alias = alias;
55+
return newThing;
56+
}
57+
58+
protected abstract U copy();
59+
}

0 commit comments

Comments
 (0)