Permalink
Browse files

Refactor building the update manager

  • Loading branch information...
1 parent 43b99f2 commit fe0ec855419e1deba47277c96275a16ecf79bc9a @jonleighton jonleighton committed Aug 9, 2011
@@ -310,12 +310,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
@@ -594,11 +594,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
@@ -508,11 +508,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
@@ -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

0 comments on commit fe0ec85

Please sign in to comment.