Skip to content

Commit 549c458

Browse files
authored
Merge pull request #24 from jeffgbutler/master
Column JDBC Type Should be Optional
2 parents 5f2cc70 + 8e23600 commit 549c458

File tree

8 files changed

+47
-31
lines changed

8 files changed

+47
-31
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
<dependency>
143143
<groupId>org.springframework</groupId>
144144
<artifactId>spring-jdbc</artifactId>
145-
<version>5.0.2.RELEASE</version>
145+
<version>5.0.3.RELEASE</version>
146146
<scope>test</scope>
147147
</dependency>
148148
<dependency>

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

Lines changed: 2 additions & 2 deletions
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.
@@ -36,7 +36,7 @@ public interface BindableColumn<T> extends BasicColumn {
3636
@Override
3737
BindableColumn<T> as(String alias);
3838

39-
JDBCType jdbcType();
39+
Optional<JDBCType> jdbcType();
4040

4141
Optional<String> typeHandler();
4242
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public class SqlColumn<T> implements BindableColumn<T>, SortSpecification {
2727
protected SqlTable table;
2828
protected JDBCType jdbcType;
2929
protected boolean isDescending = false;
30-
protected Optional<String> alias = Optional.empty();
31-
protected Optional<String> typeHandler;
30+
protected String alias;
31+
protected String typeHandler;
3232

3333
private SqlColumn(Builder builder) {
3434
name = Objects.requireNonNull(builder.name);
35-
jdbcType = Objects.requireNonNull(builder.jdbcType);
35+
jdbcType = builder.jdbcType;
3636
table = Objects.requireNonNull(builder.table);
37-
typeHandler = Optional.ofNullable(builder.typeHandler);
37+
typeHandler = builder.typeHandler;
3838
}
3939

4040
protected SqlColumn(SqlColumn<?> sqlColumn) {
@@ -51,18 +51,18 @@ public String name() {
5151
}
5252

5353
@Override
54-
public JDBCType jdbcType() {
55-
return jdbcType;
54+
public Optional<JDBCType> jdbcType() {
55+
return Optional.ofNullable(jdbcType);
5656
}
5757

5858
@Override
5959
public Optional<String> alias() {
60-
return alias;
60+
return Optional.ofNullable(alias);
6161
}
6262

6363
@Override
6464
public Optional<String> typeHandler() {
65-
return typeHandler;
65+
return Optional.ofNullable(typeHandler);
6666
}
6767

6868
@Override
@@ -75,7 +75,7 @@ public SortSpecification descending() {
7575
@Override
7676
public SqlColumn<T> as(String alias) {
7777
SqlColumn<T> column = new SqlColumn<>(this);
78-
column.alias = Optional.of(alias);
78+
column.alias = alias;
7979
return column;
8080
}
8181

@@ -86,7 +86,7 @@ public boolean isDescending() {
8686

8787
@Override
8888
public String aliasOrName() {
89-
return alias.orElse(name);
89+
return alias().orElse(name);
9090
}
9191

9292
@Override
@@ -98,14 +98,20 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
9898

9999
public <S> SqlColumn<S> withTypeHandler(String typeHandler) {
100100
SqlColumn<S> column = new SqlColumn<>(this);
101-
column.typeHandler = Optional.of(typeHandler);
101+
column.typeHandler = typeHandler;
102102
return column;
103103
}
104104

105105
private String applyTableAlias(String tableAlias) {
106106
return tableAlias + "." + name(); //$NON-NLS-1$
107107
}
108108

109+
public static <T> SqlColumn<T> of(String name, SqlTable table) {
110+
return SqlColumn.withName(name)
111+
.withTable(table)
112+
.build();
113+
}
114+
109115
public static <T> SqlColumn<T> of(String name, SqlTable table, JDBCType jdbcType) {
110116
return SqlColumn.withName(name)
111117
.withTable(table)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public String name() {
3030
return name;
3131
}
3232

33+
public <T> SqlColumn<T> column(String name) {
34+
return SqlColumn.of(name, this);
35+
}
36+
3337
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
3438
return SqlColumn.of(name, this, jdbcType);
3539
}

src/main/java/org/mybatis/dynamic/sql/render/MyBatis3RenderingStrategy.java

Lines changed: 13 additions & 5 deletions
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.
@@ -24,10 +24,18 @@ public String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefi
2424
+ prefix
2525
+ "." //$NON-NLS-1$
2626
+ parameterName
27-
+ ",jdbcType=" //$NON-NLS-1$
28-
+ column.jdbcType().getName()
29-
+ column.typeHandler().map(th -> ",typeHandler=" + th) //$NON-NLS-1$
30-
.orElse("") //$NON-NLS-1$
27+
+ renderJdbcType(column)
28+
+ renderTypeHandler(column)
3129
+ "}"; //$NON-NLS-1$
3230
}
31+
32+
private String renderTypeHandler(BindableColumn<?> column) {
33+
return column.typeHandler().map(th -> ",typeHandler=" + th) //$NON-NLS-1$
34+
.orElse(""); //$NON-NLS-1$
35+
}
36+
37+
private String renderJdbcType(BindableColumn<?> column) {
38+
return column.jdbcType().map(jt -> ",jdbcType=" + jt.getName()) //$NON-NLS-1$
39+
.orElse(""); //$NON-NLS-1$
40+
}
3341
}

src/main/java/org/mybatis/dynamic/sql/select/aggregate/AbstractAggregate.java

Lines changed: 4 additions & 4 deletions
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.
@@ -30,15 +30,15 @@
3030
*/
3131
public abstract class AbstractAggregate<T extends AbstractAggregate<T>> implements BasicColumn {
3232
protected BasicColumn column;
33-
protected Optional<String> alias = Optional.empty();
33+
protected String alias;
3434

3535
protected AbstractAggregate(BasicColumn column) {
3636
this.column = Objects.requireNonNull(column);
3737
}
3838

3939
@Override
4040
public Optional<String> alias() {
41-
return alias;
41+
return Optional.ofNullable(alias);
4242
}
4343

4444
@Override
@@ -49,7 +49,7 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
4949
@Override
5050
public T as(String alias) {
5151
T copy = copy();
52-
copy.alias = Optional.of(alias);
52+
copy.alias = alias;
5353
return copy;
5454
}
5555

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public U as(String alias) {
4242
}
4343

4444
@Override
45-
public JDBCType jdbcType() {
45+
public Optional<JDBCType> jdbcType() {
4646
return column.jdbcType();
4747
}
4848

src/test/java/examples/generated/always/spring/GeneratedAlwaysDynamicSqlSupport.java

Lines changed: 5 additions & 7 deletions
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.
@@ -15,8 +15,6 @@
1515
*/
1616
package examples.generated.always.spring;
1717

18-
import java.sql.JDBCType;
19-
2018
import org.mybatis.dynamic.sql.SqlColumn;
2119
import org.mybatis.dynamic.sql.SqlTable;
2220

@@ -28,10 +26,10 @@ public final class GeneratedAlwaysDynamicSqlSupport {
2826
public static final SqlColumn<String> fullName = generatedAlways.fullName;
2927

3028
public static final class GeneratedAlways extends SqlTable {
31-
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
32-
public final SqlColumn<String> firstName = column("first_name", JDBCType.VARCHAR);
33-
public final SqlColumn<String> lastName = column("last_name", JDBCType.VARCHAR);
34-
public final SqlColumn<String> fullName = column("full_name", JDBCType.VARCHAR);
29+
public final SqlColumn<Integer> id = column("id");
30+
public final SqlColumn<String> firstName = column("first_name");
31+
public final SqlColumn<String> lastName = column("last_name");
32+
public final SqlColumn<String> fullName = column("full_name");
3533

3634
public GeneratedAlways() {
3735
super("GeneratedAlways");

0 commit comments

Comments
 (0)