Class: Polo::Adapters::Postgres

Inherits:
Object
  • Object
show all
Defined in:
lib/polo/adapters/postgres.rb

Instance Method Summary collapse

Instance Method Details

#ignore_transform(inserts, records) ⇒ Object

Internal: Transforms an INSERT with PostgreSQL-specific syntax. Ignores

records that alread exist in the table. To do this, it uses
a heuristic, i.e. checks if there is a record with the same id
in the table.
See: http://stackoverflow.com/a/6527838/32816

inserts - The Array of INSERT statements. records - The Array of Arel objects.

Returns the Array of transformed INSERT statements.



21
22
23
24
25
26
27
28
29
# File 'lib/polo/adapters/postgres.rb', line 21

def ignore_transform(inserts, records)
  insert_and_record = inserts.zip(records)
  insert_and_record.map do |insert, record|
    table_name = record.class.arel_table.name
    id = record[:id]
    insert = insert.gsub(/VALUES \((.+)\)$/m, 'SELECT \\1')
    insert << " WHERE NOT EXISTS (SELECT 1 FROM #{table_name} WHERE id=#{id});"
  end
end

#on_duplicate_key_update(inserts, records) ⇒ Object

TODO: Implement UPSERT. This command became available in 9.1.

See: www.the-art-of-web.com/sql/upsert/



7
8
9
# File 'lib/polo/adapters/postgres.rb', line 7

def on_duplicate_key_update(inserts, records)
  raise 'on_duplicate: :override is not currently supported in the PostgreSQL adapter'
end