mirror of
https://github.com/aljazceru/turso.git
synced 2026-01-15 14:14:20 +01:00
Merge 'bindings/java: Implement setObject(int, Object) in JDBC4PreparedStatement' from Orange banana
## Purpose * Implement `setObject(int, Object)` to support binding of common Java types to SQL parameters in JDBC4. * This implementation currently covers only standard JDBC4 supported types. LOB and stream bindings are not yet implemented. ## Changes * Implemented JDBC4PreparedStatement#setObject(int, Object) handling for * `String`, `Integer`, `Long`, `Boolean`, `Double`, `Float`, `Byte`, `Short` * `byte[]`, `Date`, `Time`, `Timestamp`, `BigDecimal` * Added validation for unsupported types (`Blob`, `Clob`, `InputStream`, `Reader`) * Added corresponding unit test `testSetObjectCoversAllSupportedTypes` to verify correctness ## Note * Additional work (e.g., LOB/Stream handling) will be addressed separately once driver support is available. ## Related Issue #615 Reviewed-by: Kim Seon Woo (@seonWKim) Closes #3864
This commit is contained in:
@@ -189,7 +189,47 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
|
||||
|
||||
@Override
|
||||
public void setObject(int parameterIndex, Object x) throws SQLException {
|
||||
// TODO
|
||||
requireNonNull(this.statement);
|
||||
if (x == null) {
|
||||
this.statement.bindNull(parameterIndex);
|
||||
return;
|
||||
}
|
||||
if (x instanceof String) {
|
||||
setString(parameterIndex, (String) x);
|
||||
} else if (x instanceof Integer) {
|
||||
setInt(parameterIndex, (Integer) x);
|
||||
} else if (x instanceof Long) {
|
||||
setLong(parameterIndex, (Long) x);
|
||||
} else if (x instanceof Boolean) {
|
||||
setBoolean(parameterIndex, (Boolean) x);
|
||||
} else if (x instanceof Double) {
|
||||
setDouble(parameterIndex, (Double) x);
|
||||
} else if (x instanceof Float) {
|
||||
setFloat(parameterIndex, (Float) x);
|
||||
} else if (x instanceof Byte) {
|
||||
setByte(parameterIndex, (Byte) x);
|
||||
} else if (x instanceof Short) {
|
||||
setShort(parameterIndex, (Short) x);
|
||||
} else if (x instanceof byte[]) {
|
||||
setBytes(parameterIndex, (byte[]) x);
|
||||
} else if (x instanceof Timestamp) {
|
||||
setTimestamp(parameterIndex, (Timestamp) x);
|
||||
} else if (x instanceof Date) {
|
||||
setDate(parameterIndex, (Date) x);
|
||||
} else if (x instanceof Time) {
|
||||
setTime(parameterIndex, (Time) x);
|
||||
} else if (x instanceof BigDecimal) {
|
||||
setBigDecimal(parameterIndex, (BigDecimal) x);
|
||||
} else if (x instanceof Blob
|
||||
|| x instanceof Clob
|
||||
|| x instanceof InputStream
|
||||
|| x instanceof Reader) {
|
||||
throw new SQLException(
|
||||
"setObject does not yet support LOB or Stream types because the corresponding set methods are unimplemented. Type found: "
|
||||
+ x.getClass().getName());
|
||||
} else {
|
||||
throw new SQLException("Unsupported object type in setObject: " + x.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -338,6 +338,56 @@ class JDBC4PreparedStatementTest {
|
||||
assertArrayEquals(new byte[] {4, 5, 6}, rs.getBytes(4));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetObjectCoversAllSupportedTypes() throws SQLException {
|
||||
connection
|
||||
.prepareStatement(
|
||||
"CREATE TABLE test ("
|
||||
+ "col1 INTEGER, "
|
||||
+ "col2 REAL, "
|
||||
+ "col3 TEXT, "
|
||||
+ "col4 BLOB, "
|
||||
+ "col5 INTEGER, "
|
||||
+ "col6 TEXT, "
|
||||
+ "col7 TEXT, "
|
||||
+ "col8 TEXT, "
|
||||
+ "col9 TEXT"
|
||||
+ ")")
|
||||
.execute();
|
||||
|
||||
PreparedStatement stmt =
|
||||
connection.prepareStatement("INSERT INTO test VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
|
||||
stmt.setObject(1, 42);
|
||||
stmt.setObject(2, 3.141592d);
|
||||
stmt.setObject(3, "string_value");
|
||||
stmt.setObject(4, new byte[] {1, 2, 3});
|
||||
stmt.setObject(5, 1L);
|
||||
stmt.setObject(6, java.sql.Date.valueOf("2025-10-30"));
|
||||
stmt.setObject(7, java.sql.Time.valueOf("10:45:00"));
|
||||
stmt.setObject(8, java.sql.Timestamp.valueOf("2025-10-30 10:45:00"));
|
||||
stmt.setObject(9, new java.math.BigDecimal("12345.6789"));
|
||||
|
||||
stmt.execute();
|
||||
|
||||
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
|
||||
ResultSet rs = stmt2.executeQuery();
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(42, rs.getInt(1));
|
||||
assertEquals(3.141592d, rs.getDouble(2), 0.000001);
|
||||
assertEquals("string_value", rs.getString(3));
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, rs.getBytes(4));
|
||||
assertTrue(rs.getBoolean(5));
|
||||
assertEquals(java.sql.Date.valueOf("2025-10-30"), rs.getDate(6));
|
||||
assertEquals(java.sql.Time.valueOf("10:45:00"), rs.getTime(7));
|
||||
assertEquals(java.sql.Timestamp.valueOf("2025-10-30 10:45:00"), rs.getTimestamp(8));
|
||||
String decimalText = rs.getString(9);
|
||||
assertEquals(
|
||||
new java.math.BigDecimal("12345.6789").stripTrailingZeros(),
|
||||
new java.math.BigDecimal(decimalText).stripTrailingZeros());
|
||||
}
|
||||
|
||||
@Test
|
||||
void execute_insert_should_return_number_of_inserted_elements() throws Exception {
|
||||
connection.prepareStatement("CREATE TABLE test (col INTEGER)").execute();
|
||||
|
||||
Reference in New Issue
Block a user