From d3c22e7d7870c8f8ac23b017e300cb44d5819242 Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Sat, 20 Aug 2016 09:00:45 +0100 Subject: [PATCH] Escape columns in DUPLICATE KEY statements (#42) --- lib/polo/adapters/mysql.rb | 2 +- spec/adapters/mysql_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/polo/adapters/mysql.rb b/lib/polo/adapters/mysql.rb index 7b6fe40..4d1be1a 100644 --- a/lib/polo/adapters/mysql.rb +++ b/lib/polo/adapters/mysql.rb @@ -5,7 +5,7 @@ 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})" + "`#{key}` = VALUES(`#{key}`)" end on_dup_syntax = "ON DUPLICATE KEY UPDATE #{values_syntax.join(', ')}" diff --git a/spec/adapters/mysql_spec.rb b/spec/adapters/mysql_spec.rb index 4df5044..7b211f8 100644 --- a/spec/adapters/mysql_spec.rb +++ b/spec/adapters/mysql_spec.rb @@ -29,7 +29,7 @@ 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)} + %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