diff options
author | Tamir Duberstein <tamird@squareup.com> | 2015-02-23 09:58:22 -0800 |
---|---|---|
committer | Tamir Duberstein <tamird@squareup.com> | 2015-02-23 09:58:22 -0800 |
commit | d86e20ccbea226bb6e42da508bc040fd03ab473f (patch) | |
tree | b0fd958a2a7cdb947ffb8ce1e933c531941fea3a | |
parent | 65135d00a854edfd1684f5163db8e68fdb956ae0 (diff) | |
download | rails-d86e20ccbea226bb6e42da508bc040fd03ab473f.tar.gz rails-d86e20ccbea226bb6e42da508bc040fd03ab473f.tar.bz2 rails-d86e20ccbea226bb6e42da508bc040fd03ab473f.zip |
Special limited delete handling in MSSQL
Refernce:
https://technet.microsoft.com/en-us/library/ms175486%28v=sql.105%29.aspx
-rw-r--r-- | lib/arel/visitors/mssql.rb | 17 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 4 | ||||
-rw-r--r-- | test/visitors/test_mssql.rb | 9 |
3 files changed, 28 insertions, 2 deletions
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 diff --git a/test/visitors/test_mssql.rb b/test/visitors/test_mssql.rb index 7574aeb0a2..fe228bce4b 100644 --- a/test/visitors/test_mssql.rb +++ b/test/visitors/test_mssql.rb @@ -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) |