Active Record 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 SchemaStatements
are very useful.
- A
- C
- D
- E
- I
- L
- P
- R
- S
-
- schema_cache,
- schema_cache=,
- schema_version,
- shard,
- supports_advisory_locks?,
- supports_bulk_alter?,
- supports_check_constraints?,
- supports_comments?,
- supports_comments_in_create?,
- supports_common_table_expressions?,
- supports_concurrent_connections?,
- supports_datetime_with_precision?,
- supports_ddl_transactions?,
- supports_deferrable_constraints?,
- supports_explain?,
- supports_expression_index?,
- supports_extensions?,
- supports_foreign_keys?,
- supports_foreign_tables?,
- supports_index_sort_order?,
- supports_indexes_in_create?,
- supports_insert_conflict_target?,
- supports_insert_on_duplicate_skip?,
- supports_insert_on_duplicate_update?,
- supports_insert_returning?,
- supports_json?,
- supports_lazy_transactions?,
- supports_materialized_views?,
- supports_optimizer_hints?,
- supports_partial_index?,
- supports_partitioned_indexes?,
- supports_savepoints?,
- supports_transaction_isolation?,
- supports_validate_constraints?,
- supports_views?,
- supports_virtual_columns?
- T
- U
- V
- ActiveSupport::Callbacks
- ActiveRecord::ConnectionAdapters::DatabaseLimits
- ActiveRecord::ConnectionAdapters::QueryCache
- ActiveRecord::ConnectionAdapters::Savepoints
Constants
ADAPTER_NAME | = | "Abstract" |
COMMENT_REGEX | = | %r{(?:--.*\n)|/\*(?:[^*]|\*[^/])*\*/}m |
SIMPLE_INT | = | /\A\d+\z/ |
TYPE_MAP | = | Type::TypeMap.new.tap { |m| initialize_type_map(m) } |
Attributes
[R] | in_use? | |
[R] | lock | |
[R] | logger | |
[R] | owner | |
[RW] | pool | |
[R] | visitor |
Class Public methods
database_exists?(config) Link
Does the database for this adapter exist?
type_cast_config_to_boolean(config) Link
type_cast_config_to_integer(config) Link
Instance Public methods
active?() Link
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.
adapter_name() Link
Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.
all_foreign_keys_valid?() Link
Override to check all foreign key constraints in a database.
clear_cache!() Link
Clear any caching the database adapter may be doing.
close() Link
Check the connection back in to the connection pool
disable_extension(name) Link
This is meant to be implemented by the adapters that support extensions
disable_referential_integrity() Link
Override to turn off referential integrity while executing &block
.
discard!() Link
Immediately forget this connection ever existed. Unlike disconnect!, this will not communicate with the server.
After calling this method, the behavior of all other methods becomes undefined. This is called internally just before a forked process gets rid of a connection that belonged to its parent.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 547 def discard! # This should be overridden by concrete adapters. # # Prevent @connection's finalizer from touching the socket, or # otherwise communicating with its server, when it is collected. if schema_cache.connection == self schema_cache.connection = nil end end
disconnect!() Link
Disconnects from the database if already connected. Otherwise, this method does nothing.
enable_extension(name) Link
This is meant to be implemented by the adapters that support extensions
expire() Link
this method must only be called while holding connection pool's mutex
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 249 def expire if in_use? if @owner != Thread.current raise ActiveRecordError, "Cannot expire connection, " \ "it is owned by a different thread: #{@owner}. " \ "Current thread: #{Thread.current}." end @idle_since = Process.clock_gettime(Process::CLOCK_MONOTONIC) @owner = nil else raise ActiveRecordError, "Cannot expire connection, it is not currently leased." end end
extensions() Link
A list of extensions, to be filled in by adapters that support them.
index_algorithms() Link
A list of index algorithms, to be filled by adapters that support them.
lease() Link
this method must only be called while holding connection pool's mutex
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 208 def lease if in_use? msg = +"Cannot lease connection, " if @owner == Thread.current msg << "it is already leased by the current thread." else msg << "it is already in use by a different thread: #{@owner}. " \ "Current thread: #{Thread.current}." end raise ActiveRecordError, msg end @owner = Thread.current end
prefetch_primary_key?(table_name = nil) Link
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.
preventing_writes?() Link
Determines whether writes are currently being prevented.
Returns true if the connection is a replica.
If the application is using legacy handling, returns true if connection_handler.prevent_writes
is set.
If the application is using the new connection handling will return true based on current_preventing_writes
.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 141 def preventing_writes? return true if replica? return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord.legacy_connection_handling return false if connection_class.nil? connection_class.current_preventing_writes end
raw_connection() Link
Provides access to the underlying database driver for this adapter. For example, this method returns a Mysql2::Client object in case of Mysql2Adapter
, and a PG::Connection object in case of PostgreSQLAdapter
.
This is useful for when you need to call a proprietary method such as PostgreSQL's lo_* methods.
reconnect!() Link
Disconnects from the database if already connected, and establishes a new connection with the database. Implementors should call super if they override the default implementation.
replica?() Link
requires_reloading?() Link
Returns true if its required to reload the connection between requests for development mode.
reset!() Link
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.
role() Link
The role (e.g. :writing
) for the current connection. In a non-multi role application, :writing
is returned.
schema_cache() Link
schema_cache=(cache) Link
schema_version() Link
Returns the version identifier of the schema currently available in the database. This is generally equal to the number of the highest- numbered migration that has been executed, or 0 if no schema information is present / the database is empty.
shard() Link
The shard (e.g. :default
) for the current connection. In a non-sharded application, :default
is returned.
supports_advisory_locks?() Link
Does this adapter support application-enforced advisory locking?
supports_bulk_alter?() Link
supports_check_constraints?() Link
Does this adapter support creating check constraints?
supports_comments?() Link
Does this adapter support metadata comments on database objects (tables, columns, indexes)?
supports_comments_in_create?() Link
Can comments for tables, columns, and indexes be specified in create/alter table statements?
supports_common_table_expressions?() Link
supports_concurrent_connections?() Link
supports_datetime_with_precision?() Link
Does this adapter support datetime with precision?
supports_ddl_transactions?() Link
Does this adapter support DDL rollbacks in transactions? That is, would CREATE TABLE or ALTER TABLE get rolled back by a transaction?
supports_deferrable_constraints?() Link
Does this adapter support creating deferrable constraints?
supports_explain?() Link
Does this adapter support explain?
supports_expression_index?() Link
Does this adapter support expression indices?
supports_extensions?() Link
Does this adapter support database extensions?
supports_foreign_keys?() Link
Does this adapter support creating foreign key constraints?
supports_foreign_tables?() Link
Does this adapter support foreign/external tables?
supports_index_sort_order?() Link
Does this adapter support index sort order?
supports_indexes_in_create?() Link
Does this adapter support creating indexes in the same statement as creating the table?
supports_insert_conflict_target?() Link
supports_insert_on_duplicate_skip?() Link
supports_insert_on_duplicate_update?() Link
supports_insert_returning?() Link
supports_json?() Link
Does this adapter support json data type?
supports_lazy_transactions?() Link
supports_materialized_views?() Link
Does this adapter support materialized views?
supports_optimizer_hints?() Link
Does this adapter support optimizer hints?
supports_partial_index?() Link
Does this adapter support partial indices?
supports_partitioned_indexes?() Link
supports_savepoints?() Link
Does this adapter support savepoints?
supports_transaction_isolation?() Link
Does this adapter support setting the isolation level for a transaction?
supports_validate_constraints?() Link
Does this adapter support creating invalid constraints?
supports_views?() Link
Does this adapter support views?
supports_virtual_columns?() Link
Does this adapter support virtual columns?
throw_away!() Link
Removes the connection from the pool and disconnect it.
unprepared_statement() Link
use_metadata_table?() Link
verify!() Link
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.
Instance Private methods
log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false, &block) Link
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 755 def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false, &block) # :doc: @instrumenter.instrument( "sql.active_record", sql: sql, name: name, binds: binds, type_casted_binds: type_casted_binds, statement_name: statement_name, async: async, connection: self) do @lock.synchronize(&block) rescue => e raise translate_exception_class(e, sql, binds) end end