diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-23 22:16:50 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-02-23 22:16:50 -0300 |
commit | b91dcb23af5917d8a62afc8a5a72780aaf11f64b (patch) | |
tree | b0fd958a2a7cdb947ffb8ce1e933c531941fea3a /lib | |
parent | aac9da257f291ad8d2d4f914528881c240848bb2 (diff) | |
parent | d86e20ccbea226bb6e42da508bc040fd03ab473f (diff) | |
download | rails-b91dcb23af5917d8a62afc8a5a72780aaf11f64b.tar.gz rails-b91dcb23af5917d8a62afc8a5a72780aaf11f64b.tar.bz2 rails-b91dcb23af5917d8a62afc8a5a72780aaf11f64b.zip |
Merge pull request #356 from tamird/limited-delete
Limited delete
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/crud.rb | 1 | ||||
-rw-r--r-- | lib/arel/delete_manager.rb | 5 | ||||
-rw-r--r-- | lib/arel/nodes/delete_statement.rb | 2 | ||||
-rw-r--r-- | lib/arel/visitors/mssql.rb | 17 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 10 |
5 files changed, 30 insertions, 5 deletions
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/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 7dfa86a575..ba176a552c 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -74,14 +74,14 @@ 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 " - inject_join o.wheres, collector, AND - else - collector + collector << ' WHERE ' + 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 |