Skip to content

Commit ba57625

Browse files
authored
Merge pull request #609 from jeffgbutler/misc-updates
Misc Small Updates
2 parents 9d7bc0b + d2d8651 commit ba57625

15 files changed

+146
-32
lines changed

src/main/java/org/mybatis/dynamic/sql/delete/render/DeleteRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private Optional<FragmentAndParameters> calculateLimitClause() {
9292
}
9393

9494
private FragmentAndParameters renderLimitClause(Long limit) {
95-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
95+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
9696
String jdbcPlaceholder =
9797
renderingStrategy.getFormattedJdbcPlaceholder(RenderingStrategy.DEFAULT_PARAMETER_PREFIX, mapKey);
9898

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
@@ -85,7 +85,7 @@ private Optional<FieldAndValueAndParameters> buildNullFragment(AbstractColumnMap
8585
}
8686

8787
private Optional<FieldAndValueAndParameters> buildFragment(AbstractColumnMapping mapping, Object value) {
88-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
88+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
8989

9090
String jdbcPlaceholder = mapping.mapColumn(c -> calculateJdbcPlaceholder(c, mapKey));
9191

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,83 @@
1919

2020
import org.mybatis.dynamic.sql.BindableColumn;
2121

22+
/**
23+
* A rendering strategy is used to generate a platform specific binding.
24+
* <p>
25+
* Rendering strategies are used during the rendering phase of statement generation.
26+
* All generated SQL statements include the generated statement itself, and a map of parameters that
27+
* should be bound to the statement at execution time. For example, a generated select statement may
28+
* look like this when rendered for MyBatis:
29+
* <p>
30+
* <code>select foo from bar where id = #{parameters.p1,jdbcType=INTEGER}</code>
31+
* <p>
32+
* In this case, the binding is <code>#{parameters.p1,jdbcType=INTEGER}</code>. MyBatis knows how to interpret this
33+
* binding - it will look for a value in the <code>parameters.p1</code> property of the parameter object
34+
* passed to the statement and bind it as a prepared statement parameter when executing the statement.
35+
*/
2236
public abstract class RenderingStrategy {
2337
public static final String DEFAULT_PARAMETER_PREFIX = "parameters"; //$NON-NLS-1$
2438

25-
public static String formatParameterMapKey(AtomicInteger sequence) {
39+
public String formatParameterMapKey(AtomicInteger sequence) {
2640
return "p" + sequence.getAndIncrement(); //$NON-NLS-1$
2741
}
2842

43+
/**
44+
* This method generates a binding for a parameter to a placeholder in a generated SQL statement.
45+
* <p>
46+
* This binding is appropriate when there can be a mapping between a parameter and a known target column,
47+
* In MyBatis, the binding can specify type information based on the column. The bindings are specific
48+
* to the target framework.
49+
* <p>
50+
* For MyBatis, a binding looks like this: "#{prefix.parameterName,jdbcType=xxx,typeHandler=xxx,javaType=xxx}"
51+
* <p>
52+
* For Spring, a binding looks like this: ":parameterName"
53+
*
54+
* @param column column definition used for generating type details in a MyBatis binding. Ignored for Spring.
55+
* @param prefix parameter prefix used for locating the parameters in a SQL provider object. Typically, will be
56+
* {@link RenderingStrategy#DEFAULT_PARAMETER_PREFIX}. This is ignored for Spring.
57+
* @param parameterName name of the parameter. Typically generated by calling
58+
* {@link RenderingStrategy#formatParameterMapKey(AtomicInteger)}
59+
* @return the generated binding
60+
*/
2961
public abstract String getFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName);
3062

63+
/**
64+
* This method generates a binding for a parameter to a placeholder in a generated SQL statement.
65+
* <p>
66+
* This binding is appropriate when the parameter is bound to placeholder that is not a known column (such as
67+
* a limit or offset parameter). The bindings are specific to the target framework.
68+
* <p>
69+
* For MyBatis, a binding looks like this: "#{prefix.parameterName}"
70+
* <p>
71+
* For Spring, a binding looks like this: ":parameterName"
72+
*
73+
* @param prefix parameter prefix used for locating the parameters in a SQL provider object. Typically, will be
74+
* {@link RenderingStrategy#DEFAULT_PARAMETER_PREFIX}. This is ignored for Spring.
75+
* @param parameterName name of the parameter. Typically generated by calling
76+
* {@link RenderingStrategy#formatParameterMapKey(AtomicInteger)}
77+
* @return the generated binding
78+
*/
3179
public abstract String getFormattedJdbcPlaceholder(String prefix, String parameterName);
3280

81+
/**
82+
* This method generates a binding for a parameter to a placeholder in a generated multirow insert statement.
83+
* <p>
84+
* This binding is specifically for use with a multirow insert. The Spring implementation changes the binding
85+
* to match values expected for a multirow insert statement. For MyBatis, the binding is the same
86+
* as {@link RenderingStrategy#getFormattedJdbcPlaceholder(BindableColumn, String, String)}.
87+
* <p>
88+
* For MyBatis, a binding looks like this: "#{prefix.parameterName,jdbcType=xxx,typeHandler=xxx,javaType=xxx}"
89+
* <p>
90+
* For Spring, a binding looks like this: ":prefix.parameterName"
91+
*
92+
* @param column column definition used for generating type details in a MyBatis binding. Ignored for Spring.
93+
* @param prefix parameter prefix used for locating the parameters in a SQL provider object. Typically, will be
94+
* {@link RenderingStrategy#DEFAULT_PARAMETER_PREFIX}. This is ignored for Spring.
95+
* @param parameterName name of the parameter. Typically generated by calling
96+
* {@link RenderingStrategy#formatParameterMapKey(AtomicInteger)}
97+
* @return the generated binding
98+
*/
3399
public String getMultiRowFormattedJdbcPlaceholder(BindableColumn<?> column, String prefix, String parameterName) {
34100
return getFormattedJdbcPlaceholder(column, prefix, parameterName);
35101
}

src/main/java/org/mybatis/dynamic/sql/select/render/FetchFirstPagingModelRenderer.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2222
import org.mybatis.dynamic.sql.select.PagingModel;
2323
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
24+
import org.mybatis.dynamic.sql.util.InternalError;
2425
import org.mybatis.dynamic.sql.util.Messages;
2526

2627
public class FetchFirstPagingModelRenderer {
@@ -49,11 +50,12 @@ private FragmentAndParameters renderWithOffset(Long offset) {
4950

5051
private FragmentAndParameters renderFetchFirstRowsOnly() {
5152
return pagingModel.fetchFirstRows().map(this::renderFetchFirstRowsOnly)
52-
.orElseThrow(() -> new InvalidSqlException(Messages.getInternalErrorString(13)));
53+
.orElseThrow(() ->
54+
new InvalidSqlException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_13)));
5355
}
5456

5557
private FragmentAndParameters renderFetchFirstRowsOnly(Long fetchFirstRows) {
56-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
58+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
5759
return FragmentAndParameters
5860
.withFragment("fetch first " + renderPlaceholder(mapKey) //$NON-NLS-1$
5961
+ " rows only") //$NON-NLS-1$
@@ -62,16 +64,16 @@ private FragmentAndParameters renderFetchFirstRowsOnly(Long fetchFirstRows) {
6264
}
6365

6466
private FragmentAndParameters renderOffsetOnly(Long offset) {
65-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
67+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
6668
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(mapKey) //$NON-NLS-1$
6769
+ " rows") //$NON-NLS-1$
6870
.withParameter(mapKey, offset)
6971
.build();
7072
}
7173

7274
private FragmentAndParameters renderOffsetAndFetchFirstRows(Long offset, Long fetchFirstRows) {
73-
String mapKey1 = RenderingStrategy.formatParameterMapKey(sequence);
74-
String mapKey2 = RenderingStrategy.formatParameterMapKey(sequence);
75+
String mapKey1 = renderingStrategy.formatParameterMapKey(sequence);
76+
String mapKey2 = renderingStrategy.formatParameterMapKey(sequence);
7577
return FragmentAndParameters.withFragment("offset " + renderPlaceholder(mapKey1) //$NON-NLS-1$
7678
+ " rows fetch first " + renderPlaceholder(mapKey2) //$NON-NLS-1$
7779
+ " rows only") //$NON-NLS-1$

src/main/java/org/mybatis/dynamic/sql/select/render/LimitAndOffsetPagingModelRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public FragmentAndParameters render() {
4242
}
4343

4444
private FragmentAndParameters renderLimitOnly() {
45-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
45+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
4646
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(mapKey)) //$NON-NLS-1$
4747
.withParameter(mapKey, limit)
4848
.build();
4949
}
5050

5151
private FragmentAndParameters renderLimitAndOffset(Long offset) {
52-
String mapKey1 = RenderingStrategy.formatParameterMapKey(sequence);
53-
String mapKey2 = RenderingStrategy.formatParameterMapKey(sequence);
52+
String mapKey1 = renderingStrategy.formatParameterMapKey(sequence);
53+
String mapKey2 = renderingStrategy.formatParameterMapKey(sequence);
5454
return FragmentAndParameters.withFragment("limit " + renderPlaceholder(mapKey1) //$NON-NLS-1$
5555
+ " offset " + renderPlaceholder(mapKey2)) //$NON-NLS-1$
5656
.withParameter(mapKey1, limit)

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
@@ -127,7 +127,7 @@ public Optional<FragmentAndParameters> visit(ColumnToColumnMapping mapping) {
127127
}
128128

129129
private <T> Optional<FragmentAndParameters> buildFragment(AbstractColumnMapping mapping, T value) {
130-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
130+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
131131

132132
String jdbcPlaceholder = mapping.mapColumn(c -> calculateJdbcPlaceholder(c, mapKey));
133133
String setPhrase = mapping.mapColumn(aliasedColumnNameFunction)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private Optional<FragmentAndParameters> calculateLimitClause() {
122122
}
123123

124124
private FragmentAndParameters renderLimitClause(Long limit) {
125-
String mapKey = RenderingStrategy.formatParameterMapKey(sequence);
125+
String mapKey = renderingStrategy.formatParameterMapKey(sequence);
126126
String jdbcPlaceholder =
127127
renderingStrategy.getFormattedJdbcPlaceholder(RenderingStrategy.DEFAULT_PARAMETER_PREFIX, mapKey);
128128

src/main/java/org/mybatis/dynamic/sql/util/GeneralInsertMappingVisitor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@
1818
public abstract class GeneralInsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
1919
@Override
2020
public final R visit(SelectMapping mapping) {
21-
throw new UnsupportedOperationException(Messages.getInternalErrorString(1));
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_1));
2222
}
2323

2424
@Override
2525
public final R visit(PropertyMapping mapping) {
26-
throw new UnsupportedOperationException(Messages.getInternalErrorString(2));
26+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_2));
2727
}
2828

2929
@Override
3030
public final R visit(PropertyWhenPresentMapping mapping) {
31-
throw new UnsupportedOperationException(Messages.getInternalErrorString(3));
31+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_3));
3232
}
3333

