module ActiveRecord::Import::PostgreSQLAdapter

Constants

MIN_VERSION_FOR_UPSERT

Public Instance Methods

database_version() click to toggle source
Calls superclass method
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 227
def database_version
  defined?(postgresql_version) ? postgresql_version : super
end
next_value_for_sequence(sequence_name) click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 73
def next_value_for_sequence(sequence_name)
  %{nextval('#{sequence_name}')}
end
returning_selections(options) click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 99
def returning_selections(options)
  selections = []
  column_names = Array(options[:model].column_names)

  selections += Array(options[:primary_key]) if options[:primary_key].present?
  selections += Array(options[:returning]) if options[:returning].present?

  selections.map do |selection|
    column_names.include?(selection.to_s) ? "\"#{selection}\"" : selection
  end
end
split_ids_and_results( selections, options ) click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 51
def split_ids_and_results( selections, options )
  ids = []
  returning_values = []

  columns = Array(selections[:columns])
  values = Array(selections[:values])
  id_indexes = Array(options[:primary_key]).map { |key| columns.index(key) }
  returning_columns = columns.reject.with_index { |_, index| id_indexes.include?(index) }
  returning_indexes = returning_columns.map { |column| columns.index(column) }

  values.each do |value|
    value_array = Array(value)
    ids << id_indexes.map { |index| value_array[index] }
    returning_values << returning_indexes.map { |index| value_array[index] }
  end

  ids.map!(&:first) if id_indexes.size == 1
  returning_values.map!(&:first) if returning_columns.size == 1

  [ids, returning_values, returning_columns]
end
sql_for_conflict_target( args = {} ) click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 196
def sql_for_conflict_target( args = {} )
  constraint_name = args[:constraint_name]
  conflict_target = args[:conflict_target]
  index_predicate = args[:index_predicate]
  if constraint_name.present?
    "ON CONSTRAINT #{constraint_name} "
  elsif conflict_target.present?
    sql = "(#{Array( conflict_target ).reject( &:blank? ).join( ', ' )}) "
    sql += "WHERE #{index_predicate} " if index_predicate
    sql
  end
end
sql_for_default_conflict_target( table_name, primary_key ) click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 209
def sql_for_default_conflict_target( table_name, primary_key )
  conflict_target = Array(primary_key).join(', ')
  "(#{conflict_target}) " if conflict_target.present?
end
supports_on_duplicate_key_update?() click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 219
def supports_on_duplicate_key_update?
  database_version >= MIN_VERSION_FOR_UPSERT
end
supports_setting_primary_key_of_imported_objects?() click to toggle source
# File lib/activerecord-import/adapters/postgresql_adapter.rb, line 223
def supports_setting_primary_key_of_imported_objects?
  true
end