10 | * Calling this method before providing data will generally cause an exception. 11 | *
12 | * Not calling this method after your last batch of parameters will cause them 13 | * to be ignored. 14 | * 15 | * @return {code this} for chaining 16 | * @see PreparedStatement#addBatch() 17 | */ 18 | BatchUpdate next(); 19 | 20 | /** 21 | * Ends the current batch of parameters. 22 | *
23 | * Calling this method before providing data will generally cause an exception. 24 | *
25 | * Not calling this method after your last batch of parameters will cause them
26 | * to be ignored.
27 | *
28 | * @return {code this} for chaining
29 | * @see PreparedStatement#addBatch()
30 | */
31 | BatchUpdate next(String sql);
32 |
33 | /**
34 | * Executes the query and return the amount of rows modified by this query.
35 | *
36 | * @return the amount of rows modified by this query
37 | * @see PreparedStatement#executeBatch()
38 | */
39 | int[] counts();
40 |
41 | /**
42 | * Executes the query and return the amount of rows modified by this query as a long.
43 | *
44 | * @return the amount of rows modified by this query
45 | * @see PreparedStatement#executeLargeBatch()
46 | */
47 | long[] largeCounts();
48 |
49 | /**
50 | * Executes the query and return the sum of the amount of rows modified by each batch.
51 | *
52 | * @return the amount of rows modified by this query
53 | * @see PreparedStatement#executeBatch()
54 | */
55 | int count();
56 |
57 | /**
58 | * Executes the query and return the sum of the amount of rows modified by each batch.
59 | *
60 | * @return the amount of rows modified by this query
61 | * @see PreparedStatement#executeLargeBatch()
62 | */
63 | long largeCount();
64 | }
65 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/Execute.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams;
2 |
3 | import java.sql.PreparedStatement;
4 |
5 | public interface Execute
33 | * See here for the types
34 | * supported by this method.
35 | *
36 | * @param params parameters to set
37 | * @return {@code this} for chaining
38 | */
39 | Provider with(Object... params);
40 |
41 | /**
42 | * Sets the designated parameter based on the type of the parameter given.
43 | *
44 | * See here for the types
45 | * supported by this method.
46 | *
47 | * @param index the index of the parameter to set (starting from 1)
48 | * @param x the value to set
49 | * @return {@code this} for chaining
50 | * @throws NullPointerException if {@code x} is {@code null}
51 | */
52 | Provider set(int index, Object x);
53 |
54 | /**
55 | * Sets the designated parameter to the given {@link Array}.
56 | *
57 | * @param index the index of the parameter to set (starting from 1)
58 | * @param x the value to set
59 | * @return {@code this} for chaining
60 | * @see PreparedStatement#setArray(int, Array)
61 | */
62 | Provider setArray(int index, Array x);
63 |
64 | /**
65 | * Sets the designated parameter to the given {@link InputStream}.
66 | *
67 | * @param index the index of the parameter to set (starting from 1)
68 | * @param x the value to set
69 | * @return {@code this} for chaining
70 | * @see PreparedStatement#setAsciiStream(int, InputStream)
71 | */
72 | Provider setAsciiStream(int index, InputStream x);
73 |
74 | /**
75 | * Sets the designated parameter to the given {@link InputStream}.
76 | *
77 | * @param index the index of the parameter to set (starting from 1)
78 | * @param x the value to set
79 | * @param length the number of bytes in the stream
80 | * @return {@code this} for chaining
81 | * @see PreparedStatement#setAsciiStream(int, InputStream, int)
82 | */
83 | Provider setAsciiStream(int index, InputStream x, int length);
84 |
85 | /**
86 | * Sets the designated parameter to the given {@link InputStream}.
87 | *
88 | * @param index the index of the parameter to set (starting from 1)
89 | * @param x the value to set
90 | * @param length the number of bytes in the stream
91 | * @return {@code this} for chaining
92 | * @see PreparedStatement#setAsciiStream(int, InputStream, long)
93 | */
94 | Provider setAsciiStream(int index, InputStream x, long length);
95 |
96 | /**
97 | * Sets the designated parameter to the given {@link BigDecimal}.
98 | *
99 | * @param index the index of the parameter to set (starting from 1)
100 | * @param x the value to set
101 | * @return {@code this} for chaining
102 | * @see PreparedStatement#setBigDecimal(int, BigDecimal)
103 | */
104 | Provider setBigDecimal(int index, BigDecimal x);
105 |
106 | /**
107 | * Sets the designated parameter to the given {@link InputStream}.
108 | *
109 | * @param index the index of the parameter to set (starting from 1)
110 | * @param x the value to set
111 | * @return {@code this} for chaining
112 | * @see PreparedStatement#setBinaryStream(int, InputStream)
113 | */
114 | Provider setBinaryStream(int index, InputStream x);
115 |
116 | /**
117 | * Sets the designated parameter to the given {@link InputStream}.
118 | *
119 | * @param index the index of the parameter to set (starting from 1)
120 | * @param x the value to set
121 | * @param length the number of bytes in the stream
122 | * @return {@code this} for chaining
123 | * @see PreparedStatement#setBinaryStream(int, InputStream, int)
124 | */
125 | Provider setBinaryStream(int index, InputStream x, int length);
126 |
127 | /**
128 | * Sets the designated parameter to the given {@link InputStream}.
129 | *
130 | * @param index the index of the parameter to set (starting from 1)
131 | * @param x the value to set
132 | * @param length the number of bytes in the stream
133 | * @return {@code this} for chaining
134 | * @see PreparedStatement#setBinaryStream(int, InputStream, long)
135 | */
136 | Provider setBinaryStream(int index, InputStream x, long length);
137 |
138 | /**
139 | * Sets the designated parameter to the given {@link Blob}.
140 | *
141 | * @param index the index of the parameter to set (starting from 1)
142 | * @param x the value to set
143 | * @return {@code this} for chaining
144 | * @see PreparedStatement#setBlob(int, Blob)
145 | */
146 | Provider setBlob(int index, Blob x);
147 |
148 | /**
149 | * Sets the designated parameter to the given {@link InputStream}.
150 | *
151 | * @param index the index of the parameter to set (starting from 1)
152 | * @param x the value to set
153 | * @return {@code this} for chaining
154 | * @see PreparedStatement#setBlob(int, InputStream)
155 | */
156 | Provider setBlob(int index, InputStream x);
157 |
158 | /**
159 | * Sets the designated parameter to the given {@link InputStream}.
160 | *
161 | * @param index the index of the parameter to set (starting from 1)
162 | * @param x the value to set
163 | * @param length the number of bytes in the stream
164 | * @return {@code this} for chaining
165 | * @see PreparedStatement#setBlob(int, InputStream, long)
166 | */
167 | Provider setBlob(int index, InputStream x, long length);
168 |
169 | /**
170 | * Sets the designated parameter to the given {@code boolean}.
171 | *
172 | * @param index the index of the parameter to set (starting from 1)
173 | * @param x the value to set
174 | * @return {@code this} for chaining
175 | * @see PreparedStatement#setBoolean(int, boolean)
176 | */
177 | Provider setBoolean(int index, boolean x);
178 |
179 | /**
180 | * Sets the designated parameter to the given {@code byte}.
181 | *
182 | * @param index the index of the parameter to set (starting from 1)
183 | * @param x the value to set
184 | * @return {@code this} for chaining
185 | * @see PreparedStatement#setByte(int, byte)
186 | */
187 | Provider setByte(int index, byte x);
188 |
189 | /**
190 | * Sets the designated parameter to the given {@code byte[]}.
191 | *
192 | * @param index the index of the parameter to set (starting from 1)
193 | * @param x the value to set
194 | * @return {@code this} for chaining
195 | * @see PreparedStatement#setBytes(int, byte[])
196 | */
197 | Provider setBytes(int index, byte x[]);
198 |
199 | /**
200 | * Sets the designated parameter to the given {@link Reader}.
201 | *
202 | * @param index the index of the parameter to set (starting from 1)
203 | * @param x the value to set
204 | * @return {@code this} for chaining
205 | * @see PreparedStatement#setCharacterStream(int, Reader)
206 | */
207 | Provider setCharacterStream(int index, Reader x);
208 |
209 | /**
210 | * Sets the designated parameter to the given {@link Reader}.
211 | *
212 | * @param index the index of the parameter to set (starting from 1)
213 | * @param x the value to set
214 | * @param length the number of characters in the stream
215 | * @return {@code this} for chaining
216 | * @see PreparedStatement#setCharacterStream(int, Reader, int)
217 | */
218 | Provider setCharacterStream(int index, Reader x, int length);
219 |
220 | /**
221 | * Sets the designated parameter to the given {@link Reader}.
222 | *
223 | * @param index the index of the parameter to set (starting from 1)
224 | * @param x the value to set
225 | * @param length the number of characters in the stream
226 | * @return {@code this} for chaining
227 | * @see PreparedStatement#setCharacterStream(int, Reader, long)
228 | */
229 | Provider setCharacterStream(int index, Reader x, long length);
230 |
231 | /**
232 | * Sets the designated parameter to the given {@link Clob}.
233 | *
234 | * @param index the index of the parameter to set (starting from 1)
235 | * @param x the value to set
236 | * @return {@code this} for chaining
237 | * @see PreparedStatement#setClob(int, Clob)
238 | */
239 | Provider setClob(int index, Clob x);
240 |
241 | /**
242 | * Sets the designated parameter to the given {@link Reader}.
243 | *
244 | * @param index the index of the parameter to set (starting from 1)
245 | * @param x the value to set
246 | * @return {@code this} for chaining
247 | * @see PreparedStatement#setClob(int, Reader)
248 | */
249 | Provider setClob(int index, Reader x);
250 |
251 | /**
252 | * Sets the designated parameter to the given {@link Reader}.
253 | *
254 | * @param index the index of the parameter to set (starting from 1)
255 | * @param x the value to set
256 | * @param length the number of characters in the stream
257 | * @return {@code this} for chaining
258 | * @see PreparedStatement#setClob(int, Reader, long)
259 | */
260 | Provider setClob(int index, Reader x, long length);
261 |
262 | /**
263 | * Sets the designated parameter to the given {@link Date}.
264 | *
265 | * @param index the index of the parameter to set (starting from 1)
266 | * @param x the value to set
267 | * @return {@code this} for chaining
268 | * @see PreparedStatement#setDate(int, Date)
269 | */
270 | Provider setDate(int index, Date x);
271 |
272 | /**
273 | * Sets the designated parameter to the given {@link Date}.
274 | *
275 | * @param index the index of the parameter to set (starting from 1)
276 | * @param x the value to set
277 | * @param cal the {@link Calendar} object the driver will use to construct the date
278 | * @return {@code this} for chaining
279 | * @see PreparedStatement#setDate(int, Date, Calendar)
280 | */
281 | Provider setDate(int index, Date x, Calendar cal);
282 |
283 | /**
284 | * Sets the designated parameter to the given {@code double}.
285 | *
286 | * @param index the index of the parameter to set (starting from 1)
287 | * @param x the value to set
288 | * @return {@code this} for chaining
289 | * @see PreparedStatement#setDouble(int, double)
290 | */
291 | Provider setDouble(int index, double x);
292 |
293 | /**
294 | * Sets the designated parameter to the given {@code float}.
295 | *
296 | * @param index the index of the parameter to set (starting from 1)
297 | * @param x the value to set
298 | * @return {@code this} for chaining
299 | * @see PreparedStatement#setFloat(int, float)
300 | */
301 | Provider setFloat(int index, float x);
302 |
303 | /**
304 | * Sets the designated parameter to the given {@code int}.
305 | *
306 | * @param index the index of the parameter to set (starting from 1)
307 | * @param x the value to set
308 | * @return {@code this} for chaining
309 | * @see PreparedStatement#setInt(int, int)
310 | */
311 | Provider setInt(int index, int x);
312 |
313 | /**
314 | * Sets the designated parameter to the given {@code long}.
315 | *
316 | * @param index the index of the parameter to set (starting from 1)
317 | * @param x the value to set
318 | * @return {@code this} for chaining
319 | * @see PreparedStatement#setLong(int, long)
320 | */
321 | Provider setLong(int index, long x);
322 |
323 | /**
324 | * Sets the designated parameter to the given {@link Reader}.
325 | *
326 | * @param index the index of the parameter to set (starting from 1)
327 | * @param x the value to set
328 | * @return {@code this} for chaining
329 | * @see PreparedStatement#setNCharacterStream(int, Reader)
330 | */
331 | Provider setNCharacterStream(int index, Reader x);
332 |
333 | /**
334 | * Sets the designated parameter to the given {@link Reader}.
335 | *
336 | * @param index the index of the parameter to set (starting from 1)
337 | * @param x the value to set
338 | * @param length the number of characters in the stream
339 | * @return {@code this} for chaining
340 | * @see PreparedStatement#setNCharacterStream(int, Reader, long length)
341 | */
342 | Provider setNCharacterStream(int index, Reader x, long length);
343 |
344 | /**
345 | * Sets the designated parameter to the given {@link NClob}.
346 | *
347 | * @param index the index of the parameter to set (starting from 1)
348 | * @param x the value to set
349 | * @return {@code this} for chaining
350 | * @see PreparedStatement#setNClob(int, NClob)
351 | */
352 | Provider setNClob(int index, NClob x);
353 |
354 | /**
355 | * Sets the designated parameter to the given {@link Reader}.
356 | *
357 | * @param index the index of the parameter to set (starting from 1)
358 | * @param x the value to set
359 | * @return {@code this} for chaining
360 | * @see PreparedStatement#setNClob(int, Reader)
361 | */
362 | Provider setNClob(int index, Reader x);
363 |
364 | /**
365 | * Sets the designated parameter to the given {@link Reader}.
366 | *
367 | * @param index the index of the parameter to set (starting from 1)
368 | * @param x the value to set
369 | * @param length the number of characters in the stream
370 | * @return {@code this} for chaining
371 | * @see PreparedStatement#setNClob(int, Reader, long)
372 | */
373 | Provider setNClob(int index, Reader x, long length);
374 |
375 | /**
376 | * Sets the designated parameter to the given {@link String}.
377 | *
378 | * @param index the index of the parameter to set (starting from 1)
379 | * @param x the value to set
380 | * @return {@code this} for chaining
381 | * @see PreparedStatement#setNString(int, String)
382 | */
383 | Provider setNString(int index, String x);
384 |
385 | /**
386 | * Sets the designated parameter to the given {@code int}.
387 | *
388 | * @param index the index of the parameter to set (starting from 1)
389 | * @param sqlType the SQL type code defined in {@link Types}
390 | * @return {@code this} for chaining
391 | * @see PreparedStatement#setNull(int, int)
392 | */
393 | Provider setNull(int index, int sqlType);
394 |
395 | /**
396 | * Sets the designated parameter to the given {@code int}.
397 | *
398 | * @param index the index of the parameter to set (starting from 1)
399 | * @param sqlType the SQL type code defined in {@link Types}
400 | * @param typeName the fully-qualified name of an SQL user-defined type;
401 | * ignored if the parameter is not a user-defined type or REF
402 | * @return {@code this} for chaining
403 | * @see PreparedStatement#setNull(int, int, String)
404 | */
405 | Provider setNull(int index, int sqlType, String typeName);
406 |
407 | /**
408 | * Sets the designated parameter to the given {@link Object}.
409 | *
410 | * @param index the index of the parameter to set (starting from 1)
411 | * @param x the value to set
412 | * @return {@code this} for chaining
413 | * @see PreparedStatement#setObject(int, Object)
414 | */
415 | Provider setObject(int index, Object x);
416 |
417 | /**
418 | * Sets the designated parameter to the given {@link Object}.
419 | *
420 | * @param index the index of the parameter to set (starting from 1)
421 | * @param x the value to set
422 | * @param targetSqlType the SQL type code defined in {@link Types}
423 | * @return {@code this} for chaining
424 | * @see PreparedStatement#setObject(int, Object, int)
425 | */
426 | Provider setObject(int index, Object x, int targetSqlType);
427 |
428 | /**
429 | * Sets the designated parameter to the given {@link Object}.
430 | *
431 | * @param index the index of the parameter to set (starting from 1)
432 | * @param x the value to set
433 | * @param targetSqlType the SQL type code defined in {@link Types}
434 | * @param scaleOrLength for {@code java.sql.Types.DECIMAL}
435 | * or {@code java.sql.Types.NUMERIC}, this is the number of digits
436 | * after the decimal point. For Java Object types {@link InputStream}
437 | * and {@link Reader}, this is the length of the data in the stream
438 | * or reader. For all other types, this value will be ignored.
439 | * @return {@code this} for chaining
440 | * @see PreparedStatement#setObject(int, Object, int, int)
441 | */
442 | Provider setObject(int index, Object x, int targetSqlType, int scaleOrLength);
443 |
444 | /**
445 | * Sets the designated parameter to the given {@link Object}.
446 | *
447 | * @param index the index of the parameter to set (starting from 1)
448 | * @param x the value to set
449 | * @param targetSqlType the SQL type code defined in {@link Types}
450 | * @return {@code this} for chaining
451 | * @see PreparedStatement#setObject(int, Object, SQLType)
452 | */
453 | Provider setObject(int index, Object x, SQLType targetSqlType);
454 |
455 | /**
456 | * Sets the designated parameter to the given {@link Object}.
457 | *
458 | * @param index the index of the parameter to set (starting from 1)
459 | * @param x the value to set
460 | * @param targetSqlType the SQL type code defined in {@link Types}
461 | * @param scaleOrLength for {@code java.sql.Types.DECIMAL}
462 | * or {@code java.sql.Types.NUMERIC}, this is the number of digits
463 | * after the decimal point. For Java Object types {@link InputStream}
464 | * and {@link Reader}, this is the length of the data in the stream
465 | * or reader. For all other types, this value will be ignored.
466 | * @return {@code this} for chaining
467 | * @see PreparedStatement#setObject(int, Object, SQLType, int)
468 | */
469 | Provider setObject(int index, Object x, SQLType targetSqlType, int scaleOrLength);
470 |
471 | /**
472 | * Sets the designated parameter to the given {@link Ref}.
473 | *
474 | * @param index the index of the parameter to set (starting from 1)
475 | * @param x the value to set
476 | * @return {@code this} for chaining
477 | * @see PreparedStatement#setRef(int, Ref)
478 | */
479 | Provider setRef(int index, Ref x);
480 |
481 | /**
482 | * Sets the designated parameter to the given {@link RowId}.
483 | *
484 | * @param index the index of the parameter to set (starting from 1)
485 | * @param x the value to set
486 | * @return {@code this} for chaining
487 | * @see PreparedStatement#setRowId(int, RowId)
488 | */
489 | Provider setRowId(int index, RowId x);
490 |
491 | /**
492 | * Sets the designated parameter to the given {@code short}.
493 | *
494 | * @param index the index of the parameter to set (starting from 1)
495 | * @param x the value to set
496 | * @return {@code this} for chaining
497 | * @see PreparedStatement#setShort(int, short)
498 | */
499 | Provider setShort(int index, short x);
500 |
501 | /**
502 | * Sets the designated parameter to the given {@link SQLXML}.
503 | *
504 | * @param index the index of the parameter to set (starting from 1)
505 | * @param x the value to set
506 | * @return {@code this} for chaining
507 | * @see PreparedStatement#setSQLXML(int, SQLXML)
508 | */
509 | Provider setSQLXML(int index, SQLXML x);
510 |
511 | /**
512 | * Sets the designated parameter to the given {@link String}.
513 | *
514 | * @param index the index of the parameter to set (starting from 1)
515 | * @param x the value to set
516 | * @return {@code this} for chaining
517 | * @see PreparedStatement#setString(int, String)
518 | */
519 | Provider setString(int index, String x);
520 |
521 | /**
522 | * Sets the designated parameter to the given {@link Time}.
523 | *
524 | * @param index the index of the parameter to set (starting from 1)
525 | * @param x the value to set
526 | * @return {@code this} for chaining
527 | * @see PreparedStatement#setTime(int, Time)
528 | */
529 | Provider setTime(int index, Time x);
530 |
531 | /**
532 | * Sets the designated parameter to the given {@link Time}.
533 | *
534 | * @param index the index of the parameter to set (starting from 1)
535 | * @param x the value to set
536 | * @param cal the {@link Calendar} object the driver will use to construct the date
537 | * @return {@code this} for chaining
538 | * @see PreparedStatement#setTime(int, Time, Calendar)
539 | */
540 | Provider setTime(int index, Time x, Calendar cal);
541 |
542 | /**
543 | * Sets the designated parameter to the given {@link Timestamp}.
544 | *
545 | * @param index the index of the parameter to set (starting from 1)
546 | * @param x the value to set
547 | * @return {@code this} for chaining
548 | * @see PreparedStatement#setTimestamp(int, Timestamp)
549 | */
550 | Provider setTimestamp(int index, Timestamp x);
551 |
552 | /**
553 | * Sets the designated parameter to the given {@link Timestamp}.
554 | *
555 | * @param index the index of the parameter to set (starting from 1)
556 | * @param x the value to set
557 | * @param cal the {@link Calendar} object the driver will use to construct the date
558 | * @return {@code this} for chaining
559 | * @see PreparedStatement#setTimestamp(int, Timestamp, Calendar)
560 | */
561 | Provider setTimestamp(int index, Timestamp x, Calendar cal);
562 |
563 | /**
564 | * Sets the designated parameter to the given {@link URL}.
565 | *
566 | * @param index the index of the parameter to set (starting from 1)
567 | * @param x the value to set
568 | * @return {@code this} for chaining
569 | * @see PreparedStatement#setURL(int, URL)
570 | */
571 | Provider setURL(int index, URL x);
572 | }
573 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/PreparedStatementBinderByIndex.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams;
2 |
3 | import java.sql.PreparedStatement;
4 | import java.sql.SQLException;
5 |
6 | @FunctionalInterface
7 | public interface PreparedStatementBinderByIndex
26 | * This method should be called with a {@code try}-with-resources construct
27 | * to ensure that the underlying {@link java.sql.Statement} and {@link
28 | * ResultSet} are correctly closed.
29 | *
30 | * @param mapping the mapping function
31 | * @param
75 | * Note that the query is not actually executed until a mapping method
76 | * of {@link Query} is called.
77 | *
78 | * @param sql the sql query
79 | * @return an object to parametrize the statement and map the query result
80 | */
81 | default Query query(String sql) {
82 | return query(conn -> conn.prepareStatement(sql));
83 | }
84 |
85 | /**
86 | * Manually prepares a DML query from a {@link Connection}.
87 | *
88 | * @param preparer the code creating a {@link PreparedStatement} from a
89 | * {@link Connection}
90 | * @return an object to parametrize and execute the DML statement
91 | */
92 | Update update(SqlFunction
97 | * Not that the query is not actually executed until you invoke a
98 | * method from {@link Update}.
99 | *
100 | * @param sql the sql query
101 | * @return an object to parametrize the statement and retrieve the number
102 | * of rows affected by this query
103 | */
104 | default Update update(String sql) {
105 | return update(conn -> conn.prepareStatement(sql));
106 | }
107 |
108 | /**
109 | * Prepares a DML statement to provide it multiple batches of parameters.
110 | *
111 | * Note that the query is not actually executed until you invoke a
112 | * count method from {@link BatchUpdate}.
113 | *
114 | * @param sql the sql query
115 | * @return an object to parametrize the statement and retrieve counts
116 | * of affected rows
117 | */
118 | BatchUpdate batchUpdate(String sql);
119 |
120 | /**
121 | * Prepares a query.
122 | *
123 | * Note that this method is not executed until you call {@link Execute#execute()}.
124 | *
125 | * @param sql the sql query
126 | * @return an object to parametrize the statement and execute the query
127 | */
128 | Execute
133 | * Note that this method is not executed until you call {@link Execute#execute()}.
134 | *
135 | * @param sql the sql query
136 | * @return an object to parametrize the statement and execute the query
137 | * @see Connection#prepareCall(String)
138 | */
139 | Execute
8 | * This object implements {@link AutoCloseable} so that you can write code like
9 | *
56 | * Closing the transaction closes the underlying connection.
57 | */
58 | @Override
59 | void close();
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/UncheckedSqlException.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams;
2 |
3 | import java.sql.SQLException;
4 |
5 | /**
6 | * Wraps a {@link SQLException} into an unchecked exception.
7 | */
8 | public class UncheckedSqlException extends RuntimeException {
9 |
10 | public UncheckedSqlException(SQLException exception) {
11 | super(exception);
12 | }
13 |
14 | public UncheckedSqlException(String message, SQLException exception) {
15 | super(message, exception);
16 | }
17 |
18 | @Override
19 | public SQLException getCause() {
20 | return (SQLException) super.getCause();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/Update.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams;
2 |
3 | import java.sql.PreparedStatement;
4 |
5 | public interface Update extends ParameterProvider
19 | * Note that this class is not part of the public API of this library and as such, is
20 | * not required to stay compatible between versions.
21 | * @see ParameterProviderImpl#with(Object...)
22 | * @see ParameterProviderImpl#set(int, Object)
23 | */
24 | public final class SqlBindings {
25 |
26 | public class Bindings
4 | * {@code sql-streams} abstracts the {@link java.sql JDBC API} by providing tools to
5 | * easily handle set parameters of prepared statements and map database result sets to
6 | * classes.
7 | *
8 | * The heart of this library is the {@link be.bendem.sqlstreams.Sql} class which provides
9 | * static factories as well as the methods used to execute DML and DDL sql queries. These
10 | * methods generally come in two flavors: prepared and non prepared.
11 | *
12 | * The prepared methods return a {@code Prepared_____} instance implementing the {@link
13 | * be.bendem.sqlstreams.ParameterProvider} interface so as to allow you to set your query
14 | * parameters before executing your query. You'll most likely want to use the {@link
15 | * be.bendem.sqlstreams.ParameterProvider#with(java.lang.Object...)} method to set
16 | * parameters of the most common types.
17 | *
18 | * The non prepared methods are generally shortcuts for the prepared methods.
19 | *
20 | * You should make sure to always close instances you get from the library that
21 | * implement {@link java.lang.AutoCloseable} (applies to {@link java.util.stream.Stream}s
22 | * as well). This is generally best done using the {@code try}-with-resources construct
23 | * as such:
24 | * {@code try (Transaction tr = sql.transaction()) {
10 | * // Code in transaction
11 | * } // Automatic rollback
12 | * }
13 | */
14 | public interface Transaction extends Sql {
15 |
16 | enum IsolationLevel {
17 | NONE(Connection.TRANSACTION_NONE),
18 | READ_UNCOMMITTED(Connection.TRANSACTION_READ_UNCOMMITTED),
19 | READ_COMMITTED(Connection.TRANSACTION_READ_COMMITTED),
20 | REPEATABLE_READ(Connection.TRANSACTION_REPEATABLE_READ),
21 | SERIALIZABLE(Connection.TRANSACTION_SERIALIZABLE);
22 |
23 | public final int isolationLevel;
24 |
25 | IsolationLevel(int isolationLevel) {
26 | this.isolationLevel = isolationLevel;
27 | }
28 | }
29 |
30 | /**
31 | * Commits the current transaction.
32 | *
33 | * @return {@code this} for chaining
34 | * @see Connection#commit()
35 | */
36 | Transaction commit();
37 |
38 | /**
39 | * Rollbacks the current transaction.
40 | *
41 | * @return {@code this} for chaining
42 | * @see Connection#rollback()
43 | */
44 | Transaction rollback();
45 |
46 | /**
47 | * Gets the underlying {@link Connection} of this transaction.
48 | *
49 | * @return the underlying Connection object
50 | */
51 | Connection getConnection();
52 |
53 | /**
54 | * Closes the current transaction, rolling back any changes not committed.
55 | * {@code try (PreparedUpdate update = sql.update("update salaries set amount = amount * 2")) {
25 | * // use update here
26 | * }}
27 | *
28 | * Transactions
29 | * Transactions are handled using {@link be.bendem.sqlstreams.Sql#transaction()}. Unlike
30 | * the JDBC API, closing a transaction (and thus the underlying connection) is guaranteed
31 | * to rollback your current transaction. As such, transactional code is best written
32 | * using the {@code try}-with-resources construct as such:
33 | * {@code try(Transaction transaction = sql.transaction()) {
34 | * transaction.update("update salaries set amount = amount * 2");
35 | * transaction.commit();
36 | * } catch (UncheckedSqlException e) {
37 | * // Handle the exception
38 | * } // automatic rollback of uncommitted data}
39 | *
40 | * Mapping
41 | * This library offers a few facilities to map each row of a result set to a class.
42 | *
43 | * Auto-magic mapping
44 | * Magic mapping provides automatic mapping between sql types and java types for java
45 | * primitives (byte, short, int, long, boolean), their boxed equivalent, {@link
46 | * java.lang.String}, {@link java.sql.Date}, {@link java.sql.Time}, {@link
47 | * java.sql.Timestamp}, {@link java.time.LocalDate}, {@link java.time.LocalTime} and
48 | * {@link java.time.LocalDateTime}.
49 | *
50 | * Manual mapping
51 | * If you need a more complex mapping method, you can use {@link
52 | * be.bendem.sqlstreams.Query#map(be.bendem.sqlstreams.util.SqlFunction)} to map
53 | * each row of the result set using your own code.
54 | */
55 | package be.bendem.sqlstreams;
56 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/util/Closeable.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams.util;
2 |
3 | public interface Closeable extends AutoCloseable {
4 |
5 | @Override
6 | void close();
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/be/bendem/sqlstreams/util/DummyDataSource.java:
--------------------------------------------------------------------------------
1 | package be.bendem.sqlstreams.util;
2 |
3 | import javax.sql.DataSource;
4 | import java.io.PrintWriter;
5 | import java.sql.Connection;
6 | import java.sql.SQLException;
7 | import java.sql.SQLFeatureNotSupportedException;
8 | import java.util.logging.Logger;
9 |
10 | /**
11 | * A stupid {@link DataSource} implementation that throws {@link UnsupportedOperationException} for every method except
12 | * {@link #getConnection()} and {@link #getConnection(String, String)}.
13 | *
14 | * This {@code DataSource} is used by {@link be.bendem.sqlstreams.Sql#connect(Connection)}.
15 | */
16 | abstract class DummyDataSource implements DataSource {
17 |
18 | /**
19 | * Parameters passed to this method are ignored, this is equivalent to calling {@link #getConnection()} directly.
20 | */
21 | @Override
22 | public Connection getConnection(String username, String password) throws SQLException {
23 | return getConnection();
24 | }
25 |
26 | @Override
27 | public