diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-09-30 18:30:47 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2018-09-30 21:21:54 +0900 |
commit | 859fba7c4bf7d33b4f9655914ed4bdc85380552e (patch) | |
tree | fd777f6b29c1b02575248e3dfe4cfcba13968472 /activerecord/lib/arel/visitors | |
parent | 298629cefd845caf2cea28eb9cee2b538accc4ee (diff) | |
download | rails-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')
-rw-r--r-- | activerecord/lib/arel/visitors/mysql.rb | 22 | ||||
-rw-r--r-- | activerecord/lib/arel/visitors/to_sql.rb | 34 |
2 files changed, 30 insertions, 26 deletions
diff --git a/activerecord/lib/arel/visitors/mysql.rb b/activerecord/lib/arel/visitors/mysql.rb index ee75b6bb25..0f7d5aa803 100644 --- a/activerecord/lib/arel/visitors/mysql.rb +++ b/activerecord/lib/arel/visitors/mysql.rb @@ -65,6 +65,19 @@ module Arel # :nodoc: all 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 + collector << ", " + visit o.right, collector + collector << ") " + collector + end + + def collect_where_for(o, collector) unless o.wheres.empty? collector << " WHERE " collector = inject_join o.wheres, collector, " AND " @@ -77,15 +90,6 @@ module Arel # :nodoc: all maybe_visit o.limit, collector end - - def visit_Arel_Nodes_Concat(o, collector) - collector << " CONCAT(" - visit o.left, collector - collector << ", " - visit o.right, collector - collector << ") " - collector - end end end end 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 |