Skip to content

Commit

Permalink
Merge pull request #1 from ceberly/master
Browse files Browse the repository at this point in the history
upsert with :replace and more than one column to update was generating invalid SQL
  • Loading branch information
rcarver committed Jun 1, 2012
2 parents c8600c9 + d0c9970 commit be24233
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/orel/table.rb
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions spec/table_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit be24233

Please sign in to comment.