3 class ConnectionSpecification
#:nodoc:
4 attr_reader
:config, :adapter_method
5 def initialize (config
, adapter_method
)
6 @config, @adapter_method = config
, adapter_method
10 # The connection handler
11 cattr_accessor
:connection_handler, :instance_writer => false
12 @
@connection_handler = ConnectionAdapters
::ConnectionHandler.new
14 # Returns the connection currently associated with the class. This can
15 # also be used to "borrow" the connection to do database work that isn't
16 # easily done without going straight to SQL.
21 # Establishes the connection to the database. Accepts a hash as input where
22 # the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
23 # example for regular databases (MySQL, Postgresql, etc):
25 # ActiveRecord::Base.establish_connection(
26 # :adapter => "mysql",
27 # :host => "localhost",
28 # :username => "myuser",
29 # :password => "mypass",
30 # :database => "somedatabase"
33 # Example for SQLite database:
35 # ActiveRecord::Base.establish_connection(
36 # :adapter => "sqlite",
37 # :database => "path/to/dbfile"
40 # Also accepts keys as strings (for parsing from YAML for example):
42 # ActiveRecord::Base.establish_connection(
43 # "adapter" => "sqlite",
44 # "database" => "path/to/dbfile"
47 # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
48 # may be returned on an error.
49 def self.establish_connection(spec
= nil)
52 raise AdapterNotSpecified
unless defined? RAILS_ENV
53 establish_connection(RAILS_ENV
)
54 when ConnectionSpecification
55 @
@connection_handler.establish_connection(name
, spec
)
57 if configuration
= configurations
[spec
.to_s
]
58 establish_connection(configuration
)
60 raise AdapterNotSpecified
, "#{spec} database is not configured"
63 spec
= spec
.symbolize_keys
64 unless spec
.key
?(:adapter) then raise AdapterNotSpecified
, "database configuration does not specify adapter" end
68 gem
"activerecord-#{spec[:adapter]}-adapter"
69 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
72 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
74 raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{$!})"
78 adapter_method
= "#{spec[:adapter]}_connection"
79 if !respond_to
?(adapter_method
)
80 raise AdapterNotFound
, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
84 establish_connection(ConnectionSpecification
.new(spec
, adapter_method
))
89 # Deprecated and no longer has any effect.
91 ActiveSupport
::Deprecation.warn("ActiveRecord::Base.allow_concurrency has been deprecated and no longer has any effect. Please remove all references to allow_concurrency.")
94 # Deprecated and no longer has any effect.
95 def allow_concurrency
=(flag
)
96 ActiveSupport
::Deprecation.warn("ActiveRecord::Base.allow_concurrency= has been deprecated and no longer has any effect. Please remove all references to allow_concurrency=.")
99 # Deprecated and no longer has any effect.
100 def verification_timeout
101 ActiveSupport
::Deprecation.warn("ActiveRecord::Base.verification_timeout has been deprecated and no longer has any effect. Please remove all references to verification_timeout.")
104 # Deprecated and no longer has any effect.
105 def verification_timeout
=(flag
)
106 ActiveSupport
::Deprecation.warn("ActiveRecord::Base.verification_timeout= has been deprecated and no longer has any effect. Please remove all references to verification_timeout=.")
109 # Returns the connection currently associated with the class. This can
110 # also be used to "borrow" the connection to do database work unrelated
111 # to any of the specific Active Records.
117 connection_handler
.retrieve_connection_pool(self)
120 def retrieve_connection
121 connection_handler
.retrieve_connection(self)
125 connection_handler
.connected
?(self)
128 def remove_connection(klass
= self)
129 connection_handler
.remove_connection(klass
)
132 delegate
:clear_active_connections!, :clear_reloadable_connections!,
133 :clear_all_connections!,:verify_active_connections!, :to => :connection_handler