3434
@Override
3535
public final R visit(ColumnToColumnMapping columnMapping) {
36-
throw new UnsupportedOperationException(Messages.getInternalErrorString(4));
36+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_4));
3737
}
3838
}

src/main/java/org/mybatis/dynamic/sql/util/InsertMappingVisitor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@
1818
public abstract class InsertMappingVisitor<R> implements ColumnMappingVisitor<R> {
1919
@Override
2020
public final <T> R visit(ValueMapping<T> mapping) {
21-
throw new UnsupportedOperationException(Messages.getInternalErrorString(5));
21+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_5));
2222
}
2323

2424
@Override
2525
public final <T> R visit(ValueOrNullMapping<T> mapping) {
26-
throw new UnsupportedOperationException(Messages.getInternalErrorString(6));
26+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_6));
2727
}
2828

2929
@Override
3030
public final <T> R visit(ValueWhenPresentMapping<T> mapping) {
31-
throw new UnsupportedOperationException(Messages.getInternalErrorString(7));
31+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_7));
3232
}
3333

3434
@Override
3535
public final R visit(SelectMapping mapping) {
36-
throw new UnsupportedOperationException(Messages.getInternalErrorString(8));
36+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_8));
3737
}
3838

3939
@Override
4040
public final R visit(ColumnToColumnMapping columnMapping) {
41-
throw new UnsupportedOperationException(Messages.getInternalErrorString(9));
41+
throw new UnsupportedOperationException(Messages.getInternalErrorString(InternalError.INTERNAL_ERROR_9));
4242
}
4343
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016-2023 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+
* https://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.util;
17+
18+
/**
19+
* Enum for managing internal error numbers.
20+
*/
21+
public enum InternalError {
22+
INTERNAL_ERROR_1(1),
23+
INTERNAL_ERROR_2(2),
24+
INTERNAL_ERROR_3(3),
25+
INTERNAL_ERROR_4(4),
26+
INTERNAL_ERROR_5(5),
27+
INTERNAL_ERROR_6(6),
28+
INTERNAL_ERROR_7(7),
29+
INTERNAL_ERROR_8(8),
30+
INTERNAL_ERROR_9(9),
31+
INTERNAL_ERROR_10(10),
32+
INTERNAL_ERROR_11(11),
33+
INTERNAL_ERROR_12(12),
34+
INTERNAL_ERROR_13(13);
35+
36+
private final int number;
37+
38+
InternalError(int number) {
39+
this.number = number;
40+
}
41+
42+
public int getNumber() {
43+
return number;
44+
}
45+
}

0 commit comments

Comments
 (0)