diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-06-23 17:18:37 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-06-23 17:18:37 -0300 |
commit | ae0c58fed1cd3aca1edd75a249fa99bc9571a8a3 (patch) | |
tree | 08e9b2ee509b6aea9dbbe5c7f24c7c37dde6796c /lib/arel/engines | |
parent | 3e6ad6e5838d20c946d7a286cb34be12aae177ff (diff) | |
download | rails-ae0c58fed1cd3aca1edd75a249fa99bc9571a8a3.tar.gz rails-ae0c58fed1cd3aca1edd75a249fa99bc9571a8a3.tar.bz2 rails-ae0c58fed1cd3aca1edd75a249fa99bc9571a8a3.zip |
Build valid SQL query for SQLite3 and PostgreSQL when updating records with limited conditions
Diffstat (limited to 'lib/arel/engines')
-rw-r--r-- | lib/arel/engines/sql/relations/writes.rb | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/lib/arel/engines/sql/relations/writes.rb b/lib/arel/engines/sql/relations/writes.rb index f7073654e9..471fceabd8 100644 --- a/lib/arel/engines/sql/relations/writes.rb +++ b/lib/arel/engines/sql/relations/writes.rb @@ -24,8 +24,7 @@ module Arel build_query \ "UPDATE #{table_sql} SET", assignment_sql, - ("WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? ), - ("LIMIT #{taken}" unless taken.blank? ) + build_update_conditions_sql end protected @@ -39,5 +38,22 @@ module Arel assignments.value end end + + def build_update_conditions_sql + conditions = "" + conditions << " WHERE #{wheres.collect(&:to_sql).join('\n\tAND ')}" unless wheres.blank? + conditions << " ORDER BY #{order_clauses.join(', ')}" unless orders.blank? + + unless taken.blank? + conditions << " LIMIT #{taken}" + + if engine.adapter_name != "MySQL" + quote_primary_key = engine.quote_column_name(table.name.classify.constantize.primary_key) + conditions = "WHERE #{quote_primary_key} IN (SELECT #{quote_primary_key} FROM #{engine.connection.quote_table_name table.name} #{conditions})" + end + end + + conditions + end end end |