Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
Object
  • Object
show all
Includes:
PgArrayParser
Defined in:
lib/ar_pg_array/schema.rb

Overview

:nodoc:

Constant Summary

Constants included from PgArrayParser

PgArrayParser::CURLY_BRACKETS, PgArrayParser::ESCAPE_HASH, PgArrayParser::NIL, PgArrayParser::NULL, PgArrayParser::SQUARE_BRACKETS

Instance Method Summary collapse

Methods included from PgArrayParser

#_parse_pgarray, #_parse_safe_pgarray, #_prepare_pg_string_array, #_remap_array, #parse_numeric_pgarray, #parse_pgarray, #parse_safe_pgarray, #prepare_pg_float_array, #prepare_pg_integer_array, #prepare_pg_safe_array, #prepare_pg_text_array

Instance Method Details

#add_column_with_postgresql_arrays(table, column, type, options = {}) ⇒ Object



213
214
215
216
217
218
# File 'lib/ar_pg_array/schema.rb', line 213

def add_column_with_postgresql_arrays( table, column, type, options = {} )
  if type.to_s =~ /^(.+)_array$/ && options[:default].is_a?(Array)
    options = options.merge(:default => prepare_array_for_arel_by_base_type(options[:default], $1))
  end
  add_column_without_postgresql_arrays( table, column, type, options )
end

#prepare_array_for_arel_by_base_type(value, base_type) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ar_pg_array/schema.rb', line 161

def prepare_array_for_arel_by_base_type(value, base_type)
  case base_type.to_sym
    when :integer
      prepare_pg_integer_array(value)
    when :float
      prepare_pg_float_array(value)
    when :string, :text, :other
      prepare_pg_text_array(value)
    when :datetime, :timestamp, :time
      prepare_pg_string_array(value, base_type)
    when :decimal, :boolean, :date, :safe
      prepare_pg_safe_array(value)
    else
      raise "Unsupported array base type #{base_type} for arel"
  end
end

#prepare_pg_string_array(value, base_type, column = nil) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/ar_pg_array/schema.rb', line 178

def prepare_pg_string_array(value, base_type, column=nil)
  base_column= if column
                 column.base_column
               else
                 PostgreSQLColumn::BASE_TYPE_COLUMNS[base_type.to_sym]
               end
  _prepare_pg_string_array(value){|v| quote_without_postgresql_arrays(v, base_column)}
end

#quote_array_by_base_type(value, base_type, column = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/ar_pg_array/schema.rb', line 149

def quote_array_by_base_type(value, base_type, column = nil)
  case base_type.to_sym
  when :integer, :float, :decimal, :boolean, :date, :safe, :datetime, :timestamp, :time
    "'#{ prepare_array_for_arel_by_base_type(value, base_type) }'"
  when :string, :text, :other
    pa = prepare_array_for_arel_by_base_type(value, base_type)
    "'#{ quote_string( pa ) }'"
  else
    "'#{ prepare_pg_string_array(value, base_type, column) }'"
  end
end

#quote_with_postgresql_arrays(value, column = nil) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/ar_pg_array/schema.rb', line 140

def quote_with_postgresql_arrays(value, column = nil)
  if Array === value && column && "#{column.type}" =~ /^(.+)_array$/
    quote_array_by_base_type(value, $1, column)
  else
    quote_without_postgresql_arrays(value, column)
  end
end

#type_to_sql_with_postgresql_arrays(type, limit = nil, precision = nil, scale = nil) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/ar_pg_array/schema.rb', line 221

def type_to_sql_with_postgresql_arrays(type, limit = nil, precision = nil, scale = nil)
  if type.to_s =~ /^(.+)_array$/
    type_to_sql_without_postgresql_arrays($1.to_sym, limit, precision, scale)+'[]'
  else
    type_to_sql_without_postgresql_arrays(type, limit, precision, scale)
  end
end