4
require 'bigdecimal/util'
6
# TODO: Autoload these files
7
require 'active_record/connection_adapters/abstract/schema_definitions'
8
require 'active_record/connection_adapters/abstract/schema_statements'
9
require 'active_record/connection_adapters/abstract/database_statements'
10
require 'active_record/connection_adapters/abstract/quoting'
11
require 'active_record/connection_adapters/abstract/connection_pool'
12
require 'active_record/connection_adapters/abstract/connection_specification'
13
require 'active_record/connection_adapters/abstract/query_cache'
16
module ConnectionAdapters # :nodoc:
17
# ActiveRecord supports multiple database systems. AbstractAdapter and
18
# related classes form the abstraction layer which makes this possible.
19
# An AbstractAdapter represents a connection to a database, and provides an
20
# abstract interface for database-specific functionality such as establishing
21
# a connection, escaping values, building the right SQL fragments for ':offset'
22
# and ':limit' options, etc.
24
# All the concrete database adapters follow the interface laid down in this class.
25
# ActiveRecord::Base.connection returns an AbstractAdapter object, which
28
# Most of the methods in the adapter are useful during migrations. Most
29
# notably, the instance methods provided by SchemaStatement are very useful.
31
include Quoting, DatabaseStatements, SchemaStatements
33
include ActiveSupport::Callbacks
34
define_callbacks :checkout, :checkin
38
def initialize(connection, logger = nil) #:nodoc:
39
@connection, @logger = connection, logger
41
@last_verification = 0
42
@query_cache_enabled = false
45
# Returns the human-readable name of the adapter. Use mixed case - one
46
# can always use downcase if needed.
51
# Does this adapter support migrations? Backend specific, as the
52
# abstract adapter always returns +false+.
53
def supports_migrations?
57
# Can this adapter determine the primary key for tables not attached
58
# to an ActiveRecord class, such as join tables? Backend specific, as
59
# the abstract adapter always returns +false+.
60
def supports_primary_key?
64
# Does this adapter support using DISTINCT within COUNT? This is +true+
65
# for all adapters except sqlite.
66
def supports_count_distinct?
70
# Does this adapter support DDL rollbacks in transactions? That is, would
71
# CREATE TABLE or ALTER TABLE get rolled back by a transaction? PostgreSQL,
72
# SQL Server, and others support this. MySQL and others do not.
73
def supports_ddl_transactions?
77
# Does this adapter support savepoints? PostgreSQL and MySQL do, SQLite
79
def supports_savepoints?
83
# Should primary key values be selected from their corresponding
84
# sequence before the insert statement? If true, next_sequence_value
85
# is called before each insert to set the record's primary key.
86
# This is false for all adapters but Firebird.
87
def prefetch_primary_key?(table_name = nil)
91
def reset_runtime #:nodoc:
92
rt, @runtime = @runtime, 0
96
# QUOTING ==================================================
98
# Override to return the quoted table name. Defaults to column quoting.
99
def quote_table_name(name)
100
quote_column_name(name)
103
# REFERENTIAL INTEGRITY ====================================
105
# Override to turn off referential integrity while executing <tt>&block</tt>.
106
def disable_referential_integrity(&block)
110
# CONNECTION MANAGEMENT ====================================
112
# Checks whether the connection to the database is still active. This includes
113
# checking whether the database is actually capable of responding, i.e. whether
114
# the connection isn't stale.
119
# Disconnects from the database if already connected, and establishes a
120
# new connection with the database.
125
# Disconnects from the database if already connected. Otherwise, this
126
# method does nothing.
131
# Reset the state of this connection, directing the DBMS to clear
132
# transactions and other connection-related server-side state. Usually a
133
# database-dependent operation.
135
# The default implementation does nothing; the implementation should be
136
# overridden by concrete adapters.
138
# this should be overridden by concrete adapters
141
# Returns true if its safe to reload the connection between requests for development mode.
142
def requires_reloading?
146
# Checks whether the connection to the database is still active (i.e. not stale).
147
# This is done under the hood by calling <tt>active?</tt>. If the connection
148
# is no longer active, then this method will reconnect to the database.
149
def verify!(*ignored)
150
reconnect! unless active?
153
# Provides access to the underlying database driver for this adapter. For
154
# example, this method returns a Mysql object in case of MysqlAdapter,
155
# and a PGconn object in case of PostgreSQLAdapter.
157
# This is useful for when you need to call a proprietary method such as
158
# PostgreSQL's lo_* methods.
163
def open_transactions
164
@open_transactions ||= 0
167
def increment_open_transactions
168
@open_transactions ||= 0
169
@open_transactions += 1
172
def decrement_open_transactions
173
@open_transactions -= 1
176
def transaction_joinable=(joinable)
177
@transaction_joinable = joinable
183
def rollback_to_savepoint
186
def release_savepoint
189
def current_savepoint_name
190
"active_record_#{open_transactions}"
193
def log_info(sql, name, ms)
194
if @logger && @logger.debug?
195
name = '%s (%.1fms)' % [name || 'SQL', ms]
196
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
204
ms = Benchmark.ms { result = yield }
206
log_info(sql, name, ms)
209
log_info(sql, name, 0)
212
rescue Exception => e
213
# Log message and raise exception.
214
# Set last_verification to 0, so that connection gets verified
215
# upon reentering the request loop
216
@last_verification = 0
217
message = "#{e.class.name}: #{e.message}: #{sql}"
218
log_info(message, name, 0)
219
raise ActiveRecord::StatementInvalid, message
222
def format_log_entry(message, dump = nil)
223
if ActiveRecord::Base.colorize_logging
226
message_color, dump_color = "4;36;1", "0;1"
229
message_color, dump_color = "4;35;1", "0"
232
log_entry = " \e[#{message_color}m#{message}\e[0m "
233
log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
236
"%s %s" % [message, dump]