Class: ActiveRecord::ConnectionAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Includes:
DatabaseStatements, Quoting, SchemaStatements
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.

Constant Summary collapse

@@row_even =
true

Instance Method Summary collapse

Methods included from Quoting

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

Methods included from DatabaseStatements

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

Methods included from SchemaStatements

#add_column, #add_column_options!, #add_index, #change_column, #change_column_default, #columns, #create_table, #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:



25
26
27
28
29
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 25

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)


66
67
68
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 66

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.



33
34
35
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 33

def adapter_name
  'Abstract'
end

#disconnect!Object

Close this connection



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

def disconnect!
  @active = false
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)


53
54
55
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 53

def prefetch_primary_key?(table_name = nil)
  false
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



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

def raw_connection
  @connection
end

#reconnect!Object

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



71
72
73
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 71

def reconnect!
  @active = true
end

#reset_runtimeObject

:nodoc:



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

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)


45
46
47
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 45

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)


39
40
41
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 39

def supports_migrations?
  false
end

#verify!(timeout) ⇒ Object

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



82
83
84
85
86
87
88
# File 'lib/active_record/connection_adapters/abstract_adapter.rb', line 82

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