Skip to content

Commit 8269e39

Browse files
committed
Read affected rows from either results or handle
1 parent 17d0b78 commit 8269e39

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +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-
30-
# binding.pry if $DEBUG
31-
32-
notification_payload[:affected_rows] = affected_rows(result)
29+
notification_payload[:affected_rows] = affected_rows
3330
notification_payload[:row_count] = result.count
3431
result
3532
end
@@ -42,11 +39,18 @@ def cast_result(raw_result)
4239
end
4340
end
4441

42+
# Returns the affected rows from results.
4543
def affected_rows(raw_result)
44+
raw_result&.first&.fetch('AffectedRows', nil)
45+
end
4646

47-
# raw_result.first['AffectedRows']
48-
49-
raw_result&.first&.fetch('AffectedRows', 0) || 0
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
5054
end
5155

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

6165
def internal_exec_sql_query(sql, conn)
6266
handle = internal_raw_execute(sql, conn)
63-
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)
6470
ensure
6571
finish_statement_handle(handle)
6672
end
@@ -75,11 +81,6 @@ def exec_update(sql, name = nil, binds = [])
7581
super(sql, name, binds)
7682
end
7783

78-
# def exec_insert_all(sql, name)
79-
# sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
80-
# super(sql, name)
81-
# end
82-
8384
def begin_db_transaction
8485
internal_execute("BEGIN TRANSACTION", "TRANSACTION", allow_retry: true, materialize_transactions: false)
8586
end
@@ -191,8 +192,6 @@ def execute_procedure(proc_name, *variables)
191192
end
192193

193194
result = result.each.map { |row| row.is_a?(Hash) ? row.with_indifferent_access : row }
194-
195-
notification_payload[:affected_rows] = affected_rows(result)
196195
notification_payload[:row_count] = result.count
197196
result
198197
end
@@ -446,12 +445,15 @@ def handle_to_names_and_values(handle, options = {})
446445
end
447446
results = handle.each(query_options)
448447

449-
columns = handle.fields
450-
# If query returns multiple result sets, only return the columns of the last one.
451-
columns = columns.last if columns.any? && columns.all? { |e| e.is_a?(Array) }
452-
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
453452

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

457459
def finish_statement_handle(handle)

0 commit comments

Comments
 (0)