File tree Expand file tree Collapse file tree 6 files changed +28
-8
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 6 files changed +28
-8
lines changed Original file line number Diff line number Diff line change
1
+ * Allow nested functions as safe SQL string
2
+
3
+ * Michael Siegfried*
4
+
1
5
* Allow ` destroy_association_async_job= ` to be configured with a class string instead of a constant.
2
6
3
7
Defers an autoloading dependency between ` ActiveRecord::Base ` and ` ActiveJob::Base `
Original file line number Diff line number Diff line change @@ -167,7 +167,7 @@ def column_name_with_order_matcher # :nodoc:
167
167
(
168
168
(?:
169
169
# table_name.column_name | function(one or no argument)
170
- ((?:\w +\. )?\w +) | \w +\( (?:|\g <2>)\)
170
+ ((?:\w +\. )?\w + | \w +\( (?:|\g <2>)\) )
171
171
)
172
172
(?:(?:\s +AS)?\s +\w +)?
173
173
)
@@ -191,7 +191,7 @@ def column_name_with_order_matcher # :nodoc:
191
191
(
192
192
(?:
193
193
# table_name.column_name | function(one or no argument)
194
- ((?:\w +\. )?\w +) | \w +\( (?:|\g <2>)\)
194
+ ((?:\w +\. )?\w + | \w +\( (?:|\g <2>)\) )
195
195
)
196
196
(?:\s +ASC|\s +DESC)?
197
197
(?:\s +NULLS\s +(?:FIRST|LAST))?
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ def column_name_with_order_matcher
84
84
(
85
85
(?:
86
86
# `table_name`.`column_name` | function(one or no argument)
87
- ((?:\w +\. |`\w +`\. )?(?:\w +|`\w +`)) | \w +\( (?:|\g <2>)\)
87
+ ((?:\w +\. |`\w +`\. )?(?:\w +|`\w +`) | \w +\( (?:|\g <2>)\) )
88
88
)
89
89
(?:(?:\s +AS)?\s +(?:\w +|`\w +`))?
90
90
)
@@ -97,7 +97,7 @@ def column_name_with_order_matcher
97
97
(
98
98
(?:
99
99
# `table_name`.`column_name` | function(one or no argument)
100
- ((?:\w +\. |`\w +`\. )?(?:\w +|`\w +`)) | \w +\( (?:|\g <2>)\)
100
+ ((?:\w +\. |`\w +`\. )?(?:\w +|`\w +`) | \w +\( (?:|\g <2>)\) )
101
101
)
102
102
(?:\s +COLLATE\s +(?:\w +|"\w +"))?
103
103
(?:\s +ASC|\s +DESC)?
Original file line number Diff line number Diff line change @@ -134,7 +134,7 @@ def column_name_with_order_matcher
134
134
(
135
135
(?:
136
136
# "schema_name"."table_name"."column_name"::type_name | function(one or no argument)::type_name
137
- ((?:\w +\. |"\w +"\. ){,2}(?:\w +|"\w +")(?:::\w +)?) | \w +\( (?:|\g <2>)\) (?:::\w +)?
137
+ ((?:\w +\. |"\w +"\. ){,2}(?:\w +|"\w +")(?:::\w +)? | \w +\( (?:|\g <2>)\) (?:::\w +)?)
138
138
)
139
139
(?:(?:\s +AS)?\s +(?:\w +|"\w +"))?
140
140
)
@@ -147,7 +147,7 @@ def column_name_with_order_matcher
147
147
(
148
148
(?:
149
149
# "schema_name"."table_name"."column_name"::type_name | function(one or no argument)::type_name
150
- ((?:\w +\. |"\w +"\. ){,2}(?:\w +|"\w +")(?:::\w +)?) | \w +\( (?:|\g <2>)\) (?:::\w +)?
150
+ ((?:\w +\. |"\w +"\. ){,2}(?:\w +|"\w +")(?:::\w +)? | \w +\( (?:|\g <2>)\) (?:::\w +)?)
151
151
)
152
152
(?:\s +COLLATE\s +"\w +")?
153
153
(?:\s +ASC|\s +DESC)?
Original file line number Diff line number Diff line change @@ -86,7 +86,7 @@ def column_name_with_order_matcher
86
86
(
87
87
(?:
88
88
# "table_name"."column_name" | function(one or no argument)
89
- ((?:\w +\. |"\w +"\. )?(?:\w +|"\w +")) | \w +\( (?:|\g <2>)\)
89
+ ((?:\w +\. |"\w +"\. )?(?:\w +|"\w +") | \w +\( (?:|\g <2>)\) )
90
90
)
91
91
(?:(?:\s +AS)?\s +(?:\w +|"\w +"))?
92
92
)
@@ -99,7 +99,7 @@ def column_name_with_order_matcher
99
99
(
100
100
(?:
101
101
# "table_name"."column_name" | function(one or no argument)
102
- ((?:\w +\. |"\w +"\. )?(?:\w +|"\w +")) | \w +\( (?:|\g <2>)\)
102
+ ((?:\w +\. |"\w +"\. )?(?:\w +|"\w +") | \w +\( (?:|\g <2>)\) )
103
103
)
104
104
(?:\s +COLLATE\s +(?:\w +|"\w +"))?
105
105
(?:\s +ASC|\s +DESC)?
Original file line number Diff line number Diff line change @@ -180,6 +180,14 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
180
180
assert_equal ids_expected , ids
181
181
end
182
182
183
+ test "order: allows nested functions" do
184
+ ids_expected = Post . order ( Arel . sql ( "author_id, length(trim(title))" ) ) . pluck ( :id )
185
+
186
+ ids = Post . order ( "author_id, length(trim(title))" ) . pluck ( :id )
187
+
188
+ assert_equal ids_expected , ids
189
+ end
190
+
183
191
test "order: logs deprecation warning for unrecognized column" do
184
192
e = assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
185
193
Post . order ( "REPLACE(title, 'misc', 'zzzz')" )
@@ -253,6 +261,14 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
253
261
assert_equal titles_expected , titles
254
262
end
255
263
264
+ test "pluck: allows nested functions" do
265
+ title_lengths_expected = Post . pluck ( Arel . sql ( "length(trim(title))" ) )
266
+
267
+ title_lengths = Post . pluck ( "length(trim(title))" )
268
+
269
+ assert_equal title_lengths_expected , title_lengths
270
+ end
271
+
256
272
test "pluck: disallows invalid column name" do
257
273
assert_raises ( ActiveRecord ::UnknownAttributeReference ) do
258
274
Post . pluck ( "REPLACE(title, 'misc', 'zzzz')" )
You can’t perform that action at this time.
0 commit comments