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.
- active?
- adapter_name
- disconnect!
- format_log_entry
- log
- log_info
- prefetch_primary_key?
- raw_connection
- reconnect!
- requires_reloading?
- supports_count_distinct?
- supports_migrations?
- verify!
Is this connection active and ready to perform queries?
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 68 68: def active? 69: @active != false 70: end
Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 35 35: def adapter_name 36: 'Abstract' 37: end
Close this connection
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 78 78: def disconnect! 79: @active = false 80: 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.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 55 55: def prefetch_primary_key?(table_name = nil) 56: false 57: end
Provides access to the underlying database connection. Useful for when you need to call a proprietary method such as postgresql‘s lo_* methods
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 101 101: def raw_connection 102: @connection 103: end
Close this connection and open a new one in its place.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 73 73: def reconnect! 74: @active = true 75: 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.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 84 84: def requires_reloading? 85: false 86: end
Does this adapter support using DISTINCT within COUNT? This is true for all adapters except sqlite.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 47 47: def supports_count_distinct? 48: true 49: end
Does this adapter support migrations? Backend specific, as the abstract adapter always returns false.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 41 41: def supports_migrations? 42: false 43: end
Lazily verify this connection, calling +active?+ only if it hasn‘t been called for timeout seconds.
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 90 90: def verify!(timeout) 91: now = Time.now.to_i 92: if (now - @last_verification) > timeout 93: reconnect! unless active? 94: @last_verification = now 95: end 96: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 142 142: def format_log_entry(message, dump = nil) 143: if ActiveRecord::Base.colorize_logging 144: if @@row_even 145: @@row_even = false 146: message_color, dump_color = "4;36;1", "0;1" 147: else 148: @@row_even = true 149: message_color, dump_color = "4;35;1", "0" 150: end 151: 152: log_entry = " \e[#{message_color}m#{message}\e[0m " 153: log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump 154: log_entry 155: else 156: "%s %s" % [message, dump] 157: end 158: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 106 106: def log(sql, name) 107: if block_given? 108: if @logger and @logger.level <= Logger::INFO 109: result = nil 110: seconds = Benchmark.realtime { result = yield } 111: @runtime += seconds 112: log_info(sql, name, seconds) 113: result 114: else 115: yield 116: end 117: else 118: log_info(sql, name, 0) 119: nil 120: end 121: rescue Exception => e 122: # Log message and raise exception. 123: # Set last_verfication to 0, so that connection gets verified 124: # upon reentering the request loop 125: @last_verification = 0 126: message = "#{e.class.name}: #{e.message}: #{sql}" 127: log_info(message, name, 0) 128: raise ActiveRecord::StatementInvalid, message 129: end
[ show source ]
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 131 131: def log_info(sql, name, runtime) 132: return unless @logger 133: 134: @logger.debug( 135: format_log_entry( 136: "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})", 137: sql.gsub(/ +/, " ") 138: ) 139: ) 140: end