Skip to content

Commit 6ff20c3

Browse files
authored
Merge pull request #375 from youngsofun/ts
fix: refactor and fix set null Object in PrepareStatement.
2 parents 4c634a3 + 1d4ede2 commit 6ff20c3

File tree

6 files changed

+192
-177
lines changed

6 files changed

+192
-177
lines changed

databend-jdbc/src/main/java/com/databend/jdbc/DatabendPreparedStatement.java

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ private static String formatDoubleLiteral(double x) {
104104
}
105105

106106
private static String formatBigDecimalLiteral(BigDecimal x) {
107-
if (x == null) {
108-
return "null";
109-
}
110-
111107
return x.toString();
112108
}
113109

@@ -293,14 +289,18 @@ private static boolean isBatchInsert(String sql) {
293289
return matcher.find() && !sql.contains(DATABEND_KEYWORDS_SELECT);
294290
}
295291

296-
private void setValueSimple(int index, String value) {
292+
private void setValueStringNoQuote(int index, String value) {
297293
batchInsertUtils.setPlaceHolderValue(index, value, value);
298294
}
299295

300296
private void setValueString(int index, String value) {
301297
batchInsertUtils.setPlaceHolderValue(index, String.format("'%s'", value), value);
302298
}
303299

300+
private void setValueNull(int index) {
301+
setValue(index, "null", "\\N");
302+
}
303+
304304
private void setValue(int index, String value, String csvValue) {
305305
batchInsertUtils.setPlaceHolderValue(index, value, csvValue);
306306
}
@@ -309,70 +309,78 @@ private void setValue(int index, String value, String csvValue) {
309309
public void setNull(int i, int i1)
310310
throws SQLException {
311311
checkOpen();
312-
setValue(i, "null", "\\N");
312+
setValueNull(i);
313313
}
314314

315315
@Override
316316
public void setBoolean(int i, boolean b)
317317
throws SQLException {
318318
checkOpen();
319-
setValueSimple(i, formatBooleanLiteral(b));
319+
setValueStringNoQuote(i, formatBooleanLiteral(b));
320320
}
321321

322322
@Override
323323
public void setByte(int i, byte b)
324324
throws SQLException {
325325
checkOpen();
326-
setValueSimple(i, formatByteLiteral(b));
326+
setValueStringNoQuote(i, formatByteLiteral(b));
327327
}
328328

329329
@Override
330330
public void setShort(int i, short i1)
331331
throws SQLException {
332332
checkOpen();
333-
setValueSimple(i, formatShortLiteral(i1));
333+
setValueStringNoQuote(i, formatShortLiteral(i1));
334334
}
335335

336336
@Override
337337
public void setInt(int i, int i1)
338338
throws SQLException {
339339
checkOpen();
340-
setValueSimple(i, formatIntLiteral(i1));
340+
setValueStringNoQuote(i, formatIntLiteral(i1));
341341
}
342342

343343
@Override
344344
public void setLong(int i, long l)
345345
throws SQLException {
346346
checkOpen();
347-
setValueSimple(i, formatLongLiteral(l));
347+
setValueStringNoQuote(i, formatLongLiteral(l));
348348
}
349349

350350
@Override
351351
public void setFloat(int i, float v)
352352
throws SQLException {
353353
checkOpen();
354-
setValueSimple(i, formatFloatLiteral(v));
354+
setValueStringNoQuote(i, formatFloatLiteral(v));
355355
}
356356

357357
@Override
358358
public void setDouble(int i, double v)
359359
throws SQLException {
360360
checkOpen();
361-
setValueSimple(i, formatDoubleLiteral(v));
361+
setValueStringNoQuote(i, formatDoubleLiteral(v));
362362
}
363363

364364
@Override
365365
public void setBigDecimal(int i, BigDecimal v)
366366
throws SQLException {
367367
checkOpen();
368-
setValueSimple(i, formatBigDecimalLiteral(v));
368+
if (v == null) {
369+
setValueNull(i);
370+
} else {
371+
setValueStringNoQuote(i, formatBigDecimalLiteral(v));
372+
}
369373
}
370374

371375
@Override
372376
public void setString(int i, String s)
373377
throws SQLException {
374378
checkOpen();
375379
String quoted = s;
380+
if (s == null) {
381+
setValueNull(i);
382+
return;
383+
}
376384
if (s.contains("'")) {
377385
quoted = s.replace("'", "\\'");
378386
}
@@ -385,18 +393,17 @@ public void setString(int i, String s)
385393
public void setBytes(int i, byte[] v)
386394
throws SQLException {
387395
checkOpen();
388-
setValueSimple(i, formatBytesLiteral(v));
396+
setValueStringNoQuote(i, formatBytesLiteral(v));
389397
}
390398

391399
@Override
392400
public void setDate(int i, Date date)
393401
throws SQLException {
394402
checkOpen();
395403
if (date == null) {
396-
setValueSimple(i, null);
404+
setValueNull(i);
397405
} else {
398-
String s = date.toString();
399-
setValue(i, String.format("'%s'", s), s);
406+
setValueString(i, date.toString());
400407
}
401408
}
402409

@@ -405,7 +412,7 @@ public void setTime(int i, Time v)
405412
throws SQLException {
406413
checkOpen();
407414
if (v == null) {
408-
setValueSimple(i, null);
415+
setValueNull(i);
409416
} else {
410417
setValueString(i, v.toString());
411418
}
@@ -416,7 +423,7 @@ public void setTimestamp(int i, Timestamp v)
416423
throws SQLException {
417424
checkOpen();
418425
if (v == null) {
419-
setValueSimple(i, null);
426+
setValueNull(i);
420427
} else {
421428
setValueString(i, v.toInstant().toString());
422429
}
@@ -452,7 +459,7 @@ public void setObject(int parameterIndex, Object x, int targetSqlType)
452459
throws SQLException {
453460
checkOpen();
454461
if (x == null) {
455-
setNull(parameterIndex, Types.NULL);
462+
setValueNull(parameterIndex);
456463
return;
457464
}
458465
switch (targetSqlType) {
@@ -513,10 +520,8 @@ public void setObject(int parameterIndex, Object x, int targetSqlType)
513520
setString(parameterIndex, toTimeWithTimeZoneLiteral(x));
514521
return;
515522
case Types.TIMESTAMP:
516-
setString(parameterIndex, toTimestampLiteral(x));
517-
return;
518523
case Types.TIMESTAMP_WITH_TIMEZONE:
519-
setString(parameterIndex, toTimestampWithTimeZoneLiteral(x));
524+
setString(parameterIndex, toTimestampLiteral(x));
520525
return;
521526
case Types.OTHER:
522527
case Types.JAVA_OBJECT:
@@ -534,7 +539,7 @@ public void setObject(int parameterIndex, Object x)
534539
throws SQLException {
535540
checkOpen();
536541
if (x == null) {
537-
setNull(parameterIndex, Types.NULL);
542+
setValueNull(parameterIndex);
538543
} else if (x instanceof Boolean) {
539544
setBoolean(parameterIndex, (Boolean) x);
540545
} else if (x instanceof Byte) {
@@ -654,24 +659,11 @@ public void setRef(int i, Ref ref)
654659
throw new SQLFeatureNotSupportedException("PreparedStatement", "setRef");
655660
}
656661

657-
@Override
658-
public void setBlob(int i, Blob x)
659-
throws SQLException {
660-
if (x != null) {
661-
setBinaryStream(i, x.getBinaryStream());
662-
} else {
663-
setNull(i, Types.BLOB);
664-
}
665-
}
666662

667663
@Override
668664
public void setClob(int i, Clob x)
669665
throws SQLException {
670-
if (x != null) {
671-
setCharacterStream(i, x.getCharacterStream());
672-
} else {
673-
setNull(i, Types.CLOB);
674-
}
666+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob");
675667
}
676668

677669
@Override
@@ -689,13 +681,13 @@ public ResultSetMetaData getMetaData()
689681
@Override
690682
public void setDate(int i, Date date, Calendar calendar)
691683
throws SQLException {
692-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setDate");
684+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setDate(int, Date, Calendar)");
693685
}
694686

695687
@Override
696688
public void setTime(int i, Time time, Calendar calendar)
697689
throws SQLException {
698-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setTime");
690+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setTime(int, Time, Calendar)");
699691
}
700692

701693
@Override
@@ -708,7 +700,7 @@ public void setTimestamp(int i, Timestamp timestamp, Calendar calendar)
708700
@Override
709701
public void setNull(int i, int i1, String s)
710702
throws SQLException {
711-
setNull(i, i1);
703+
setValueNull(i);
712704
}
713705

714706
@Override
@@ -752,14 +744,10 @@ public void setNClob(int i, NClob nClob)
752744
@Override
753745
public void setClob(int i, Reader reader, long l)
754746
throws SQLException {
755-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob");
747+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob(int, Reader, long)");
756748
}
757749

758-
@Override
759-
public void setBlob(int i, InputStream inputStream, long l)
760-
throws SQLException {
761-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setBlob");
762-
}
750+
763751

764752
@Override
765753
public void setNClob(int i, Reader reader, long l)
@@ -776,7 +764,7 @@ public void setSQLXML(int i, SQLXML sqlxml)
776764
@Override
777765
public void setObject(int i, Object o, int i1, int i2)
778766
throws SQLException {
779-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setObject");
767+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setObject(int, Object, int, int)");
780768
}
781769

782770
@Override
@@ -807,6 +795,10 @@ public void setAsciiStream(int i, InputStream inputStream)
807795
public void setBinaryStream(int i, InputStream inputStream)
808796
throws SQLException {
809797
checkOpen();
798+
if (inputStream == null) {
799+
setValueNull(i);
800+
return;
801+
}
810802
try {
811803
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
812804
int nRead;
@@ -818,10 +810,10 @@ public void setBinaryStream(int i, InputStream inputStream)
818810
byte[] bytes = buffer.toByteArray();
819811
if (BASE64_STR.equalsIgnoreCase(connection().binaryFormat())) {
820812
String base64String = bytesToBase64(bytes);
821-
setValueSimple(i, base64String);
813+
setValueStringNoQuote(i, base64String);
822814
} else {
823815
String hexString = bytesToHex(bytes);
824-
setValueSimple(i, hexString);
816+
setValueStringNoQuote(i, hexString);
825817
}
826818
} catch (IOException e) {
827819
throw new SQLException("Error reading InputStream", e);
@@ -855,15 +847,28 @@ public void setNCharacterStream(int i, Reader reader)
855847
@Override
856848
public void setClob(int i, Reader reader)
857849
throws SQLException {
858-
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob");
850+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setClob(int, Reader)");
851+
}
852+
853+
public void setBlob(int i, Blob x)
854+
throws SQLException {
855+
// never get null
856+
setBinaryStream(i, x.getBinaryStream());
859857
}
860858

861859
@Override
862860
public void setBlob(int i, InputStream inputStream)
863861
throws SQLException {
862+
// never get null
864863
setBinaryStream(i, inputStream);
865864
}
866865

866+
@Override
867+
public void setBlob(int i, InputStream inputStream, long l)
868+
throws SQLException {
869+
throw new SQLFeatureNotSupportedException("PreparedStatement", "setBlob(int, InputStream, long)");
870+
}
871+
867872
@Override
868873
public void setNClob(int i, Reader reader)
869874
throws SQLException {
@@ -932,16 +937,6 @@ private String toTimestampLiteral(Object value)
932937
throw invalidConversion(value, "timestamp");
933938
}
934939

935-
private String toTimestampWithTimeZoneLiteral(Object value)
936-
throws SQLException {
937-
if (value instanceof String) {
938-
return (String) value;
939-
} else if (value instanceof OffsetDateTime) {
940-
return OFFSET_TIME_FORMATTER.format((OffsetDateTime) value);
941-
}
942-
throw invalidConversion(value, "timestamp with time zone");
943-
}
944-
945940
private String toTimeWithTimeZoneLiteral(Object value)
946941
throws SQLException {
947942
if (value instanceof OffsetTime) {
@@ -953,5 +948,4 @@ private String toTimeWithTimeZoneLiteral(Object value)
953948
}
954949
throw invalidConversion(value, "time with time zone");
955950
}
956-
957951
}

databend-jdbc/src/main/java/com/databend/jdbc/parser/BatchInsertUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ public String[] getValuesCSV() {
7878
}
7979
String[] values = new String[placeHolderEntriesCSV.lastKey() + 1];
8080
for (Map.Entry<Integer, String> elem : placeHolderEntriesCSV.entrySet()) {
81-
values[elem.getKey()] = elem.getValue();
81+
String value = elem.getValue();
82+
if (value == null) {
83+
value = "\\N";
84+
}
85+
values[elem.getKey()] = value;
8286
}
8387
return values;
8488
}

0 commit comments

Comments
 (0)