Skip to content

Commit 470c569

Browse files
committed
check TinyTds FalseClass response in execute_procedure and raw_connection_run
1 parent 91701a6 commit 470c569

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def execute_procedure(proc_name, *variables)
167167
log(sql, name) do
168168
case @connection_options[:mode]
169169
when :dblib
170-
result = ensure_established_connection! { @connection.execute(sql) }
170+
result = ensure_established_connection! { dblib_execute(sql) }
171171
options = { as: :hash, cache_rows: true, timezone: ActiveRecord::Base.default_timezone || :utc }
172172
result.each(options) do |row|
173173
r = row.with_indifferent_access
@@ -357,13 +357,7 @@ def sp_executesql_sql(sql, types, params, name)
357357
def raw_connection_do(sql)
358358
case @connection_options[:mode]
359359
when :dblib
360-
result = ensure_established_connection! { @connection.execute(sql) }
361-
362-
# TinyTDS returns false instead of raising an exception if connection fails.
363-
# Getting around this by raising an exception ourselves while this PR
364-
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
365-
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
366-
360+
result = ensure_established_connection! { dblib_execute(sql) }
367361
result.do
368362
end
369363
ensure
@@ -428,7 +422,7 @@ def _raw_select(sql, options = {})
428422
def raw_connection_run(sql)
429423
case @connection_options[:mode]
430424
when :dblib
431-
ensure_established_connection! { @connection.execute(sql) }
425+
ensure_established_connection! { dblib_execute(sql) }
432426
end
433427
end
434428

@@ -463,6 +457,15 @@ def finish_statement_handle(handle)
463457
handle
464458
end
465459

460+
def dblib_execute(sql)
461+
@connection.execute(sql).tap do |result|
462+
# TinyTDS returns false instead of raising an exception if connection fails.
463+
# Getting around this by raising an exception ourselves while this PR
464+
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
465+
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
466+
end
467+
end
468+
466469
def ensure_established_connection!
467470
raise TinyTds::Error, 'SQL Server client is not connected' unless @connection
468471

0 commit comments

Comments
 (0)