Skip to content

Commit a258eb8

Browse files
committed
Initial work on per-column formatting
1 parent 01c4e8e commit a258eb8

File tree

14 files changed

+476
-11
lines changed

14 files changed

+476
-11
lines changed

pom.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@
165165
</plugins>
166166
</reporting>
167167

168+
<dependencyManagement>
169+
<dependencies>
170+
<dependency>
171+
<groupId>org.testcontainers</groupId>
172+
<artifactId>testcontainers-bom</artifactId>
173+
<version>1.13.0</version>
174+
<type>pom</type>
175+
<scope>import</scope>
176+
</dependency>
177+
</dependencies>
178+
</dependencyManagement>
179+
168180
<dependencies>
169181
<dependency>
170182
<groupId>org.jetbrains.kotlin</groupId>
@@ -252,6 +264,23 @@
252264
<version>2.2</version>
253265
<scope>test</scope>
254266
</dependency>
267+
268+
<dependency>
269+
<groupId>org.testcontainers</groupId>
270+
<artifactId>postgresql</artifactId>
271+
<scope>test</scope>
272+
</dependency>
273+
<dependency>
274+
<groupId>org.testcontainers</groupId>
275+
<artifactId>junit-jupiter</artifactId>
276+
<scope>test</scope>
277+
</dependency>
278+
<dependency>
279+
<groupId>org.postgresql</groupId>
280+
<artifactId>postgresql</artifactId>
281+
<version>42.2.10</version>
282+
<scope>test</scope>
283+
</dependency>
255284
</dependencies>
256285

257286
<scm>

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

Lines changed: 7 additions & 1 deletion
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,6 +18,8 @@
1818
import java.sql.JDBCType;
1919
import java.util.Optional;
2020

21+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
22+
2123
/**
2224
* Describes additional attributes of columns that are necessary for binding the column as a JDBC parameter.
2325
* Columns in where clauses are typically bound.
@@ -39,4 +41,8 @@ public interface BindableColumn<T> extends BasicColumn {
3941
Optional<JDBCType> jdbcType();
4042

4143
Optional<String> typeHandler();
44+
45+
default Optional<RenderingStrategy> renderingStrategy() {
46+
return Optional.empty();
47+
}
4248
}

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 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.
@@ -19,6 +19,7 @@
1919
import java.util.Objects;
2020
import java.util.Optional;
2121

22+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2223
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2324

2425
public class SqlColumn<T> implements BindableColumn<T>, SortSpecification {
@@ -29,6 +30,7 @@ public class SqlColumn<T> implements BindableColumn<T>, SortSpecification {
2930
protected boolean isDescending = false;
3031
protected String alias;
3132
protected String typeHandler;
33+
protected RenderingStrategy renderingStrategy;
3234

3335
private SqlColumn(Builder builder) {
3436
name = Objects.requireNonNull(builder.name);
@@ -44,12 +46,17 @@ protected SqlColumn(SqlColumn<?> sqlColumn) {
4446
isDescending = sqlColumn.isDescending;
4547
alias = sqlColumn.alias;
4648
typeHandler = sqlColumn.typeHandler;
49+
renderingStrategy = sqlColumn.renderingStrategy;
4750
}
4851

4952
public String name() {
5053
return name;
5154
}
5255

56+
public SqlTable table() {
57+
return table;
58+
}
59+
5360
@Override
5461
public Optional<JDBCType> jdbcType() {
5562
return Optional.ofNullable(jdbcType);
@@ -96,12 +103,23 @@ public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
96103
.orElseGet(this::name);
97104
}
98105

106+
@Override
107+
public Optional<RenderingStrategy> renderingStrategy() {
108+
return Optional.ofNullable(renderingStrategy);
109+
}
110+
99111
public SqlColumn<T> withTypeHandler(String typeHandler) {
100112
SqlColumn<T> column = new SqlColumn<>(this);
101113
column.typeHandler = typeHandler;
102114
return column;
103115
}
104116

117+
public <S> SqlColumn<S> withRenderingStrategy(RenderingStrategy renderingStrategy) {
118+
SqlColumn<S> column = new SqlColumn<>(this);
119+
column.renderingStrategy = renderingStrategy;
120+
return column;
121+
}
122+
105123
private String applyTableAlias(String tableAlias) {
106124
return tableAlias + "." + name(); //$NON-NLS-1$
107125
}

src/main/java/org/mybatis/dynamic/sql/insert/render/MultiRowValuePhraseVisitor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 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.
@@ -35,7 +35,8 @@ public FieldAndValue visit(PropertyMapping mapping) {
3535
}
3636

3737
private Function<SqlColumn<?>, String> toMultiRowJdbcPlaceholder(String parameterName) {
38-
return column -> renderingStrategy.getFormattedJdbcPlaceholder(column, "records[%s]", //$NON-NLS-1$
39-
parameterName);
38+
return column -> column.renderingStrategy().orElse(renderingStrategy)
39+
.getFormattedJdbcPlaceholder(column, "records[%s]", //$NON-NLS-1$
40+
parameterName);
4041
}
4142
}

src/main/java/org/mybatis/dynamic/sql/insert/render/ValuePhraseVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 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.
@@ -62,6 +62,7 @@ public FieldAndValue visit(PropertyMapping mapping) {
6262
}
6363

6464
private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
65-
return column -> renderingStrategy.getFormattedJdbcPlaceholder(column, "record", parameterName); //$NON-NLS-1$
65+
return column -> column.renderingStrategy().orElse(renderingStrategy)
66+
.getFormattedJdbcPlaceholder(column, "record", parameterName); //$NON-NLS-1$
6667
}
6768
}

src/main/java/org/mybatis/dynamic/sql/update/render/SetPhraseVisitor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 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.
@@ -110,7 +110,7 @@ public FragmentAndParameters visit(ColumnMapping mapping) {
110110
}
111111

112112
private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
113-
return column -> renderingStrategy
113+
return column -> column.renderingStrategy().orElse(renderingStrategy)
114114
.getFormattedJdbcPlaceholder(column, RenderingStrategy.DEFAULT_PARAMETER_PREFIX, parameterName);
115115
}
116116
}

src/main/java/org/mybatis/dynamic/sql/where/render/WhereConditionVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2016-2019 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.
@@ -126,7 +126,8 @@ private FragmentAndParameters toFragmentAndParameters(T value) {
126126
}
127127

128128
private String getFormattedJdbcPlaceholder(String mapKey) {
129-
return renderingStrategy.getFormattedJdbcPlaceholder(column, parameterPrefix, mapKey);
129+
return column.renderingStrategy().orElse(renderingStrategy)
130+
.getFormattedJdbcPlaceholder(column, parameterPrefix, mapKey);
130131
}
131132

132133
private String columnName() {

0 commit comments

Comments
 (0)