Skip to content

Commit 5d6d4d1

Browse files
authored
Merge pull request rails#53230 from kamipo/allow_select_with_reserved_words_aliases
Allow `select` with reserved words aliases
2 parents f07592f + eae6aba commit 5d6d4d1

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,14 +2227,16 @@ def arel_column_aliases_from_hash(fields)
22272227
case columns_aliases
22282228
when Hash
22292229
columns_aliases.map do |column, column_alias|
2230-
arel_column_with_table(table_name, column.to_s).as(column_alias.to_s)
2230+
arel_column_with_table(table_name, column.to_s)
2231+
.as(model.adapter_class.quote_column_name(column_alias.to_s))
22312232
end
22322233
when Array
22332234
columns_aliases.map do |column|
22342235
arel_column_with_table(table_name, column.to_s)
22352236
end
22362237
when String, Symbol
2237-
arel_column(key).as(columns_aliases.to_s)
2238+
arel_column(key)
2239+
.as(model.adapter_class.quote_column_name(columns_aliases.to_s))
22382240
end
22392241
end
22402242
end

activerecord/test/cases/relation/select_test.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def test_select_with_non_field_values
1919
end
2020

2121
def test_select_with_non_field_hash_values
22-
expected = %r/\ASELECT 1 AS a, foo\(\) AS b, #{Regexp.escape(quote_table_name("bar"))} AS c FROM/
22+
q = -> name { Regexp.escape(quote_table_name(name)) }
23+
expected = %r/\ASELECT 1 AS #{q["a"]}, foo\(\) AS #{q["b"]}, #{q["bar"]} AS #{q["c"]} FROM/
2324
assert_match expected, Post.select("1" => :a, "foo()" => :b, :bar => :c).to_sql
2425
end
2526

@@ -30,6 +31,13 @@ def test_select_with_hash_argument
3031
assert_equal "Welcome to the weblog", post.post_title
3132
end
3233

34+
def test_select_with_reserved_words_aliases
35+
post = Post.select("UPPER(title)" => :from, title: :group).first
36+
37+
assert_equal "WELCOME TO THE WEBLOG", post.from
38+
assert_equal "Welcome to the weblog", post.group
39+
end
40+
3341
def test_select_with_one_level_hash_argument
3442
post = Post.select("UPPER(title)" => :title, title: :post_title).first
3543

@@ -38,7 +46,8 @@ def test_select_with_one_level_hash_argument
3846
end
3947

4048
def test_select_with_not_exists_field
41-
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("foo"))} AS post_title FROM/
49+
q = -> name { Regexp.escape(quote_table_name(name)) }
50+
expected = %r/\ASELECT #{q["foo"]} AS #{q["post_title"]} FROM/
4251
assert_match expected, Post.select(foo: :post_title).to_sql
4352

4453
skip if sqlite3_adapter_strict_strings_disabled?
@@ -49,7 +58,8 @@ def test_select_with_not_exists_field
4958
end
5059

5160
def test_select_with_hash_with_not_exists_field
52-
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("posts.bar"))} AS post_title FROM/
61+
q = -> name { Regexp.escape(quote_table_name(name)) }
62+
expected = %r/\ASELECT #{q["posts.bar"]} AS #{q["post_title"]} FROM/
5363
assert_match expected, Post.select(posts: { bar: :post_title }).to_sql
5464

5565
assert_raises(ActiveRecord::StatementInvalid) do
@@ -58,7 +68,8 @@ def test_select_with_hash_with_not_exists_field
5868
end
5969

6070
def test_select_with_hash_array_value_with_not_exists_field
61-
expected = %r/\ASELECT #{Regexp.escape(quote_table_name("posts.bar"))}, #{Regexp.escape(quote_table_name("posts.id"))} FROM/
71+
q = -> name { Regexp.escape(quote_table_name(name)) }
72+
expected = %r/\ASELECT #{q["posts.bar"]}, #{q["posts.id"]} FROM/
6273
assert_match expected, Post.select(posts: [:bar, :id]).to_sql
6374

6475
assert_raises(ActiveRecord::StatementInvalid) do

0 commit comments

Comments
 (0)