提交 8e123847 编写于 作者: R Ryuta Kamizono

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"]]
```
上级 140ec68c
...@@ -348,10 +348,14 @@ def update_all(updates) ...@@ -348,10 +348,14 @@ def update_all(updates)
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 @@ def load_records(records) ...@@ -625,6 +629,14 @@ def load_records(records)
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?
......
...@@ -37,6 +37,10 @@ def visit_Arel_Nodes_Bin(o, collector) ...@@ -37,6 +37,10 @@ def visit_Arel_Nodes_Bin(o, collector)
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册