Class: ActiveRecord::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Includes:
DatabaseStatements, Quoting, SchemaStatements, QueryCache
Defined in:
lib/active_record/connection_adapters/abstract_adapter.rb

Overview

All the concrete database adapters follow the interface laid down in this class. You can use this interface directly by borrowing the database connection from the Base with Base.connection.

Most of the methods in the adapter are useful during migrations. Most notably, SchemaStatements#create_table, SchemaStatements#drop_table, SchemaStatements#add_index, SchemaStatements#remove_index, SchemaStatements#add_column, SchemaStatements#change_column and SchemaStatements#remove_column are very useful.

Direct Known Subclasses

MysqlAdapter, PostgreSQLAdapter, SQLiteAdapter

Constant Summary collapse

@@row_even =
true

Instance Method Summary collapse

Methods included from QueryCache

#cache, #uncached

Methods included from Quoting

#quote, #quote_column_name, #quote_string, #quoted_date, #quoted_false, #quoted_string_prefix, #quoted_true

Methods included from DatabaseStatements

#add_limit!, #add_limit_offset!, #add_lock!, #begin_db_transaction, #commit_db_transaction, #default_sequence_name, #delete, #empty_insert_statement, #execute, #insert, #insert_fixture, #reset_sequence!, #rollback_db_transaction, #select_all, #select_one, #select_rows, #select_value, #select_values, #transaction, #update

Methods included from SchemaStatements

#add_column, #add_column_options!, #add_index, #add_order_by_for_association_limiting!, #change_column, #change_column_default, #columns, #create_table, #distinct, #drop_table, #dump_schema_information, #index_name, #initialize_schema_information, #native_database_types, #remove_column, #remove_index, #rename_column, #rename_table, #structure_dump, #table_alias_for, #table_alias_length, #type_to_sql

Constructor Details

#initialize(connection, logger = nil) ⇒ AbstractAdapter

:nodoc:



29
30
31
32
33
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 29

def initialize(connection, logger = nil) #:nodoc:
  @connection, @logger = connection, logger
  @runtime = 0
  @last_verification = 0
end

Instance Method Details

#active?Boolean

Is this connection active and ready to perform queries?

Returns:

  • (Boolean)


83
84
85
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 83

def active?
  @active != false
end

#adapter_nameObject

Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.



37
38
39
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 37

def adapter_name
  'Abstract'
end

#disable_referential_integrity(&block) ⇒ Object

Override to turn off referential integrity while executing &block



76
77
78
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 76

def disable_referential_integrity(&block)
  yield
end

#disconnect!Object

Close this connection



93
94
95
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 93

def disconnect!
  @active = false
end

#log_info(sql, name, runtime) ⇒ Object



120
121
122
123
124
125
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 120

def log_info(sql, name, runtime)
  if @logger && @logger.debug?
    name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})"
    @logger.debug format_log_entry(name, sql.squeeze(' '))
  end
end

#prefetch_primary_key?(table_name = nil) ⇒ Boolean

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.

Returns:

  • (Boolean)


57
58
59
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 57

def prefetch_primary_key?(table_name = nil)
  false
end

#quote_table_name(name) ⇒ Object

Override to return the quoted table name if the database needs it



69
70
71
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 69

def quote_table_name(name)
  name
end

#raw_connectionObject

Provides access to the underlying database connection. Useful for when you need to call a proprietary method such as postgresql’s lo_* methods



116
117
118
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 116

def raw_connection
  @connection
end

#reconnect!Object

Close this connection and open a new one in its place.



88
89
90
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 88

def reconnect!
  @active = true
end

#requires_reloading?Boolean

Returns true if its safe to reload the connection between requests for development mode. This is not the case for Ruby/MySQL and it’s not necessary for any adapters except SQLite.

Returns:

  • (Boolean)


99
100
101
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 99

def requires_reloading?
  false
end

#reset_runtimeObject

:nodoc:



61
62
63
64
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 61

def reset_runtime #:nodoc:
  rt, @runtime = @runtime, 0
  rt
end

#supports_count_distinct?Boolean

Does this adapter support using DISTINCT within COUNT? This is true for all adapters except sqlite.

Returns:

  • (Boolean)


49
50
51
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 49

def supports_count_distinct?
  true
end

#supports_migrations?Boolean

Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.

Returns:

  • (Boolean)


43
44
45
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 43

def supports_migrations?
  false
end

#verify!(timeout) ⇒ Object

Lazily verify this connection, calling active? only if it hasn’t been called for timeout seconds.



105
106
107
108
109
110
111
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 105

def verify!(timeout)
  now = Time.now.to_i
  if (now - @last_verification) > timeout
    reconnect! unless active?
    @last_verification = now
  end
end