@@ -47,7 +47,7 @@ public class DuckDBPreparedStatement implements PreparedStatement {
47
47
volatile boolean closeOnCompletion = false ;
48
48
49
49
private DuckDBResultSet selectResult = null ;
50
- private int updateResult = 0 ;
50
+ private long updateResult = 0 ;
51
51
52
52
private boolean returnsChangedRows = false ;
53
53
private boolean returnsNothing = false ;
@@ -188,7 +188,7 @@ private boolean execute(boolean startTransaction) throws SQLException {
188
188
189
189
if (returnsChangedRows ) {
190
190
if (selectResult .next ()) {
191
- updateResult = selectResult .getInt (1 );
191
+ updateResult = selectResult .getLong (1 );
192
192
}
193
193
selectResult .close ();
194
194
}
@@ -208,6 +208,12 @@ public ResultSet executeQuery() throws SQLException {
208
208
209
209
@ Override
210
210
public int executeUpdate () throws SQLException {
211
+ long res = executeLargeUpdate ();
212
+ return intFromLong (res );
213
+ }
214
+
215
+ @ Override
216
+ public long executeLargeUpdate () throws SQLException {
211
217
requireNonBatch ();
212
218
execute ();
213
219
if (!(returnsChangedRows || returnsNothing )) {
@@ -232,8 +238,14 @@ public ResultSet executeQuery(String sql) throws SQLException {
232
238
233
239
@ Override
234
240
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 {
235
247
prepare (sql );
236
- return executeUpdate ();
248
+ return executeLargeUpdate ();
237
249
}
238
250
239
251
@ Override
@@ -377,12 +389,22 @@ public void setMaxFieldSize(int max) throws SQLException {
377
389
378
390
@ Override
379
391
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 {
380
402
checkOpen ();
381
403
return 0 ;
382
404
}
383
405
384
406
@ Override
385
- public void setMaxRows ( int max ) throws SQLException {
407
+ public void setLargeMaxRows ( long max ) throws SQLException {
386
408
checkOpen ();
387
409
}
388
410
@@ -450,12 +472,14 @@ public ResultSet getResultSet() throws SQLException {
450
472
return to_return ;
451
473
}
452
474
453
- private Integer getUpdateCountInternal () throws SQLException {
475
+ private long getUpdateCountInternal () throws SQLException {
454
476
if (isClosed ()) {
455
477
throw new SQLException ("Statement was closed" );
456
478
}
457
479
if (stmtRef == null ) {
458
- throw new SQLException ("Prepare something first" );
480
+ // It is not required by JDBC spec to return anything in this case,
481
+ // but clients can call this method before preparing/executing the query
482
+ return -1 ;
459
483
}
460
484
461
485
if (returnsResultSet || returnsNothing || selectResult .isFinished ()) {
@@ -465,11 +489,17 @@ private Integer getUpdateCountInternal() throws SQLException {
465
489
}
466
490
467
491
@ Override
468
- public int getUpdateCount () throws SQLException {
492
+ public long getLargeUpdateCount () throws SQLException {
469
493
// getUpdateCount can only be called once per result
470
- int to_return = getUpdateCountInternal ();
494
+ long res = getUpdateCountInternal ();
471
495
updateResult = -1 ;
472
- return to_return ;
496
+ return res ;
497
+ }
498
+
499
+ @ Override
500
+ public int getUpdateCount () throws SQLException {
501
+ long res = getLargeUpdateCount ();
502
+ return intFromLong (res );
473
503
}
474
504
475
505
@ Override
@@ -534,6 +564,12 @@ public void clearBatch() throws SQLException {
534
564
535
565
@ Override
536
566
public int [] executeBatch () throws SQLException {
567
+ long [] res = executeLargeBatch ();
568
+ return intArrayFromLong (res );
569
+ }
570
+
571
+ @ Override
572
+ public long [] executeLargeBatch () throws SQLException {
537
573
checkOpen ();
538
574
try {
539
575
if (this .isPreparedStatement ) {
@@ -546,26 +582,26 @@ public int[] executeBatch() throws SQLException {
546
582
}
547
583
}
548
584
549
- private int [] executeBatchedPreparedStatement () throws SQLException {
550
- int [] updateCounts = new int [this .batchedParams .size ()];
585
+ private long [] executeBatchedPreparedStatement () throws SQLException {
586
+ long [] updateCounts = new long [this .batchedParams .size ()];
551
587
552
588
startTransaction ();
553
589
for (int i = 0 ; i < this .batchedParams .size (); i ++) {
554
590
params = this .batchedParams .get (i );
555
591
execute (false );
556
- updateCounts [i ] = getUpdateCount ();
592
+ updateCounts [i ] = getUpdateCountInternal ();
557
593
}
558
594
return updateCounts ;
559
595
}
560
596
561
- private int [] executeBatchedStatements () throws SQLException {
562
- int [] updateCounts = new int [this .batchedStatements .size ()];
597
+ private long [] executeBatchedStatements () throws SQLException {
598
+ long [] updateCounts = new long [this .batchedStatements .size ()];
563
599
564
600
startTransaction ();
565
601
for (int i = 0 ; i < this .batchedStatements .size (); i ++) {
566
602
prepare (this .batchedStatements .get (i ));
567
603
execute (false );
568
- updateCounts [i ] = getUpdateCount ();
604
+ updateCounts [i ] = getUpdateCountInternal ();
569
605
}
570
606
return updateCounts ;
571
607
}
@@ -590,22 +626,40 @@ public ResultSet getGeneratedKeys() throws SQLException {
590
626
591
627
@ Override
592
628
public int executeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
629
+ long res = executeLargeUpdate (sql , autoGeneratedKeys );
630
+ return intFromLong (res );
631
+ }
632
+
633
+ @ Override
634
+ public long executeLargeUpdate (String sql , int autoGeneratedKeys ) throws SQLException {
593
635
if (NO_GENERATED_KEYS == autoGeneratedKeys ) {
594
- return executeUpdate (sql );
636
+ return executeLargeUpdate (sql );
595
637
}
596
638
throw new SQLFeatureNotSupportedException ("executeUpdate(String sql, int autoGeneratedKeys)" );
597
639
}
598
640
599
641
@ Override
600
642
public int executeUpdate (String sql , int [] columnIndexes ) throws SQLException {
643
+ long res = executeLargeUpdate (sql , columnIndexes );
644
+ return intFromLong (res );
645
+ }
646
+
647
+ @ Override
648
+ public long executeLargeUpdate (String sql , int [] columnIndexes ) throws SQLException {
601
649
if (columnIndexes == null || columnIndexes .length == 0 ) {
602
- return executeUpdate (sql );
650
+ return executeLargeUpdate (sql );
603
651
}
604
652
throw new SQLFeatureNotSupportedException ("executeUpdate(String sql, int[] columnIndexes)" );
605
653
}
606
654
607
655
@ Override
608
656
public int executeUpdate (String sql , String [] columnNames ) throws SQLException {
657
+ long res = executeLargeUpdate (sql , columnNames );
658
+ return intFromLong (res );
659
+ }
660
+
661
+ @ Override
662
+ public long executeLargeUpdate (String sql , String [] columnNames ) throws SQLException {
609
663
if (columnNames == null || columnNames .length == 0 ) {
610
664
return executeUpdate (sql );
611
665
}
@@ -1085,4 +1139,20 @@ private void setCharacterReaderInternal(int parameterIndex, Reader reader, long
1085
1139
String str = readToString (wrappedReader );
1086
1140
setObject (parameterIndex , str );
1087
1141
}
1142
+
1143
+ private int intFromLong (long val ) {
1144
+ if (val <= Integer .MAX_VALUE ) {
1145
+ return (int ) val ;
1146
+ } else {
1147
+ return Integer .MAX_VALUE ;
1148
+ }
1149
+ }
1150
+
1151
+ private int [] intArrayFromLong (long [] arr ) {
1152
+ int [] res = new int [arr .length ];
1153
+ for (int i = 0 ; i < arr .length ; i ++) {
1154
+ res [i ] = intFromLong (arr [i ]);
1155
+ }
1156
+ return res ;
1157
+ }
1088
1158
}
0 commit comments