Skip to content
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

Register adapter lease connection #1156

Closed
wants to merge 6 commits into from
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ module AttributeMethods
private

def attributes_for_update(attribute_names)
return super unless self.class.connection.adapter_name == "SQLServer"
self.class.with_connection do |connection|
return super(attribute_names) unless connection.sqlserver?

super.reject do |name|
column = self.class.columns_hash[name]
column && column.respond_to?(:is_identity?) && column.is_identity?
super(attribute_names).reject do |name|
column = self.class.columns_hash[name]
column && column.respond_to?(:is_identity?) && column.is_identity?
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module SQLServer
module CoreExt
module Calculations
def calculate(operation, column_name)
if klass.connection.sqlserver?
_calculate(operation, column_name)
else
super
klass.with_connection do |connection|
if connection.sqlserver?
_calculate(operation, column_name)
else
super
end
end
end

Expand Down Expand Up @@ -54,9 +56,10 @@ def _calculate(operation, column_name)
end

def build_count_subquery(relation, column_name, distinct)
return super unless klass.connection.adapter_name == "SQLServer"

super(relation.unscope(:order), column_name, distinct)
klass.with_connection do |connection|
relation = relation.unscope(:order) if connection.sqlserver?
super(relation, column_name, distinct)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ module Explain
SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/

def exec_explain(queries, options = [])
return super unless connection.adapter_name == "SQLServer"
with_connection do |connection|
return super(queries, options) unless connection.sqlserver?

unprepared_queries = queries.map do |(sql, binds)|
[unprepare_sqlserver_statement(sql, binds), binds]
unprepared_queries = queries.map do |(sql, binds)|
[unprepare_sqlserver_statement(sql, binds), binds]
end
super(unprepared_queries, options)
end
super(unprepared_queries, options)
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ module FinderMethods
private

def construct_relation_for_exists(conditions)
if klass.connection.sqlserver?
_construct_relation_for_exists(conditions)
else
super
klass.with_connection do |connection|
if connection.sqlserver?
_construct_relation_for_exists(conditions)
else
super
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ module SQLServer
module CoreExt
module LoaderQuery
def load_records_for_keys(keys, &block)
return super unless scope.connection.sqlserver?
scope.with_connection do |connection|
return super unless connection.sqlserver?

return [] if keys.empty?
return [] if keys.empty?

if association_key_name.is_a?(Array)
query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }
if association_key_name.is_a?(Array)
query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }

keys.each_with_object(query_constraints) do |values_set, constraints|
association_key_name.zip(values_set).each do |key_name, value|
constraints[key_name] << value
keys.each_with_object(query_constraints) do |values_set, constraints|
association_key_name.zip(values_set).each do |key_name, value|
constraints[key_name] << value
end
end
end

scope.where(query_constraints).load(&block)
else
keys.each_slice(in_clause_length).flat_map do |slice|
scope.where(association_key_name => slice).load(&block).records
scope.where(query_constraints).load(&block)
else
keys.each_slice(in_clause_length).flat_map do |slice|
scope.where(association_key_name => slice).load(&block).records
end
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/active_record/connection_adapters/sqlserver_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
require "active_record/connection_adapters/sqlserver/table_definition"
require "active_record/connection_adapters/sqlserver/quoting"
require "active_record/connection_adapters/sqlserver/utils"
require "active_record/sqlserver_base"
require "active_record/connection_adapters/sqlserver_column"
require "active_record/tasks/sqlserver_database_tasks"

module ActiveRecord
module ConnectionAdapters
register "sqlserver", "ActiveRecord::ConnectionAdapters::SQLServerAdapter", "active_record/connection_adapters/sqlserver_adapter"

class SQLServerAdapter < AbstractAdapter
include SQLServer::Version,
SQLServer::Quoting,
Expand Down
13 changes: 0 additions & 13 deletions lib/active_record/sqlserver_base.rb

This file was deleted.

6 changes: 3 additions & 3 deletions test/cases/adapter_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
end

it "test table existence across database schemas" do
arunit_connection = Topic.connection
arunit2_connection = College.connection
arunit_connection = Topic.lease_connection
arunit2_connection = College.lease_connection

arunit_database = arunit_connection.pool.db_config.database
arunit2_database = arunit2_connection.pool.db_config.database
Expand Down Expand Up @@ -508,7 +508,7 @@ class AdapterTestSQLServer < ActiveRecord::TestCase

describe "block writes to a database" do
def setup
@conn = ActiveRecord::Base.connection
@conn = ActiveRecord::Base.lease_connection
end

def test_errors_when_an_insert_query_is_called_while_preventing_writes
Expand Down
Loading
Loading