Skip to content

Commit 19268ed

Browse files
committed
Introduce Parameter Object: QueryIntent
Ref: rails/rails#55897
1 parent 321f2be commit 19268ed

3 files changed

Lines changed: 42 additions & 17 deletions

File tree

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def drop_table(*table_names, **options)
3333

3434
def indexes(table_name)
3535
data = begin
36-
select("EXEC sp_helpindex #{quote(table_name)}", "SCHEMA")
36+
intent = QueryIntent.new(raw_sql: "EXEC sp_helpindex #{quote(table_name)}", name: "SCHEMA")
37+
select(intent)
3738
rescue
3839
[]
3940
end

lib/active_record/connection_adapters/sqlserver/showplan.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def with_showplan_on
3030
end
3131

3232
def set_showplan_option(enable = true)
33-
sql = "SET #{showplan_option} #{enable ? "ON" : "OFF"}"
34-
raw_execute(sql, "SCHEMA")
33+
intent = QueryIntent.new(raw_sql: "SET #{showplan_option} #{enable ? "ON" : "OFF"}", name: "SCHEMA")
34+
raw_execute(intent)
3535
rescue
3636
raise ActiveRecordError, "#{showplan_option} could not be turned #{enable ? "ON" : "OFF"}, perhaps you do not have SHOWPLAN permissions?"
3737
end

0 commit comments

Comments
 (0)