Module: Imparcial::Driver::AbstractTypemap
- Included in:
- AbstractAdapter
- Defined in:
- lib/imparcial/driver/abstract/typemap.rb
Instance Method Summary collapse
-
#column_to_field(column = {}) ⇒ Object
Description Transform a regular SQL column into Imparcial’s hash.
-
#default_size_for_types ⇒ Object
Description Default size can be very handy when users are lazy to supply them or they expect a given type to be in a default size.
-
#field_to_column(field = {}) ⇒ Object
Description Make an Imparcial’s hash into SQl’s.
-
#field_to_column_sql(field = {}) ⇒ Object
Description Transform a regular field into SQL syntax.
-
#parse_field(field) ⇒ Object
Description Validate a field.
-
#parse_fields(fields = [], &block) ⇒ Object
Description In order to work with fields in RDBAL, you have to provide field datas in hashes.
-
#regular_types ⇒ Object
Description Those are types shared among all databases.
-
#sql_types ⇒ Object
Description The opposite of regular_types.
Instance Method Details
#column_to_field(column = {}) ⇒ Object
Description
Transform a regular SQL column into Imparcial’s hash.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 110 def column_to_field ( column = {} ) field = {} # Apply the appropriate type. type = sql_types[column[:type].upcase] if not type raise OptionError.new('Cannot map column type: ' + column[:type].to_s) end field[:name] = column[:name] field[:type] = type if column[:size] field[:size] = unquote_value(column[:size]) end field end |
#default_size_for_types ⇒ Object
Description
Default size can be very handy when users are lazy to supply them or they expect a given type to be in a default size.
41 42 43 44 45 46 47 48 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 41 def default_size_for_types { :string => 255, :boolean => 1 } end |
#field_to_column(field = {}) ⇒ Object
Description
Make an Imparcial’s hash into SQl’s
140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 140 def field_to_column ( field = {} ) column = {} column[:name] = quote(field[:name]) column[:type] = regular_types[field[:type]] column[:size] = field[:size] column end |
#field_to_column_sql(field = {}) ⇒ Object
Description
Transform a regular field into SQL syntax.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 155 def field_to_column_sql ( field = {} ) column = field_to_column field syntax = "#{column[:name]} #{column[:type]}" syntax += "(#{column[:size]})" if column[:size] syntax end |
#parse_field(field) ⇒ Object
Description
Validate a field.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 55 def parse_field ( field ) # No name? error! if not field[:name] raise OptionError.new('A name must be supplied') end # No type? error! if not field[:type] raise OptionError.new('A type must be supplied') end # Simple conversions. field[:name] = field[:name].to_s field[:type] = field[:type].to_sym regular_types[field[:type]].length == 0 rescue raise OptionError.new("#{field[:type]} cannot be found") # Set default size if nothing has been supplied. field[:size] = default_size_for_types[field[:type]] unless field[:size] end |
#parse_fields(fields = [], &block) ⇒ Object
Description
In order to work with fields in RDBAL, you have to provide field datas in hashes. This function is basically evaluate them and pass into a block.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 91 def parse_fields ( fields = [], &block ) # At least a field must be supplied. raise OptionError.new unless fields raise OptionError.new if fields.length < 1 for field in fields parse_field field yield field end end |
#regular_types ⇒ Object
Description
Those are types shared among all databases. Of course they are not enough. Feel free to override them if necessary.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 12 def regular_types { :integer => 'INTEGER', :string => 'VARCHAR', :float => 'FLOAT', :datetime => 'DATETIME', :time => 'TIME', :date => 'DATE', :text => 'TEXT', :boolean => 'TINYINT', :serial => 'SERIAL' } end |
#sql_types ⇒ Object
Description
The opposite of regular_types.
31 32 33 34 35 |
# File 'lib/imparcial/driver/abstract/typemap.rb', line 31 def sql_types regular_types.invert end |