Sha256: 2ebab49a81ea723724e9b3d763cb94811d6804231262ccd43b807e32c6dd5bd1

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

module Polo
  module Adapters
    class Postgres
      # TODO: Implement UPSERT. This command became available in 9.1.
      #
      # See: http://www.the-art-of-web.com/sql/upsert/
      def on_duplicate_key_update(inserts, records)
        raise 'on_duplicate: :override is not currently supported in the PostgreSQL adapter'
      end

      # 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.
      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
polo-0.5.0 lib/polo/adapters/postgres.rb
polo-0.4.1 lib/polo/adapters/postgres.rb
polo-0.4.0 lib/polo/adapters/postgres.rb
polo-0.3.0 lib/polo/adapters/postgres.rb