Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor building the update manager

  • Loading branch information...
commit 24ade58875da9e7ae80aa0640f3374a80b202202 1 parent e81e508
@jonleighton jonleighton authored
View
8 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -337,12 +337,10 @@ def sanitize_limit(limit)
# on mysql (even when aliasing the tables), but mysql allows using JOIN directly in
# an UPDATE statement, so in the mysql adapters we redefine this to do that.
def join_to_update(update, select) #:nodoc:
- subselect = select.clone
- subselect.ast.cores.last.projections = [update.ast.key]
+ subselect = select.ast.clone
+ subselect.cores.last.projections = [update.ast.key]
- update.ast.limit = nil
- update.ast.orders = []
- update.wheres = [update.ast.key.in(subselect)]
+ update.where update.ast.key.in(subselect)
end
protected
View
5 activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb
@@ -616,11 +616,10 @@ def join_to_update(update, select) #:nodoc:
subselect = Arel::SelectManager.new(select.engine, subsubselect)
subselect.project(Arel::Table.new('__active_record_temp')[update.ast.key.name])
- update.ast.limit = nil
- update.ast.orders = []
- update.wheres = [update.ast.key.in(subselect)]
+ update.where update.ast.key.in(subselect)
else
update.table select.ast.cores.last.source
+ update.wheres = select.constraints
end
end
View
5 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -522,11 +522,10 @@ def join_to_update(update, select) #:nodoc:
subselect = Arel::SelectManager.new(select.engine, subsubselect)
subselect.project(Arel::Table.new('__active_record_temp')[update.ast.key.name])
- update.ast.limit = nil
- update.ast.orders = []
- update.wheres = [update.ast.key.in(subselect)]
+ update.where update.ast.key.in(subselect)
else
update.table select.ast.cores.last.source
+ update.wheres = select.constraints
end
end
View
11 activerecord/lib/active_record/relation.rb
@@ -216,17 +216,18 @@ def update_all(updates, conditions = nil, options = {})
if conditions || options.present?
where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates)
else
- stmt = arel.compile_update(Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)))
+ stmt = Arel::UpdateManager.new(arel.engine)
+
+ stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
+ stmt.table(table)
stmt.key = table[primary_key]
if joins_values.any?
@klass.connection.join_to_update(stmt, arel)
else
- if limit = arel.limit
- stmt.take limit
- end
-
+ stmt.take(arel.limit)
stmt.order(*arel.orders)
+ stmt.wheres = arel.constraints
end
@klass.connection.update stmt, 'SQL', bind_values
Please sign in to comment.
Something went wrong with that request. Please try again.