~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'benchmark'
2
 
require 'date'
3
 
require 'bigdecimal'
4
 
require 'bigdecimal/util'
5
 
 
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'
14
 
 
15
 
module ActiveRecord
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.
23
 
    #
24
 
    # All the concrete database adapters follow the interface laid down in this class.
25
 
    # ActiveRecord::Base.connection returns an AbstractAdapter object, which
26
 
    # you can use.
27
 
    #
28
 
    # Most of the methods in the adapter are useful during migrations. Most
29
 
    # notably, the instance methods provided by SchemaStatement are very useful.
30
 
    class AbstractAdapter
31
 
      include Quoting, DatabaseStatements, SchemaStatements
32
 
      include QueryCache
33
 
      include ActiveSupport::Callbacks
34
 
      define_callbacks :checkout, :checkin
35
 
 
36
 
      @@row_even = true
37
 
 
38
 
      def initialize(connection, logger = nil) #:nodoc:
39
 
        @connection, @logger = connection, logger
40
 
        @runtime = 0
41
 
        @last_verification = 0
42
 
        @query_cache_enabled = false
43
 
      end
44
 
 
45
 
      # Returns the human-readable name of the adapter.  Use mixed case - one
46
 
      # can always use downcase if needed.
47
 
      def adapter_name
48
 
        'Abstract'
49
 
      end
50
 
 
51
 
      # Does this adapter support migrations?  Backend specific, as the
52
 
      # abstract adapter always returns +false+.
53
 
      def supports_migrations?
54
 
        false
55
 
      end
56
 
 
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?
61
 
        false
62
 
      end
63
 
 
64
 
      # Does this adapter support using DISTINCT within COUNT?  This is +true+
65
 
      # for all adapters except sqlite.
66
 
      def supports_count_distinct?
67
 
        true
68
 
      end
69
 
 
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?
74
 
        false
75
 
      end
76
 
      
77
 
      # Does this adapter support savepoints? PostgreSQL and MySQL do, SQLite
78
 
      # does not.
79
 
      def supports_savepoints?
80
 
        false
81
 
      end
82
 
 
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)
88
 
        false
89
 
      end
90
 
 
91
 
      def reset_runtime #:nodoc:
92
 
        rt, @runtime = @runtime, 0
93
 
        rt
94
 
      end
95
 
 
96
 
      # QUOTING ==================================================
97
 
 
98
 
      # Override to return the quoted table name. Defaults to column quoting.
99
 
      def quote_table_name(name)
100
 
        quote_column_name(name)
101
 
      end
102
 
 
103
 
      # REFERENTIAL INTEGRITY ====================================
104
 
 
105
 
      # Override to turn off referential integrity while executing <tt>&block</tt>.
106
 
      def disable_referential_integrity(&block)
107
 
        yield
108
 
      end
109
 
 
110
 
      # CONNECTION MANAGEMENT ====================================
111
 
 
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.
115
 
      def active?
116
 
        @active != false
117
 
      end
118
 
 
119
 
      # Disconnects from the database if already connected, and establishes a
120
 
      # new connection with the database.
121
 
      def reconnect!
122
 
        @active = true
123
 
      end
124
 
 
125
 
      # Disconnects from the database if already connected. Otherwise, this
126
 
      # method does nothing.
127
 
      def disconnect!
128
 
        @active = false
129
 
      end
130
 
 
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.
134
 
      #
135
 
      # The default implementation does nothing; the implementation should be
136
 
      # overridden by concrete adapters.
137
 
      def reset!
138
 
        # this should be overridden by concrete adapters
139
 
      end
140
 
 
141
 
      # Returns true if its safe to reload the connection between requests for development mode.
142
 
      def requires_reloading?
143
 
        true
144
 
      end
145
 
 
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?
151
 
      end
152
 
 
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.
156
 
      #
157
 
      # This is useful for when you need to call a proprietary method such as
158
 
      # PostgreSQL's lo_* methods.
159
 
      def raw_connection
160
 
        @connection
161
 
      end
162
 
 
163
 
      def open_transactions
164
 
        @open_transactions ||= 0
165
 
      end
166
 
 
167
 
      def increment_open_transactions
168
 
        @open_transactions ||= 0
169
 
        @open_transactions += 1
170
 
      end
171
 
 
172
 
      def decrement_open_transactions
173
 
        @open_transactions -= 1
174
 
      end
175
 
 
176
 
      def transaction_joinable=(joinable)
177
 
        @transaction_joinable = joinable
178
 
      end
179
 
 
180
 
      def create_savepoint
181
 
      end
182
 
 
183
 
      def rollback_to_savepoint
184
 
      end
185
 
 
186
 
      def release_savepoint
187
 
      end
188
 
 
189
 
      def current_savepoint_name
190
 
        "active_record_#{open_transactions}"
191
 
      end
192
 
 
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(' ')))
197
 
        end
198
 
      end
199
 
 
200
 
      protected
201
 
        def log(sql, name)
202
 
          if block_given?
203
 
            result = nil
204
 
            ms = Benchmark.ms { result = yield }
205
 
            @runtime += ms
206
 
            log_info(sql, name, ms)
207
 
            result
208
 
          else
209
 
            log_info(sql, name, 0)
210
 
            nil
211
 
          end
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
220
 
        end
221
 
 
222
 
        def format_log_entry(message, dump = nil)
223
 
          if ActiveRecord::Base.colorize_logging
224
 
            if @@row_even
225
 
              @@row_even = false
226
 
              message_color, dump_color = "4;36;1", "0;1"
227
 
            else
228
 
              @@row_even = true
229
 
              message_color, dump_color = "4;35;1", "0"
230
 
            end
231
 
 
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
234
 
            log_entry
235
 
          else
236
 
            "%s  %s" % [message, dump]
237
 
          end
238
 
        end
239
 
    end
240
 
  end
241
 
end