Module: ActiveRecord::ConnectionHandling
- Included in:
- Base
- Defined in:
- activerecord/lib/active_record/connection_adapters/mysql_adapter.rb,
activerecord/lib/active_record/connection_handling.rb,
activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb,
activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb,
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
Overview
:nodoc:
Constant Summary collapse
- VALID_CONN_PARAMS =
[:host, :hostaddr, :port, :dbname, :user, :password, :connect_timeout, :client_encoding, :options, :application_name, :fallback_application_name, :keepalives, :keepalives_idle, :keepalives_interval, :keepalives_count, :tty, :sslmode, :requiressl, :sslcompression, :sslcert, :sslkey, :sslrootcert, :sslcrl, :requirepeer, :krbsrvname, :gsslib, :service]
Instance Method Summary collapse
-
#clear_cache! ⇒ Object
:nodoc:.
-
#connected? ⇒ Boolean
Returns
true
if Active Record is connected. -
#connection ⇒ Object
Returns the connection currently associated with the class.
-
#connection_config ⇒ Object
Returns the configuration of the associated connection as a hash:.
- #connection_id ⇒ Object
- #connection_id=(connection_id) ⇒ Object
- #connection_pool ⇒ Object
-
#establish_connection(spec = ENV["DATABASE_URL"]) ⇒ Object
Establishes the connection to the database.
-
#mysql2_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
-
#mysql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
-
#postgresql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
- #remove_connection(klass = self) ⇒ Object
- #retrieve_connection ⇒ Object
-
#sqlite3_connection(config) ⇒ Object
sqlite3 adapter reuses sqlite_connection.
Instance Method Details
#clear_cache! ⇒ Object
:nodoc:
91 92 93 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 91 def clear_cache! # :nodoc: connection.schema_cache.clear! end |
#connected? ⇒ Boolean
Returns true
if Active Record is connected.
83 84 85 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 83 def connected? connection_handler.connected?(self) end |
#connection ⇒ Object
Returns the connection currently associated with the class. This can also be used to “borrow” the connection to do database work unrelated to any of the specific Active Records.
52 53 54 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 52 def connection retrieve_connection end |
#connection_config ⇒ Object
Returns the configuration of the associated connection as a hash:
ActiveRecord::Base.connection_config
# => {pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}
Please use only for reading.
70 71 72 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 70 def connection_config connection_pool.spec.config end |
#connection_id ⇒ Object
56 57 58 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 56 def connection_id ActiveRecord::RuntimeRegistry.connection_id end |
#connection_id=(connection_id) ⇒ Object
60 61 62 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 60 def connection_id=(connection_id) ActiveRecord::RuntimeRegistry.connection_id = connection_id end |
#connection_pool ⇒ Object
74 75 76 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 74 def connection_pool connection_handler.retrieve_connection_pool(self) or raise ConnectionNotEstablished end |
#establish_connection(spec = ENV["DATABASE_URL"]) ⇒ Object
Establishes the connection to the database. Accepts a hash as input where the :adapter
key must be specified with the name of a database adapter (in lower-case) example for regular databases (MySQL, Postgresql, etc):
ActiveRecord::Base.establish_connection(
adapter: "mysql",
host: "localhost",
username: "myuser",
password: "mypass",
database: "somedatabase"
)
Example for SQLite database:
ActiveRecord::Base.establish_connection(
adapter: "sqlite",
database: "path/to/dbfile"
)
Also accepts keys as strings (for parsing from YAML for example):
ActiveRecord::Base.establish_connection(
"adapter" => "sqlite",
"database" => "path/to/dbfile"
)
Or a URL:
ActiveRecord::Base.establish_connection(
"postgres://myuser:mypass@localhost/somedatabase"
)
The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError may be returned on an error.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 37 def establish_connection(spec = ENV["DATABASE_URL"]) resolver = ConnectionAdapters::ConnectionSpecification::Resolver.new spec, configurations spec = resolver.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 |
#mysql2_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb', line 9 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) = [config[:host], config[:username], config[:password], config[:database], config[:port], config[:socket], 0] ConnectionAdapters::Mysql2Adapter.new(client, logger, , config) end |
#mysql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'activerecord/lib/active_record/connection_adapters/mysql_adapter.rb', line 21 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) = [host, username, password, database, port, socket, default_flags] ConnectionAdapters::MysqlAdapter.new(mysql, logger, , config) end |
#postgresql_connection(config) ⇒ Object
Establishes a connection to the database that’s used by all Active Record objects
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb', line 27 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 |
#remove_connection(klass = self) ⇒ Object
87 88 89 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 87 def remove_connection(klass = self) connection_handler.remove_connection(klass) end |
#retrieve_connection ⇒ Object
78 79 80 |
# File 'activerecord/lib/active_record/connection_handling.rb', line 78 def retrieve_connection connection_handler.retrieve_connection(self) end |
#sqlite3_connection(config) ⇒ Object
sqlite3 adapter reuses sqlite_connection.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb', line 11 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 defined?(Rails.root) && ':memory:' != config[:database] config[:database] = File.(config[:database], Rails.root) end db = SQLite3::Database.new( config[:database], :results_as_hash => true ) db.busy_timeout(ConnectionAdapters::SQLite3Adapter.type_cast_config_to_integer(config[:timeout])) if config[:timeout] ConnectionAdapters::SQLite3Adapter.new(db, logger, config) end |