lib/arel/extensions/delete_statement.rb in arel_toolkit-0.4.3 vs lib/arel/extensions/delete_statement.rb in arel_toolkit-0.4.4

- old
+ new

@@ -7,15 +7,18 @@ class DeleteStatement module DeleteStatementExtension attr_accessor :using attr_accessor :with attr_accessor :returning + attr_accessor :orders def initialize(relation = nil, wheres = []) super @returning = [] + @orders = [] + @using = [] end end prepend DeleteStatementExtension end @@ -28,27 +31,29 @@ if o.with collector = visit o.with, collector collector << ' ' end - collector << 'DELETE FROM ' - collector = visit o.relation, collector + if Gem.loaded_specs['activerecord'].version >= Gem::Version.new('6.0.0') + o = prepare_delete_statement(o) - if o.using - collector << ' USING ' - collector = inject_join o.using, collector, ', ' + if has_join_sources?(o) + collector << 'DELETE ' + visit o.relation.left, collector + collector << ' FROM ' + else + collector << 'DELETE FROM ' + end + else + collector << 'DELETE FROM ' end - if o.wheres.any? - collector << ' WHERE ' - collector = inject_join o.wheres, collector, ' AND ' - end + collector = visit o.relation, collector - unless o.returning.empty? - collector << ' RETURNING ' - collector = inject_join o.returning, collector, ', ' - end - + collect_nodes_for o.using, collector, ' USING ', ', ' + collect_nodes_for o.wheres, collector, ' WHERE ', ' AND ' + collect_nodes_for o.returning, collector, ' RETURNING ', ', ' + collect_nodes_for o.orders, collector, ' ORDER BY ' maybe_visit o.limit, collector end # rubocop:enable Metrics/AbcSize end