Froze rails gems
[depot.git] / vendor / rails / activerecord / lib / active_record / connection_adapters / abstract / connection_specification.rb
1 module ActiveRecord
2 class Base
3 class ConnectionSpecification #:nodoc:
4 attr_reader :config, :adapter_method
5 def initialize (config, adapter_method)
6 @config, @adapter_method = config, adapter_method
7 end
8 end
9
10 # The connection handler
11 cattr_accessor :connection_handler, :instance_writer => false
12 @@connection_handler = ConnectionAdapters::ConnectionHandler.new
13
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.
17 def connection
18 self.class.connection
19 end
20
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):
24 #
25 # ActiveRecord::Base.establish_connection(
26 # :adapter => "mysql",
27 # :host => "localhost",
28 # :username => "myuser",
29 # :password => "mypass",
30 # :database => "somedatabase"
31 # )
32 #
33 # Example for SQLite database:
34 #
35 # ActiveRecord::Base.establish_connection(
36 # :adapter => "sqlite",
37 # :database => "path/to/dbfile"
38 # )
39 #
40 # Also accepts keys as strings (for parsing from YAML for example):
41 #
42 # ActiveRecord::Base.establish_connection(
43 # "adapter" => "sqlite",
44 # "database" => "path/to/dbfile"
45 # )
46 #
47 # The exceptions AdapterNotSpecified, AdapterNotFound and ArgumentError
48 # may be returned on an error.
49 def self.establish_connection(spec = nil)
50 case spec
51 when nil
52 raise AdapterNotSpecified unless defined? RAILS_ENV
53 establish_connection(RAILS_ENV)
54 when ConnectionSpecification
55 @@connection_handler.establish_connection(name, spec)
56 when Symbol, String
57 if configuration = configurations[spec.to_s]
58 establish_connection(configuration)
59 else
60 raise AdapterNotSpecified, "#{spec} database is not configured"
61 end
62 else
63 spec = spec.symbolize_keys
64 unless spec.key?(:adapter) then raise AdapterNotSpecified, "database configuration does not specify adapter" end
65
66 begin
67 require 'rubygems'
68 gem "activerecord-#{spec[:adapter]}-adapter"
69 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
70 rescue LoadError
71 begin
72 require "active_record/connection_adapters/#{spec[:adapter]}_adapter"
73 rescue LoadError
74 raise "Please install the #{spec[:adapter]} adapter: `gem install activerecord-#{spec[:adapter]}-adapter` (#{$!})"
75 end
76 end
77
78 adapter_method = "#{spec[:adapter]}_connection"
79 if !respond_to?(adapter_method)
80 raise AdapterNotFound, "database configuration specifies nonexistent #{spec[:adapter]} adapter"
81 end
82
83 remove_connection
84 establish_connection(ConnectionSpecification.new(spec, adapter_method))
85 end
86 end
87
88 class << self
89 # Deprecated and no longer has any effect.
90 def allow_concurrency
91 ActiveSupport::Deprecation.warn("ActiveRecord::Base.allow_concurrency has been deprecated and no longer has any effect. Please remove all references to allow_concurrency.")
92 end
93
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=.")
97 end
98
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.")
102 end
103
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=.")
107 end
108
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.
112 def connection
113 retrieve_connection
114 end
115
116 def connection_pool
117 connection_handler.retrieve_connection_pool(self)
118 end
119
120 def retrieve_connection
121 connection_handler.retrieve_connection(self)
122 end
123
124 def connected?
125 connection_handler.connected?(self)
126 end
127
128 def remove_connection(klass = self)
129 connection_handler.remove_connection(klass)
130 end
131
132 delegate :clear_active_connections!, :clear_reloadable_connections!,
133 :clear_all_connections!,:verify_active_connections!, :to => :connection_handler
134 end
135 end
136 end