45
45
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcTemplate ;
46
46
import org .springframework .jdbc .core .namedparam .SimplePropertySqlParameterSource ;
47
47
import org .springframework .jdbc .core .namedparam .SqlParameterSource ;
48
+ import org .springframework .jdbc .support .JdbcAccessor ;
48
49
import org .springframework .jdbc .support .KeyHolder ;
49
50
import org .springframework .jdbc .support .rowset .SqlRowSet ;
50
51
import org .springframework .util .Assert ;
62
63
*/
63
64
final class DefaultJdbcClient implements JdbcClient {
64
65
65
- private final JdbcOperations classicOps ;
66
-
67
66
private final NamedParameterJdbcOperations namedParamOps ;
68
67
69
68
private final ConversionService conversionService ;
@@ -81,7 +80,6 @@ public DefaultJdbcClient(JdbcOperations jdbcTemplate) {
81
80
82
81
public DefaultJdbcClient (NamedParameterJdbcOperations jdbcTemplate , @ Nullable ConversionService conversionService ) {
83
82
Assert .notNull (jdbcTemplate , "JdbcTemplate must not be null" );
84
- this .classicOps = jdbcTemplate .getJdbcOperations ();
85
83
this .namedParamOps = jdbcTemplate ;
86
84
this .conversionService =
87
85
(conversionService != null ? conversionService : DefaultConversionService .getSharedInstance ());
@@ -90,22 +88,63 @@ public DefaultJdbcClient(NamedParameterJdbcOperations jdbcTemplate, @Nullable Co
90
88
91
89
@ Override
92
90
public StatementSpec sql (String sql ) {
93
- return new DefaultStatementSpec (sql );
91
+ return new DefaultStatementSpec (sql , this . namedParamOps );
94
92
}
95
93
96
94
97
95
private class DefaultStatementSpec implements StatementSpec {
98
96
99
97
private final String sql ;
100
98
101
- private final List <Object > indexedParams = new ArrayList <>();
99
+ private JdbcOperations classicOps ;
100
+
101
+ private NamedParameterJdbcOperations namedParamOps ;
102
+
103
+ private @ Nullable JdbcTemplate customTemplate ;
104
+
105
+ private final List <@ Nullable Object > indexedParams = new ArrayList <>();
102
106
103
107
private final MapSqlParameterSource namedParams = new MapSqlParameterSource ();
104
108
105
109
private SqlParameterSource namedParamSource = this .namedParams ;
106
110
107
- public DefaultStatementSpec (String sql ) {
111
+ public DefaultStatementSpec (String sql , NamedParameterJdbcOperations namedParamOps ) {
108
112
this .sql = sql ;
113
+ this .classicOps = namedParamOps .getJdbcOperations ();
114
+ this .namedParamOps = namedParamOps ;
115
+ }
116
+
117
+ private JdbcTemplate enforceCustomTemplate () {
118
+ if (this .customTemplate == null ) {
119
+ if (!(this .classicOps instanceof JdbcAccessor original )) {
120
+ throw new IllegalStateException (
121
+ "Needs to be bound to a JdbcAccessor for custom settings support: " + this .classicOps );
122
+ }
123
+ this .customTemplate = new JdbcTemplate (original );
124
+ this .classicOps = this .customTemplate ;
125
+ this .namedParamOps = (this .namedParamOps instanceof NamedParameterJdbcTemplate originalNamedParam ?
126
+ new NamedParameterJdbcTemplate (originalNamedParam , this .customTemplate ) :
127
+ new NamedParameterJdbcTemplate (this .customTemplate ));
128
+ }
129
+ return this .customTemplate ;
130
+ }
131
+
132
+ @ Override
133
+ public StatementSpec withFetchSize (int fetchSize ) {
134
+ enforceCustomTemplate ().setFetchSize (fetchSize );
135
+ return this ;
136
+ }
137
+
138
+ @ Override
139
+ public StatementSpec withMaxRows (int maxRows ) {
140
+ enforceCustomTemplate ().setMaxRows (maxRows );
141
+ return this ;
142
+ }
143
+
144
+ @ Override
145
+ public StatementSpec withQueryTimeout (int queryTimeout ) {
146
+ enforceCustomTemplate ().setQueryTimeout (queryTimeout );
147
+ return this ;
109
148
}
110
149
111
150
@ Override
@@ -220,41 +259,41 @@ public <T> MappedQuerySpec<T> query(RowMapper<T> rowMapper) {
220
259
@ Override
221
260
public void query (RowCallbackHandler rch ) {
222
261
if (useNamedParams ()) {
223
- namedParamOps .query (this .sql , this .namedParamSource , rch );
262
+ this . namedParamOps .query (this .sql , this .namedParamSource , rch );
224
263
}
225
264
else {
226
- classicOps .query (statementCreatorForIndexedParams (), rch );
265
+ this . classicOps .query (statementCreatorForIndexedParams (), rch );
227
266
}
228
267
}
229
268
230
269
@ Override
231
270
public <T > T query (ResultSetExtractor <T > rse ) {
232
271
T result = (useNamedParams () ?
233
- namedParamOps .query (this .sql , this .namedParamSource , rse ) :
234
- classicOps .query (statementCreatorForIndexedParams (), rse ));
272
+ this . namedParamOps .query (this .sql , this .namedParamSource , rse ) :
273
+ this . classicOps .query (statementCreatorForIndexedParams (), rse ));
235
274
Assert .state (result != null , "No result from ResultSetExtractor" );
236
275
return result ;
237
276
}
238
277
239
278
@ Override
240
279
public int update () {
241
280
return (useNamedParams () ?
242
- namedParamOps .update (this .sql , this .namedParamSource ) :
243
- classicOps .update (statementCreatorForIndexedParams ()));
281
+ this . namedParamOps .update (this .sql , this .namedParamSource ) :
282
+ this . classicOps .update (statementCreatorForIndexedParams ()));
244
283
}
245
284
246
285
@ Override
247
286
public int update (KeyHolder generatedKeyHolder ) {
248
287
return (useNamedParams () ?
249
- namedParamOps .update (this .sql , this .namedParamSource , generatedKeyHolder ) :
250
- classicOps .update (statementCreatorForIndexedParamsWithKeys (null ), generatedKeyHolder ));
288
+ this . namedParamOps .update (this .sql , this .namedParamSource , generatedKeyHolder ) :
289
+ this . classicOps .update (statementCreatorForIndexedParamsWithKeys (null ), generatedKeyHolder ));
251
290
}
252
291
253
292
@ Override
254
293
public int update (KeyHolder generatedKeyHolder , String ... keyColumnNames ) {
255
294
return (useNamedParams () ?
256
- namedParamOps .update (this .sql , this .namedParamSource , generatedKeyHolder , keyColumnNames ) :
257
- classicOps .update (statementCreatorForIndexedParamsWithKeys (keyColumnNames ), generatedKeyHolder ));
295
+ this . namedParamOps .update (this .sql , this .namedParamSource , generatedKeyHolder , keyColumnNames ) :
296
+ this . classicOps .update (statementCreatorForIndexedParamsWithKeys (keyColumnNames ), generatedKeyHolder ));
258
297
}
259
298
260
299
private boolean useNamedParams () {
0 commit comments