| Class | ActiveRecord::ConnectionAdapters::AbstractAdapter |
| In: |
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
|
| Parent: | Object |
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.
Is this connection active and ready to perform queries?
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 70 def active? @active != false end
Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 37 def adapter_name 'Abstract' end
Close this connection
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 80 def disconnect! @active = false end
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 107 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
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 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 57 def prefetch_primary_key?(table_name = nil) false end
Provides access to the underlying database connection. Useful for when you need to call a proprietary method such as postgresql’s lo_* methods
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 103 def raw_connection @connection end
Close this connection and open a new one in its place.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 75 def reconnect! @active = true end
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.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 86 def requires_reloading? false end
Does this adapter support using DISTINCT within COUNT? This is true for all adapters except sqlite.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 49 def supports_count_distinct? true end
Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 43 def supports_migrations? false end
Lazily verify this connection, calling +active?+ only if it hasn’t been called for timeout seconds.
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 92 def verify!(timeout) now = Time.now.to_i if (now - @last_verification) > timeout reconnect! unless active? @last_verification = now end end
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 140 def format_log_entry(message, dump = nil) if ActiveRecord::Base.colorize_logging if @@row_even @@row_even = false message_color, dump_color = "4;36;1", "0;1" else @@row_even = true message_color, dump_color = "4;35;1", "0" end log_entry = " \e[#{message_color}m#{message}\e[0m " log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump log_entry else "%s %s" % [message, dump] end end
# File activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 115 def log(sql, name) if block_given? if @logger and @logger.level <= Logger::INFO result = nil seconds = Benchmark.realtime { result = yield } @runtime += seconds log_info(sql, name, seconds) result else yield end else log_info(sql, name, 0) nil end rescue Exception => e # Log message and raise exception. # Set last_verification to 0, so that connection gets verified # upon reentering the request loop @last_verification = 0 message = "#{e.class.name}: #{e.message}: #{sql}" log_info(message, name, 0) raise ActiveRecord::StatementInvalid, message end