Skip to content

Commit 8cb5a2a

Browse files
committed
[postgres] use timestamp string helper for all timestamps
pgjdbc always converts the Timestamp value to a string. Instead of RubyTime -> Timestamp -> Calendar -> StringBuilder -> String use the new helpers for the more direct RubyTime -> StringBuilder -> String
1 parent b33c587 commit 8cb5a2a

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed

src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -328,47 +328,9 @@ protected void setTimestampParameter(final ThreadContext context,
328328
final Connection connection, final PreparedStatement statement,
329329
final int index, IRubyObject value,
330330
final IRubyObject attribute, final int type) throws SQLException {
331-
332-
if ( value instanceof RubyFloat ) {
333-
final double doubleValue = ( (RubyFloat) value ).getValue();
334-
if ( Double.isInfinite(doubleValue) ) {
335-
setTimestampInfinity(statement, index, doubleValue);
336-
return;
337-
}
338-
}
339-
340-
RubyTime timeValue = toTime(context, value);
341-
342-
final Timestamp timestamp;
343-
344-
if (timeValue.getDateTime().getYear() > 0) {
345-
timeValue = timeInDefaultTimeZone(context, timeValue);
346-
DateTime dateTime = timeValue.getDateTime();
347-
timestamp = new Timestamp(dateTime.getMillis());
348-
349-
if (timeValue.getNSec() > 0) timestamp.setNanos((int) (timestamp.getNanos() + timeValue.getNSec()));
350-
351-
statement.setTimestamp(index, timestamp, getCalendar(dateTime.getZone()));
352-
}
353-
else {
354-
setTimestampBC(statement, index, timeValue);
355-
}
356-
}
357-
358-
private static void setTimestampBC(final PreparedStatement statement,
359-
final int index, final RubyTime timeValue) throws SQLException {
360-
DateTime dateTime = timeValue.getDateTime();
361-
@SuppressWarnings("deprecated")
362-
Timestamp timestamp = new Timestamp(dateTime.getYear() - 1900,
363-
dateTime.getMonthOfYear() - 1,
364-
dateTime.getDayOfMonth(),
365-
dateTime.getHourOfDay(),
366-
dateTime.getMinuteOfHour(),
367-
dateTime.getSecondOfMinute(),
368-
dateTime.getMillisOfSecond() * 1_000_000 + (int) timeValue.getNSec()
369-
);
370-
371-
statement.setObject(index, timestamp);
331+
// PGJDBC uses strings internally anyway, so using Timestamp doesn't do any good
332+
String tsString = PgDateTimeUtils.timestampValueToString(context, value, null, true);
333+
statement.setObject(index, tsString, Types.OTHER);
372334
}
373335

374336
private static void setTimestampInfinity(final PreparedStatement statement,
@@ -390,7 +352,8 @@ protected void setTimeParameter(final ThreadContext context,
390352
final int index, IRubyObject value,
391353
final IRubyObject attribute, final int type) throws SQLException {
392354
// to handle more fractional second precision than (default) 59.123 only
393-
super.setTimestampParameter(context, connection, statement, index, value, attribute, type);
355+
String timeStr = DateTimeUtils.timeString(context, value, DateTimeZone.UTC, true);
356+
statement.setObject(index, timeStr, Types.OTHER);
394357
}
395358

396359
@Override

src/java/arjdbc/util/DateTimeUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,25 @@ public static String timestampTimeToString(final ThreadContext context,
644644
return sb.toString();
645645
}
646646

647+
/**
648+
* Converts a ruby time to a java string, optionally with timezone and timezone adjustment
649+
* @param context
650+
* @param value the ruby value, typically a Time
651+
* @param zone DateTimeZone to adjust to, optional
652+
* @param withZone include timezone in the string?
653+
* @return time as string
654+
*/
655+
public static String timeString(final ThreadContext context,
656+
final IRubyObject value, DateTimeZone zone, boolean withZone) {
657+
StringBuilder sb = new StringBuilder(21);
658+
RubyTime timeValue = toTime(context, value);
659+
DateTime dt = timeValue.getDateTime();
660+
if (zone != null) dt = dateTimeInZone(dt, zone);
661+
662+
appendTime(sb, dt.getChronology(), dt.getMillis(), (int) timeValue.getUSec(), withZone);
663+
return sb.toString();
664+
}
665+
647666
private static void appendTime(StringBuilder sb, Chronology chrono,
648667
long millis, int usec, boolean withZone) {
649668
sb.append(NUMBERS[chrono.hourOfDay().get(millis)]);

0 commit comments

Comments
 (0)