-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
SqlStatements.java
245 lines (218 loc) · 7.81 KB
/
SqlStatements.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* 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
*
* http://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.jdbi.v3.core.statement;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.annotation.Nullable;
import org.jdbi.v3.core.config.JdbiConfig;
import org.jdbi.v3.meta.Beta;
/**
* Configuration holder for {@link SqlStatement}s.
*/
public final class SqlStatements implements JdbiConfig<SqlStatements> {
private final Map<String, Object> attributes;
private TemplateEngine templateEngine;
private SqlParser sqlParser;
private SqlLogger sqlLogger;
private Integer queryTimeout;
private boolean allowUnusedBindings;
private final Collection<StatementCustomizer> customizers = new CopyOnWriteArrayList<>();
public SqlStatements() {
attributes = Collections.synchronizedMap(new HashMap<>());
templateEngine = new DefinedAttributeTemplateEngine();
sqlParser = new ColonPrefixSqlParser();
sqlLogger = SqlLogger.NOP_SQL_LOGGER;
queryTimeout = null;
}
private SqlStatements(SqlStatements that) {
this.attributes = Collections.synchronizedMap(that.getAttributes()); // already copied
this.templateEngine = that.templateEngine;
this.sqlParser = that.sqlParser;
this.sqlLogger = that.sqlLogger;
this.queryTimeout = that.queryTimeout;
this.allowUnusedBindings = that.allowUnusedBindings;
this.customizers.addAll(that.customizers);
}
/**
* Define an attribute for {@link StatementContext} for statements executed by Jdbi.
*
* @param key the key for the attribute
* @param value the value for the attribute
* @return this
*/
public SqlStatements define(String key, Object value) {
attributes.put(key, value);
return this;
}
/**
* Defines attributes for each key/value pair in the Map.
*
* @param values map of attributes to define.
* @return this
*/
public SqlStatements defineMap(final Map<String, ?> values) {
if (values != null) {
attributes.putAll(values);
}
return this;
}
/**
* Obtain the value of an attribute
*
* @param key the name of the attribute
* @return the value of the attribute
*/
public Object getAttribute(String key) {
return attributes.get(key);
}
/**
* Returns the attributes which will be applied to {@link SqlStatement SQL statements} created by Jdbi.
*
* @return the defined attributes.
*/
public Map<String, Object> getAttributes() {
return new HashMap<>(attributes);
}
/**
* Provides a means for custom statement modification. Common customizations
* have their own methods, such as {@link Query#setMaxRows(int)}
*
* @param customizer instance to be used to customize a statement
* @return this
*/
public SqlStatements addCustomizer(final StatementCustomizer customizer) {
this.customizers.add(customizer);
return this;
}
/**
* @return the template engine which renders the SQL template prior to
* parsing parameters.
*/
public TemplateEngine getTemplateEngine() {
return templateEngine;
}
/**
* Sets the {@link TemplateEngine} used to render SQL for all
* {@link SqlStatement SQL statements} executed by Jdbi. The default
* engine replaces <code><name></code>-style tokens
* with attributes {@link StatementContext#define(String, Object) defined}
* on the statement context.
*
* @param templateEngine the new template engine.
* @return this
*/
public SqlStatements setTemplateEngine(TemplateEngine templateEngine) {
this.templateEngine = templateEngine;
return this;
}
public SqlParser getSqlParser() {
return sqlParser;
}
/**
* Sets the {@link SqlParser} used to parse parameters in SQL statements
* executed by Jdbi. The default parses colon-prefixed named parameter
* tokens, e.g. <code>:name</code>.
*
* @param sqlParser the new SQL parser.
* @return this
*/
public SqlStatements setSqlParser(SqlParser sqlParser) {
this.sqlParser = sqlParser;
return this;
}
/**
* @return the timing collector
*
* @deprecated use {@link #getSqlLogger} instead
*/
@Deprecated
public TimingCollector getTimingCollector() {
return (elapsed, ctx) -> sqlLogger.logAfterExecution(ctx);
}
/**
* Sets the {@link TimingCollector} used to collect timing about the {@link SqlStatement SQL statements} executed
* by Jdbi. The default collector does nothing.
*
* @deprecated use {@link #setSqlLogger} instead
* @param timingCollector the new timing collector
* @return this
*/
@Deprecated
public SqlStatements setTimingCollector(TimingCollector timingCollector) {
this.sqlLogger = timingCollector == null ? SqlLogger.NOP_SQL_LOGGER : new SqlLogger() {
@Override
public void logAfterExecution(StatementContext context) {
timingCollector.collect(context.getElapsedTime(ChronoUnit.NANOS), context);
}
};
return this;
}
public SqlLogger getSqlLogger() {
return sqlLogger;
}
public SqlStatements setSqlLogger(SqlLogger sqlLogger) {
this.sqlLogger = sqlLogger == null ? SqlLogger.NOP_SQL_LOGGER : sqlLogger;
return this;
}
@Beta
public Integer getQueryTimeout() {
return queryTimeout;
}
/**
* Jdbi does not implement its own timeout mechanism: it simply calls {@link java.sql.Statement#setQueryTimeout}, leaving timeout handling to your jdbc driver.
*
* @param seconds the time in seconds to wait for a query to complete; 0 to disable the timeout; null to leave it at defaults (i.e. Jdbi will not call {@code setQueryTimeout(int)})
* @return this
*/
@Beta
public SqlStatements setQueryTimeout(@Nullable Integer seconds) {
if (seconds != null && seconds < 0) {
throw new IllegalArgumentException("queryTimeout must not be < 0");
}
this.queryTimeout = seconds;
return this;
}
public boolean isUnusedBindingAllowed() {
return allowUnusedBindings;
}
/**
* Sets whether or not an exception should be thrown when any arguments are given to a query but not actually used in it. Unused bindings tend to be bugs or oversights, but can also just be convenient. Defaults to false: unused bindings are not allowed.
*
* @see org.jdbi.v3.core.argument.Argument
* @param allowUnusedBindings the new setting
* @return this
*/
public SqlStatements setUnusedBindingAllowed(boolean allowUnusedBindings) {
this.allowUnusedBindings = allowUnusedBindings;
return this;
}
void customize(Statement statement) throws SQLException {
if (queryTimeout != null) {
statement.setQueryTimeout(queryTimeout);
}
}
@Override
public SqlStatements createCopy() {
return new SqlStatements(this);
}
Collection<StatementCustomizer> getCustomizers() {
return customizers;
}
}