Skip to content

Commit

Permalink
make sure Postgre's native part does type-cast on timestamp2ruby (for…
Browse files Browse the repository at this point in the history
… correctness)
  • Loading branch information
kares committed Sep 11, 2015
1 parent 4c5522a commit 2cafaff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/java/arjdbc/jdbc/RubyJdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -1892,7 +1892,11 @@ public static IRubyObject setRawDateTime(final IRubyObject self, final IRubyObje
return value;
}

private static IRubyObject typeCastFromDatabase(final ThreadContext context,
/**
* @return AR::Type-casted value
* @since 1.3.18
*/
protected static IRubyObject typeCastFromDatabase(final ThreadContext context,
final IRubyObject adapter, final RubySymbol typeName, final RubyString value) {
final IRubyObject type = adapter.callMethod(context, "lookup_cast_type", typeName);
return type.callMethod(context, "type_cast_from_database", value);
Expand All @@ -1915,6 +1919,7 @@ protected IRubyObject dateToRuby(final ThreadContext context,
if ( adapter.isNil() ) return strValue; // NOTE: we warn on init_connection

if ( usesType(runtime) ) {
// NOTE: this CAN NOT be 100% correct - as :date is just a type guess!
return typeCastFromDatabase(context, adapter, runtime.newSymbol("date"), strValue);
}
return adapter.callMethod(context, "_string_to_date", strValue);
Expand All @@ -1937,6 +1942,7 @@ protected IRubyObject timeToRuby(final ThreadContext context,
if ( adapter.isNil() ) return strValue; // NOTE: we warn on init_connection

if ( usesType(runtime) ) {
// NOTE: this CAN NOT be 100% correct - as :time is just a type guess!
return typeCastFromDatabase(context, adapter, runtime.newSymbol("time"), strValue);
}
return adapter.callMethod(context, "_string_to_time", strValue);
Expand All @@ -1959,6 +1965,7 @@ protected IRubyObject timestampToRuby(final ThreadContext context, // TODO
if ( adapter.isNil() ) return strValue; // NOTE: we warn on init_connection

if ( usesType(runtime) ) {
// NOTE: this CAN NOT be 100% correct - as :timestamp is just a type guess!
return typeCastFromDatabase(context, adapter, runtime.newSymbol("timestamp"), strValue);
}
return adapter.callMethod(context, "_string_to_timestamp", strValue);
Expand Down Expand Up @@ -2325,8 +2332,7 @@ protected int jdbcTypeFor(final ThreadContext context, final Ruby runtime,
internedType = "array";
}
else {
final RubySymbol columnType = resolveColumnType(context, runtime, column);
internedType = columnType.asJavaString();
internedType = resolveColumnType(context, runtime, column).asJavaString();
}
}
else {
Expand Down Expand Up @@ -3124,7 +3130,11 @@ private static IRubyObject defaultValueFromResultSet(final Ruby runtime, final R
return defaultValue == null ? runtime.getNil() : RubyString.newUnicodeString(runtime, defaultValue);
}

private static boolean usesType(final Ruby runtime) { // AR 4.2
/**
* Internal API that might be subject to change!
* @since 1.3.18
*/
protected static boolean usesType(final Ruby runtime) { // AR 4.2
return runtime.getModule("ActiveRecord").getConstantAt("Type") != null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ protected IRubyObject timestampToRuby(final ThreadContext context,
if ( rawDateTime != null && rawDateTime.booleanValue() ) return strValue;

final IRubyObject adapter = callMethod(context, "adapter"); // self.adapter
if ( usesType(runtime) ) {
return typeCastFromDatabase(context, adapter, runtime.newSymbol("datetime"), strValue);
}

if ( adapter.isNil() ) return strValue; // NOTE: we warn on init_connection
return adapter.callMethod(context, "_string_to_timestamp", strValue);
}
Expand Down

0 comments on commit 2cafaff

Please sign in to comment.