Skip to content

Commit 6c60c84

Browse files
committed
Add a static method to more easily construct an SqlColumn
- Fixes #2
1 parent 380a299 commit 6c60c84

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

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

Lines changed: 14 additions & 1 deletion
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.
@@ -96,10 +96,23 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
9696
.orElseGet(this::name);
9797
}
9898

99+
public <S> SqlColumn<S> withTypeHandler(String typeHandler) {
100+
SqlColumn<S> column = new SqlColumn<>(this);
101+
column.typeHandler = Optional.of(typeHandler);
102+
return column;
103+
}
104+
99105
private String applyTableAlias(String tableAlias) {
100106
return tableAlias + "." + name(); //$NON-NLS-1$
101107
}
102108

109+
public static <T> SqlColumn<T> of(String name, SqlTable table, JDBCType jdbcType) {
110+
return SqlColumn.withName(name)
111+
.withTable(table)
112+
.withJdbcType(jdbcType)
113+
.build();
114+
}
115+
103116
public static Builder withName(String name) {
104117
return new Builder().withName(name);
105118
}

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

Lines changed: 3 additions & 10 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.
@@ -31,18 +31,11 @@ public String name() {
3131
}
3232

3333
public <T> SqlColumn<T> column(String name, JDBCType jdbcType) {
34-
return SqlColumn.withName(name)
35-
.withJdbcType(jdbcType)
36-
.withTable(this)
37-
.build();
34+
return SqlColumn.of(name, this, jdbcType);
3835
}
3936

4037
public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandler) {
41-
return SqlColumn.withName(name)
42-
.withJdbcType(jdbcType)
43-
.withTypeHandler(typeHandler)
44-
.withTable(this)
45-
.build();
38+
return SqlColumn.of(name, this, jdbcType).withTypeHandler(typeHandler);
4639
}
4740

4841
public static SqlTable of(String name) {

src/test/java/org/mybatis/dynamic/sql/mybatis3/CriterionRendererTest.java

Lines changed: 6 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.
@@ -130,7 +130,11 @@ public void testNoAliasWithoutIgnore() {
130130
@Test
131131
public void testTypeHandler() {
132132
SqlTable table = SqlTable.of("foo");
133-
SqlColumn<Date> column = table.column("id", JDBCType.DATE, "foo.Bar");
133+
SqlColumn<Date> column = SqlColumn.withName("id")
134+
.withTable(table)
135+
.withJdbcType(JDBCType.DATE)
136+
.withTypeHandler("foo.Bar")
137+
.build();
134138
IsEqualTo<Date> condition = IsEqualTo.of(() -> new Date());
135139
SqlCriterion<Date> criterion = SqlCriterion.withColumn(column)
136140
.withCondition(condition)

0 commit comments

Comments
 (0)