module ActiveRecord::ConnectionHandling

def clear_cache! # :nodoc:

:nodoc:
def clear_cache! # :nodoc:
  connection.schema_cache.clear!
end

def connected?

Returns +true+ if Active Record is connected.
def connected?
  connection_handler.connected?(self)
end

def connection

to any of the specific Active Records.
also be used to "borrow" the connection to do database work unrelated
Returns the connection currently associated with the class. This can
def connection
  retrieve_connection
end

def connection_config

Please use only for reading.

# => {pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}
ActiveRecord::Base.connection_config

Returns the configuration of the associated connection as a hash:
def connection_config
  connection_pool.spec.config
end

def connection_id

def connection_id
  ActiveRecord::RuntimeRegistry.connection_id
end

def connection_id=(connection_id)

def connection_id=(connection_id)
  ActiveRecord::RuntimeRegistry.connection_id = connection_id
end

def connection_pool

def connection_pool
  connection_handler.retrieve_connection_pool(self) or raise ConnectionNotEstablished
end

def establish_connection(spec = nil)

may be returned on an error.
The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError

ActiveRecord::Base.establish_connection(:production)

configuration hash:
a symbol can also be given as argument, representing a key in the
automatically loads the contents of config/database.yml into it),
In case ActiveRecord::Base.configurations is set (Rails

)
"postgres://myuser:mypass@localhost/somedatabase"
ActiveRecord::Base.establish_connection(

Or a URL:

)
"database" => "path/to/dbfile"
"adapter" => "sqlite3",
ActiveRecord::Base.establish_connection(

Also accepts keys as strings (for parsing from YAML for example):

)
database: "path/to/dbfile"
adapter: "sqlite3",
ActiveRecord::Base.establish_connection(

Example for SQLite database:

)
database: "somedatabase"
password: "mypass",
username: "myuser",
host: "localhost",
adapter: "mysql",
ActiveRecord::Base.establish_connection(

example for regular databases (MySQL, Postgresql, etc):
the :adapter key must be specified with the name of a database adapter (in lower-case)
Establishes the connection to the database. Accepts a hash as input where
def establish_connection(spec = nil)
  spec     ||= DEFAULT_ENV.call.to_sym
  resolver =   ConnectionAdapters::ConnectionSpecification::Resolver.new configurations
  spec     =   resolver.spec(spec)
  unless respond_to?(spec.adapter_method)
    raise AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
  end
  remove_connection
  connection_handler.establish_connection self, spec
end

def mysql2_connection(config)

Establishes a connection to the database that's used by all Active Record objects.
:nodoc:
def mysql2_connection(config)
  config = config.symbolize_keys
  config[:username] = 'root' if config[:username].nil?
  if Mysql2::Client.const_defined? :FOUND_ROWS
    config[:flags] = Mysql2::Client::FOUND_ROWS
  end
  client = Mysql2::Client.new(config)
  options = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0]
  ConnectionAdapters::Mysql2Adapter.new(client, logger, options, config)
rescue Mysql2::Error => error
  if error.message.include?("Unknown database")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end

def mysql_connection(config)

Establishes a connection to the database that's used by all Active Record objects.
:nodoc:
def mysql_connection(config)
  config = config.symbolize_keys
  host     = config[:host]
  port     = config[:port]
  socket   = config[:socket]
  username = config[:username] ? config[:username].to_s : 'root'
  password = config[:password].to_s
  database = config[:database]
  mysql = Mysql.init
  mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslca] || config[:sslkey]
  default_flags = Mysql.const_defined?(:CLIENT_MULTI_RESULTS) ? Mysql::CLIENT_MULTI_RESULTS : 0
  default_flags |= Mysql::CLIENT_FOUND_ROWS if Mysql.const_defined?(:CLIENT_FOUND_ROWS)
  options = [host, username, password, database, port, socket, default_flags]
  ConnectionAdapters::MysqlAdapter.new(mysql, logger, options, config)
rescue Mysql::Error => error
  if error.message.include?("Unknown database")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end

def postgresql_connection(config)

Establishes a connection to the database that's used by all Active Record objects
def postgresql_connection(config)
  conn_params = config.symbolize_keys
  conn_params.delete_if { |_, v| v.nil? }
  # Map ActiveRecords param names to PGs.
  conn_params[:user] = conn_params.delete(:username) if conn_params[:username]
  conn_params[:dbname] = conn_params.delete(:database) if conn_params[:database]
  # Forward only valid config params to PGconn.connect.
  conn_params.keep_if { |k, _| VALID_CONN_PARAMS.include?(k) }
  # The postgres drivers don't allow the creation of an unconnected PGconn object,
  # so just pass a nil connection object for the time being.
  ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, conn_params, config)
end

def remove_connection(klass = self)

def remove_connection(klass = self)
  connection_handler.remove_connection(klass)
end

def retrieve_connection

def retrieve_connection
  connection_handler.retrieve_connection(self)
end

def sqlite3_connection(config)

sqlite3 adapter reuses sqlite_connection.
:nodoc:
def sqlite3_connection(config)
  # Require database.
  unless config[:database]
    raise ArgumentError, "No database file specified. Missing argument: database"
  end
  # Allow database path relative to Rails.root, but only if the database
  # path is not the special path that tells sqlite to build a database only
  # in memory.
  if ':memory:' != config[:database]
    config[:database] = File.expand_path(config[:database], Rails.root) if defined?(Rails.root)
    dirname = File.dirname(config[:database])
    Dir.mkdir(dirname) unless File.directory?(dirname)
  end
  db = SQLite3::Database.new(
    config[:database].to_s,
    :results_as_hash => true
  )
  db.busy_timeout(ConnectionAdapters::SQLite3Adapter.type_cast_config_to_integer(config[:timeout])) if config[:timeout]
  ConnectionAdapters::SQLite3Adapter.new(db, logger, nil, config)
rescue Errno::ENOENT => error
  if error.message.include?("No such file or directory")
    raise ActiveRecord::NoDatabaseError.new(error.message, error)
  else
    raise
  end
end