Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Revision 8670, 5.6 kB (checked in by bitsweat, 2 years ago)

Remove dead code. Closes #10698 [garru]

  • Property svn:executable set to *
Line 
1 require 'benchmark'
2 require 'date'
3 require 'bigdecimal'
4 require 'bigdecimal/util'
5
6 require 'active_record/connection_adapters/abstract/schema_definitions'
7 require 'active_record/connection_adapters/abstract/schema_statements'
8 require 'active_record/connection_adapters/abstract/database_statements'
9 require 'active_record/connection_adapters/abstract/quoting'
10 require 'active_record/connection_adapters/abstract/connection_specification'
11 require 'active_record/connection_adapters/abstract/query_cache'
12
13 module ActiveRecord
14   module ConnectionAdapters # :nodoc:
15     # All the concrete database adapters follow the interface laid down in this class.
16     # You can use this interface directly by borrowing the database connection from the Base with
17     # Base.connection.
18     #
19     # Most of the methods in the adapter are useful during migrations.  Most
20     # notably, SchemaStatements#create_table, SchemaStatements#drop_table,
21     # SchemaStatements#add_index, SchemaStatements#remove_index,
22     # SchemaStatements#add_column, SchemaStatements#change_column and
23     # SchemaStatements#remove_column are very useful.
24     class AbstractAdapter
25       include Quoting, DatabaseStatements, SchemaStatements
26       include QueryCache
27       @@row_even = true
28
29       def initialize(connection, logger = nil) #:nodoc:
30         @connection, @logger = connection, logger
31         @runtime = 0
32         @last_verification = 0
33         @query_cache_enabled = false
34       end
35
36       # Returns the human-readable name of the adapter.  Use mixed case - one
37       # can always use downcase if needed.
38       def adapter_name
39         'Abstract'
40       end
41
42       # Does this adapter support migrations?  Backend specific, as the
43       # abstract adapter always returns +false+.
44       def supports_migrations?
45         false
46       end
47
48       # Does this adapter support using DISTINCT within COUNT?  This is +true+
49       # for all adapters except sqlite.
50       def supports_count_distinct?
51         true
52       end
53
54       # Should primary key values be selected from their corresponding
55       # sequence before the insert statement?  If true, next_sequence_value
56       # is called before each insert to set the record's primary key.
57       # This is false for all adapters but Firebird.
58       def prefetch_primary_key?(table_name = nil)
59         false
60       end
61
62       def reset_runtime #:nodoc:
63         rt, @runtime = @runtime, 0
64         rt
65       end
66
67       # QUOTING ==================================================
68
69       # Override to return the quoted table name. Defaults to column quoting.
70       def quote_table_name(name)
71         quote_column_name(name)
72       end
73
74       # REFERENTIAL INTEGRITY ====================================
75
76       # Override to turn off referential integrity while executing +&block+
77       def disable_referential_integrity(&block)
78         yield
79       end
80
81       # CONNECTION MANAGEMENT ====================================
82
83       # Is this connection active and ready to perform queries?
84       def active?
85         @active != false
86       end
87
88       # Close this connection and open a new one in its place.
89       def reconnect!
90         @active = true
91       end
92
93       # Close this connection
94       def disconnect!
95         @active = false
96       end
97
98       # Returns true if its safe to reload the connection between requests for development mode.
99       # This is not the case for Ruby/MySQL and it's not necessary for any adapters except SQLite.
100       def requires_reloading?
101         false
102       end
103
104       # Lazily verify this connection, calling +active?+ only if it hasn't
105       # been called for +timeout+ seconds.
106       def verify!(timeout)
107         now = Time.now.to_i
108         if (now - @last_verification) > timeout
109           reconnect! unless active?
110           @last_verification = now
111         end
112       end
113
114       # Provides access to the underlying database connection. Useful for
115       # when you need to call a proprietary method such as postgresql's lo_*
116       # methods
117       def raw_connection
118         @connection
119       end
120
121       def log_info(sql, name, runtime)
122         if @logger && @logger.debug?
123           name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})"
124           @logger.debug format_log_entry(name, sql.squeeze(' '))
125         end
126       end
127
128       protected
129         def log(sql, name)
130           if block_given?
131             if @logger and @logger.debug?
132               result = nil
133               seconds = Benchmark.realtime { result = yield }
134               @runtime += seconds
135               log_info(sql, name, seconds)
136               result
137             else
138               yield
139             end
140           else
141             log_info(sql, name, 0)
142             nil
143           end
144         rescue Exception => e
145           # Log message and raise exception.
146           # Set last_verification to 0, so that connection gets verified
147           # upon reentering the request loop
148           @last_verification = 0
149           message = "#{e.class.name}: #{e.message}: #{sql}"
150           log_info(message, name, 0)
151           raise ActiveRecord::StatementInvalid, message
152         end
153
154         def format_log_entry(message, dump = nil)
155           if ActiveRecord::Base.colorize_logging
156             if @@row_even
157               @@row_even = false
158               message_color, dump_color = "4;36;1", "0;1"
159             else
160               @@row_even = true
161               message_color, dump_color = "4;35;1", "0"
162             end
163
164             log_entry = "  \e[#{message_color}m#{message}\e[0m   "
165             log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
166             log_entry
167           else
168             "%s  %s" % [message, dump]
169           end
170         end
171     end
172   end
173 end
Note: See TracBrowser for help on using the browser.