@@ -13,10 +13,12 @@ def write_query?(sql) # :nodoc:
1313 !READ_QUERY . match? ( sql . b )
1414 end
1515
16- def perform_query ( raw_connection , sql , binds , type_casted_binds , prepare :, notification_payload :, batch :)
17- unless binds . nil? || binds . empty?
18- types , params = sp_executesql_types_and_parameters ( binds )
19- sql = sp_executesql_sql ( sql , types , params , notification_payload [ :name ] )
16+ def perform_query ( raw_connection , intent )
17+ sql = intent . processed_sql
18+
19+ unless intent . binds . nil? || intent . binds . empty?
20+ types , params = sp_executesql_types_and_parameters ( intent . binds )
21+ sql = sp_executesql_sql ( intent . processed_sql , types , params , intent . notification_payload [ :name ] )
2022 end
2123
2224 id_insert_table_name = query_requires_identity_insert? ( sql )
@@ -30,8 +32,8 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
3032 end
3133
3234 verified!
33- notification_payload [ :affected_rows ] = affected_rows
34- notification_payload [ :row_count ] = result . count
35+ intent . notification_payload [ :affected_rows ] = affected_rows
36+ intent . notification_payload [ :row_count ] = result . count
3537 result
3638 end
3739
@@ -62,14 +64,34 @@ def internal_exec_sql_query(sql, conn)
6264 finish_statement_handle ( handle )
6365 end
6466
65- def exec_delete ( sql , name = nil , binds = [ ] )
66- sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
67- super
67+ # Executes the delete statement and returns the number of rows affected.
68+ def delete ( arel , name = nil , binds = [ ] )
69+ intent = QueryIntent . new ( arel : arel , name : name , binds : binds )
70+
71+ # Compile Arel to get SQL
72+ compile_arel_in_intent ( intent )
73+
74+ # Start of monkey-patch
75+ sql = intent . processed_sql . present? ? intent . processed_sql : intent . raw_sql
76+ intent . processed_sql = "#{ sql } ; SELECT @@ROWCOUNT AS AffectedRows"
77+ # End of monkey-patch
78+
79+ affected_rows ( raw_execute ( intent ) )
6880 end
6981
70- def exec_update ( sql , name = nil , binds = [ ] )
71- sql = sql . dup << "; SELECT @@ROWCOUNT AS AffectedRows"
72- super
82+ # Executes the update statement and returns the number of rows affected.
83+ def update ( arel , name = nil , binds = [ ] )
84+ intent = QueryIntent . new ( arel : arel , name : name , binds : binds )
85+
86+ # Compile Arel to get SQL
87+ compile_arel_in_intent ( intent )
88+
89+ # Start of monkey-patch
90+ sql = intent . processed_sql . present? ? intent . processed_sql : intent . raw_sql
91+ intent . processed_sql = "#{ sql } ; SELECT @@ROWCOUNT AS AffectedRows"
92+ # End of monkey-patch
93+
94+ affected_rows ( raw_execute ( intent ) )
7395 end
7496
7597 def begin_db_transaction
@@ -237,9 +259,11 @@ def execute_procedure(proc_name, *variables)
237259 end . join ( ", " )
238260 sql = "EXEC #{ proc_name } #{ vars } " . strip
239261
240- log ( sql , "Execute Procedure" ) do |notification_payload |
262+ intent = QueryIntent . new ( processed_sql : sql )
263+
264+ log ( intent , "Execute Procedure" ) do |notification_payload |
241265 with_raw_connection do |conn |
242- result = internal_raw_execute ( sql , conn )
266+ result = internal_raw_execute ( intent . processed_sql , conn )
243267 verified!
244268 options = { as : :hash , cache_rows : true , timezone : ActiveRecord . default_timezone || :utc }
245269
0 commit comments