Implement setTime

This commit is contained in:
김선우
2025-08-28 14:35:01 +09:00
parent a61ad217f1
commit a9ae588159
3 changed files with 97 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.nio.ByteBuffer;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
@@ -125,14 +126,20 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
if (x == null) {
this.statement.bindNull(parameterIndex);
} else {
String dateStr = x.toString();
this.statement.bindBlob(parameterIndex, dateStr.getBytes());
long time = x.getTime();
this.statement.bindBlob(parameterIndex, ByteBuffer.allocate(Long.BYTES).putLong(time).array());
}
}
@Override
public void setTime(int parameterIndex, Time 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
@@ -223,7 +230,7 @@ public final class JDBC4PreparedStatement extends JDBC4Statement implements Prep
@Override
public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException {
// TODO
setTime(parameterIndex, x);
}
@Override

View File

@@ -5,6 +5,7 @@ import java.io.Reader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
import java.nio.ByteBuffer;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
@@ -146,15 +147,47 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
}
@Override
@SkipNullableCheck
@Nullable
public Date getDate(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 Date(time);
}
}
if (result instanceof String) {
return Date.valueOf((String) result);
}
throw new SQLException("Cannot convert value to Date: " + result.getClass());
});
}
@Override
@SkipNullableCheck
public Time getTime(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 Time(time);
}
}
if (result instanceof String) {
return Time.valueOf((String) result);
}
throw new SQLException("Cannot convert value to Date: " + result.getClass());
});
}
@Override
@@ -238,9 +271,26 @@ public final class JDBC4ResultSet implements ResultSet, ResultSetMetaData {
}
@Override
@SkipNullableCheck
@Nullable
public Date getDate(String columnLabel) throws SQLException {
throw new UnsupportedOperationException("not implemented");
final Object result = resultSet.get(columnLabel);
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 Date(time);
}
}
// Try to parse as string if it's stored as TEXT
if (result instanceof String) {
return Date.valueOf((String) result);
}
throw new SQLException("Cannot convert value to Date: " + result.getClass());
});
}
@Override

View File

@@ -10,6 +10,7 @@ import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.Properties;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -242,14 +243,40 @@ class JDBC4PreparedStatementTest {
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
ResultSet rs = stmt2.executeQuery();
JDBC4ResultSet rs = (JDBC4ResultSet) stmt2.executeQuery();
assertTrue(rs.next());
assertEquals(date1, rs.getDate(1));
assertTrue(rs.next());
assertEquals(date2, rs.getDate(1));
assertTrue(rs.next());
assertEquals(date3, rs.getDate(1));
}
@Test
void testSetTime() throws SQLException {
connection.prepareStatement("CREATE TABLE test (col BLOB)").execute();
PreparedStatement stmt =
connection.prepareStatement("INSERT INTO test (col) VALUES (?), (?), (?)");
Time time1 = new Time(1000000000000L);
Time time2 = new Time(1500000000000L);
Time time3 = new Time(2000000000000L);
stmt.setTime(1, time1);
stmt.setTime(2, time2);
stmt.setTime(3, time3);
stmt.execute();
PreparedStatement stmt2 = connection.prepareStatement("SELECT * FROM test;");
JDBC4ResultSet rs = (JDBC4ResultSet) stmt2.executeQuery();
assertTrue(rs.next());
assertArrayEquals(date1.toString().getBytes(), rs.getBytes(1));
assertEquals(time1, rs.getTime(1));
assertTrue(rs.next());
assertArrayEquals(date2.toString().getBytes(), rs.getBytes(1));
assertEquals(time2, rs.getTime(1));
assertTrue(rs.next());
assertArrayEquals(date3.toString().getBytes(), rs.getBytes(1));
assertEquals(time3, rs.getTime(1));
}
@Test