Skip to content

Commit 4d208ac

Browse files
committed
Allow query_requires_identity_insert? method to return quoted table name in situations where the INSERT parts are not quoted themselves.
1 parent 92c2d77 commit 4d208ac

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11

22
MASTER
33

4+
* Allow query_requires_identity_insert? method to return quoted table name in situations where the
5+
INSERT parts are not quoted themselves. [Gary/iawgens, Richard Penwell, Ken Collins]
6+
47
* Fixed namespace in calling test_sqlserver_odbc within test_unicode_types. [Gary/iawgens]
58

69
* Columns with multi-line defaults work correctly. [bfabry]

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ def query_requires_identity_insert?(sql)
946946
if insert_sql?(sql)
947947
table_name = get_table_name(sql)
948948
id_column = identity_column(table_name)
949-
id_column && sql =~ /INSERT[^(]+\([^)]*\[#{id_column.name}\][^)]*\)/i ? table_name : false
949+
id_column && sql =~ /INSERT[^(]+\([^)]*\b(#{id_column.name})\b,?[^)]*\)/i ? quote_table_name(table_name) : false
950950
else
951951
false
952952
end

test/cases/adapter_test_sqlserver.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,14 @@ def setup
328328

329329
setup do
330330
@identity_insert_sql = "INSERT INTO [funny_jokes] ([id],[name]) VALUES(420,'Knock knock')"
331+
@identity_insert_sql_unquoted = "INSERT INTO funny_jokes (id, name) VALUES(420, 'Knock knock')"
332+
@identity_insert_sql_unordered = "INSERT INTO [funny_jokes] ([name],[id]) VALUES('Knock knock',420)"
331333
end
332334

333-
should 'return quoted table_name to #query_requires_identity_insert? when INSERT sql contains id_column' do
335+
should 'return quoted table_name to #query_requires_identity_insert? when INSERT sql contains id column' do
334336
assert_equal '[funny_jokes]', @connection.send(:query_requires_identity_insert?,@identity_insert_sql)
337+
assert_equal '[funny_jokes]', @connection.send(:query_requires_identity_insert?,@identity_insert_sql_unquoted)
338+
assert_equal '[funny_jokes]', @connection.send(:query_requires_identity_insert?,@identity_insert_sql_unordered)
335339
end
336340

337341
should 'return false to #query_requires_identity_insert? for normal SQL' do

0 commit comments

Comments
 (0)