Skip to content

Commit

Permalink
Support setString for all YDB types (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex268 authored Nov 6, 2024
2 parents d8b51b4 + 50cf58f commit cd6da23
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,9 @@ jobs:
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
42 changes: 39 additions & 3 deletions jdbc/src/main/java/tech/ydb/jdbc/common/MappingSetters.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ private static byte castAsByte(PrimitiveType type, Object x) throws SQLException
return ((Long) x).byteValue();
} else if (x instanceof Boolean) {
return (byte) (((Boolean) x) ? 1 : 0);
} else if (x instanceof String) {
try {
return Byte.parseByte((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -265,6 +271,12 @@ private static short castAsShort(PrimitiveType type, Object x) throws SQLExcepti
return ((Long) x).shortValue();
} else if (x instanceof Boolean) {
return (short) (((Boolean) x) ? 1 : 0);
} else if (x instanceof String) {
try {
return Short.parseShort((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -286,6 +298,12 @@ private static int castAsInt(PrimitiveType type, Object x) throws SQLException {
return (int) ((Date) x).toLocalDate().toEpochDay();
} else if (x instanceof Timestamp) {
return (int) ((Timestamp) x).getTime();
} else if (x instanceof String) {
try {
return Integer.parseInt((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -309,6 +327,12 @@ private static long castAsLong(PrimitiveType type, Object x) throws SQLException
return ((Date) x).toLocalDate().toEpochDay();
} else if (x instanceof Timestamp) {
return ((Timestamp) x).getTime();
} else if (x instanceof String) {
try {
return Long.parseLong((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -324,6 +348,12 @@ private static float castAsFloat(PrimitiveType type, Object x) throws SQLExcepti
return (Byte) x;
} else if (x instanceof Boolean) {
return ((Boolean) x) ? 1f : 0f;
} else if (x instanceof String) {
try {
return Float.parseFloat((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -343,6 +373,12 @@ private static double castAsDouble(PrimitiveType type, Object x) throws SQLExcep
return (Byte) x;
} else if (x instanceof Boolean) {
return ((Boolean) x) ? 1d : 0d;
} else if (x instanceof String) {
try {
return Double.parseDouble((String) x);
} catch (NumberFormatException e) {
throw castNotSupported(type, x, e);
}
}
throw castNotSupported(type, x);
}
Expand All @@ -353,6 +389,8 @@ private static boolean castAsBoolean(PrimitiveType type, Object x) throws SQLExc
} else if (x instanceof Number) {
long lValue = ((Number) x).longValue();
return lValue > 0;
} else if (x instanceof String) {
return Boolean.parseBoolean((String) x);
}
throw castNotSupported(type, x);
}
Expand All @@ -370,13 +408,11 @@ private static PrimitiveValue castToInterval(PrimitiveType type, Object x) throw
} else if (x instanceof Long) {
return PrimitiveValue.newInterval((Long) x);
} else if (x instanceof String) {
Duration parsed;
try {
parsed = Duration.parse((String) x);
return PrimitiveValue.newInterval(Duration.parse((String) x));
} catch (DateTimeParseException e) {
throw castNotSupported(type, x, e);
}
return PrimitiveValue.newInterval(parsed);
}
throw castNotSupported(type, x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ private void fillRowValues(PreparedStatement statement, int id, boolean castingS
statement.setFloat(11, 1.5f * id); // c_Float
statement.setDouble(12, 2.5d * id); // c_Double

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

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

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

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

@Test
public void setStringTest() throws SQLException {
String upsert = TEST_TABLE.upsertAll(SqlQueries.JdbcQuery.STANDARD);
int id = 120;
UUID uuid = UUID.nameUUIDFromBytes(("uuid" + id).getBytes());

try (PreparedStatement statement = jdbc.connection().prepareStatement(upsert)) {
statement.setString(1, String.valueOf(id)); // id
statement.setString(2, String.valueOf(id % 2 == 0)); // c_Bool
statement.setString(3, String.valueOf(id + 1)); // c_Int8
statement.setString(4, String.valueOf(id + 2)); // c_Int16
statement.setString(5, String.valueOf(id + 3)); // c_Int32
statement.setString(6, String.valueOf(id + 4)); // c_Int64
statement.setString(7, String.valueOf(id + 5)); // c_Uint8
statement.setString(8, String.valueOf(id + 6)); // c_Uint16
statement.setString(9, String.valueOf(id + 7)); // c_Uint32
statement.setString(10, String.valueOf(id + 8)); // c_Uint64
statement.setString(11, String.valueOf(1.5f * id)); // c_Float
statement.setString(12, String.valueOf(2.5d * id)); // c_Double
statement.setString(13, "bytes" + id); // c_Bytes
statement.setString(14, "Text_" + id); // c_Text
statement.setString(15, "{\"json\": " + id + "}"); // c_Json
statement.setString(16, "{\"jsonDoc\": " + id + "}"); // c_JsonDocument
statement.setString(17, "{yson=" + id + "}"); // c_Yson
statement.setString(18, uuid.toString()); // c_Uuid

Date sqlDate = new Date(TEST_TS.toEpochMilli());
LocalDateTime dateTime = LocalDateTime.ofInstant(TEST_TS, ZoneOffset.UTC).plusMinutes(id);

statement.setString(19, sqlDate.toString()); // c_Date
statement.setString(20, dateTime.toString()); // c_Datetime
statement.setString(21, TEST_TS.plusSeconds(id).toString()); // c_Timestamp
statement.setString(22, Duration.ofMinutes(id).toString()); // c_Interval

statement.setString(23, BigDecimal.valueOf(10000 + id, 3).toString()); // c_Decimal
statement.setString(24, BigDecimal.valueOf(20000 + id, 0).toString()); // c_BigDecimal
statement.setString(25, BigDecimal.valueOf(30000 + id, 6).toString()); // c_BankDecimal
statement.execute();
}

try (Statement statement = jdbc.connection().createStatement()) {
try (ResultSet rs = statement.executeQuery(TEST_TABLE.selectSQL())) {
assertRowValues(rs, id);
Assertions.assertFalse(rs.next());
}
}
};

@Test
public void tableRowTest() throws SQLException {
String upsert = TEST_TABLE.upsertAll(SqlQueries.JdbcQuery.BATCHED);
Expand Down

0 comments on commit cd6da23

Please sign in to comment.