module ActiveRecord::ConnectionAdapters::Jdbc::TypeCast
Type casting methods taken from AR 4.1's Column class. @private Simply to quickly “hack-in” 4.2 compatibility.
Constants
- FALSE_VALUES
- ISO_DATE
module Format
- ISO_DATETIME
- TRUE_VALUES
Public Instance Methods
binary_to_string(value)
click to toggle source
Used to convert from BLOBs to Strings
# File lib/arjdbc/jdbc/type_cast.rb, line 30 def binary_to_string(value) value end
string_to_dummy_time(string)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 53 def string_to_dummy_time(string) return string unless string.is_a?(String) return nil if string.empty? dummy_time_string = "2000-01-01 #{string}" fast_string_to_time(dummy_time_string) || begin time_hash = Date._parse(dummy_time_string) return nil if time_hash[:hour].nil? new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction)) end end
string_to_time(string)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 45 def string_to_time(string) return string unless string.is_a?(String) return nil if string.empty? return string if string =~ /^-?infinity$/.freeze fast_string_to_time(string) || fallback_string_to_time(string) end
value_to_boolean(value)
click to toggle source
convert something to a boolean
# File lib/arjdbc/jdbc/type_cast.rb, line 67 def value_to_boolean(value) if value.is_a?(String) && value.empty? nil else TRUE_VALUES.include?(value) end end
value_to_date(value)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 34 def value_to_date(value) if value.is_a?(String) return nil if value.empty? fast_string_to_date(value) || fallback_string_to_date(value) elsif value.respond_to?(:to_date) value.to_date else value end end
value_to_decimal(value)
click to toggle source
convert something to a BigDecimal
# File lib/arjdbc/jdbc/type_cast.rb, line 96 def value_to_decimal(value) # Using .class is faster than .is_a? and # subclasses of BigDecimal will be handled # in the else clause if value.class == BigDecimal value elsif value.respond_to?(:to_d) value.to_d else value.to_s.to_d end end
value_to_integer(value)
click to toggle source
Used to convert values to integer. handle the case when an integer column is used to store boolean values
# File lib/arjdbc/jdbc/type_cast.rb, line 86 def value_to_integer(value) case value when TrueClass, FalseClass value ? 1 : 0 else value.to_i rescue nil end end
Protected Instance Methods
fallback_string_to_date(string)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 152 def fallback_string_to_date(string) new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday)) end
fallback_string_to_time(string)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 156 def fallback_string_to_time(string) time_hash = Date._parse(string) time_hash[:sec_fraction] = microseconds(time_hash) time_hash[:year] *= -1 if time_hash[:zone] == 'BC' new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)) end
fast_string_to_date(string)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 138 def fast_string_to_date(string) if string =~ ISO_DATE new_date $1.to_i, $2.to_i, $3.to_i end end
fast_string_to_time(string)
click to toggle source
Doesn't handle time zones.
# File lib/arjdbc/jdbc/type_cast.rb, line 145 def fast_string_to_time(string) if string =~ ISO_DATETIME microsec = ($7.to_r * 1_000_000).to_i new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec end end
microseconds(time)
click to toggle source
'0.123456' -> 123456 '1.123456' -> 123456
# File lib/arjdbc/jdbc/type_cast.rb, line 112 def microseconds(time) time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0 end
new_date(year, mon, mday)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 116 def new_date(year, mon, mday) if year && year != 0 Date.new(year, mon, mday) rescue nil end end
new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
click to toggle source
# File lib/arjdbc/jdbc/type_cast.rb, line 122 def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil) # Treat 0000-00-00 00:00:00 as nil. return nil if year.nil? || (year == 0 && mon == 0 && mday == 0) if offset time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil return nil unless time time -= offset ActiveRecord::Base.default_timezone == :utc ? time : time.getlocal else timezone = ActiveRecord::Base.default_timezone Time.public_send(timezone, year, mon, mday, hour, min, sec, microsec) rescue nil end end