Skip to content

Commit 98e57a6

Browse files
authored
Ignore casing of VALUES clause when inserting records using SQL (#1052)
1 parent 61e3c0d commit 98e57a6

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
@@ -278,11 +278,11 @@ def sql_for_insert(sql, pk, binds)
278278
id_sql_type = exclude_output_inserted.is_a?(TrueClass) ? "bigint" : exclude_output_inserted
279279
<<~SQL.squish
280280
DECLARE @ssaIdInsertTable table (#{quoted_pk} #{id_sql_type});
281-
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
281+
#{sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT INSERTED.#{quoted_pk} INTO @ssaIdInsertTable"}
282282
SELECT CAST(#{quoted_pk} AS #{id_sql_type}) FROM @ssaIdInsertTable
283283
SQL
284284
else
285-
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/), " OUTPUT INSERTED.#{quoted_pk}"
285+
sql.dup.insert sql.index(/ (DEFAULT )?VALUES/i), " OUTPUT INSERTED.#{quoted_pk}"
286286
end
287287
else
288288
"#{sql}; SELECT CAST(SCOPE_IDENTITY() AS bigint) AS Ident"
@@ -392,7 +392,7 @@ def exclude_output_inserted_table_name?(table_name, sql)
392392
self.class.exclude_output_inserted_table_names[table_name]
393393
end
394394

395-
def exec_insert_requires_identity?(sql, pk, binds)
395+
def exec_insert_requires_identity?(sql, _pk, _binds)
396396
query_requires_identity_insert?(sql)
397397
end
398398

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)