Skip to content

Commit a1bc145

Browse files
authored
Add affected_rows to sql.active_record (#1268)
1 parent ecd3486 commit a1bc145

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ def write_query?(sql) # :nodoc:
1414
end
1515

1616
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
17-
result = if id_insert_table_name = query_requires_identity_insert?(sql)
18-
# If the table name is a view, we need to get the base table name for enabling identity insert.
19-
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
17+
result, affected_rows = if id_insert_table_name = query_requires_identity_insert?(sql)
18+
# If the table name is a view, we need to get the base table name for enabling identity insert.
19+
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
2020

21-
with_identity_insert_enabled(id_insert_table_name, raw_connection) do
22-
internal_exec_sql_query(sql, raw_connection)
23-
end
24-
else
25-
internal_exec_sql_query(sql, raw_connection)
26-
end
21+
with_identity_insert_enabled(id_insert_table_name, raw_connection) do
22+
internal_exec_sql_query(sql, raw_connection)
23+
end
24+
else
25+
internal_exec_sql_query(sql, raw_connection)
26+
end
2727

2828
verified!
29+
notification_payload[:affected_rows] = affected_rows
2930
notification_payload[:row_count] = result.count
3031
result
3132
end
@@ -38,8 +39,18 @@ def cast_result(raw_result)
3839
end
3940
end
4041

42+
# Returns the affected rows from results.
4143
def affected_rows(raw_result)
42-
raw_result.first['AffectedRows']
44+
raw_result&.first&.fetch('AffectedRows', nil)
45+
end
46+
47+
# Returns the affected rows from results or handle.
48+
def affected_rows_from_results_or_handle(raw_result, handle)
49+
if affected_rows_from_result = affected_rows(raw_result)
50+
affected_rows_from_result
51+
else
52+
handle.affected_rows
53+
end
4354
end
4455

4556
def raw_execute(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, batch: false)
@@ -53,7 +64,9 @@ def raw_execute(sql, name = nil, binds = [], prepare: false, async: false, allow
5364

5465
def internal_exec_sql_query(sql, conn)
5566
handle = internal_raw_execute(sql, conn)
56-
handle_to_names_and_values(handle, ar_result: true)
67+
results = handle_to_names_and_values(handle, ar_result: true)
68+
69+
return results, affected_rows_from_results_or_handle(results, handle)
5770
ensure
5871
finish_statement_handle(handle)
5972
end
@@ -432,12 +445,15 @@ def handle_to_names_and_values(handle, options = {})
432445
end
433446
results = handle.each(query_options)
434447

435-
columns = handle.fields
436-
# If query returns multiple result sets, only return the columns of the last one.
437-
columns = columns.last if columns.any? && columns.all? { |e| e.is_a?(Array) }
438-
columns = columns.map(&:downcase) if lowercase_schema_reflection
448+
if options[:ar_result]
449+
columns = handle.fields
450+
columns = columns.last if columns.any? && columns.all? { |e| e.is_a?(Array) } # If query returns multiple result sets, only return the columns of the last one.
451+
columns = columns.map(&:downcase) if lowercase_schema_reflection
439452

440-
options[:ar_result] ? ActiveRecord::Result.new(columns, results) : results
453+
ActiveRecord::Result.new(columns, results)
454+
else
455+
results
456+
end
441457
end
442458

443459
def finish_statement_handle(handle)

test/cases/coerced_tests.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ def test_payload_row_count_on_raw_sql_coerced
372372
Book.where(author_id: nil, name: 'row count book 3').delete_all
373373
Book.lease_connection.add_index(:books, [:author_id, :name], unique: true)
374374
end
375+
376+
# Fix randomly failing test. The loading of the model's schema was affecting the test.
377+
coerce_tests! :test_payload_affected_rows
378+
def test_payload_affected_rows_coerced
379+
Book.send(:load_schema!)
380+
original_test_payload_affected_rows
381+
end
375382
end
376383
end
377384

0 commit comments

Comments
 (0)