Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add explicit order-by clause for windowed results. Fixes #161.

  • Loading branch information...
commit 4dfb66e5933bc2e4714e297b28cf603e2c623cc6 1 parent cd19c2f
@metaskills metaskills authored
View
5 CHANGELOG
@@ -1,4 +1,9 @@
+* 3.2.1 *
+
+* Add explicit order-by clause for windowed results. Fixes #161.
+
+
* 3.2.0 *
* ActiveRecord explain (SHOWPLAN) support.
View
2  lib/active_record/connection_adapters/sqlserver/version.rb
@@ -3,7 +3,7 @@ module ConnectionAdapters
module Sqlserver
module Version
- VERSION = '3.2.0'
+ VERSION = '3.2.1'
end
end
View
4 lib/arel/visitors/sqlserver.rb
@@ -102,7 +102,7 @@ def visit_Arel_Nodes_SelectStatement(o)
def visit_Arel_Nodes_UpdateStatement(o)
if o.orders.any? && o.limit.nil?
- o.limit = Nodes::Limit.new(2147483647)
+ o.limit = Nodes::Limit.new(9223372036854775807)
end
super
end
@@ -166,6 +166,7 @@ def visit_Arel_Nodes_SelectStatementWithOutOffset(o, windowed=false)
end
def visit_Arel_Nodes_SelectStatementWithOffset(o)
+ o.limit ||= Arel::Nodes::Limit.new(9223372036854775807)
orders = rowtable_orders(o)
[ "SELECT",
(visit(o.limit) if o.limit && !windowed_single_distinct_select_statement?(o)),
@@ -175,6 +176,7 @@ def visit_Arel_Nodes_SelectStatementWithOffset(o)
visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
") AS [__rnt]",
(visit(o.offset) if o.offset),
+ "ORDER BY [__rnt].[__rn] ASC"
].compact.join ' '
end
View
6 test/cases/offset_and_limit_test_sqlserver.rb
@@ -20,8 +20,8 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
context 'When selecting with offset' do
- should 'have no limit (top) if only offset is passed' do
- assert_sql(/SELECT \[__rnt\]\.\* FROM.*WHERE \[__rnt\]\.\[__rn\] > \(1\)/) { Book.all(:offset=>1) }
+ should 'have limit (top) of 2147483647 if only offset is passed' do
+ assert_sql(/SELECT TOP \(9223372036854775807\) \[__rnt\]\.\* FROM.*WHERE \[__rnt\]\.\[__rn\] > \(1\)/) { Book.all(:offset=>1) }
end
end
@@ -46,7 +46,7 @@ class OffsetAndLimitTestSqlserver < ActiveRecord::TestCase
end
should 'alter SQL to limit number of records returned offset by specified amount' do
- sql = %|EXEC sp_executesql N'SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > (5)'|
+ sql = %|EXEC sp_executesql N'SELECT TOP (3) [__rnt].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [books].[id] ASC) AS [__rn], [books].* FROM [books] ) AS [__rnt] WHERE [__rnt].[__rn] > (5) ORDER BY [__rnt].[__rn] ASC'|
assert_sql(sql) { Book.limit(3).offset(5).all }
end
View
1  test/cases/sqlserver_helper.rb
@@ -42,6 +42,7 @@ class StringDefaultsBigView < ActiveRecord::Base ; self.table_name = 'string_def
class SqlServerNaturalPkData < ActiveRecord::Base ; self.table_name = 'natural_pk_data' ; self.primary_key = 'legacy_id' ; end
class SqlServerTinyintPk < ActiveRecord::Base ; self.table_name = 'tinyint_pk_table' ; end
class SqlServerNaturalPkIntData < ActiveRecord::Base ; self.table_name = 'natural_pk_int_data' ; end
+class SqlServerOrderRowNumber < ActiveRecord::Base ; self.table_name = 'order_row_number' ; end
class SqlServerNaturalPkDataSchema < ActiveRecord::Base ; self.table_name = 'test.sql_server_schema_natural_id' ; end
class SqlServerQuotedTable < ActiveRecord::Base ; self.table_name = 'quoted-table' ; end
class SqlServerQuotedView1 < ActiveRecord::Base ; self.table_name = 'quoted-view1' ; end
View
17 test/schema/sqlserver_specific_schema.rb
@@ -91,6 +91,23 @@
t.string :name
end
+ # http://blogs.msdn.com/b/craigfr/archive/2008/03/19/ranking-functions-row-number.aspx
+ execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'order_row_number') DROP TABLE order_row_number"
+ execute <<-ORDERROWNUMBERSQL
+ CREATE TABLE [order_row_number] (id int IDENTITY, a int, b int, c int)
+ CREATE UNIQUE CLUSTERED INDEX [idx_order_row_number_id] ON [order_row_number] ([id])
+ INSERT [order_row_number] VALUES (0, 1, 8)
+ INSERT [order_row_number] VALUES (0, 3, 6)
+ INSERT [order_row_number] VALUES (0, 5, 4)
+ INSERT [order_row_number] VALUES (0, 7, 2)
+ INSERT [order_row_number] VALUES (0, 9, 0)
+ INSERT [order_row_number] VALUES (1, 0, 9)
+ INSERT [order_row_number] VALUES (1, 2, 7)
+ INSERT [order_row_number] VALUES (1, 4, 5)
+ INSERT [order_row_number] VALUES (1, 6, 3)
+ INSERT [order_row_number] VALUES (1, 8, 1)
+ ORDERROWNUMBERSQL
+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'natural_pk_data') DROP TABLE natural_pk_data"
execute <<-NATURALPKTABLESQL
CREATE TABLE natural_pk_data(
Please sign in to comment.
Something went wrong with that request. Please try again.