Browse files

upsert with :replace and more than one column to update was generatin…

…g invalid SQL
  • Loading branch information...
1 parent c8600c9 commit d0c9970aed6e6adfebf813109b6fcb0b40434b65 Chris Eberly committed May 31, 2012
Showing with 12 additions and 4 deletions.
  1. +1 −1 lib/orel/table.rb
  2. +11 −3 spec/table_spec.rb
View
2 lib/orel/table.rb
@@ -186,7 +186,7 @@ def upsert_statement(options)
when :increment
values.map { |v| "#{v}=#{v}+VALUES(#{v})" }.join(',')
when :replace
- values.map { |v| "#{v}=VALUES(#{v})" }.join(' ')
+ values.map { |v| "#{v}=VALUES(#{v})" }.join(',')
else
raise ArgumentError, "Unknown value for :with - #{with.inspect}"
end
View
14 spec/table_spec.rb
@@ -128,12 +128,20 @@
sql.should == %{INSERT INTO `users_and_things_users` (`age`, `first_name`, `last_name`) VALUES (30, 'John', 'Smith') ON DUPLICATE KEY UPDATE age=age+VALUES(age)}
end
- specify "#upsert_statement with :replace" do
+ specify "#upsert_statement with :increment and multiple updates" do
sql = subject.upsert_statement(
:insert => { :first_name => "John", :last_name => "Smith", :age => 30 },
- :update => { :values => [:age], :with => :replace }
+ :update => { :values => [:age, :last_name], :with => :increment }
)
- sql.should == %{INSERT INTO `users_and_things_users` (`age`, `first_name`, `last_name`) VALUES (30, 'John', 'Smith') ON DUPLICATE KEY UPDATE age=VALUES(age)}
+ sql.should == %{INSERT INTO `users_and_things_users` (`age`, `first_name`, `last_name`) VALUES (30, 'John', 'Smith') ON DUPLICATE KEY UPDATE age=age+VALUES(age),last_name=last_name+VALUES(last_name)}
+ end
+
+ specify "#upsert_statement with :replace and multiple updates" do
+ sql = subject.upsert_statement(
+ :insert => { :first_name => "John", :last_name => "Smith", :age => 30 },
+ :update => { :values => [:age, :last_name], :with => :replace }
+ )
+ sql.should == %{INSERT INTO `users_and_things_users` (`age`, `first_name`, `last_name`) VALUES (30, 'John', 'Smith') ON DUPLICATE KEY UPDATE age=VALUES(age),last_name=VALUES(last_name)}
end
specify "#update_statement" do

0 comments on commit d0c9970

Please sign in to comment.