Delete should respect 'limit' #104

Closed
wants to merge 1 commit into
from
View
@@ -54,6 +54,7 @@ def insert values
def compile_delete
dm = DeleteManager.new @engine
+ dm.take @ast.limit.expr if @ast.limit
dm.wheres = @ctx.wheres
dm.from @ctx.froms
dm
@@ -11,6 +11,11 @@ def from relation
self
end
+ def take limit
+ @ast.limit = Nodes::Limit.new(limit) if limit
+ self
+ end
+
def wheres= list
@ast.wheres = list
end
@@ -1,13 +1,16 @@
module Arel
module Nodes
class DeleteStatement < Arel::Nodes::Binary
+ attr_accessor :limit
+
alias :relation :left
alias :relation= :left=
alias :wheres :right
alias :wheres= :right=
def initialize relation = nil, wheres = []
super
+ @limit = nil
end
def initialize_copy other
@@ -23,7 +23,8 @@ def accept object
def visit_Arel_Nodes_DeleteStatement o
[
"DELETE FROM #{visit o.relation}",
- ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
+ ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?),
+ (visit(o.limit) if o.limit)
].compact.join ' '
end
@@ -8,6 +8,14 @@ module Arel
end
end
+ it 'handles limit properly' do
+ table = Table.new(:users)
+ dm = Arel::DeleteManager.new Table.engine
+ dm.take 10
+ dm.from table
+ assert_match(/LIMIT 10/, dm.to_sql)
+ end
+
describe 'from' do
it 'uses from' do
table = Table.new(:users)