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 101 101: def active? 102: @active != false 103: 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 46 46: def adapter_name 47: 'Abstract' 48: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 158 158: def decrement_open_transactions 159: @open_transactions -= 1 160: end
Override to turn off referential integrity while executing &block.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 92 92: def disable_referential_integrity(&block) 93: yield 94: 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 113 113: def disconnect! 114: @active = false 115: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 153 153: def increment_open_transactions 154: @open_transactions ||= 0 155: @open_transactions += 1 156: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 162 162: def log_info(sql, name, seconds) 163: if @logger && @logger.debug? 164: name = "#{name.nil? ? "SQL" : name} (#{sprintf("%.1f", seconds * 1000)}ms)" 165: @logger.debug(format_log_entry(name, sql.squeeze(' '))) 166: end 167: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 149 149: def open_transactions 150: @open_transactions ||= 0 151: 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 73 73: def prefetch_primary_key?(table_name = nil) 74: false 75: 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 85 85: def quote_table_name(name) 86: quote_column_name(name) 87: 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 145 145: def raw_connection 146: @connection 147: 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 107 107: def reconnect! 108: @active = true 109: 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 128 128: def requires_reloading? 129: true 130: 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 123 123: def reset! 124: # this should be overridden by concrete adapters 125: 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 58 58: def supports_count_distinct? 59: true 60: 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 65 65: def supports_ddl_transactions? 66: false 67: 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 52 52: def supports_migrations? 53: false 54: 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 135 135: def verify!(*ignored) 136: reconnect! unless active? 137: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 191 191: def format_log_entry(message, dump = nil) 192: if ActiveRecord::Base.colorize_logging 193: if @@row_even 194: @@row_even = false 195: message_color, dump_color = "4;36;1", "0;1" 196: else 197: @@row_even = true 198: message_color, dump_color = "4;35;1", "0" 199: end 200: 201: log_entry = " \e[#{message_color}m#{message}\e[0m " 202: log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump 203: log_entry 204: else 205: "%s %s" % [message, dump] 206: end 207: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 170 170: def log(sql, name) 171: if block_given? 172: result = nil 173: seconds = Benchmark.realtime { result = yield } 174: @runtime += seconds 175: log_info(sql, name, seconds) 176: result 177: else 178: log_info(sql, name, 0) 179: nil 180: end 181: rescue Exception => e 182: # Log message and raise exception. 183: # Set last_verification to 0, so that connection gets verified 184: # upon reentering the request loop 185: @last_verification = 0 186: message = "#{e.class.name}: #{e.message}: #{sql}" 187: log_info(message, name, 0) 188: raise ActiveRecord::StatementInvalid, message 189: end