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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
package bjc.utils.misc;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Parent class for {@link NamedPreparedStatement}
*
* Used mostly for cases where you want to prepare the bindings independently of the actual query.
*/
public class BoundPreparedStatement {
protected final Map<String, Binder> singles = new HashMap<>();
protected final Map<String, List<Binder>> lists = new HashMap<>();
protected final List<ParamSnapshot> batch = new ArrayList<>();
private static List<Binder> toBinders(Collection<?> values, int sqlType, boolean forceType) {
ArrayList<Binder> list = new ArrayList<>(values.size());
for (Object v : values) {
final Object val = v;
if (forceType) {
list.add((ps, i) -> {
if (val == null)
ps.setNull(i, sqlType);
else
ps.setObject(i, val, sqlType);
});
} else {
list.add((ps, i) -> ps.setObject(i, val));
}
}
return list;
}
/**
* Record the current set of parameters as a batch and start a new one.
*
* @return The prepared statement
*/
public BoundPreparedStatement addBatch() {
batch.add(new ParamSnapshot(new HashMap<>(singles), deepCopyLists(lists)));
return this;
}
/**
* Clear the currently bound parameters.
*
* @return The prepared statement
*/
public BoundPreparedStatement clearParameters() {
singles.clear();
lists.clear();
return this;
}
/**
* Bind a object as a parameter
*
* @param name The name of the parameter
* @param value The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setObject(String name, Object value) {
singles.put(name, (ps, i) -> ps.setObject(i, value));
return this;
}
/**
* Bind a object as a parameter
*
* @param name The name of the parameter
* @param value The value for the parameter
* @param sqlType The SQL type for the parameter (see {@link java.sql.Types})
* @return The prepared statement
*/
public BoundPreparedStatement setObject(String name, Object value, int sqlType) {
singles.put(name, (ps, i) -> {
if (value == null)
ps.setNull(i, sqlType);
else
ps.setObject(i, value, sqlType);
});
return this;
}
/**
* Bind a null as a parameter
*
* @param name The name of the parameter
* @param sqlType The SQL type for the parameter (see {@link java.sql.Types})
* @return The prepared statement
*/
public BoundPreparedStatement setNull(String name, int sqlType) {
singles.put(name, (ps, i) -> ps.setNull(i, sqlType));
return this;
}
/**
* Bind a boolean as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setBoolean(String n, boolean x) {
singles.put(n, (ps, i) -> ps.setBoolean(i, x));
return this;
}
/**
* Bind a byte as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setByte(String n, byte x) {
singles.put(n, (ps, i) -> ps.setByte(i, x));
return this;
}
/**
* Bind a short as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setShort(String n, short x) {
singles.put(n, (ps, i) -> ps.setShort(i, x));
return this;
}
/**
* Bind a int as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setInt(String n, int x) {
singles.put(n, (ps, i) -> ps.setInt(i, x));
return this;
}
/**
* Bind a long as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setLong(String n, long x) {
singles.put(n, (ps, i) -> ps.setLong(i, x));
return this;
}
/**
* Bind a float as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setFloat(String n, float x) {
singles.put(n, (ps, i) -> ps.setFloat(i, x));
return this;
}
/**
* Bind a double as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setDouble(String n, double x) {
singles.put(n, (ps, i) -> ps.setDouble(i, x));
return this;
}
/**
* Bind a {@link BigDecimal} as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setBigDecimal(String n, BigDecimal x) {
singles.put(n, (ps, i) -> ps.setBigDecimal(i, x));
return this;
}
/**
* Bind a string as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setString(String n, String x) {
singles.put(n, (ps, i) -> ps.setString(i, x));
return this;
}
/**
* Bind a byte-array as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setBytes(String n, byte[] x) {
singles.put(n, (ps, i) -> ps.setBytes(i, x));
return this;
}
/**
* Bind a date as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setDate(String n, Date x) {
singles.put(n, (ps, i) -> ps.setDate(i, x));
return this;
}
/**
* Bind a date as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @param c The calendar for the date
* @return The prepared statement
*/
public BoundPreparedStatement setDate(String n, Date x, Calendar c) {
singles.put(n, (ps, i) -> ps.setDate(i, x, c));
return this;
}
/**
* Bind a time-value as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setTime(String n, Time x) {
singles.put(n, (ps, i) -> ps.setTime(i, x));
return this;
}
/**
* Bind a time-value as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @param c The calendar for the time
* @return The prepared statement
*/
public BoundPreparedStatement setTime(String n, Time x, Calendar c) {
singles.put(n, (ps, i) -> ps.setTime(i, x, c));
return this;
}
/**
* Bind a timestamp as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setTimestamp(String n, Timestamp x) {
singles.put(n, (ps, i) -> ps.setTimestamp(i, x));
return this;
}
/**
* Bind a timestamp as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @param c The calendar for the timestamp
* @return The prepared statement
*/
public BoundPreparedStatement setTimestamp(String n, Timestamp x, Calendar c) {
singles.put(n, (ps, i) -> ps.setTimestamp(i, x, c));
return this;
}
/**
* Bind a {@link Array} as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setArray(String n, Array x) {
singles.put(n, (ps, i) -> ps.setArray(i, x));
return this;
}
/**
* Bind a {@link Blob} as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setBlob(String n, Blob x) {
singles.put(n, (ps, i) -> ps.setBlob(i, x));
return this;
}
/**
* Bind a {@link Clob} as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setClob(String n, Clob x) {
singles.put(n, (ps, i) -> ps.setClob(i, x));
return this;
}
/**
* Bind a URL as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @return The prepared statement
*/
public BoundPreparedStatement setURL(String n, java.net.URL x) {
singles.put(n, (ps, i) -> ps.setURL(i, x));
return this;
}
/**
* Bind a ASCII stream as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @param len The length of the stream
* @return The prepared statement
*/
public BoundPreparedStatement setAsciiStream(String n, java.io.InputStream x, int len) {
singles.put(n, (ps, i) -> ps.setAsciiStream(i, x, len));
return this;
}
/**
* Bind a binary stream as a parameter
*
* @param n The name of the parameter
* @param x The value for the parameter
* @param len The length of the stream
* @return The prepared statement
*/
public BoundPreparedStatement setBinaryStream(String n, java.io.InputStream x, int len) {
singles.put(n, (ps, i) -> ps.setBinaryStream(i, x, len));
return this;
}
/**
* Bind a character stream as a parameter
*
* @param n The name of the parameter
* @param r The value for the parameter
* @param len The length of the stream
* @return The prepared statement
*/
public BoundPreparedStatement setCharacterStream(String n, java.io.Reader r, int len) {
singles.put(n, (ps, i) -> ps.setCharacterStream(i, r, len));
return this;
}
/**
* Bind a list parameter to the statement
*
* @param name The name of the list parameter
* @param values The values for the list parameter
* @return The prepared statement
*/
public BoundPreparedStatement setList(String name, Collection<?> values) {
lists.put(name, toBinders(values, Types.OTHER, false));
return this;
}
/**
* Bind a list parameter to the statement
*
* @param name The name of the list parameter
* @param values The values for the list parameter
* @return The prepared statement
*/
public BoundPreparedStatement setList(String name, Object... values) {
lists.put(name, toBinders(Arrays.asList(values), Types.OTHER, false));
return this;
}
/**
* Explicit SQL type for all elements (useful if nulls might appear).
*
* @param name The name of the list parameter
* @param sqlType The SQL type for the parameters
* @param values The values for the list parameter
* @return The prepared statement
*/
public BoundPreparedStatement setList(String name, int sqlType, Collection<?> values) {
lists.put(name, toBinders(values, sqlType, true));
return this;
}
/**
* Explicit SQL type for all elements (useful if nulls might appear).
*
* @param name The name of the list parameter
* @param sqlType The SQL type for the parameters
* @param values The values for the list parameter
* @return The prepared statement
*/
public BoundPreparedStatement setList(String name, int sqlType, Object... values) {
lists.put(name, toBinders(Arrays.asList(values), sqlType, true));
return this;
}
private static Map<String, List<Binder>> deepCopyLists(Map<String, List<Binder>> src) {
Map<String, List<Binder>> m = new HashMap<>(src.size());
for (Map.Entry<String, List<Binder>> e : src.entrySet()) {
m.put(e.getKey(), new ArrayList<>(e.getValue()));
}
return m;
}
/**
* Create a new {@link BoundPreparedStatement}
*/
public BoundPreparedStatement() {
}
}
|