Module: ArJdbc::DB2::Column

Defined in:
lib/arjdbc/db2/adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast_to_date_or_time(value) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/arjdbc/db2/adapter.rb', line 111

def self.cast_to_date_or_time(value)
  return value if value.is_a? Date
  return nil if value.blank?
  # https://github.com/jruby/activerecord-jdbc-adapter/commit/c225126e025df2e98ba3386c67e2a5bc5e5a73e6
  return Time.now if value =~ /^CURRENT/
  guess_date_or_time((value.is_a? Time) ? value : cast_to_time(value))
rescue
  value
end

.cast_to_time(value) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/arjdbc/db2/adapter.rb', line 121

def self.cast_to_time(value)
  return value if value.is_a? Time
  # AS400 returns a 2 digit year, LUW returns a 4 digit year, so comp = true to help out AS400
  time = DateTime.parse(value).to_time rescue nil
  return nil unless time
  time_array = [time.year, time.month, time.day, time.hour, time.min, time.sec]
  time_array[0] ||= 2000; time_array[1] ||= 1; time_array[2] ||= 1;
  Time.send(ActiveRecord::Base.default_timezone, *time_array) rescue nil
end

.guess_date_or_time(value) ⇒ Object



131
132
133
134
135
# File 'lib/arjdbc/db2/adapter.rb', line 131

def self.guess_date_or_time(value)
  return value if value.is_a? Date
  ( value && value.hour == 0 && value.min == 0 && value.sec == 0 ) ? 
    Date.new(value.year, value.month, value.day) : value
end

Instance Method Details

#type_cast(value) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/arjdbc/db2/adapter.rb', line 83

def type_cast(value)
  return nil if value.nil? || value == 'NULL' || value =~ /^\s*NULL\s*$/i
  case type
  when :string    then value
  when :integer   then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
  when :primary_key then defined?(value.to_i) ? value.to_i : (value ? 1 : 0)
  when :float     then value.to_f
  when :datetime  then ArJdbc::DB2::Column.cast_to_date_or_time(value)
  when :date      then ArJdbc::DB2::Column.cast_to_date_or_time(value)
  when :timestamp then ArJdbc::DB2::Column.cast_to_time(value)
  when :time      then ArJdbc::DB2::Column.cast_to_time(value)
  # TODO AS400 stores binary strings in EBCDIC (CCSID 65535), need to convert back to ASCII
  else
    super
  end
end

#type_cast_code(var_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/arjdbc/db2/adapter.rb', line 100

def type_cast_code(var_name)
  case type
  when :datetime  then "ArJdbc::DB2::Column.cast_to_date_or_time(#{var_name})"
  when :date      then "ArJdbc::DB2::Column.cast_to_date_or_time(#{var_name})"
  when :timestamp then "ArJdbc::DB2::Column.cast_to_time(#{var_name})"
  when :time      then "ArJdbc::DB2::Column.cast_to_time(#{var_name})"
  else
    super
  end
end