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