| Class | Amalgalite::TypeMaps::DefaultMap |
| In: |
lib/amalgalite/type_maps/default_map.rb
|
| Parent: | Object |
An Amalgalite::TypeMap that does its best to convert between Ruby classes and known SQL data types.
Upon instantiation, DefaultMap generates a conversion map to try to figure out the best way to convert between populate SQL ‘types’ and ruby classes
A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.
# File lib/amalgalite/type_maps/default_map.rb, line 66
66: def bind_type_of( obj )
67: case obj
68: when Float
69: ::Amalgalite::SQLite3::Constants::DataType::FLOAT
70: when Fixnum
71: ::Amalgalite::SQLite3::Constants::DataType::INTEGER
72: when NilClass
73: ::Amalgalite::SQLite3::Constants::DataType::NULL
74: when ::Amalgalite::Blob
75: ::Amalgalite::SQLite3::Constants::DataType::BLOB
76: else
77: ::Amalgalite::SQLite3::Constants::DataType::TEXT
78: end
79: end
Map the incoming value to an outgoing value. For some incoming values, there will be no change, but for some (i.e. Dates and Times) there is some conversion
# File lib/amalgalite/type_maps/default_map.rb, line 86
86: def result_value_of( declared_type, value )
87: case value
88: when Numeric
89: return value
90: when NilClass
91: return value
92: when Amalgalite::Blob
93: return value
94: when String
95: if declared_type then
96: conversion_method = DefaultMap.sql_to_method( declared_type.downcase )
97: if conversion_method then
98: return send(conversion_method, value)
99: else
100: raise ::Amalgalite::Error, "Unable to convert SQL type of #{declared_type} to a Ruby class"
101: end
102: else
103: # unable to do any other conversion, just return what we have.
104: return value
105: end
106: else
107: raise ::Amalgalite::Error, "Unable to convert a class #{value.class.name} with value #{value.inspect}"
108: end
109: end