Sha256: 6b77d3128091ce42d18e56aecc50160d40f24b7d255627e35abe627648c2a57e

Contents?: true

Size: 1.88 KB

Versions: 18

Compression:

Stored size: 1.88 KB

Contents

class Simple::SQL::Connection
end

require "pg"
require "active_record"

require_relative "connection_manager"

require_relative "connection/base"
require_relative "connection/lock"
require_relative "connection/scope"
require_relative "connection/reflection"
require_relative "connection/insert"
require_relative "connection/duplicate"
require_relative "connection/type_info"

# A Connection object. This wraps an ActiveRecord::Base connection.
#
# It Method.includes the ConnectionAdapter, which implements ask, all, + friends
#
class Simple::SQL::Connection
  ConnectionManager = ::Simple::SQL::ConnectionManager

  def self.create(database_url = :auto)
    expect! database_url => [nil, :auto, String]
    new connection_class(database_url)
  end

  def self.connection_class(database_url)
    if database_url.nil?
      ::ActiveRecord::Base
    elsif database_url.is_a?(String)
      ConnectionManager.connection_class(database_url)
    elsif ::ActiveRecord::Base.connected?
      # database_url is :auto, and we are connected. This happens, for example,
      # within a rails controller. IT IS IMPORTANT NOT TO CONNECT AGAINST THE
      # ::Simple::SQL::Config.determine_url! Only so we can make sure that
      # simple-sql and ActiveRecord can be mixed freely together, i.e. they are
      # sharing the same connection.
      ::ActiveRecord::Base
    else
      ConnectionManager.connection_class(::Simple::SQL::Config.determine_url)
    end
  end

  def initialize(connection_class)
    @connection_class = connection_class
  end

  def raw_connection
    @connection_class.connection.raw_connection
  end

  def transaction(&block)
    @connection_class.connection.transaction(&block)
  end

  def disconnect!
    return unless @connection_class && @connection_class != ::ActiveRecord::Base
    @connection_class.remove_connection
  end

  extend Forwardable
  delegate [:wait_for_notify] => :raw_connection
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
simple-sql-0.5.37 lib/simple/sql/connection.rb
simple-sql-0.5.36 lib/simple/sql/connection.rb
simple-sql-0.5.35 lib/simple/sql/connection.rb
simple-sql-0.5.34 lib/simple/sql/connection.rb
simple-sql-0.5.33 lib/simple/sql/connection.rb
simple-sql-0.5.32 lib/simple/sql/connection.rb
simple-sql-0.5.31 lib/simple/sql/connection.rb
simple-sql-0.5.30 lib/simple/sql/connection.rb
simple-sql-0.5.29 lib/simple/sql/connection.rb
simple-sql-0.5.28 lib/simple/sql/connection.rb
simple-sql-0.5.27 lib/simple/sql/connection.rb
simple-sql-0.5.26 lib/simple/sql/connection.rb
simple-sql-0.5.25 lib/simple/sql/connection.rb
simple-sql-0.5.24 lib/simple/sql/connection.rb
simple-sql-0.5.23 lib/simple/sql/connection.rb
simple-sql-0.5.22 lib/simple/sql/connection.rb
simple-sql-0.5.21 lib/simple/sql/connection.rb
simple-sql-0.5.20 lib/simple/sql/connection.rb