Skip to content

Commit bc2d86a

Browse files
committed
handle ("hacked") date/time/timestamp conversion from the JDBC side on 4.2
work done by @prathamesh-sonpatki based on @sgrif's suggestion
1 parent 6f50c2b commit bc2d86a

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

lib/arjdbc/jdbc/adapter.rb

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -873,22 +873,57 @@ def self.type_cast_config_to_boolean(config)
873873

874874
# @note Used by Java API to convert dates from (custom) SELECTs (might get refactored).
875875
# @private
876-
def _string_to_date(value)
877-
jdbc_column_class.string_to_date(value)
878-
end
876+
def _string_to_date(value); jdbc_column_class.string_to_date(value) end
879877

880878
# @note Used by Java API to convert times from (custom) SELECTs (might get refactored).
881879
# @private
882-
def _string_to_time(value)
883-
jdbc_column_class.string_to_dummy_time(value)
884-
end
880+
def _string_to_time(value); jdbc_column_class.string_to_dummy_time(value) end
885881

886882
# @note Used by Java API to convert times from (custom) SELECTs (might get refactored).
887883
# @private
888-
def _string_to_timestamp(value)
889-
jdbc_column_class.string_to_time(value)
884+
def _string_to_timestamp(value); jdbc_column_class.string_to_time(value) end
885+
886+
if ActiveRecord::VERSION::STRING > '4.2'
887+
888+
# @private
889+
@@_date = nil
890+
891+
# @private
892+
def _string_to_date(value)
893+
if jdbc_column_class.respond_to?(:string_to_date)
894+
jdbc_column_class.string_to_date(value)
895+
else
896+
(@@_date ||= ActiveRecord::Type::Date.new).send(:cast_value, value)
897+
end
898+
end
899+
900+
# @private
901+
@@_time = nil
902+
903+
# @private
904+
def _string_to_time(value)
905+
if jdbc_column_class.respond_to?(:string_to_dummy_time)
906+
jdbc_column_class.string_to_dummy_time(value)
907+
else
908+
(@@_time ||= ActiveRecord::Type::Time.new).send(:cast_value, value)
909+
end
910+
end
911+
912+
# @private
913+
@@_date_time = nil
914+
915+
# @private
916+
def _string_to_timestamp(value)
917+
if jdbc_column_class.respond_to?(:string_to_time)
918+
jdbc_column_class.string_to_time(value)
919+
else
920+
(@@_date_time ||= ActiveRecord::Type::DateTime.new).send(:cast_value, value)
921+
end
922+
end
923+
890924
end
891925

926+
892927
if ActiveRecord::VERSION::MAJOR < 4 # emulating Rails 3.x compatibility
893928
JdbcConnection.raw_date_time = true if JdbcConnection.raw_date_time? == nil
894929
JdbcConnection.raw_boolean = true if JdbcConnection.raw_boolean? == nil

lib/arjdbc/jdbc/column.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def self.column_types
6161

6262
class << self
6363

64-
if ActiveRecord::VERSION::MAJOR > 3
64+
if ActiveRecord::VERSION::MAJOR > 3 && ActiveRecord::VERSION::STRING < '4.2'
6565

6666
# @private provides compatibility between AR 3.x/4.0 API
6767
def string_to_date(value); value_to_date(value) end

0 commit comments

Comments
 (0)