mirror of https://github.com/rails/rails
Improve `compile_update` and `compile_delete`
To be used from `update_all` and `delete_all`.
This commit is contained in:
parent
520bb79411
commit
cd809b3176
|
@ -469,14 +469,6 @@ module ActiveRecord
|
||||||
return relation.update_all(updates)
|
return relation.update_all(updates)
|
||||||
end
|
end
|
||||||
|
|
||||||
stmt = Arel::UpdateManager.new
|
|
||||||
stmt.table(arel.join_sources.empty? ? table : arel.source)
|
|
||||||
stmt.key = table[primary_key]
|
|
||||||
stmt.take(arel.limit)
|
|
||||||
stmt.offset(arel.offset)
|
|
||||||
stmt.order(*arel.orders)
|
|
||||||
stmt.wheres = arel.constraints
|
|
||||||
|
|
||||||
if updates.is_a?(Hash)
|
if updates.is_a?(Hash)
|
||||||
if klass.locking_enabled? &&
|
if klass.locking_enabled? &&
|
||||||
!updates.key?(klass.locking_column) &&
|
!updates.key?(klass.locking_column) &&
|
||||||
|
@ -484,12 +476,14 @@ module ActiveRecord
|
||||||
attr = table[klass.locking_column]
|
attr = table[klass.locking_column]
|
||||||
updates[attr.name] = _increment_attribute(attr)
|
updates[attr.name] = _increment_attribute(attr)
|
||||||
end
|
end
|
||||||
stmt.set _substitute_values(updates)
|
values = _substitute_values(updates)
|
||||||
else
|
else
|
||||||
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
||||||
end
|
end
|
||||||
|
|
||||||
@klass.connection.update stmt, "#{@klass} Update All"
|
stmt = arel.compile_update(values, table[primary_key])
|
||||||
|
|
||||||
|
klass.connection.update(stmt, "#{klass} Update All")
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(id = :all, attributes) # :nodoc:
|
def update(id = :all, attributes) # :nodoc:
|
||||||
|
@ -611,15 +605,9 @@ module ActiveRecord
|
||||||
return relation.delete_all
|
return relation.delete_all
|
||||||
end
|
end
|
||||||
|
|
||||||
stmt = Arel::DeleteManager.new
|
stmt = arel.compile_delete(table[primary_key])
|
||||||
stmt.from(arel.join_sources.empty? ? table : arel.source)
|
|
||||||
stmt.key = table[primary_key]
|
|
||||||
stmt.take(arel.limit)
|
|
||||||
stmt.offset(arel.offset)
|
|
||||||
stmt.order(*arel.orders)
|
|
||||||
stmt.wheres = arel.constraints
|
|
||||||
|
|
||||||
affected = @klass.connection.delete(stmt, "#{@klass} Destroy")
|
affected = klass.connection.delete(stmt, "#{klass} Destroy")
|
||||||
|
|
||||||
reset
|
reset
|
||||||
affected
|
affected
|
||||||
|
|
|
@ -4,23 +4,6 @@ module Arel # :nodoc: all
|
||||||
###
|
###
|
||||||
# FIXME hopefully we can remove this
|
# FIXME hopefully we can remove this
|
||||||
module Crud
|
module Crud
|
||||||
def compile_update(values, pk)
|
|
||||||
um = UpdateManager.new
|
|
||||||
|
|
||||||
if Nodes::SqlLiteral === values
|
|
||||||
relation = @ctx.from
|
|
||||||
else
|
|
||||||
relation = values.first.first.relation
|
|
||||||
end
|
|
||||||
um.key = pk
|
|
||||||
um.table relation
|
|
||||||
um.set values
|
|
||||||
um.take @ast.limit.expr if @ast.limit
|
|
||||||
um.order(*@ast.orders)
|
|
||||||
um.wheres = @ctx.wheres
|
|
||||||
um
|
|
||||||
end
|
|
||||||
|
|
||||||
def compile_insert(values)
|
def compile_insert(values)
|
||||||
im = create_insert
|
im = create_insert
|
||||||
im.insert values
|
im.insert values
|
||||||
|
@ -31,11 +14,24 @@ module Arel # :nodoc: all
|
||||||
InsertManager.new
|
InsertManager.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_delete
|
def compile_update(values, key = nil)
|
||||||
dm = DeleteManager.new
|
um = UpdateManager.new(source)
|
||||||
dm.take @ast.limit.expr if @ast.limit
|
um.set(values)
|
||||||
dm.wheres = @ctx.wheres
|
um.take(limit)
|
||||||
dm.from @ctx.froms
|
um.offset(offset)
|
||||||
|
um.order(*orders)
|
||||||
|
um.wheres = constraints
|
||||||
|
um.key = key
|
||||||
|
um
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile_delete(key = nil)
|
||||||
|
dm = DeleteManager.new(source)
|
||||||
|
dm.take(limit)
|
||||||
|
dm.offset(offset)
|
||||||
|
dm.order(*orders)
|
||||||
|
dm.wheres = constraints
|
||||||
|
dm.key = key
|
||||||
dm
|
dm
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue