3 class ConnectionSpecification #:nodoc:
4 attr_reader :config, :adapter_method
5 def initialize (config, adapter_method)
6 @config, @adapter_method = config, adapter_method
12 # The connection handler
13 cattr_accessor :connection_handler, :instance_writer => false
14 @@connection_handler = ConnectionAdapters::ConnectionHandler.new
16 # Returns the connection currently associated with the class. This can
17 # also be used to "borrow" the connection to do database work that isn't
18 # easily done without going straight to SQL.
23 # Establishes the connection to the database. Accepts a hash as input where
24 # the <tt>:adapter</tt> key must be specified with the name of a database adapter (in lower-case)
25 # example for regular databases (MySQL, Postgresql, etc):
27 # ActiveRecord::Base.establish_connection(
28 # :adapter => "mysql",
29 # :host => "localhost",
30 # :username => "myuser",
31 # :password => "mypass",
32 # :database => "somedatabase"
35 # Example for SQLite database:
37 # ActiveRecord::Base.establish_connection(
38 # :adapter => "sqlite",
39 # :database => "path/to/dbfile"
42 # Also accepts keys as strings (for parsing from YAML for example):
44 # ActiveRecord::Base.establish_connection(
45 # "adapter" => "sqlite",
46 # "database" => "path/to/dbfile"
49 # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
50 # may be returned on an error.
51 def self.establish_connection(spec = nil)
54 raise AdapterNotSpecified unless defined? RAILS_ENV
55 establish_connection(RAILS_ENV)
56 when ConnectionSpecification
57 @@connection_handler.establish_connection(name, spec)
59 if configuration = configurations[spec.to_s]
60 establish_connection(configuration)
62 raise AdapterNotSpecified, "#{spec} database is not configured"
65 spec = spec.symbolize_keys
66 unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end
70 gem "activerecord-#{spec[:adapter]}-adapter"
71 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
74 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
76 raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{$!})"
80 adapter_method = "#{spec[:adapter]}_connection"
81 if !respond_to?(adapter_method)
82 raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
86 establish_connection(ConnectionSpecification.new(spec, adapter_method))
91 # Deprecated and no longer has any effect.
93 ActiveSupport::Deprecation.warn("ActiveRecord::Base.allow_concurrency has been deprecated and no longer has any effect. Please remove all references to allow_concurrency.")
96 # Deprecated and no longer has any effect.
97 def allow_concurrency=(flag)
98 ActiveSupport::Deprecation.warn("ActiveRecord::Base.allow_concurrency= has been deprecated and no longer has any effect. Please remove all references to allow_concurrency=.")
101 # Deprecated and no longer has any effect.
102 def verification_timeout
103 ActiveSupport::Deprecation.warn("ActiveRecord::Base.verification_timeout has been deprecated and no longer has any effect. Please remove all references to verification_timeout.")
106 # Deprecated and no longer has any effect.
107 def verification_timeout=(flag)
108 ActiveSupport::Deprecation.warn("ActiveRecord::Base.verification_timeout= has been deprecated and no longer has any effect. Please remove all references to verification_timeout=.")
111 # Returns the connection currently associated with the class. This can
112 # also be used to "borrow" the connection to do database work unrelated
113 # to any of the specific Active Records.
119 connection_handler.retrieve_connection_pool(self)
122 def retrieve_connection
123 connection_handler.retrieve_connection(self)
126 # Returns true if +ActiveRecord+ is connected.
128 connection_handler.connected?(self)
131 def remove_connection(klass = self)
132 connection_handler.remove_connection(klass)
135 delegate :clear_active_connections!, :clear_reloadable_connections!,
136 :clear_all_connections!,:verify_active_connections!, :to => :connection_handler