Skip to content

Commit a9a0777

Browse files
authored
Removed legacy connection handling (#1121)
1 parent 46d304a commit a9a0777

File tree

4 files changed

+24
-71
lines changed

4 files changed

+24
-71
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

+12-58
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transac
1919
log(sql, name, async: async) do
2020
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
2121
result = if id_insert_table_name = query_requires_identity_insert?(sql)
22-
with_identity_insert_enabled(id_insert_table_name, conn) { _execute(sql, conn, perform_do: true) }
22+
with_identity_insert_enabled(id_insert_table_name, conn) { internal_raw_execute(sql, conn, perform_do: true) }
2323
else
24-
_execute(sql, conn, perform_do: true)
24+
internal_raw_execute(sql, conn, perform_do: true)
2525
end
2626
end
2727
end
@@ -49,11 +49,11 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
4949
# TODO: Look into refactoring this.
5050
if id_insert_table_name = query_requires_identity_insert?(sql)
5151
with_identity_insert_enabled(id_insert_table_name, conn) do
52-
handle = _execute(sql, conn)
52+
handle = internal_raw_execute(sql, conn)
5353
result = handle_to_names_and_values(handle, options)
5454
end
5555
else
56-
handle = _execute(sql, conn)
56+
handle = internal_raw_execute(sql, conn)
5757
result = handle_to_names_and_values(handle, options)
5858
end
5959
ensure
@@ -176,7 +176,7 @@ def execute_procedure(proc_name, *variables)
176176

177177
log(sql, "Execute Procedure") do
178178
with_raw_connection do |conn|
179-
result = _execute(sql, conn)
179+
result = internal_raw_execute(sql, conn)
180180
options = { as: :hash, cache_rows: true, timezone: ActiveRecord.default_timezone || :utc }
181181

182182
result.each(options) do |row|
@@ -305,28 +305,13 @@ def sql_for_insert(sql, pk, binds, returning)
305305
# === SQLServer Specific ======================================== #
306306

307307
def set_identity_insert(table_name, conn, enable)
308-
_execute("SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}", conn , perform_do: true)
308+
internal_raw_execute("SET IDENTITY_INSERT #{table_name} #{enable ? 'ON' : 'OFF'}", conn , perform_do: true)
309309
rescue Exception
310310
raise ActiveRecordError, "IDENTITY_INSERT could not be turned #{enable ? 'ON' : 'OFF'} for table #{table_name}"
311311
end
312312

313313
# === SQLServer Specific (Executing) ============================ #
314314

315-
# TODO: Adapter should be refactored to use `with_raw_connection` to translate exceptions.
316-
def sp_executesql(sql, name, binds, options = {})
317-
options[:ar_result] = true if options[:fetch] != :rows
318-
319-
unless without_prepared_statement?(binds)
320-
types, params = sp_executesql_types_and_parameters(binds)
321-
sql = sp_executesql_sql(sql, types, params, name)
322-
end
323-
324-
raw_select sql, name, binds, options
325-
rescue => original_exception
326-
translated_exception = translate_exception_class(original_exception, sql, binds)
327-
raise translated_exception
328-
end
329-
330315
def sp_executesql_types_and_parameters(binds)
331316
types, params = [], []
332317
binds.each_with_index do |attr, index|
@@ -377,13 +362,6 @@ def sp_executesql_sql(sql, types, params, name)
377362
sql.freeze
378363
end
379364

380-
def raw_connection_do(sql)
381-
result = ensure_established_connection! { dblib_execute(sql) }
382-
result.do
383-
ensure
384-
@update_sql = false
385-
end
386-
387365
# === SQLServer Specific (Identity Inserts) ===================== #
388366

389367
def use_output_inserted?
@@ -422,21 +400,14 @@ def identity_columns(table_name)
422400

423401
# === SQLServer Specific (Selecting) ============================ #
424402

425-
def raw_select(sql, name = "SQL", binds = [], options = {})
426-
log(sql, name, binds, async: options[:async]) { _raw_select(sql, options) }
427-
end
403+
def _raw_select(sql, conn, options = {})
404+
handle = internal_raw_execute(sql, conn)
428405

429-
def _raw_select(sql, options = {})
430-
handle = raw_connection_run(sql)
431406
handle_to_names_and_values(handle, options)
432407
ensure
433408
finish_statement_handle(handle)
434409
end
435410

436-
def raw_connection_run(sql)
437-
ensure_established_connection! { dblib_execute(sql) }
438-
end
439-
440411
def handle_to_names_and_values(handle, options = {})
441412
query_options = {}.tap do |qo|
442413
qo[:timezone] = ActiveRecord.default_timezone || :utc
@@ -453,33 +424,16 @@ def finish_statement_handle(handle)
453424
handle
454425
end
455426

456-
# TODO: Rename
457-
def _execute(sql, conn, perform_do: false)
427+
# TinyTDS returns false instead of raising an exception if connection fails.
428+
# Getting around this by raising an exception ourselves while PR
429+
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
430+
def internal_raw_execute(sql, conn, perform_do: false)
458431
result = conn.execute(sql).tap do |_result|
459-
# TinyTDS returns false instead of raising an exception if connection fails.
460-
# Getting around this by raising an exception ourselves while PR
461-
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
462432
raise TinyTds::Error, "failed to execute statement" if _result.is_a?(FalseClass)
463433
end
464434

465435
perform_do ? result.do : result
466436
end
467-
468-
# TODO: Remove
469-
def dblib_execute(sql)
470-
@raw_connection.execute(sql).tap do |result|
471-
# TinyTDS returns false instead of raising an exception if connection fails.
472-
# Getting around this by raising an exception ourselves while this PR
473-
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
474-
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
475-
end
476-
end
477-
478-
def ensure_established_connection!
479-
raise TinyTds::Error, 'SQL Server client is not connected' unless @raw_connection
480-
481-
yield
482-
end
483437
end
484438
end
485439
end

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

+7-5
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def primary_keys_select(table_name)
124124
binds << Relation::QueryAttribute.new("TABLE_NAME", identifier.object, nv128)
125125
binds << Relation::QueryAttribute.new("TABLE_SCHEMA", identifier.schema, nv128) unless identifier.schema.blank?
126126

127-
sp_executesql(sql, "SCHEMA", binds).map { |r| r["name"] }
127+
internal_exec_query(sql, "SCHEMA", binds).map { |row| row["name"] }
128128
end
129129

130130
def rename_table(table_name, new_name, **options)
@@ -424,12 +424,13 @@ def column_definitions(table_name)
424424
view_tblnm = view_table_name(table_name) if view_exists
425425

426426
if view_exists
427-
results = sp_executesql %{
427+
sql = <<~SQL
428428
SELECT LOWER(c.COLUMN_NAME) AS [name], c.COLUMN_DEFAULT AS [default]
429429
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
430430
WHERE c.TABLE_NAME = #{quote(view_tblnm)}
431-
}.squish, "SCHEMA", []
432-
default_functions = results.each.with_object({}) {|row, out| out[row["name"]] = row["default"] }.compact
431+
SQL
432+
results = internal_exec_query(sql, "SCHEMA")
433+
default_functions = results.each.with_object({}) { |row, out| out[row["name"]] = row["default"] }.compact
433434
end
434435

435436
sql = column_definitions_sql(database, identifier)
@@ -438,7 +439,8 @@ def column_definitions(table_name)
438439
nv128 = SQLServer::Type::UnicodeVarchar.new limit: 128
439440
binds << Relation::QueryAttribute.new("TABLE_NAME", identifier.object, nv128)
440441
binds << Relation::QueryAttribute.new("TABLE_SCHEMA", identifier.schema, nv128) unless identifier.schema.blank?
441-
results = sp_executesql(sql, "SCHEMA", binds)
442+
results = internal_exec_query(sql, "SCHEMA", binds)
443+
442444
columns = results.map do |ci|
443445
ci = ci.symbolize_keys
444446
ci[:_type] = ci[:type]

lib/active_record/connection_adapters/sqlserver/showplan.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Showplan
1414

1515
def explain(arel, binds = [], options = [])
1616
sql = to_sql(arel)
17-
result = with_showplan_on { sp_executesql(sql, "EXPLAIN", binds) }
17+
result = with_showplan_on { internal_exec_query(sql, "EXPLAIN", binds) }
1818
printer = showplan_printer.new(result)
1919
printer.pp
2020
end
@@ -30,7 +30,7 @@ def with_showplan_on
3030

3131
def set_showplan_option(enable = true)
3232
sql = "SET #{showplan_option} #{enable ? 'ON' : 'OFF'}"
33-
raw_connection_do(sql)
33+
raw_execute(sql, "SCHEMA")
3434
rescue Exception
3535
raise ActiveRecordError, "#{showplan_option} could not be turned #{enable ? 'ON' : 'OFF'}, perhaps you do not have SHOWPLAN permissions?"
3636
end

lib/active_record/connection_adapters/sqlserver_adapter.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,7 @@ def disable_referential_integrity
234234
# === Abstract Adapter (Connection Management) ================== #
235235

236236
def active?
237-
return false unless @raw_connection
238-
239-
raw_connection_do "SELECT 1"
240-
true
237+
@raw_connection&.active?
241238
rescue *connection_errors
242239
false
243240
end
@@ -504,7 +501,7 @@ def version_year
504501
end
505502

506503
def sqlserver_version
507-
@sqlserver_version ||= _raw_select("SELECT @@version", fetch: :rows).first.first.to_s
504+
@sqlserver_version ||= _raw_select("SELECT @@version", @raw_connection, fetch: :rows).first.first.to_s
508505
end
509506

510507
private
@@ -529,7 +526,7 @@ def configure_connection
529526
@raw_connection.execute("SET TEXTSIZE 2147483647").do
530527
@raw_connection.execute("SET CONCAT_NULL_YIELDS_NULL ON").do
531528

532-
@spid = _raw_select("SELECT @@SPID", fetch: :rows).first.first
529+
@spid = _raw_select("SELECT @@SPID", @raw_connection, fetch: :rows).first.first
533530

534531
initialize_dateformatter
535532
use_database

0 commit comments

Comments
 (0)