Implement setTimestamp

This commit is contained in:
김선우
2025-08-28 14:49:16 +09:00
parent 6c93d78394
commit b50011d96b
3 changed files with 55 additions and 12 deletions

View File

@@ -144,7 +144,13 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
@Override
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException {
// TODO
requireNonNull(this.statement);
if (x == null) {
this.statement.bindNull(parameterIndex);
} else {
long time = x.getTime();
this.statement.bindBlob(parameterIndex, ByteBuffer.allocate(Long.BYTES).putLong(time).array());
}
}
@Override
@@ -235,7 +241,8 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
@Override
public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException {
// TODO
// TODO: Apply calendar timezone conversion
setTimestamp(parameterIndex, x);
}
@Override

View File

@@ -161,9 +161,6 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
return new Date(time);
}
}
if (result instanceof String) {
return Date.valueOf((String) result);
}
throw new SQLException("Cannot convert value to Date: " + result.getClass());
});
}
@@ -183,9 +180,6 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
return new Time(time);
}
}
if (result instanceof String) {
return Time.valueOf((String) result);
}
throw new SQLException("Cannot convert value to Date: " + result.getClass());
});
}
@@ -193,7 +187,20 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
@Override
@SkipNullableCheck
public Timestamp getTimestamp(int columnIndex) throws SQLException {
throw new UnsupportedOperationException("not implemented");
final Object result = resultSet.get(columnIndex);
if (result == null) {
return null;
}
return wrapTypeConversion(() -> {
if (result instanceof byte[]) {
byte[] bytes = (byte[]) result;
if (bytes.length == Long.BYTES) {
long time = ByteBuffer.wrap(bytes).getLong();
return new Timestamp(time);
}
}
throw new SQLException("Cannot convert value to Timestamp: " + result.getClass());
});
}
@Override
@@ -302,7 +309,7 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
@Override
@SkipNullableCheck
public Timestamp getTimestamp(String columnLabel) throws SQLException {
throw new UnsupportedOperationException("not implemented");
return getTimestamp(findColumn(columnLabel));
}
@Override
@@ -818,13 +825,15 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
@Override
@SkipNullableCheck
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
throw new UnsupportedOperationException("not implemented");
// TODO: Apply calendar timezone conversion
return getTimestamp(columnIndex);
}
@Override
@SkipNullableCheck
public Timestamp getTimestamp(String columnLabel, Calendar cal) throws SQLException {
throw new UnsupportedOperationException("not implemented");
// TODO: Apply calendar timezone conversion
return getTimestamp(findColumn(columnLabel));
}
@Override

View File

@@ -11,6 +11,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -279,6 +280,32 @@ class JDBC4PreparedStatementTest {
assertEquals(time3, rs.getTime(1));
}
@Test
void testSetTimestamp() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col BLOB)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
Timestamp timestamp1 = new Timestamp(1000000000000L);
Timestamp timestamp2 = new Timestamp(1500000000000L);
Timestamp timestamp3 = new Timestamp(2000000000000L);
stmt.setTimestamp(1, timestamp1);
stmt.setTimestamp(2, timestamp2);
stmt.setTimestamp(3, timestamp3);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
JDBC4ResultSet rs = (JDBC4ResultSet) stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(timestamp1, rs.getTimestamp(1));
assertTrue(rs.next());
assertEquals(timestamp2, rs.getTimestamp(1));
assertTrue(rs.next());
assertEquals(timestamp3, rs.getTimestamp(1));
}
@Test
void testInsertMultipleTypes() throws SQLException {
connection