aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/arel/visitors/to_sql.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-09-30 18:30:47 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-09-30 21:21:54 +0900
commit859fba7c4bf7d33b4f9655914ed4bdc85380552e (patch)
treefd777f6b29c1b02575248e3dfe4cfcba13968472 /activerecord/lib/arel/visitors/to_sql.rb
parent298629cefd845caf2cea28eb9cee2b538accc4ee (diff)
downloadrails-859fba7c4bf7d33b4f9655914ed4bdc85380552e.tar.gz
rails-859fba7c4bf7d33b4f9655914ed4bdc85380552e.tar.bz2
rails-859fba7c4bf7d33b4f9655914ed4bdc85380552e.zip
Handle DELETE with LIMIT in Arel
MySQL supports DELETE with LIMIT and ORDER BY. https://dev.mysql.com/doc/refman/8.0/en/delete.html Before: ``` Post Destroy (1.0ms) DELETE FROM `posts` WHERE `posts`.`id` IN (SELECT `id` FROM (SELECT `posts`.`id` FROM `posts` WHERE `posts`.`author_id` = ? ORDER BY `posts`.`id` ASC LIMIT ?) __active_record_temp) [["author_id", 1], ["LIMIT", 1]] ``` After: ``` Post Destroy (0.4ms) DELETE FROM `posts` WHERE `posts`.`author_id` = ? ORDER BY `posts`.`id` ASC LIMIT ? [["author_id", 1], ["LIMIT", 1]] ```
Diffstat (limited to 'activerecord/lib/arel/visitors/to_sql.rb')
-rw-r--r--activerecord/lib/arel/visitors/to_sql.rb34
1 files changed, 17 insertions, 17 deletions
diff --git a/activerecord/lib/arel/visitors/to_sql.rb b/activerecord/lib/arel/visitors/to_sql.rb
index 87f8c36f9b..575bfd6e36 100644
--- a/activerecord/lib/arel/visitors/to_sql.rb
+++ b/activerecord/lib/arel/visitors/to_sql.rb
@@ -76,12 +76,8 @@ module Arel # :nodoc: all
def visit_Arel_Nodes_DeleteStatement(o, collector)
collector << "DELETE FROM "
collector = visit o.relation, collector
- if o.wheres.any?
- collector << WHERE
- collector = inject_join o.wheres, collector, AND
- end
- maybe_visit o.limit, collector
+ collect_where_for(o, collector)
end
# FIXME: we should probably have a 2-pass visitor for this
@@ -97,12 +93,6 @@ module Arel # :nodoc: all
end
def visit_Arel_Nodes_UpdateStatement(o, collector)
- if o.orders.empty? && o.limit.nil?
- wheres = o.wheres
- else
- wheres = [Nodes::In.new(o.key, [build_subselect(o.key, o)])]
- end
-
collector << "UPDATE "
collector = visit o.relation, collector
unless o.values.empty?
@@ -110,12 +100,7 @@ module Arel # :nodoc: all
collector = inject_join o.values, collector, ", "
end
- unless wheres.empty?
- collector << " WHERE "
- collector = inject_join wheres, collector, " AND "
- end
-
- collector
+ collect_where_for(o, collector)
end
def visit_Arel_Nodes_InsertStatement(o, collector)
@@ -814,6 +799,21 @@ module Arel # :nodoc: all
}
end
+ def collect_where_for(o, collector)
+ if o.orders.empty? && o.limit.nil?
+ wheres = o.wheres
+ else
+ wheres = [Nodes::In.new(o.key, [build_subselect(o.key, o)])]
+ end
+
+ unless wheres.empty?
+ collector << " WHERE "
+ collector = inject_join wheres, collector, " AND "
+ end
+
+ collector
+ end
+
def infix_value(o, collector, value)
collector = visit o.left, collector
collector << value