Class | ActiveRecord::ConnectionAdapters::AbstractAdapter |
In: |
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
|
Parent: | Object |
ActiveRecord supports multiple database systems. AbstractAdapter and related classes form the abstraction layer which makes this possible. An AbstractAdapter represents a connection to a database, and provides an abstract interface for database-specific functionality such as establishing a connection, escaping values, building the right SQL fragments for ’:offset’ and ’:limit’ options, etc.
All the concrete database adapters follow the interface laid down in this class. ActiveRecord::Base.connection returns an AbstractAdapter object, which you can use.
Most of the methods in the adapter are useful during migrations. Most notably, the instance methods provided by SchemaStatement are very useful.
Checks whether the connection to the database is still active. This includes checking whether the database is actually capable of responding, i.e. whether the connection isn‘t stale.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 115 115: def active? 116: @active != false 117: end
Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 47 47: def adapter_name 48: 'Abstract' 49: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 180 180: def create_savepoint 181: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 189 189: def current_savepoint_name 190: "active_record_#{open_transactions}" 191: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 172 172: def decrement_open_transactions 173: @open_transactions -= 1 174: end
Override to turn off referential integrity while executing &block.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 106 106: def disable_referential_integrity(&block) 107: yield 108: end
Disconnects from the database if already connected. Otherwise, this method does nothing.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 127 127: def disconnect! 128: @active = false 129: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 167 167: def increment_open_transactions 168: @open_transactions ||= 0 169: @open_transactions += 1 170: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 193 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
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 163 163: def open_transactions 164: @open_transactions ||= 0 165: end
Should primary key values be selected from their corresponding sequence before the insert statement? If true, next_sequence_value is called before each insert to set the record‘s primary key. This is false for all adapters but Firebird.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 87 87: def prefetch_primary_key?(table_name = nil) 88: false 89: end
Override to return the quoted table name. Defaults to column quoting.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 99 99: def quote_table_name(name) 100: quote_column_name(name) 101: end
Provides access to the underlying database driver for this adapter. For example, this method returns a Mysql object in case of MysqlAdapter, and a PGconn object in case of PostgreSQLAdapter.
This is useful for when you need to call a proprietary method such as PostgreSQL‘s lo_* methods.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 159 159: def raw_connection 160: @connection 161: end
Disconnects from the database if already connected, and establishes a new connection with the database.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 121 121: def reconnect! 122: @active = true 123: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 186 186: def release_savepoint 187: end
Returns true if its safe to reload the connection between requests for development mode.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 142 142: def requires_reloading? 143: true 144: end
Reset the state of this connection, directing the DBMS to clear transactions and other connection-related server-side state. Usually a database-dependent operation.
The default implementation does nothing; the implementation should be overridden by concrete adapters.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 137 137: def reset! 138: # this should be overridden by concrete adapters 139: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 183 183: def rollback_to_savepoint 184: end
Does this adapter support using DISTINCT within COUNT? This is true for all adapters except sqlite.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 66 66: def supports_count_distinct? 67: true 68: end
Does this adapter support DDL rollbacks in transactions? That is, would CREATE TABLE or ALTER TABLE get rolled back by a transaction? PostgreSQL, SQL Server, and others support this. MySQL and others do not.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 73 73: def supports_ddl_transactions? 74: false 75: end
Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 53 53: def supports_migrations? 54: false 55: end
Can this adapter determine the primary key for tables not attached to an ActiveRecord class, such as join tables? Backend specific, as the abstract adapter always returns false.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 60 60: def supports_primary_key? 61: false 62: end
Does this adapter support savepoints? PostgreSQL and MySQL do, SQLite does not.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 79 79: def supports_savepoints? 80: false 81: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 176 176: def transaction_joinable=(joinable) 177: @transaction_joinable = joinable 178: end
Checks whether the connection to the database is still active (i.e. not stale). This is done under the hood by calling active?. If the connection is no longer active, then this method will reconnect to the database.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 149 149: def verify!(*ignored) 150: reconnect! unless active? 151: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 222 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
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 201 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