mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
859fba7c4b
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]] ```
95 lines
2.5 KiB
Ruby
95 lines
2.5 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Arel # :nodoc: all
|
|
module Visitors
|
|
class MySQL < Arel::Visitors::ToSql
|
|
private
|
|
def visit_Arel_Nodes_Union(o, collector, suppress_parens = false)
|
|
unless suppress_parens
|
|
collector << "( "
|
|
end
|
|
|
|
collector = case o.left
|
|
when Arel::Nodes::Union
|
|
visit_Arel_Nodes_Union o.left, collector, true
|
|
else
|
|
visit o.left, collector
|
|
end
|
|
|
|
collector << " UNION "
|
|
|
|
collector = case o.right
|
|
when Arel::Nodes::Union
|
|
visit_Arel_Nodes_Union o.right, collector, true
|
|
else
|
|
visit o.right, collector
|
|
end
|
|
|
|
if suppress_parens
|
|
collector
|
|
else
|
|
collector << " )"
|
|
end
|
|
end
|
|
|
|
def visit_Arel_Nodes_Bin(o, collector)
|
|
collector << "BINARY "
|
|
visit o.expr, collector
|
|
end
|
|
|
|
def visit_Arel_Nodes_UnqualifiedColumn(o, collector)
|
|
visit o.expr, collector
|
|
end
|
|
|
|
###
|
|
# :'(
|
|
# http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
|
|
def visit_Arel_Nodes_SelectStatement(o, collector)
|
|
if o.offset && !o.limit
|
|
o.limit = Arel::Nodes::Limit.new(18446744073709551615)
|
|
end
|
|
super
|
|
end
|
|
|
|
def visit_Arel_Nodes_SelectCore(o, collector)
|
|
o.froms ||= Arel.sql("DUAL")
|
|
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
|
|
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 "
|
|
end
|
|
|
|
unless o.orders.empty?
|
|
collector << " ORDER BY "
|
|
collector = inject_join o.orders, collector, ", "
|
|
end
|
|
|
|
maybe_visit o.limit, collector
|
|
end
|
|
end
|
|
end
|
|
end
|