Skip to content

Commit 8ad566f

Browse files
authored
Merge pull request #219 from jeffgbutler/gh_197
Add support for functions that change column data types
2 parents ba7dc5d + 4e25f14 commit 8ad566f

File tree

8 files changed

+368
-20
lines changed

8 files changed

+368
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
1010

1111
- Added a general insert statement that does not require a separate record class to hold values for the insert. ([#201](https://github.com/mybatis/mybatis-dynamic-sql/issues/201))
1212
- Added the capability to specify a rendering strategy on a column to override the defaut rendering strategy for a statement. This will allow certain edge cases where a parameter marker needs to be formatted in a unique way (for example, "::jsonb" needs to be added to parameter markers for JSON fields in PostgreSQL) ([#200](https://github.com/mybatis/mybatis-dynamic-sql/issues/200))
13+
- Added the ability to write a function that will change the column data type ([#197](https://github.com/mybatis/mybatis-dynamic-sql/issues/197))
1314

1415
## Release 1.1.4 - November 23, 2019
1516

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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 examples.type_conversion;
17+
18+
import java.sql.JDBCType;
19+
20+
import org.mybatis.dynamic.sql.SqlColumn;
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
23+
public final class MyFilesDynamicSqlSupport {
24+
public static final MyFiles myfiles = new MyFiles();
25+
public static final SqlColumn<Integer> fileId = myfiles.fileId;
26+
public static final SqlColumn<byte[]> fileContents = myfiles.fileContents;
27+
28+
public static final class MyFiles extends SqlTable {
29+
public final SqlColumn<Integer> fileId = column("file_id", JDBCType.INTEGER);
30+
public final SqlColumn<byte[]> fileContents = column("file_contents", JDBCType.LONGVARBINARY);
31+
32+
public MyFiles() {
33+
super("MyFiles");
34+
}
35+
}
36+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 examples.type_conversion;
17+
18+
import java.util.Map;
19+
20+
import org.apache.ibatis.annotations.InsertProvider;
21+
import org.apache.ibatis.annotations.SelectProvider;
22+
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
23+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
24+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
25+
26+
public interface MyFilesMapper {
27+
28+
@InsertProvider(type=SqlProviderAdapter.class, method="generalInsert")
29+
int insert(GeneralInsertStatementProvider insertStatement);
30+
31+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
32+
Map<String, Object> generalSelect(SelectStatementProvider selectStatement);
33+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 examples.type_conversion;
17+
18+
import java.sql.JDBCType;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.BindableColumn;
22+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
23+
import org.mybatis.dynamic.sql.select.function.AbstractTypeConvertingFunction;
24+
25+
public class ToBase64 extends AbstractTypeConvertingFunction<byte[], String, ToBase64> {
26+
27+
protected ToBase64(BindableColumn<byte[]> column) {
28+
super(column);
29+
}
30+
31+
@Override
32+
public Optional<JDBCType> jdbcType() {
33+
return Optional.of(JDBCType.VARCHAR);
34+
}
35+
36+
@Override
37+
public Optional<String> typeHandler() {
38+
return Optional.empty();
39+
}
40+
41+
@Override
42+
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
43+
return "TO_BASE64(" //$NON-NLS-1$
44+
+ column.renderWithTableAlias(tableAliasCalculator)
45+
+ ")"; //$NON-NLS-1$
46+
}
47+
48+
@Override
49+
protected ToBase64 copy() {
50+
return new ToBase64(column);
51+
}
52+
53+
public static ToBase64 toBase64(BindableColumn<byte[]> column) {
54+
return new ToBase64(column);
55+
}
56+
}

0 commit comments

Comments
 (0)