Class: ODBCAdapter::TypeCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/odbc_adapter/type_caster.rb

Constant Summary collapse

TYPES =

When fetching a result set, the Ruby ODBC driver converts all ODBC SQL types to an equivalent Ruby type; with the exception of SQL_DATE, SQL_TIME and SQL_TIMESTAMP.

[
  ODBC::SQL_DATE,
  ODBC::SQL_TIME,
  ODBC::SQL_TIMESTAMP
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(idx) ⇒ TypeCaster

Returns a new instance of TypeCaster.



14
15
16
# File 'lib/odbc_adapter/type_caster.rb', line 14

def initialize(idx)
  @idx = idx
end

Instance Attribute Details

#idxObject (readonly)

Returns the value of attribute idx.



12
13
14
# File 'lib/odbc_adapter/type_caster.rb', line 12

def idx
  @idx
end

Class Method Details

.build_from(columns) ⇒ Object

Build a list of casters from a list of columns



36
37
38
39
40
# File 'lib/odbc_adapter/type_caster.rb', line 36

def self.build_from(columns)
  columns.each_with_index.each_with_object([]) do |(column, idx), casters|
    casters << new(idx) if TYPES.include?(column.type)
  end
end

Instance Method Details

#cast(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/odbc_adapter/type_caster.rb', line 18

def cast(value)
  case value
  when ODBC::TimeStamp
    Time.gm(value.year, value.month, value.day, value.hour, value.minute, value.second)
  when ODBC::Time
    now = DateTime.now
    Time.gm(now.year, now.month, now.day, value.hour, value.minute, value.second)
  when ODBC::Date
    Date.new(value.year, value.month, value.day)
  else
    value
  end
rescue
  # Handle pre-epoch dates
  DateTime.new(value.year, value.month, value.day, value.hour, value.minute, value.second)
end