From 65135d00a854edfd1684f5163db8e68fdb956ae0 Mon Sep 17 00:00:00 2001 From: Bradford Folkens Date: Wed, 29 Feb 2012 09:47:28 -0600 Subject: Delete should respect 'limit' Conflicts: lib/arel/visitors/to_sql.rb --- lib/arel/crud.rb | 1 + lib/arel/delete_manager.rb | 5 +++++ lib/arel/nodes/delete_statement.rb | 2 ++ lib/arel/visitors/to_sql.rb | 6 +++--- 4 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/arel/crud.rb b/lib/arel/crud.rb index 2dfe27445c..d310c7381f 100644 --- a/lib/arel/crud.rb +++ b/lib/arel/crud.rb @@ -31,6 +31,7 @@ module Arel def compile_delete dm = DeleteManager.new + dm.take @ast.limit.expr if @ast.limit dm.wheres = @ctx.wheres dm.from @ctx.froms dm diff --git a/lib/arel/delete_manager.rb b/lib/arel/delete_manager.rb index af33c60740..20e988e01f 100644 --- a/lib/arel/delete_manager.rb +++ b/lib/arel/delete_manager.rb @@ -11,6 +11,11 @@ module Arel self end + def take limit + @ast.limit = Nodes::Limit.new(Nodes.build_quoted(limit)) if limit + self + end + def wheres= list @ast.wheres = list end diff --git a/lib/arel/nodes/delete_statement.rb b/lib/arel/nodes/delete_statement.rb index 3bac8225ec..8aaf8ca0b6 100644 --- a/lib/arel/nodes/delete_statement.rb +++ b/lib/arel/nodes/delete_statement.rb @@ -1,6 +1,8 @@ module Arel module Nodes class DeleteStatement < Arel::Nodes::Binary + attr_accessor :limit + alias :relation :left alias :relation= :left= alias :wheres :right diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 7dfa86a575..6084f5746c 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -78,10 +78,10 @@ module Arel collector = visit o.relation, collector if o.wheres.any? collector << " WHERE " - inject_join o.wheres, collector, AND - else - collector + collector = inject_join o.wheres, collector, AND end + + maybe_visit o.limit, collector end # FIXME: we should probably have a 2-pass visitor for this -- cgit v1.2.3 From d86e20ccbea226bb6e42da508bc040fd03ab473f Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 23 Feb 2015 09:58:22 -0800 Subject: Special limited delete handling in MSSQL Refernce: https://technet.microsoft.com/en-us/library/ms175486%28v=sql.105%29.aspx --- lib/arel/visitors/mssql.rb | 17 +++++++++++++++++ lib/arel/visitors/to_sql.rb | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/arel/visitors/mssql.rb b/lib/arel/visitors/mssql.rb index 7c65ad33f2..92362a0c5f 100644 --- a/lib/arel/visitors/mssql.rb +++ b/lib/arel/visitors/mssql.rb @@ -66,6 +66,23 @@ module Arel end end + def visit_Arel_Nodes_DeleteStatement o, collector + collector << 'DELETE ' + if o.limit + collector << 'TOP (' + visit o.limit.expr, collector + collector << ') ' + end + collector << 'FROM ' + collector = visit o.relation, collector + if o.wheres.any? + collector << ' WHERE ' + inject_join o.wheres, collector, AND + else + collector + end + end + def determine_order_by orders, x if orders.any? orders diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 6084f5746c..ba176a552c 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -74,10 +74,10 @@ module Arel end def visit_Arel_Nodes_DeleteStatement o, collector - collector << "DELETE FROM " + collector << 'DELETE FROM ' collector = visit o.relation, collector if o.wheres.any? - collector << " WHERE " + collector << ' WHERE ' collector = inject_join o.wheres, collector, AND end -- cgit v1.2.3