1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make update_all preparable

Before:

```
  Pet Update All (0.8ms)  UPDATE `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` SET `pets`.`name` = 'Bob' WHERE `toys`.`name` = ?  [["name", "Bone"]]
```

After:

```
  Pet Update All (1.1ms)  UPDATE `pets` LEFT OUTER JOIN `toys` ON `toys`.`pet_id` = `pets`.`pet_id` SET `pets`.`name` = ? WHERE `toys`.`name` = ?  [["name", "Bob"], ["name", "Bone"]]
```
This commit is contained in:
Ryuta Kamizono 2018-09-28 01:03:13 +09:00
parent 140ec68c0d
commit 8e123847e8
2 changed files with 18 additions and 2 deletions

View file

@ -348,10 +348,14 @@ module ActiveRecord
end end
stmt = Arel::UpdateManager.new stmt = Arel::UpdateManager.new
stmt.set Arel.sql(@klass.sanitize_sql_for_assignment(updates, table.name))
stmt.table(table) stmt.table(table)
if updates.is_a?(Hash)
stmt.set _substitute_values(updates)
else
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
end
if has_join_values? || offset_value if has_join_values? || offset_value
@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key)) @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
else else
@ -625,6 +629,14 @@ module ActiveRecord
end end
private private
def _substitute_values(values)
values.map do |name, value|
attr = arel_attribute(name)
type = klass.type_for_attribute(attr.name)
bind = predicate_builder.build_bind_attribute(attr.name, type.cast(value))
[attr, bind]
end
end
def has_join_values? def has_join_values?
joins_values.any? || left_outer_joins_values.any? joins_values.any? || left_outer_joins_values.any?

View file

@ -37,6 +37,10 @@ module Arel # :nodoc: all
visit o.expr, collector visit o.expr, collector
end end
def visit_Arel_Nodes_UnqualifiedColumn(o, collector)
visit o.expr, collector
end
### ###
# :'( # :'(
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214