Skip to content

Commit f209a63

Browse files
authored
Merge pull request #216 from jeffgbutler/per-column-formatting
Add support for overriding rendering strategy per column
2 parents 37aaa0d + 893443f commit f209a63

19 files changed

+674
-13
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ jdk:
44
- openjdk11
55
- openjdk8
66

7+
services:
8+
- docker
9+
710
after_success:
811
- chmod -R 777 ./travis/after_success.sh
912
- ./travis/after_success.sh

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
99
### Added
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))
12+
- 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))
1213

1314
## Release 1.1.4 - November 23, 2019
1415

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<sonar.tests>src/test/java,src/test/kotlin</sonar.tests>
4545
<jacoco.version>0.8.5</jacoco.version>
4646
<kotlin.code.style>official</kotlin.code.style>
47+
<test.containers.version>1.14.3</test.containers.version>
4748
</properties>
4849

4950
<build>
@@ -252,6 +253,25 @@
252253
<version>2.2</version>
253254
<scope>test</scope>
254255
</dependency>
256+
257+
<dependency>
258+
<groupId>org.testcontainers</groupId>
259+
<artifactId>postgresql</artifactId>
260+
<version>${test.containers.version}</version>
261+
<scope>test</scope>
262+
</dependency>
263+
<dependency>
264+
<groupId>org.testcontainers</groupId>
265+
<artifactId>junit-jupiter</artifactId>
266+
<version>${test.containers.version}</version>
267+
<scope>test</scope>
268+
</dependency>
269+
<dependency>
270+
<groupId>org.postgresql</groupId>
271+
<artifactId>postgresql</artifactId>
272+
<version>42.2.14</version>
273+
<scope>test</scope>
274+
</dependency>
255275
</dependencies>
256276

257277
<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/GeneralInsertValuePhraseVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public <R> FieldAndValueAndParameters visit(ValueMapping<R> mapping) {
6969
}
7070

7171
private Function<SqlColumn<?>, String> toJdbcPlaceholder(String parameterName) {
72-
return column -> renderingStrategy
72+
return column -> column.renderingStrategy().orElse(renderingStrategy)
7373
.getFormattedJdbcPlaceholder(column, RenderingStrategy.DEFAULT_PARAMETER_PREFIX, parameterName);
7474
}
7575
}

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public FragmentAndParameters visit(ColumnToColumnMapping 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/util/AbstractColumnMapping.java

Lines changed: 1 addition & 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.

0 commit comments

Comments
 (0)