Class | ActiveRecord::ConnectionAdapters::AbstractAdapter |
In: |
vendor/rails/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 vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 66 66: def active? 67: @active != false 68: end
Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 33 33: def adapter_name 34: 'Abstract' 35: end
Close this connection
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 76 76: def disconnect! 77: @active = false 78: 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 vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 53 53: def prefetch_primary_key?(table_name = nil) 54: false 55: 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 vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 93 93: def raw_connection 94: @connection 95: end
Close this connection and open a new one in its place.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 71 71: def reconnect! 72: @active = true 73: end
Does this adapter support using DISTINCT within COUNT? This is true for all adapters except sqlite.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 45 45: def supports_count_distinct? 46: true 47: end
Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 39 39: def supports_migrations? 40: false 41: end
Lazily verify this connection, calling +active?+ only if it hasn‘t been called for timeout seconds.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 82 82: def verify!(timeout) 83: now = Time.now.to_i 84: if (now - @last_verification) > timeout 85: reconnect! unless active? 86: @last_verification = now 87: end 88: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 134 134: def format_log_entry(message, dump = nil) 135: if ActiveRecord::Base.colorize_logging 136: if @@row_even 137: @@row_even = false 138: message_color, dump_color = "4;36;1", "0;1" 139: else 140: @@row_even = true 141: message_color, dump_color = "4;35;1", "0" 142: end 143: 144: log_entry = " \e[#{message_color}m#{message}\e[0m " 145: log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump 146: log_entry 147: else 148: "%s %s" % [message, dump] 149: end 150: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 98 98: def log(sql, name) 99: if block_given? 100: if @logger and @logger.level <= Logger::INFO 101: result = nil 102: seconds = Benchmark.realtime { result = yield } 103: @runtime += seconds 104: log_info(sql, name, seconds) 105: result 106: else 107: yield 108: end 109: else 110: log_info(sql, name, 0) 111: nil 112: end 113: rescue Exception => e 114: # Log message and raise exception. 115: # Set last_verfication to 0, so that connection gets verified 116: # upon reentering the request loop 117: @last_verification = 0 118: message = "#{e.class.name}: #{e.message}: #{sql}" 119: log_info(message, name, 0) 120: raise ActiveRecord::StatementInvalid, message 121: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 123 123: def log_info(sql, name, runtime) 124: return unless @logger 125: 126: @logger.debug( 127: format_log_entry( 128: "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", 129: sql.gsub(/ +/, " ") 130: ) 131: ) 132: end