Module RR::ConnectionExtenders

  1. lib/rubyrep/connection_extenders/connection_extenders.rb
  2. lib/rubyrep/connection_extenders/postgresql_extender.rb
  3. lib/rubyrep/connection_extenders/mysql_extender.rb
  4. lib/rubyrep/connection_extenders/jdbc_extender.rb
  5. show all

Connection extenders provide additional database specific functionality not coming in the ActiveRecord library. This module itself only provides functionality to register and retrieve such connection extenders.

Public class methods

clear_db_connection_cache ()

Free up all cached connections

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 148
    def self.clear_db_connection_cache
      @@connection_cache = {}
    end
connection_cache ()

Returns the connection cache hash.

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 95
    def self.connection_cache; @@connection_cache; end
connection_cache= (cache)

Sets a new connection cache

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 98
    def self.connection_cache=(cache)
      @@connection_cache = cache
    end
db_connect (config)

Creates database connections by calling db_connect_without_cache with the provided config configuration hash. A new database connection is created only if no according cached connection is available.

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 120
    def self.db_connect(config)
      if not use_cache?
        db_connection = db_connect_without_cache config
      else
        config_dump = Marshal.dump config.reject {|key, | [:proxy_host, :proxy_port, :logger].include? key}
        config_checksum = Digest::SHA1.hexdigest(config_dump)
        @@connection_cache ||= {}

        db_connection = connection_cache[config_checksum]
        unless db_connection and db_connection.active?
          db_connection = db_connect_without_cache config
          connection_cache[config_checksum] = db_connection
        end
      end

      install_logger db_connection, config

      db_connection
    end
db_connect_without_cache (config)

Creates an ActiveRecord database connection according to the provided config connection hash. Possible values of this parameter are described in ActiveRecord::Base#establish_connection. The database connection is extended with the correct ConnectionExtenders module.

ActiveRecord only allows one database connection per class. (It disconnects the existing database connection if a new connection is established.) To go around this, we delete ActiveRecord’s memory of the existing database connection as soon as it is created.

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 49
    def self.db_connect_without_cache(config)
      if RUBY_PLATFORM =~ /java/
        adapter = config[:adapter]
        
        # As recommended in the activerecord-jdbc-adapter use the jdbc versions
        # of the Adapters. E. g. instead of "postgresql", "jdbcpostgresql".
        adapter = 'jdbc' + adapter unless adapter =~ /^jdbc/

        DummyActiveRecord.establish_connection(config.merge(:adapter => adapter))
      else
        DummyActiveRecord.establish_connection(config)
      end
      connection = DummyActiveRecord.connection
      
      # Delete the database connection from ActiveRecords's 'memory'
      ActiveRecord::Base.connection_handler.connection_pools.delete DummyActiveRecord.name
      
      extender = ""
      if RUBY_PLATFORM =~ /java/
        extender = :jdbc
      elsif ConnectionExtenders.extenders.include? config[:adapter].to_sym
        extender = config[:adapter].to_sym
      else
        raise "No ConnectionExtender available for :#{config[:adapter]}"
      end
      connection.extend ConnectionExtenders.extenders[extender]
      
      # Hack to get Postgres schema support under JRuby to par with the standard
      # ruby version
      if RUBY_PLATFORM =~ /java/ and config[:adapter].to_sym == :postgresql
        connection.extend RR::ConnectionExtenders::PostgreSQLExtender
        connection.initialize_search_path
      end

      replication_module = ReplicationExtenders.extenders[config[:adapter].to_sym]
      connection.extend replication_module if replication_module
      
      connection
    end
extenders ()

Returns a Hash of currently registered connection extenders. (Empty Hash if no connection extenders were defined.)

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 23
    def self.extenders
      @extenders ||= {}
      @extenders
    end
install_logger (db_connection, config)

Installs the configured logger (if any) into the database connection.

  • db_connection: database connection (as produced by db_connect)
  • config: database configuration (as provided to db_connect)
[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 105
    def self.install_logger(db_connection, config)
      if config[:logger]
        if config[:logger].respond_to?(:debug)
          logger = config[:logger]
        else
          logger = Logger.new(config[:logger])
        end
        db_connection.instance_variable_set :@logger, logger
      end
    end
register (extender)

Registers one or multiple connection extender. extender is a Hash with

key::   The adapter symbol as used by ActiveRecord::Connection Adapters, e. g. :postgresql
value:: Name of the module implementing the connection extender
[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 32
    def self.register(extender)
      @extenders ||= {}
      @extenders.merge! extender
    end
use_cache? ()

Returns the current cache status (true if caching is used; false otherwise).

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 92
    def self.use_cache?; @@use_cache; end
use_db_connection_cache (status)

If status == true: enable the cache. If status == false: don’ use cache Returns the old connection caching status

[show source]
# File lib/rubyrep/connection_extenders/connection_extenders.rb, line 142
    def self.use_db_connection_cache(status)
      old_status, @@use_cache = @@use_cache, status
      old_status
    end