Skip to content

Commit 5a6bb45

Browse files
committed
Do not wrap EXPLAIN SQL in sp_executesql
1 parent aedf993 commit 5a6bb45

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ module DatabaseStatements
1010
# TODO: replace `internal_exec_query` by `perform_query`.
1111

1212
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
13-
unless binds.nil? || binds.empty?
14-
types, params = sp_executesql_types_and_parameters(binds)
15-
16-
# TODO: `name` parameter does not exist.
17-
sql = sp_executesql_sql(sql, types, params)
18-
end
13+
# unless binds.nil? || binds.empty?
14+
# types, params = sp_executesql_types_and_parameters(binds)
15+
#
16+
# # TODO: `name` parameter does not exist.
17+
# sql = sp_executesql_sql(sql, types, params)
18+
# end
1919

2020

2121
result = if id_insert_table_name = query_requires_identity_insert?(sql)
@@ -73,6 +73,8 @@ def cast_result(raw_result)
7373
# binding.pry
7474
end
7575

76+
77+
7678
def affected_rows(raw_result)
7779

7880
raw_result.first['AffectedRows']
@@ -112,6 +114,19 @@ def write_query?(sql) # :nodoc:
112114
# end
113115

114116

117+
def raw_execute(sql, name = nil, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, batch: false)
118+
119+
unless binds.nil? || binds.empty?
120+
types, params = sp_executesql_types_and_parameters(binds)
121+
122+
123+
sql = sp_executesql_sql(sql, types, params, name)
124+
end
125+
126+
127+
super
128+
end
129+
115130

116131
def internal_exec_sql_query(sql, conn)
117132
handle = internal_raw_execute(sql, conn)
@@ -425,19 +440,20 @@ def basic_attribute_type?(type)
425440
type.is_a?(NilClass)
426441
end
427442

428-
# TODO: `name` was removed from the method signature.
429-
def sp_executesql_sql(sql, types, params)
430-
# if name == "EXPLAIN"
431-
# params.each.with_index do |param, index|
432-
# substitute_at_finder = /(@#{index})(?=(?:[^']|'[^']*')*$)/ # Finds unquoted @n values.
433-
# sql = sql.sub substitute_at_finder, param.to_s
434-
# end
435-
# else
443+
444+
def sp_executesql_sql(sql, types, params, name)
445+
if name == "EXPLAIN"
446+
params.each.with_index do |param, index|
447+
substitute_at_finder = /(@#{index})(?=(?:[^']|'[^']*')*$)/ # Finds unquoted @n values.
448+
sql = sql.sub substitute_at_finder, param.to_s
449+
end
450+
else
436451
types = quote(types.join(", "))
437452
params = params.map.with_index { |p, i| "@#{i} = #{p}" }.join(", ") # Only p is needed, but with @i helps explain regexp.
438453
sql = "EXEC sp_executesql #{quote(sql)}"
439454
sql += ", #{types}, #{params}" unless params.empty?
440-
# end
455+
end
456+
441457
sql.freeze
442458
end
443459

lib/active_record/connection_adapters/sqlserver/showplan.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ module Showplan
1313
OPTIONS = [OPTION_ALL, OPTION_TEXT, OPTION_XML]
1414

1515
def explain(arel, binds = [], options = [])
16-
sql = to_sql(arel)
17-
result = with_showplan_on { internal_exec_query(sql, "EXPLAIN", binds) }
16+
sql = to_sql(arel)
17+
result = with_showplan_on { internal_exec_query(sql, "EXPLAIN", binds) }
1818
printer = showplan_printer.new(result)
19+
1920
printer.pp
2021
end
2122

0 commit comments

Comments
 (0)