forked from mybatis/mybatis-dynamic-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRenderingContext.java
172 lines (144 loc) · 6.8 KB
/
RenderingContext.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql.render;
import static org.mybatis.dynamic.sql.util.StringUtilities.spaceBefore;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import org.mybatis.dynamic.sql.BindableColumn;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
/**
* This class encapsulates all the supporting items related to rendering, and contains many utility methods
* used during the rendering process.
*
* @since 1.5.1
* @author Jeff Butler
*/
public class RenderingContext {
private final RenderingStrategy renderingStrategy;
private final AtomicInteger sequence;
private final TableAliasCalculator tableAliasCalculator;
private final String configuredParameterName;
private final String calculatedParameterName;
private final StatementConfiguration statementConfiguration;
private RenderingContext(Builder builder) {
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
configuredParameterName = builder.parameterName;
tableAliasCalculator = Objects.requireNonNull(builder.tableAliasCalculator);
statementConfiguration = Objects.requireNonNull(builder.statementConfiguration);
// reasonable defaults
sequence = builder.sequence == null ? new AtomicInteger(1) : builder.sequence;
calculatedParameterName = builder.parameterName == null ? RenderingStrategy.DEFAULT_PARAMETER_PREFIX
: builder.parameterName + "." + RenderingStrategy.DEFAULT_PARAMETER_PREFIX; //$NON-NLS-1$
}
public TableAliasCalculator tableAliasCalculator() {
// this method can be removed when the renderWithTableAlias method is removed from BasicColumn
return tableAliasCalculator;
}
private String nextMapKey() {
return renderingStrategy.formatParameterMapKey(sequence);
}
private String renderedPlaceHolder(String mapKey) {
return renderingStrategy.getFormattedJdbcPlaceholder(calculatedParameterName, mapKey);
}
private <T> String renderedPlaceHolder(String mapKey, BindableColumn<T> column) {
return column.renderingStrategy().orElse(renderingStrategy)
.getFormattedJdbcPlaceholder(column, calculatedParameterName, mapKey);
}
public RenderedParameterInfo calculateParameterInfo() {
String mapKey = nextMapKey();
return new RenderedParameterInfo(mapKey, renderedPlaceHolder(mapKey));
}
public <T> RenderedParameterInfo calculateParameterInfo(BindableColumn<T> column) {
String mapKey = nextMapKey();
return new RenderedParameterInfo(mapKey, renderedPlaceHolder(mapKey, column));
}
public <T> String aliasedColumnName(SqlColumn<T> column) {
return tableAliasCalculator.aliasForColumn(column.table())
.map(alias -> aliasedColumnName(column, alias))
.orElseGet(column::name);
}
public <T> String aliasedColumnName(SqlColumn<T> column, String explicitAlias) {
return explicitAlias + "." + column.name(); //$NON-NLS-1$
}
public String aliasedTableName(SqlTable table) {
return tableAliasCalculator.aliasForTable(table)
.map(a -> table.tableNameAtRuntime() + spaceBefore(a))
.orElseGet(table::tableNameAtRuntime);
}
public boolean isNonRenderingClauseAllowed() {
return statementConfiguration.isNonRenderingWhereClauseAllowed();
}
public boolean isEmptyListConditionRenderingAllowed() {
return statementConfiguration.isEmptyListConditionRenderingAllowed();
}
/**
* Create a new rendering context based on this, with the table alias calculator modified to include the
* specified child table alias calculator. This is used by the query expression renderer when the alias calculator
* may change during rendering.
*
* @param childTableAliasCalculator the child table alias calculator
* @return a new rendering context whose table alias calculator is composed of the former calculator as parent, and
* the new child calculator
*/
public RenderingContext withChildTableAliasCalculator(TableAliasCalculator childTableAliasCalculator) {
TableAliasCalculator tac = new TableAliasCalculatorWithParent.Builder()
.withParent(tableAliasCalculator)
.withChild(childTableAliasCalculator)
.build();
return new Builder()
.withRenderingStrategy(this.renderingStrategy)
.withSequence(this.sequence)
.withParameterName(this.configuredParameterName)
.withTableAliasCalculator(tac)
.withStatementConfiguration(statementConfiguration)
.build();
}
public static Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
return new Builder().withRenderingStrategy(renderingStrategy);
}
public static class Builder {
private RenderingStrategy renderingStrategy;
private AtomicInteger sequence;
private TableAliasCalculator tableAliasCalculator = TableAliasCalculator.empty();
private String parameterName;
private StatementConfiguration statementConfiguration;
public Builder withRenderingStrategy(RenderingStrategy renderingStrategy) {
this.renderingStrategy = renderingStrategy;
return this;
}
public Builder withSequence(AtomicInteger sequence) {
this.sequence = sequence;
return this;
}
public Builder withTableAliasCalculator(TableAliasCalculator tableAliasCalculator) {
this.tableAliasCalculator = tableAliasCalculator;
return this;
}
public Builder withParameterName(String parameterName) {
this.parameterName = parameterName;
return this;
}
public Builder withStatementConfiguration(StatementConfiguration statementConfiguration) {
this.statementConfiguration = statementConfiguration;
return this;
}
public RenderingContext build() {
return new RenderingContext(this);
}
}
}