@@ -14,22 +14,19 @@ def write_query?(sql) # :nodoc:
14
14
end
15
15
16
16
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 )
20
20
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
27
27
28
28
verified!
29
-
30
- # binding.pry if $DEBUG
31
-
32
- notification_payload [ :affected_rows ] = affected_rows ( result )
29
+ notification_payload [ :affected_rows ] = affected_rows
33
30
notification_payload [ :row_count ] = result . count
34
31
result
35
32
end
@@ -42,11 +39,18 @@ def cast_result(raw_result)
42
39
end
43
40
end
44
41
42
+ # Returns the affected rows from results.
45
43
def affected_rows ( raw_result )
44
+ raw_result &.first &.fetch ( 'AffectedRows' , nil )
45
+ end
46
46
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
50
54
end
51
55
52
56
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
60
64
61
65
def internal_exec_sql_query ( sql , conn )
62
66
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 )
64
70
ensure
65
71
finish_statement_handle ( handle )
66
72
end
@@ -75,11 +81,6 @@ def exec_update(sql, name = nil, binds = [])
75
81
super ( sql , name , binds )
76
82
end
77
83
78
- # def exec_insert_all(sql, name)
79
- # sql = sql.dup << "; SELECT @@ROWCOUNT AS AffectedRows"
80
- # super(sql, name)
81
- # end
82
-
83
84
def begin_db_transaction
84
85
internal_execute ( "BEGIN TRANSACTION" , "TRANSACTION" , allow_retry : true , materialize_transactions : false )
85
86
end
@@ -191,8 +192,6 @@ def execute_procedure(proc_name, *variables)
191
192
end
192
193
193
194
result = result . each . map { |row | row . is_a? ( Hash ) ? row . with_indifferent_access : row }
194
-
195
- notification_payload [ :affected_rows ] = affected_rows ( result )
196
195
notification_payload [ :row_count ] = result . count
197
196
result
198
197
end
@@ -446,12 +445,15 @@ def handle_to_names_and_values(handle, options = {})
446
445
end
447
446
results = handle . each ( query_options )
448
447
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
453
452
454
- options [ :ar_result ] ? ActiveRecord ::Result . new ( columns , results ) : results
453
+ ActiveRecord ::Result . new ( columns , results )
454
+ else
455
+ results
456
+ end
455
457
end
456
458
457
459
def finish_statement_handle ( handle )
0 commit comments