Skip to content

Commit 260e93b

Browse files
committed
Altered limited_update_conditions regex conditions, the .* would greedily fail if the where_sql had WHERE in a table or field, etc.
1 parent c2ab307 commit 260e93b

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

CHANGELOG

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
MASTER
33

4-
*
4+
* Altered limited_update_conditions regex conditions, the .* would greedily fail
5+
if the where_sql had WHERE in a table or field, etc. [Ransom Briggs]
56

67
* Changing test to allow ENV['ARUNIT_DB_NAME'] as the database name for the test units.
78
Matches up with AR conventions. [Ransom Briggs]

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def case_sensitive_equality_operator
522522
end
523523

524524
def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
525-
match_data = where_sql.match(/(.*)WHERE/)
525+
match_data = where_sql.match(/^(.*?[\]\) ])WHERE[\[\( ]/)
526526
limit = match_data[1]
527527
where_sql.sub!(limit,'')
528528
"WHERE #{quoted_primary_key} IN (SELECT #{limit} #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"

test/cases/adapter_test_sqlserver.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,15 @@ def setup
128128
assert !@connection.send(:insert_sql?,'UPDATE...')
129129
assert !@connection.send(:insert_sql?,'SELECT...')
130130
end
131+
132+
context 'for #limited_update_conditions' do
133+
134+
should 'only match up to the first WHERE' do
135+
where_sql = "TOP 1 WHERE ([posts].author_id = 1 and [posts].columnWHEREname = 2) ORDER BY posts.id"
136+
assert_equal "WHERE bar IN (SELECT TOP 1 bar FROM foo WHERE ([posts].author_id = 1 and [posts].columnWHEREname = 2) ORDER BY posts.id)", @connection.limited_update_conditions(where_sql, 'foo', 'bar')
137+
end
138+
139+
end
131140

132141
context 'for #sql_for_association_limiting?' do
133142

0 commit comments

Comments
 (0)