|
| 1 | +require 'cases/helper_sqlserver' |
| 2 | +require 'models/post' |
| 3 | +require 'models/author' |
| 4 | + |
| 5 | +class InClauseTestSQLServer < ActiveRecord::TestCase |
| 6 | + |
| 7 | + fixtures :posts, :authors |
| 8 | + |
| 9 | + # The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified. |
| 10 | + |
| 11 | + it 'removes ordering from subqueries' do |
| 12 | + |
| 13 | + ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| |
| 14 | + puts payload[:sql] |
| 15 | + end |
| 16 | + |
| 17 | + # binding.pry |
| 18 | + |
| 19 | + # $debugger = true |
| 20 | + |
| 21 | + # authors_subquery = Author.where(name: ['David', 'Mary']).order(:id) |
| 22 | + authors_subquery = Author.where(name: ['David', 'Mary', 'Bob']).order(:name) |
| 23 | + posts = Post.where(author: authors_subquery) |
| 24 | + |
| 25 | + assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]" |
| 26 | + assert_not_includes posts.to_sql, "ORDER BY [authors].[name]" |
| 27 | + assert_equal 10, posts.length |
| 28 | + |
| 29 | + |
| 30 | + end |
| 31 | + |
| 32 | + it 'does not remove ordering from subquery that includes a limit' do |
| 33 | + ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| |
| 34 | + puts payload[:sql] |
| 35 | + end |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + # EXEC sp_executesql N'SELECT [posts].* FROM [posts] WHERE [posts].[author_id] IN (SELECT [authors].[id] FROM [authors] |
| 40 | + # WHERE [authors].[name] IN (@0, @1, @2) ORDER BY [authors].[name] ASC OFFSET 0 ROWS FETCH NEXT @3 ROWS ONLY)', |
| 41 | + # N'@0 nvarchar(4000), @1 nvarchar(4000), @2 nvarchar(4000), @3 int', @0 = N'David', @1 = N'Mary', @2 = N'Bob', @3 = 2 |
| 42 | + |
| 43 | + authors_subquery = Author.where(name: ['David', 'Mary', 'Bob']).order(:name).limit(2) |
| 44 | + posts = Post.where(author: authors_subquery) |
| 45 | + |
| 46 | + assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]" |
| 47 | + |
| 48 | + # $debugger = true |
| 49 | + |
| 50 | + assert_includes posts.to_sql, "ORDER BY [authors].[name]" |
| 51 | + assert_equal 7, posts.length |
| 52 | + end |
| 53 | + |
| 54 | + # |
| 55 | + it 'does not remove ordering from subquery that includes an offset' do |
| 56 | + ActiveSupport::Notifications.subscribe('sql.active_record') do |_name, _start, _finish, _id, payload| |
| 57 | + puts payload[:sql] |
| 58 | + end |
| 59 | + |
| 60 | + # EXEC sp_executesql N'SELECT [posts].* FROM [posts] WHERE [posts].[author_id] IN (SELECT [authors].[id] FROM [authors] |
| 61 | + # WHERE [authors].[name] IN (@0, @1, @2) ORDER BY [authors].[name] ASC OFFSET @3 ROWS)', |
| 62 | + # N'@0 nvarchar(4000), @1 nvarchar(4000), @2 nvarchar(4000), @3 int', @0 = N'David', @1 = N'Mary', @2 = N'Bob', @3 = 1 |
| 63 | + |
| 64 | + |
| 65 | + authors_subquery = Author.where(name: ['David', 'Mary', 'Bob']).order(:name).offset(1) |
| 66 | + posts = Post.where(author: authors_subquery) |
| 67 | + |
| 68 | + assert_includes authors_subquery.to_sql, "ORDER BY [authors].[name]" |
| 69 | + assert_includes posts.to_sql, "ORDER BY [authors].[name]" |
| 70 | + assert_equal 8, posts.length |
| 71 | + end |
| 72 | + |
| 73 | + |
| 74 | +end |
0 commit comments