Skip to content

Commit 5b85300

Browse files
committed
Added small batch. Updated version.
1 parent 6a1907b commit 5b85300

4 files changed

Lines changed: 258 additions & 14 deletions

File tree

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ SQL (C) Black Rook Software
33
by Matt Tropiano et al. (see AUTHORS.txt)
44

55

6+
Changed in 1.2.0
7+
----------------
8+
9+
- `Added` "Large" batch calls to separate from "small" batch calls - some Drivers do not implement "large" batches.
10+
11+
612
Changed in 1.1.1
713
----------------
814

src/main/java/com/blackrook/sql/SQL.java

Lines changed: 177 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,9 @@ public static SQLResult getUpdateResult(Connection connection, String query, Obj
368368
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
369369
* @throws UnsupportedOperationException if not implemented by the driver.
370370
* @since 1.1.0
371+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(Connection, String, Object[][])} for <code>long[]</code>.
371372
*/
372-
public static long[] getUpdateBatch(Connection connection, String query, Object[][] parameterList)
373+
public static int[] getUpdateBatch(Connection connection, String query, Object[][] parameterList)
373374
{
374375
return getUpdateBatch(connection, query, SQLCallable.DEFAULT_BATCH_SIZE, Arrays.asList(parameterList));
375376
}
@@ -384,8 +385,9 @@ public static long[] getUpdateBatch(Connection connection, String query, Object[
384385
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
385386
* @throws UnsupportedOperationException if not implemented by the driver.
386387
* @since 1.1.0
388+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(Connection, String, int, Object[][])} for <code>long[]</code>.
387389
*/
388-
public static long[] getUpdateBatch(Connection connection, String query, int granularity, Object[][] parameterList)
390+
public static int[] getUpdateBatch(Connection connection, String query, int granularity, Object[][] parameterList)
389391
{
390392
return getUpdateBatch(connection, query, granularity, Arrays.asList(parameterList));
391393
}
@@ -399,8 +401,9 @@ public static long[] getUpdateBatch(Connection connection, String query, int gra
399401
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
400402
* @throws UnsupportedOperationException if not implemented by the driver.
401403
* @since 1.1.0
404+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(Connection, String, int, Collection)} for <code>long[]</code>.
402405
*/
403-
public static long[] getUpdateBatch(Connection connection, String query, Collection<Object[]> parameterList)
406+
public static int[] getUpdateBatch(Connection connection, String query, Collection<Object[]> parameterList)
404407
{
405408
return getUpdateBatch(connection, query, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
406409
}
@@ -415,8 +418,9 @@ public static long[] getUpdateBatch(Connection connection, String query, Collect
415418
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
416419
* @throws UnsupportedOperationException if not implemented by the driver.
417420
* @since 1.1.0
421+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(Connection, String, int, Collection)} for <code>long[]</code>.
418422
*/
419-
public static long[] getUpdateBatch(Connection connection, String query, int granularity, Collection<Object[]> parameterList)
423+
public static int[] getUpdateBatch(Connection connection, String query, int granularity, Collection<Object[]> parameterList)
420424
{
421425
try (PreparedStatement statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS))
422426
{
@@ -428,6 +432,75 @@ public static long[] getUpdateBatch(Connection connection, String query, int gra
428432
}
429433
}
430434

435+
/**
436+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
437+
* @param connection the connection to create a prepared statement and execute from.
438+
* @param query the query statement to execute.
439+
* @param parameterList the list of parameter sets to pass to the query for each update.
440+
* @return the update result returned (usually number of rows affected and or generated ids).
441+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
442+
* @throws UnsupportedOperationException if not implemented by the driver.
443+
* @since 1.2.0
444+
*/
445+
public static long[] getUpdateLargeBatch(Connection connection, String query, Object[][] parameterList)
446+
{
447+
return getUpdateLargeBatch(connection, query, SQLCallable.DEFAULT_BATCH_SIZE, Arrays.asList(parameterList));
448+
}
449+
450+
/**
451+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
452+
* @param connection the connection to create a prepared statement and execute from.
453+
* @param query the query statement to execute.
454+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
455+
* @param parameterList the list of parameter sets to pass to the query for each update.
456+
* @return the update result returned (usually number of rows affected and or generated ids).
457+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
458+
* @throws UnsupportedOperationException if not implemented by the driver.
459+
* @since 1.2.0
460+
*/
461+
public static long[] getUpdateLargeBatch(Connection connection, String query, int granularity, Object[][] parameterList)
462+
{
463+
return getUpdateLargeBatch(connection, query, granularity, Arrays.asList(parameterList));
464+
}
465+
466+
/**
467+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
468+
* @param connection the connection to create a prepared statement and execute from.
469+
* @param query the query statement to execute.
470+
* @param parameterList the list of parameter sets to pass to the query for each update.
471+
* @return the update result returned (usually number of rows affected and or generated ids).
472+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
473+
* @throws UnsupportedOperationException if not implemented by the driver.
474+
* @since 1.2.0
475+
*/
476+
public static long[] getUpdateLargeBatch(Connection connection, String query, Collection<Object[]> parameterList)
477+
{
478+
return getUpdateLargeBatch(connection, query, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
479+
}
480+
481+
/**
482+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
483+
* @param connection the connection to create a prepared statement and execute from.
484+
* @param query the query statement to execute.
485+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
486+
* @param parameterList the list of parameter sets to pass to the query for each update.
487+
* @return the update result returned (usually number of rows affected and or generated ids).
488+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
489+
* @throws UnsupportedOperationException if not implemented by the driver.
490+
* @since 1.2.0
491+
*/
492+
public static long[] getUpdateLargeBatch(Connection connection, String query, int granularity, Collection<Object[]> parameterList)
493+
{
494+
try (PreparedStatement statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS))
495+
{
496+
return callLargeBatch(statement, granularity, parameterList);
497+
}
498+
catch (SQLException e)
499+
{
500+
throw new SQLRuntimeException(e);
501+
}
502+
}
503+
431504
/**
432505
* Performs an update query (INSERT, DELETE, UPDATE, or other commands that do not return rows)
433506
* and extracts each set of result data into a SQLResult.
@@ -684,8 +757,9 @@ public static SQLResult callStatement(PreparedStatement statement, boolean updat
684757
* @throws SQLException if a SQL exception occurs.
685758
* @throws UnsupportedOperationException if not implemented by the driver.
686759
* @since 1.1.0
760+
* @since 1.2.0, returns <code>int[]</code>. See {@link #callLargeBatch(PreparedStatement, Object[][])} for <code>long[]</code>.
687761
*/
688-
public static long[] callBatch(PreparedStatement statement, Object[][] parameterList) throws SQLException
762+
public static int[] callBatch(PreparedStatement statement, Object[][] parameterList) throws SQLException
689763
{
690764
return callBatch(statement, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
691765
}
@@ -700,8 +774,9 @@ public static long[] callBatch(PreparedStatement statement, Object[][] parameter
700774
* @throws SQLException if a SQL exception occurs.
701775
* @throws UnsupportedOperationException if not implemented by the driver.
702776
* @since 1.1.0
777+
* @since 1.2.0, returns <code>int[]</code>. See {@link #callLargeBatch(PreparedStatement, int, Object[][])} for <code>long[]</code>.
703778
*/
704-
public static long[] callBatch(PreparedStatement statement, int granularity, Object[][] parameterList) throws SQLException
779+
public static int[] callBatch(PreparedStatement statement, int granularity, Object[][] parameterList) throws SQLException
705780
{
706781
return callBatch(statement, granularity, Arrays.asList(parameterList));
707782
}
@@ -716,8 +791,9 @@ public static long[] callBatch(PreparedStatement statement, int granularity, Obj
716791
* @throws SQLException if a SQL exception occurs.
717792
* @throws UnsupportedOperationException if not implemented by the driver.
718793
* @since 1.1.0
794+
* @since 1.2.0, returns <code>int[]</code>. See {@link #callLargeBatch(PreparedStatement, Collection)} for <code>long[]</code>.
719795
*/
720-
public static long[] callBatch(PreparedStatement statement, Collection<Object[]> parameterList) throws SQLException
796+
public static int[] callBatch(PreparedStatement statement, Collection<Object[]> parameterList) throws SQLException
721797
{
722798
return callBatch(statement, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
723799
}
@@ -732,8 +808,101 @@ public static long[] callBatch(PreparedStatement statement, Collection<Object[]>
732808
* @throws SQLException if a SQL exception occurs.
733809
* @throws UnsupportedOperationException if not implemented by the driver.
734810
* @since 1.1.0
811+
* @since 1.2.0, returns <code>int[]</code>. See {@link #callLargeBatch(PreparedStatement, int, Collection)} for <code>long[]</code>.
812+
*/
813+
public static int[] callBatch(PreparedStatement statement, int granularity, Collection<Object[]> parameterList) throws SQLException
814+
{
815+
int[] out = new int[parameterList.size()];
816+
int cursor = 0;
817+
int batch = 0;
818+
819+
for (Object[] parameters : parameterList)
820+
{
821+
int n = 1;
822+
for (Object obj : parameters)
823+
statement.setObject(n++, obj);
824+
825+
statement.addBatch();
826+
batch++;
827+
828+
if (batch == granularity)
829+
{
830+
int[] execute = statement.executeBatch();
831+
System.arraycopy(execute, 0, out, cursor, execute.length);
832+
cursor += execute.length;
833+
batch = 0;
834+
}
835+
}
836+
837+
if (batch != 0)
838+
{
839+
int[] execute = statement.executeBatch();
840+
System.arraycopy(execute, 0, out, cursor, execute.length);
841+
}
842+
843+
return out;
844+
}
845+
846+
/**
847+
* Performs a series of update queries on a single statement on a connection and returns the batch result,
848+
* using a default batching amount ({@value SQLCallable#DEFAULT_BATCH_SIZE}).
849+
* @param statement the statement to execute.
850+
* @param parameterList the list of parameter sets to pass to the query for each update.
851+
* @return the amount of affected rows of each of the updates, each index corresponding to the index of the set of parameters used.
852+
* May also return {@link Statement#SUCCESS_NO_INFO} or {@link Statement#EXECUTE_FAILED} per update.
853+
* @throws SQLException if a SQL exception occurs.
854+
* @throws UnsupportedOperationException if not implemented by the driver.
855+
* @since 1.2.0
856+
*/
857+
public static long[] callLargeBatch(PreparedStatement statement, Object[][] parameterList) throws SQLException
858+
{
859+
return callLargeBatch(statement, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
860+
}
861+
862+
/**
863+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
864+
* @param statement the statement to execute.
865+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
866+
* @param parameterList the list of parameter sets to pass to the query for each update.
867+
* @return the amount of affected rows of each of the updates, each index corresponding to the index of the set of parameters used.
868+
* May also return {@link Statement#SUCCESS_NO_INFO} or {@link Statement#EXECUTE_FAILED} per update.
869+
* @throws SQLException if a SQL exception occurs.
870+
* @throws UnsupportedOperationException if not implemented by the driver.
871+
* @since 1.2.0
872+
*/
873+
public static long[] callLargeBatch(PreparedStatement statement, int granularity, Object[][] parameterList) throws SQLException
874+
{
875+
return callLargeBatch(statement, granularity, Arrays.asList(parameterList));
876+
}
877+
878+
/**
879+
* Performs a series of update queries on a single statement on a connection and returns the batch result,
880+
* using a default batching amount ({@value SQLCallable#DEFAULT_BATCH_SIZE}).
881+
* @param statement the statement to execute.
882+
* @param parameterList the list of parameter sets to pass to the query for each update.
883+
* @return the amount of affected rows of each of the updates, each index corresponding to the index of the set of parameters used.
884+
* May also return {@link Statement#SUCCESS_NO_INFO} or {@link Statement#EXECUTE_FAILED} per update.
885+
* @throws SQLException if a SQL exception occurs.
886+
* @throws UnsupportedOperationException if not implemented by the driver.
887+
* @since 1.2.0
888+
*/
889+
public static long[] callLargeBatch(PreparedStatement statement, Collection<Object[]> parameterList) throws SQLException
890+
{
891+
return callLargeBatch(statement, SQLCallable.DEFAULT_BATCH_SIZE, parameterList);
892+
}
893+
894+
/**
895+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
896+
* @param statement the statement to execute.
897+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
898+
* @param parameterList the list of parameter sets to pass to the query for each update.
899+
* @return the amount of affected rows of each of the updates, each index corresponding to the index of the set of parameters used.
900+
* May also return {@link Statement#SUCCESS_NO_INFO} or {@link Statement#EXECUTE_FAILED} per update.
901+
* @throws SQLException if a SQL exception occurs.
902+
* @throws UnsupportedOperationException if not implemented by the driver.
903+
* @since 1.2.0
735904
*/
736-
public static long[] callBatch(PreparedStatement statement, int granularity, Collection<Object[]> parameterList) throws SQLException
905+
public static long[] callLargeBatch(PreparedStatement statement, int granularity, Collection<Object[]> parameterList) throws SQLException
737906
{
738907
long[] out = new long[parameterList.size()];
739908
int cursor = 0;

src/main/java/com/blackrook/sql/SQLCallable.java

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ public interface SQLCallable
234234
* @return the update result returned (usually number of rows affected and or generated ids).
235235
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
236236
* @since 1.1.0
237+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(String, Object[][])} for <code>long[]</code>.
237238
*/
238-
default long[] getUpdateBatch(String query, Object[][] parameterList)
239+
default int[] getUpdateBatch(String query, Object[][] parameterList)
239240
{
240241
return getUpdateBatch(query, DEFAULT_BATCH_SIZE, Arrays.asList(parameterList));
241242
}
@@ -248,8 +249,9 @@ default long[] getUpdateBatch(String query, Object[][] parameterList)
248249
* @return the update result returned (usually number of rows affected and or generated ids).
249250
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
250251
* @since 1.1.0
252+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(String, int, Object[][])} for <code>long[]</code>.
251253
*/
252-
default long[] getUpdateBatch(String query, int granularity, Object[][] parameterList)
254+
default int[] getUpdateBatch(String query, int granularity, Object[][] parameterList)
253255
{
254256
return getUpdateBatch(query, granularity, Arrays.asList(parameterList));
255257
}
@@ -261,8 +263,9 @@ default long[] getUpdateBatch(String query, int granularity, Object[][] paramete
261263
* @return the update result returned (usually number of rows affected and or generated ids).
262264
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
263265
* @since 1.1.0
266+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(String, Collection)} for <code>long[]</code>.
264267
*/
265-
default long[] getUpdateBatch(String query, Collection<Object[]> parameterList)
268+
default int[] getUpdateBatch(String query, Collection<Object[]> parameterList)
266269
{
267270
return getUpdateBatch(query, DEFAULT_BATCH_SIZE, parameterList);
268271
}
@@ -275,8 +278,60 @@ default long[] getUpdateBatch(String query, Collection<Object[]> parameterList)
275278
* @return the update result returned (usually number of rows affected and or generated ids).
276279
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
277280
* @since 1.1.0
281+
* @since 1.2.0, returns <code>int[]</code>. See {@link #getUpdateLargeBatch(String, int, Collection)} for <code>long[]</code>.
278282
*/
279-
long[] getUpdateBatch(String query, int granularity, Collection<Object[]> parameterList);
283+
int[] getUpdateBatch(String query, int granularity, Collection<Object[]> parameterList);
284+
285+
/**
286+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
287+
* @param query the query statement to execute.
288+
* @param parameterList the list of parameter sets to pass to the query for each update.
289+
* @return the update result returned (usually number of rows affected and or generated ids).
290+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
291+
* @since 1.2.0
292+
*/
293+
default long[] getUpdateLargeBatch(String query, Object[][] parameterList)
294+
{
295+
return getUpdateLargeBatch(query, DEFAULT_BATCH_SIZE, Arrays.asList(parameterList));
296+
}
297+
298+
/**
299+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
300+
* @param query the query statement to execute.
301+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
302+
* @param parameterList the list of parameter sets to pass to the query for each update.
303+
* @return the update result returned (usually number of rows affected and or generated ids).
304+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
305+
* @since 1.2.0
306+
*/
307+
default long[] getUpdateLargeBatch(String query, int granularity, Object[][] parameterList)
308+
{
309+
return getUpdateLargeBatch(query, granularity, Arrays.asList(parameterList));
310+
}
311+
312+
/**
313+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
314+
* @param query the query statement to execute.
315+
* @param parameterList the list of parameter sets to pass to the query for each update.
316+
* @return the update result returned (usually number of rows affected and or generated ids).
317+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
318+
* @since 1.2.0
319+
*/
320+
default long[] getUpdateLargeBatch(String query, Collection<Object[]> parameterList)
321+
{
322+
return getUpdateLargeBatch(query, DEFAULT_BATCH_SIZE, parameterList);
323+
}
324+
325+
/**
326+
* Performs a series of update queries on a single statement on a connection and returns the batch result.
327+
* @param query the query statement to execute.
328+
* @param granularity the amount of statements to execute at a time. If 0 or less, no granularity.
329+
* @param parameterList the list of parameter sets to pass to the query for each update.
330+
* @return the update result returned (usually number of rows affected and or generated ids).
331+
* @throws SQLRuntimeException if the query cannot be executed or the query causes an error.
332+
* @since 1.2.0
333+
*/
334+
long[] getUpdateLargeBatch(String query, int granularity, Collection<Object[]> parameterList);
280335

281336
/**
282337
* Performs an update query (INSERT, DELETE, UPDATE, or other commands that do not return rows)

0 commit comments

Comments
 (0)