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, #sanitize_limit, #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!, #add_timestamps, #assume_migrated_upto_version, #change_column, #change_column_default, #change_table, #columns, #create_table, #distinct, #drop_table, #dump_schema_information, #index_name, #initialize_schema_migrations_table, #native_database_types, #remove_column, #remove_index, #remove_timestamps, #rename_column, #rename_table, #structure_dump, #table_alias_for, #table_alias_length, #table_exists?, #type_to_sql

Constructor Details

#initialize(connection, logger = nil) ⇒ AbstractAdapter

:nodoc:



29
30
31
32
33
34
# 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
  @query_cache_enabled = false
end

Instance Method Details

#active?Boolean

Is this connection active and ready to perform queries?

Returns:

  • (Boolean)


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

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.



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

def adapter_name
  'Abstract'
end

#disable_referential_integrity(&block) ⇒ Object

Override to turn off referential integrity while executing &block.



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

def disable_referential_integrity(&block)
  yield
end

#disconnect!Object

Close this connection



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

def disconnect!
  @active = false
end

#log_info(sql, name, runtime) ⇒ Object



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

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)


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

def prefetch_primary_key?(table_name = nil)
  false
end

#quote_table_name(name) ⇒ Object

Override to return the quoted table name. Defaults to column quoting.



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

def quote_table_name(name)
  quote_column_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



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

def raw_connection
  @connection
end

#reconnect!Object

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



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

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)


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

def requires_reloading?
  false
end

#reset_runtimeObject

:nodoc:



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

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)


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

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)


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

def supports_migrations?
  false
end

#verify!(timeout) ⇒ Object

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



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

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