Skip to content

Rails 6.1: Check if TinyTDS connection failed when using raw_connection_run/execute_procedure #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@

#### Added

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

Please check [6-0-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-0-stable/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def execute_procedure(proc_name, *variables)
log(sql, name) do
case @connection_options[:mode]
when :dblib
result = @connection.execute(sql)
result = dblib_connection_execute(sql)
options = { as: :hash, cache_rows: true, timezone: ActiveRecord::Base.default_timezone || :utc }
result.each(options) do |row|
r = row.with_indifferent_access
Expand All @@ -178,6 +178,15 @@ def execute_procedure(proc_name, *variables)
end
end

# TinyTDS returns false instead of raising an exception if connection fails.
# Getting around this by raising an exception ourselves while this PR
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
def dblib_connection_execute(sql)
result = @connection.execute(sql)
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)
result
end

def with_identity_insert_enabled(table_name)
table_name = quote_table_name(table_name)
set_identity_insert(table_name, true)
Expand Down Expand Up @@ -357,13 +366,7 @@ def sp_executesql_sql(sql, types, params, name)
def raw_connection_do(sql)
case @connection_options[:mode]
when :dblib
result = @connection.execute(sql)

# TinyTDS returns false instead of raising an exception if connection fails.
# Getting around this by raising an exception ourselves while this PR
# https://github.com/rails-sqlserver/tiny_tds/pull/469 is not released.
raise TinyTds::Error, "failed to execute statement" if result.is_a?(FalseClass)

result = dblib_connection_execute(sql)
result.do
end
ensure
Expand Down Expand Up @@ -428,7 +431,7 @@ def _raw_select(sql, options = {})
def raw_connection_run(sql)
case @connection_options[:mode]
when :dblib
@connection.execute(sql)
dblib_connection_execute(sql)
end
end

Expand Down