Skip to content

Commit ce0e904

Browse files
Frank-Gu-81QuChen88
authored andcommitted
Unit test for getter functions of CachedResultSet and added Timestamp support to CachedResultSet
fix: Add Timestamp support to CachedResultSet getTime() and getDate() - Implement missing instanceof Timestamp cases in convertToTime() and convertToDate() - Use new Time(timestamp.getTime()) and new Date(timestamp.getTime()) for standard JDBC behavior - Add comprehensive test coverage for Timestamp conversion scenarios - Fix object equality issues in tests by using consistent constructors Resolves missing Timestamp handling that previously fell through to string parsing. added more tests and cleaned up debug logs removed uncessary imports, fixed testing logic for getTime() and getDate() and fixed implementation error for getBytes() and getBoolean() in the original codebase updated future timestamp testing to make it more robust intead of hardcoding the future timestamp updated convertToTime() to use static timezone offset -- functionality unchanged
1 parent b600ba2 commit ce0e904

File tree

2 files changed

+382
-6
lines changed

2 files changed

+382
-6
lines changed

wrapper/src/main/java/software/amazon/jdbc/plugin/cache/CachedResultSet.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.HashMap;
3939
import java.util.Map;
4040
import java.util.Calendar;
41+
import java.util.TimeZone;
4142

4243
public class CachedResultSet implements ResultSet {
4344

@@ -68,6 +69,7 @@ public Object get(final int columnIndex) throws SQLException {
6869
protected boolean wasNullFlag;
6970
private final CachedResultSetMetaData metadata;
7071
protected static final ZoneId defaultTimeZoneId = ZoneId.systemDefault();
72+
protected static final TimeZone defaultTimeZone = TimeZone.getDefault();
7173
private final HashMap<String, Integer> columnNames;
7274
private volatile boolean closed;
7375

@@ -178,7 +180,7 @@ public boolean getBoolean(final int columnIndex) throws SQLException {
178180
final Object val = checkAndGetColumnValue(columnIndex);
179181
if (val == null) return false;
180182
if (val instanceof Boolean) return (Boolean) val;
181-
if (val instanceof Number) return ((Number) val).intValue() == 0;
183+
if (val instanceof Number) return ((Number) val).intValue() != 0;
182184
return Boolean.parseBoolean(val.toString());
183185
}
184186

@@ -251,7 +253,8 @@ public byte[] getBytes(final int columnIndex) throws SQLException {
251253
final Object val = checkAndGetColumnValue(columnIndex);
252254
if (val == null) return null;
253255
if (val instanceof byte[]) return (byte[]) val;
254-
return new byte[0];
256+
// Convert non-byte data to string, then to bytes (standard JDBC behavior)
257+
return val.toString().getBytes();
255258
}
256259

257260
private Date convertToDate(Object dateObj, Calendar cal) throws SQLException {
@@ -268,6 +271,14 @@ private Date convertToDate(Object dateObj, Calendar cal) throws SQLException {
268271
ZonedDateTime targetZonedDateTime = originalZonedDateTime.withZoneSameInstant(defaultTimeZoneId);
269272
return Date.valueOf(targetZonedDateTime.toLocalDate());
270273
}
274+
if (dateObj instanceof Timestamp) {
275+
Timestamp timestamp = (Timestamp) dateObj;
276+
long millis = timestamp.getTime();
277+
if (cal == null) return new Date(millis);
278+
long adjustedMillis = millis - cal.getTimeZone().getOffset(millis)
279+
+ defaultTimeZone.getOffset(millis);
280+
return new Date(adjustedMillis);
281+
}
271282

272283
// Note: normally the user should properly store the Date object in the DB column and
273284
// the underlying PG/MySQL/MariaDB driver would convert it into Date already in getObject()
@@ -302,6 +313,14 @@ private Time convertToTime(Object timeObj, Calendar cal) throws SQLException {
302313
OffsetTime localTime = ((OffsetTime)timeObj).withOffsetSameInstant(OffsetDateTime.now().getOffset());
303314
return Time.valueOf(localTime.toLocalTime());
304315
}
316+
if (timeObj instanceof Timestamp) {
317+
Timestamp timestamp = (Timestamp) timeObj;
318+
long millis = timestamp.getTime();
319+
if (cal == null) return new Time(millis);
320+
long adjustedMillis = millis - cal.getTimeZone().getOffset(millis)
321+
+ defaultTimeZone.getOffset(millis);
322+
return new Time(adjustedMillis);
323+
}
305324

306325
// Note: normally the user should properly store the Time object in the DB column and
307326
// the underlying PG/MySQL/MariaDB driver would convert it into Time already in getObject()

0 commit comments

Comments
 (0)