Skip to content

Commit 7222e4b

Browse files
author
Aidan Haran
committed
Check if TinyTDS connection failed when using raw_connection_run/execute_procedure
1 parent 0e0da50 commit 7222e4b

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@
2323

2424
#### Added
2525

26-
- ...
26+
- [#918](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/918) Check if TinyTDS connection failed when using raw_connection_run/execute_procedure
2727

2828
Please check [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-0-stable/CHANGELOG.md) for previous changes.

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 = @connection.execute(sql)
170+
result = dblib_connection_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
@@ -178,6 +178,15 @@ def execute_procedure(proc_name, *variables)
178178
end
179179
end
180180

181+
# TinyTDS returns false instead of raising an exception if connection fails.
182+
# Getting around this by raising an exception ourselves while this PR
183+
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
184+
def dblib_connection_execute(sql)
185+
result = @connection.execute(sql)
186+
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
187+
result
188+
end
189+
181190
def with_identity_insert_enabled(table_name)
182191
table_name = quote_table_name(table_name)
183192
set_identity_insert(table_name, true)
@@ -357,13 +366,7 @@ def sp_executesql_sql(sql, types, params, name)
357366
def raw_connection_do(sql)
358367
case @connection_options[:mode]
359368
when :dblib
360-
result = @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-
369+
result = dblib_connection_execute(sql)
367370
result.do
368371
end
369372
ensure
@@ -428,7 +431,7 @@ def _raw_select(sql, options = {})
428431
def raw_connection_run(sql)
429432
case @connection_options[:mode]
430433
when :dblib
431-
@connection.execute(sql)
434+
dblib_connection_execute(sql)
432435
end
433436
end
434437

0 commit comments

Comments
 (0)