Skip to content

Commit 0db6b22

Browse files
committed
More prepared statement enhancements
This change implements a group of `Large` methods, like `executeLargeUpdate`, that were added in Java 8 (JDBC 4.2). Testing: existing prepared statement tests are moved to a separate file and updated to cover all `Large` methods. Fixes: duckdb#205
1 parent 9118691 commit 0db6b22

File tree

3 files changed

+416
-317
lines changed

3 files changed

+416
-317
lines changed

src/main/java/org/duckdb/DuckDBPreparedStatement.java

+84-16
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class DuckDBPreparedStatement implements PreparedStatement {
4747
volatile boolean closeOnCompletion = false;
4848

4949
private DuckDBResultSet selectResult = null;
50-
private int updateResult = 0;
50+
private long updateResult = 0;
5151

5252
private boolean returnsChangedRows = false;
5353
private boolean returnsNothing = false;
@@ -188,7 +188,7 @@ private boolean execute(boolean startTransaction) throws SQLException {
188188

189189
if (returnsChangedRows) {
190190
if (selectResult.next()) {
191-
updateResult = selectResult.getInt(1);
191+
updateResult = selectResult.getLong(1);
192192
}
193193
selectResult.close();
194194
}
@@ -208,6 +208,12 @@ public ResultSet executeQuery() throws SQLException {
208208

209209
@Override
210210
public int executeUpdate() throws SQLException {
211+
long res = executeLargeUpdate();
212+
return intFromLong(res);
213+
}
214+
215+
@Override
216+
public long executeLargeUpdate() throws SQLException {
211217
requireNonBatch();
212218
execute();
213219
if (!(returnsChangedRows || returnsNothing)) {
@@ -232,8 +238,14 @@ public ResultSet executeQuery(String sql) throws SQLException {
232238

233239
@Override
234240
public int executeUpdate(String sql) throws SQLException {
241+
long res = executeLargeUpdate(sql);
242+
return intFromLong(res);
243+
}
244+
245+
@Override
246+
public long executeLargeUpdate(String sql) throws SQLException {
235247
prepare(sql);
236-
return executeUpdate();
248+
return executeLargeUpdate();
237249
}
238250

239251
@Override
@@ -377,12 +389,22 @@ public void setMaxFieldSize(int max) throws SQLException {
377389

378390
@Override
379391
public int getMaxRows() throws SQLException {
392+
return (int) getLargeMaxRows();
393+
}
394+
395+
@Override
396+
public void setMaxRows(int max) throws SQLException {
397+
setLargeMaxRows(max);
398+
}
399+
400+
@Override
401+
public long getLargeMaxRows() throws SQLException {
380402
checkOpen();
381403
return 0;
382404
}
383405

384406
@Override
385-
public void setMaxRows(int max) throws SQLException {
407+
public void setLargeMaxRows(long max) throws SQLException {
386408
checkOpen();
387409
}
388410

@@ -450,7 +472,7 @@ public ResultSet getResultSet() throws SQLException {
450472
return to_return;
451473
}
452474

453-
private Integer getUpdateCountInternal() throws SQLException {
475+
private long getUpdateCountInternal() throws SQLException {
454476
if (isClosed()) {
455477
throw new SQLException("Statement was closed");
456478
}
@@ -465,11 +487,17 @@ private Integer getUpdateCountInternal() throws SQLException {
465487
}
466488

467489
@Override
468-
public int getUpdateCount() throws SQLException {
490+
public long getLargeUpdateCount() throws SQLException {
469491
// getUpdateCount can only be called once per result
470-
int to_return = getUpdateCountInternal();
492+
long res = getUpdateCountInternal();
471493
updateResult = -1;
472-
return to_return;
494+
return res;
495+
}
496+
497+
@Override
498+
public int getUpdateCount() throws SQLException {
499+
long res = getLargeUpdateCount();
500+
return intFromLong(res);
473501
}
474502

475503
@Override
@@ -534,6 +562,12 @@ public void clearBatch() throws SQLException {
534562

535563
@Override
536564
public int[] executeBatch() throws SQLException {
565+
long[] res = executeLargeBatch();
566+
return intArrayFromLong(res);
567+
}
568+
569+
@Override
570+
public long[] executeLargeBatch() throws SQLException {
537571
checkOpen();
538572
try {
539573
if (this.isPreparedStatement) {
@@ -546,26 +580,26 @@ public int[] executeBatch() throws SQLException {
546580
}
547581
}
548582

549-
private int[] executeBatchedPreparedStatement() throws SQLException {
550-
int[] updateCounts = new int[this.batchedParams.size()];
583+
private long[] executeBatchedPreparedStatement() throws SQLException {
584+
long[] updateCounts = new long[this.batchedParams.size()];
551585

552586
startTransaction();
553587
for (int i = 0; i < this.batchedParams.size(); i++) {
554588
params = this.batchedParams.get(i);
555589
execute(false);
556-
updateCounts[i] = getUpdateCount();
590+
updateCounts[i] = getUpdateCountInternal();
557591
}
558592
return updateCounts;
559593
}
560594

561-
private int[] executeBatchedStatements() throws SQLException {
562-
int[] updateCounts = new int[this.batchedStatements.size()];
595+
private long[] executeBatchedStatements() throws SQLException {
596+
long[] updateCounts = new long[this.batchedStatements.size()];
563597

564598
startTransaction();
565599
for (int i = 0; i < this.batchedStatements.size(); i++) {
566600
prepare(this.batchedStatements.get(i));
567601
execute(false);
568-
updateCounts[i] = getUpdateCount();
602+
updateCounts[i] = getUpdateCountInternal();
569603
}
570604
return updateCounts;
571605
}
@@ -590,22 +624,40 @@ public ResultSet getGeneratedKeys() throws SQLException {
590624

591625
@Override
592626
public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
627+
long res = executeLargeUpdate(sql, autoGeneratedKeys);
628+
return intFromLong(res);
629+
}
630+
631+
@Override
632+
public long executeLargeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
593633
if (NO_GENERATED_KEYS == autoGeneratedKeys) {
594-
return executeUpdate(sql);
634+
return executeLargeUpdate(sql);
595635
}
596636
throw new SQLFeatureNotSupportedException("executeUpdate(String sql, int autoGeneratedKeys)");
597637
}
598638

599639
@Override
600640
public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
641+
long res = executeLargeUpdate(sql, columnIndexes);
642+
return intFromLong(res);
643+
}
644+
645+
@Override
646+
public long executeLargeUpdate(String sql, int[] columnIndexes) throws SQLException {
601647
if (columnIndexes == null || columnIndexes.length == 0) {
602-
return executeUpdate(sql);
648+
return executeLargeUpdate(sql);
603649
}
604650
throw new SQLFeatureNotSupportedException("executeUpdate(String sql, int[] columnIndexes)");
605651
}
606652

607653
@Override
608654
public int executeUpdate(String sql, String[] columnNames) throws SQLException {
655+
long res = executeLargeUpdate(sql, columnNames);
656+
return intFromLong(res);
657+
}
658+
659+
@Override
660+
public long executeLargeUpdate(String sql, String[] columnNames) throws SQLException {
609661
if (columnNames == null || columnNames.length == 0) {
610662
return executeUpdate(sql);
611663
}
@@ -1085,4 +1137,20 @@ private void setCharacterReaderInternal(int parameterIndex, Reader reader, long
10851137
String str = readToString(wrappedReader);
10861138
setObject(parameterIndex, str);
10871139
}
1140+
1141+
private int intFromLong(long val) {
1142+
if (val <= Integer.MAX_VALUE) {
1143+
return (int) val;
1144+
} else {
1145+
return Integer.MAX_VALUE;
1146+
}
1147+
}
1148+
1149+
private int[] intArrayFromLong(long[] arr) {
1150+
int[] res = new int[arr.length];
1151+
for (int i = 0; i < arr.length; i++) {
1152+
res[i] = intFromLong(arr[i]);
1153+
}
1154+
return res;
1155+
}
10881156
}

0 commit comments

Comments
 (0)