Skip to content

Commit

Permalink
Allow column name with COLLATE as safe SQL string (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan authored Sep 14, 2023
1 parent ebf14dc commit bb343a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_record/connection_adapters/sqlserver/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def column_name_with_order_matcher
# [database_name].[database_owner].[table_name].[column_name] | function(one or no argument)
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\]) | \w+\((?:|\g<2>)\))
)
(?:\s+COLLATE\s+\w+)?
(?:\s+ASC|\s+DESC)?
(?:\s+NULLS\s+(?:FIRST|LAST))?
)
Expand Down
12 changes: 12 additions & 0 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,18 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase

assert_equal titles_expected, titles
end

# Collation name should not be quoted. Hardcoded values for different adapters.
coerce_tests! %r{order: allows valid arguments with COLLATE}
test "order: allows valid arguments with COLLATE" do
collation_name = "Latin1_General_CS_AS_WS"

ids_expected = Post.order(Arel.sql(%Q'author_id, title COLLATE #{collation_name} DESC')).pluck(:id)

ids = Post.order(["author_id", %Q'title COLLATE #{collation_name} DESC']).pluck(:id)

assert_equal ids_expected, ids
end
end

class ReservedWordTest < ActiveRecord::TestCase
Expand Down

0 comments on commit bb343a9

Please sign in to comment.