Skip to content

Commit 50cf58f

Browse files
committed
Added setString implementation for all YDB types
1 parent 8075e38 commit 50cf58f

File tree

2 files changed

+91
-7
lines changed

2 files changed

+91
-7
lines changed

jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ private static byte castAsByte(PrimitiveType type, Object x) throws SQLException
250250
return ((Long) x).byteValue();
251251
} else if (x instanceof Boolean) {
252252
return (byte) (((Boolean) x) ? 1 : 0);
253+
} else if (x instanceof String) {
254+
try {
255+
return Byte.parseByte((String) x);
256+
} catch (NumberFormatException e) {
257+
throw castNotSupported(type, x, e);
258+
}
253259
}
254260
throw castNotSupported(type, x);
255261
}
@@ -265,6 +271,12 @@ private static short castAsShort(PrimitiveType type, Object x) throws SQLExcepti
265271
return ((Long) x).shortValue();
266272
} else if (x instanceof Boolean) {
267273
return (short) (((Boolean) x) ? 1 : 0);
274+
} else if (x instanceof String) {
275+
try {
276+
return Short.parseShort((String) x);
277+
} catch (NumberFormatException e) {
278+
throw castNotSupported(type, x, e);
279+
}
268280
}
269281
throw castNotSupported(type, x);
270282
}
@@ -286,6 +298,12 @@ private static int castAsInt(PrimitiveType type, Object x) throws SQLException {
286298
return (int) ((Date) x).toLocalDate().toEpochDay();
287299
} else if (x instanceof Timestamp) {
288300
return (int) ((Timestamp) x).getTime();
301+
} else if (x instanceof String) {
302+
try {
303+
return Integer.parseInt((String) x);
304+
} catch (NumberFormatException e) {
305+
throw castNotSupported(type, x, e);
306+
}
289307
}
290308
throw castNotSupported(type, x);
291309
}
@@ -309,6 +327,12 @@ private static long castAsLong(PrimitiveType type, Object x) throws SQLException
309327
return ((Date) x).toLocalDate().toEpochDay();
310328
} else if (x instanceof Timestamp) {
311329
return ((Timestamp) x).getTime();
330+
} else if (x instanceof String) {
331+
try {
332+
return Long.parseLong((String) x);
333+
} catch (NumberFormatException e) {
334+
throw castNotSupported(type, x, e);
335+
}
312336
}
313337
throw castNotSupported(type, x);
314338
}
@@ -324,6 +348,12 @@ private static float castAsFloat(PrimitiveType type, Object x) throws SQLExcepti
324348
return (Byte) x;
325349
} else if (x instanceof Boolean) {
326350
return ((Boolean) x) ? 1f : 0f;
351+
} else if (x instanceof String) {
352+
try {
353+
return Float.parseFloat((String) x);
354+
} catch (NumberFormatException e) {
355+
throw castNotSupported(type, x, e);
356+
}
327357
}
328358
throw castNotSupported(type, x);
329359
}
@@ -343,6 +373,12 @@ private static double castAsDouble(PrimitiveType type, Object x) throws SQLExcep
343373
return (Byte) x;
344374
} else if (x instanceof Boolean) {
345375
return ((Boolean) x) ? 1d : 0d;
376+
} else if (x instanceof String) {
377+
try {
378+
return Double.parseDouble((String) x);
379+
} catch (NumberFormatException e) {
380+
throw castNotSupported(type, x, e);
381+
}
346382
}
347383
throw castNotSupported(type, x);
348384
}
@@ -353,6 +389,8 @@ private static boolean castAsBoolean(PrimitiveType type, Object x) throws SQLExc
353389
} else if (x instanceof Number) {
354390
long lValue = ((Number) x).longValue();
355391
return lValue > 0;
392+
} else if (x instanceof String) {
393+
return Boolean.parseBoolean((String) x);
356394
}
357395
throw castNotSupported(type, x);
358396
}
@@ -370,13 +408,11 @@ private static PrimitiveValue castToInterval(PrimitiveType type, Object x) throw
370408
} else if (x instanceof Long) {
371409
return PrimitiveValue.newInterval((Long) x);
372410
} else if (x instanceof String) {
373-
Duration parsed;
374411
try {
375-
parsed = Duration.parse((String) x);
412+
return PrimitiveValue.newInterval(Duration.parse((String) x));
376413
} catch (DateTimeParseException e) {
377414
throw castNotSupported(type, x, e);
378415
}
379-
return PrimitiveValue.newInterval(parsed);
380416
}
381417
throw castNotSupported(type, x);
382418
}

