-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0c2326
commit d972497
Showing
8 changed files
with
169 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Polo | ||
module Adapters | ||
class MySQL | ||
def on_duplicate_key_update(inserts, records) | ||
insert_and_record = inserts.zip(records) | ||
insert_and_record.map do |insert, record| | ||
values_syntax = record.attributes.keys.map do |key| | ||
"#{key} = VALUES(#{key})" | ||
end | ||
|
||
on_dup_syntax = "ON DUPLICATE KEY UPDATE #{values_syntax.join(', ')}" | ||
|
||
"#{insert} #{on_dup_syntax}" | ||
end | ||
end | ||
|
||
def ignore_transform(inserts, records) | ||
inserts.map do |insert| | ||
insert.gsub("INSERT", "INSERT IGNORE") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'spec_helper' | ||
|
||
describe Polo::Adapters::MySQL do | ||
|
||
let(:adapter) { Polo::Adapters::MySQL.new } | ||
|
||
let(:netto) do | ||
AR::Chef.where(name: 'Netto').first | ||
end | ||
|
||
before(:all) do | ||
TestData.create_netto | ||
end | ||
|
||
let(:translator) { Polo::SqlTranslator.new(netto, Polo::Configuration.new(adapter: :mysql)) } | ||
|
||
describe '#ignore_transform' do | ||
it 'appends the IGNORE command after INSERTs' do | ||
insert_netto = [%q{INSERT IGNORE INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', 'nettofarah@gmail.com')}] | ||
|
||
records = translator.records | ||
inserts = translator.inserts | ||
translated_sql = adapter.ignore_transform(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
|
||
|
||
describe '#on_duplicate_key_update' do | ||
it 'appends ON DUPLICATE KEY UPDATE with all values to the current INSERT statement' do | ||
insert_netto = [ | ||
%q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', 'nettofarah@gmail.com') ON DUPLICATE KEY UPDATE id = VALUES(id), name = VALUES(name), email = VALUES(email)} | ||
] | ||
|
||
inserts = translator.inserts | ||
records = translator.records | ||
translated_sql = adapter.on_duplicate_key_update(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
|
||
describe Polo::Adapters::Postgres do | ||
|
||
let(:adapter) { Polo::Adapters::Postgres.new } | ||
|
||
let(:netto) do | ||
AR::Chef.where(name: 'Netto').first | ||
end | ||
|
||
before(:all) do | ||
TestData.create_netto | ||
end | ||
|
||
let(:translator) { Polo::SqlTranslator.new(netto, Polo::Configuration.new(adapter: :postgres)) } | ||
|
||
describe '#on_duplicate_key_update' do | ||
it 'should raise an error' do | ||
expect { adapter.on_duplicate_key_update(double(), double()) }.to raise_error('on_duplicate: :override is not currently supported in the PostgreSQL adapter') | ||
end | ||
end | ||
|
||
describe '#ignore_transform' do | ||
it 'transforms INSERT by appending WHERE NOT EXISTS clause' do | ||
|
||
insert_netto = [%q{INSERT INTO "chefs" ("id", "name", "email") SELECT 1, 'Netto', 'nettofarah@gmail.com' WHERE NOT EXISTS (SELECT 1 FROM chefs WHERE id=1);}] | ||
|
||
records = translator.records | ||
inserts = translator.inserts | ||
translated_sql = adapter.ignore_transform(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters