1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Special limited delete handling in MSSQL

Refernce:
https://technet.microsoft.com/en-us/library/ms175486%28v=sql.105%29.aspx
This commit is contained in:
Tamir Duberstein 2015-02-23 09:58:22 -08:00
parent 65135d00a8
commit d86e20ccbe
3 changed files with 28 additions and 2 deletions

View file

@ -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

View file

@ -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

View file

@ -45,6 +45,15 @@ module Arel
connection.verify
end
it 'should use TOP for limited deletes' do
stmt = Nodes::DeleteStatement.new
stmt.relation = @table
stmt.limit = Nodes::Limit.new(10)
sql = compile(stmt)
sql.must_be_like "DELETE TOP (10) FROM \"users\""
end
it 'should go over query ORDER BY if .order()' do
stmt = Nodes::SelectStatement.new
stmt.limit = Nodes::Limit.new(10)