Class: Amalgalite::TypeMaps::DefaultMap
- Inherits:
-
Object
- Object
- Amalgalite::TypeMaps::DefaultMap
- Defined in:
- lib/amalgalite/type_maps/default_map.rb
Overview
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
Constant Summary collapse
- SQL_TO_METHOD =
{ 'date' => 'date', 'datetime' => 'datetime', 'timestamp' => 'time', 'time' => 'time', 'double' => 'float', 'float' => 'float', 'real' => 'float', 'numeric' => 'float', 'decimal' => 'float', 'integer' => 'integer', 'tinyint' => 'integer', 'smallint' => 'integer', 'int' => 'integer', 'int2' => 'integer', 'int4' => 'integer', 'int8' => 'integer', 'bigint' => 'integer', 'serial' => 'integer', 'bigserial' => 'integer', 'text' => 'string', 'char' => 'string', 'string' => 'string', 'varchar' => 'string', 'character' => 'string', 'json' => 'string', 'bool' => 'boolean', 'boolean' => 'boolean', 'blob' => 'blob', 'binary' => 'blob', }.freeze
Instance Method Summary collapse
-
#bind_type_of(obj) ⇒ Object
A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.
-
#blob(str) ⇒ Object
convert a string to a blob.
-
#boolean(str) ⇒ Object
convert a string to true of false.
-
#date(str) ⇒ Object
convert a string to a date.
-
#datetime(str) ⇒ Object
convert a string to a datetime, if no timzone is found in the parsed string, set it to the local offset.
-
#float(str) ⇒ Object
convert a string to a Float.
-
#integer(str) ⇒ Object
convert an string to an Integer.
-
#result_value_of(normalized_declared_type, value) ⇒ Object
Map the incoming value to an outgoing value.
-
#string(str) ⇒ Object
convert a string to a String, yes redundant I know.
-
#time(str) ⇒ Object
convert a string to a Time.
Instance Method Details
#bind_type_of(obj) ⇒ Object
A straight logical mapping (for me at least) of basic Ruby classes to SQLite types, if nothing can be found then default to TEXT.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 59 def bind_type_of( obj ) case obj when Float ::Amalgalite::SQLite3::Constants::DataType::FLOAT when Integer ::Amalgalite::SQLite3::Constants::DataType::INTEGER when NilClass ::Amalgalite::SQLite3::Constants::DataType::NULL when ::Amalgalite::Blob ::Amalgalite::SQLite3::Constants::DataType::BLOB else ::Amalgalite::SQLite3::Constants::DataType::TEXT end end |
#blob(str) ⇒ Object
convert a string to a blob
154 155 156 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 154 def blob( str ) ::Amalgalite::Blob.new( :string => str ) end |
#boolean(str) ⇒ Object
convert a string to true of false
147 148 149 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 147 def boolean( str ) ::Amalgalite::Boolean.to_bool( str ) end |
#date(str) ⇒ Object
convert a string to a date
104 105 106 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 104 def date( str ) Date.parse( str ) end |
#datetime(str) ⇒ Object
convert a string to a datetime, if no timzone is found in the parsed string, set it to the local offset.
112 113 114 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 112 def datetime( str ) DateTime.parse( str ) end |
#float(str) ⇒ Object
convert a string to a Float
126 127 128 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 126 def float( str ) Float( str ) end |
#integer(str) ⇒ Object
convert an string to an Integer
133 134 135 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 133 def integer( str ) Float( str ).to_i end |
#result_value_of(normalized_declared_type, value) ⇒ Object
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.
It is assumed that ‘normalized_declared_type` is a downcased string. This assumption is made for performance reasons as this method is called many times during the returning of data from a query.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 83 def result_value_of( normalized_declared_type, value ) case value when Numeric, NilClass, Amalgalite::Blob return value when String return value unless normalized_declared_type conversion_method = DefaultMap::SQL_TO_METHOD[normalized_declared_type] if conversion_method then return send(conversion_method, value) else raise ::Amalgalite::Error, "Unable to convert SQL type of #{normalized_declared_type} to a Ruby class" end else raise ::Amalgalite::Error, "Unable to convert a class #{value.class.name} with value #{value.inspect}" end end |
#string(str) ⇒ Object
convert a string to a String, yes redundant I know.
140 141 142 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 140 def string( str ) str end |
#time(str) ⇒ Object
convert a string to a Time
119 120 121 |
# File 'lib/amalgalite/type_maps/default_map.rb', line 119 def time( str ) Time.parse( str ) end |