Skip to content

Commit

Permalink
Handle UPDATE/DELETE with OFFSET in Arel
Browse files Browse the repository at this point in the history
  • Loading branch information
kamipo committed Oct 1, 2018
1 parent 322c570 commit 6d40d2d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
6 changes: 4 additions & 2 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -356,11 +356,12 @@ def update_all(updates)
stmt.set Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
end

if has_join_values? || offset_value
if has_join_values?
@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
else
stmt.key = arel_attribute(primary_key)
stmt.take(arel.limit)
stmt.offset(arel.offset)
stmt.order(*arel.orders)
stmt.wheres = arel.constraints
end
Expand Down Expand Up @@ -484,11 +485,12 @@ def delete_all
stmt = Arel::DeleteManager.new
stmt.from(table)

if has_join_values? || offset_value
if has_join_values?
@klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
else
stmt.key = arel_attribute(primary_key)
stmt.take(arel.limit)
stmt.offset(arel.offset)
stmt.order(*arel.orders)
stmt.wheres = arel.constraints
end
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/arel/nodes/delete_statement.rb
Expand Up @@ -3,7 +3,7 @@
module Arel # :nodoc: all
module Nodes
class DeleteStatement < Arel::Nodes::Node
attr_accessor :left, :right, :orders, :limit, :key
attr_accessor :left, :right, :orders, :limit, :offset, :key

alias :relation :left
alias :relation= :left=
Expand All @@ -16,6 +16,7 @@ def initialize(relation = nil, wheres = [])
@right = wheres
@orders = []
@limit = nil
@offset = nil
@key = nil
end

Expand All @@ -26,7 +27,7 @@ def initialize_copy(other)
end

def hash
[self.class, @left, @right, @orders, @limit, @key].hash
[self.class, @left, @right, @orders, @limit, @offset, @key].hash
end

def eql?(other)
Expand All @@ -35,6 +36,7 @@ def eql?(other)
self.right == other.right &&
self.orders == other.orders &&
self.limit == other.limit &&
self.offset == other.offset &&
self.key == other.key
end
alias :== :eql?
Expand Down
6 changes: 4 additions & 2 deletions activerecord/lib/arel/nodes/update_statement.rb
Expand Up @@ -3,14 +3,15 @@
module Arel # :nodoc: all
module Nodes
class UpdateStatement < Arel::Nodes::Node
attr_accessor :relation, :wheres, :values, :orders, :limit, :key
attr_accessor :relation, :wheres, :values, :orders, :limit, :offset, :key

def initialize
@relation = nil
@wheres = []
@values = []
@orders = []
@limit = nil
@offset = nil
@key = nil
end

Expand All @@ -21,7 +22,7 @@ def initialize_copy(other)
end

def hash
[@relation, @wheres, @values, @orders, @limit, @key].hash
[@relation, @wheres, @values, @orders, @limit, @offset, @key].hash
end

def eql?(other)
Expand All @@ -31,6 +32,7 @@ def eql?(other)
self.values == other.values &&
self.orders == other.orders &&
self.limit == other.limit &&
self.offset == other.offset &&
self.key == other.key
end
alias :== :eql?
Expand Down
5 changes: 5 additions & 0 deletions activerecord/lib/arel/tree_manager.rb
Expand Up @@ -10,6 +10,11 @@ def take(limit)
self
end

def offset(offset)
@ast.offset = Nodes::Offset.new(Nodes.build_quoted(offset)) if offset
self
end

def order(*expr)
@ast.orders = expr
self
Expand Down
28 changes: 16 additions & 12 deletions activerecord/lib/arel/visitors/mysql.rb
Expand Up @@ -56,18 +56,6 @@ def visit_Arel_Nodes_SelectCore(o, collector)
super
end

def visit_Arel_Nodes_UpdateStatement(o, collector)
collector << "UPDATE "
collector = visit o.relation, collector

unless o.values.empty?
collector << " SET "
collector = inject_join o.values, collector, ", "
end

collect_where_for(o, collector)
end

def visit_Arel_Nodes_Concat(o, collector)
collector << " CONCAT("
visit o.left, collector
Expand All @@ -77,7 +65,23 @@ def visit_Arel_Nodes_Concat(o, collector)
collector
end

def build_subselect(key, o)
subselect = super

# Materialize subquery by adding distinct
# to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
subselect.distinct unless subselect.limit || subselect.offset || subselect.orders.any?

Nodes::SelectStatement.new.tap do |stmt|
core = stmt.cores.last
core.froms = Nodes::Grouping.new(subselect).as("__active_record_temp")
core.projections = [Arel.sql(quote_column_name(key.name))]
end
end

def collect_where_for(o, collector)
return super if o.offset

unless o.wheres.empty?
collector << " WHERE "
collector = inject_join o.wheres, collector, " AND "
Expand Down
3 changes: 2 additions & 1 deletion activerecord/lib/arel/visitors/to_sql.rb
Expand Up @@ -88,6 +88,7 @@ def build_subselect(key, o)
core.wheres = o.wheres
core.projections = [key]
stmt.limit = o.limit
stmt.offset = o.offset
stmt.orders = o.orders
stmt
end
Expand Down Expand Up @@ -800,7 +801,7 @@ def inject_join(list, collector, join_str)
end

def collect_where_for(o, collector)
if o.orders.empty? && o.limit.nil?
if o.orders.empty? && o.limit.nil? && o.offset.nil?
wheres = o.wheres
else
wheres = [Nodes::In.new(o.key, [build_subselect(o.key, o)])]
Expand Down

0 comments on commit 6d40d2d

Please sign in to comment.