jdbc/src/test/java/tech/ydb/jdbc/impl/YdbPreparedStatementTest.java

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ private void fillRowValues(PreparedStatement statement, int id, boolean castingS
255255
statement.setFloat(11, 1.5f * id); // c_Float
256256
statement.setDouble(12, 2.5d * id); // c_Double
257257

258-
statement.setBytes(13, new byte[] { (byte)id }); // c_Bytes
259-
statement.setString(14, "Text_" + id); // c_Text
258+
statement.setBytes(13, ("bytes" + id).getBytes()); // c_Bytes
259+
statement.setString(14, "Text_" + id); // c_Text
260260

261261
UUID uuid = UUID.nameUUIDFromBytes(("uuid" + id).getBytes());
262262
if (castingSupported) {
@@ -311,7 +311,7 @@ private void assertRowValues(ResultSet rs, int id) throws SQLException {
311311
Assertions.assertEquals(1.5f * id, rs.getFloat("c_Float"), 0.001f);
312312
Assertions.assertEquals(2.5d * id, rs.getDouble("c_Double"), 0.001d);
313313

314-
Assertions.assertArrayEquals(new byte[] { (byte)id }, rs.getBytes("c_Bytes"));
314+
Assertions.assertArrayEquals(("bytes" + id).getBytes(), rs.getBytes("c_Bytes"));
315315
Assertions.assertEquals("Text_" + id, rs.getString("c_Text"));
316316
Assertions.assertEquals("{\"json\": " + id + "}", rs.getString("c_Json"));
317317
Assertions.assertEquals("{\"jsonDoc\":" + id + "}", rs.getString("c_JsonDocument"));
@@ -384,7 +384,7 @@ private void assertTableRow(ResultSet rs, int id) throws SQLException {
384384
assertStructMember(PrimitiveValue.newFloat(1.5f * id), sv, "c_Float");
385385
assertStructMember(PrimitiveValue.newDouble(2.5d * id), sv, "c_Double");
386386

387-
assertStructMember(PrimitiveValue.newBytes(new byte[] { (byte)id }), sv, "c_Bytes");
387+
assertStructMember(PrimitiveValue.newBytes(("bytes" + id).getBytes()), sv, "c_Bytes");
388388
assertStructMember(PrimitiveValue.newText("Text_" + id), sv, "c_Text");
389389
assertStructMember(PrimitiveValue.newJson("{\"json\": " + id + "}"), sv, "c_Json");
390390
assertStructMember(PrimitiveValue.newJsonDocument("{\"jsonDoc\":" + id + "}"), sv, "c_JsonDocument");
@@ -449,6 +449,54 @@ public void batchUpsertAllTest(SqlQueries.JdbcQuery query) throws SQLException {
449449
}
450450
};
451451

452+
@Test
453+
public void setStringTest() throws SQLException {
454+
String upsert = TEST_TABLE.upsertAll(SqlQueries.JdbcQuery.STANDARD);
455+
int id = 120;
456+
UUID uuid = UUID.nameUUIDFromBytes(("uuid" + id).getBytes());
457+
458+
try (PreparedStatement statement = jdbc.connection().prepareStatement(upsert)) {
459+
statement.setString(1, String.valueOf(id)); // id
460+
statement.setString(2, String.valueOf(id % 2 == 0)); // c_Bool
461+
statement.setString(3, String.valueOf(id + 1)); // c_Int8
462+
statement.setString(4, String.valueOf(id + 2)); // c_Int16
463+
statement.setString(5, String.valueOf(id + 3)); // c_Int32
464+
statement.setString(6, String.valueOf(id + 4)); // c_Int64
465+
statement.setString(7, String.valueOf(id + 5)); // c_Uint8
466+
statement.setString(8, String.valueOf(id + 6)); // c_Uint16
467+
statement.setString(9, String.valueOf(id + 7)); // c_Uint32
468+
statement.setString(10, String.valueOf(id + 8)); // c_Uint64
469+
statement.setString(11, String.valueOf(1.5f * id)); // c_Float
470+
statement.setString(12, String.valueOf(2.5d * id)); // c_Double
471+
statement.setString(13, "bytes" + id); // c_Bytes
472+
statement.setString(14, "Text_" + id); // c_Text
473+
statement.setString(15, "{\"json\": " + id + "}"); // c_Json
474+
statement.setString(16, "{\"jsonDoc\": " + id + "}"); // c_JsonDocument
475+
statement.setString(17, "{yson=" + id + "}"); // c_Yson
476+
statement.setString(18, uuid.toString()); // c_Uuid
477+
478+
Date sqlDate = new Date(TEST_TS.toEpochMilli());
479+
LocalDateTime dateTime = LocalDateTime.ofInstant(TEST_TS, ZoneOffset.UTC).plusMinutes(id);
480+
481+
statement.setString(19, sqlDate.toString()); // c_Date
482+
statement.setString(20, dateTime.toString()); // c_Datetime
483+
statement.setString(21, TEST_TS.plusSeconds(id).toString()); // c_Timestamp
484+
statement.setString(22, Duration.ofMinutes(id).toString()); // c_Interval
485+
486+
statement.setString(23, BigDecimal.valueOf(10000 + id, 3).toString()); // c_Decimal
487+
statement.setString(24, BigDecimal.valueOf(20000 + id, 0).toString()); // c_BigDecimal
488+
statement.setString(25, BigDecimal.valueOf(30000 + id, 6).toString()); // c_BankDecimal
489+
statement.execute();
490+
}
491+
492+
try (Statement statement = jdbc.connection().createStatement()) {
493+
try (ResultSet rs = statement.executeQuery(TEST_TABLE.selectSQL())) {
494+
assertRowValues(rs, id);
495+
Assertions.assertFalse(rs.next());
496+
}
497+
}
498+
};
499+
452500
@Test
453501
public void tableRowTest() throws SQLException {
454502
String upsert = TEST_TABLE.upsertAll(SqlQueries.JdbcQuery.BATCHED);

0 commit comments

Comments
 (0)