Skip to content

Commit 04ff462

Browse files
aidanharanLavika
authored and
Lavika
committed
Ignore casing of VALUES clause when inserting records using SQL (rails-sqlserver#1052)
1 parent 63df930 commit 04ff462

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1052](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1052) Ignore casing of VALUES clause when inserting records using SQL
6+
17
## v7.0.2.0
28

39
[Full changelog](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/compare/v7.0.1.0...v7.0.2.0)

lib/active_record/connection_adapters/sqlserver/database_statements.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,12 @@ def sql_for_insert(sql, pk, binds)
289289
<<~SQL.squish
290290
SET NOCOUNT ON
291291
DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
292-
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
292+
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
293293
SELECT CAST(#{quoted_pk} AS #{id_sql_type}) FROM @ssaIdInsertTable;
294294
SET NOCOUNT OFF
295295
SQL
296296
else
297-
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
297+
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT INSERTED.#{quoted_pk}"
298298
end
299299
else
300300
table = get_table_name(sql)
@@ -412,7 +412,7 @@ def exclude_output_inserted_table_name?(table_name, sql)
412412
self.class.exclude_output_inserted_table_names[table_name]
413413
end
414414

415-
def exec_insert_requires_identity?(sql, pk, binds)
415+
def exec_insert_requires_identity?(sql, _pk, _binds)
416416
query_requires_identity_insert?(sql)
417417
end
418418

test/cases/adapter_test_sqlserver.rb

+12
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,16 @@ def test_doesnt_error_when_a_select_query_is_called_while_preventing_writes
532532
end
533533
end
534534
end
535+
536+
describe "exec_insert" do
537+
it 'values clause should be case-insensitive' do
538+
assert_difference("Post.count", 4) do
539+
first_insert = connection.exec_insert("INSERT INTO [posts] ([id],[title],[body]) VALUES(100, 'Title', 'Body'), (102, 'Title', 'Body')")
540+
second_insert = connection.exec_insert("INSERT INTO [posts] ([id],[title],[body]) values(113, 'Body', 'Body'), (114, 'Body', 'Body')")
541+
542+
assert_equal first_insert.rows.map(&:first), [100, 102]
543+
assert_equal second_insert.rows.map(&:first), [113, 114]
544+
end
545+
end
546+
end
535547
end

0 commit comments

Comments
 (0)