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 83 83: def active? 84: @active != false 85: 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 37 37: def adapter_name 38: 'Abstract' 39: end
Override to turn off referential integrity while executing +&block+
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 76 76: def disable_referential_integrity(&block) 77: yield 78: end
Close this connection
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 93 93: def disconnect! 94: @active = false 95: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 120 120: def log_info(sql, name, runtime) 121: if @logger && @logger.debug? 122: name = "#{name.nil? ? "SQL" : name} (#{sprintf("%f", runtime)})" 123: @logger.debug format_log_entry(name, sql.squeeze(' ')) 124: end 125: 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 57 57: def prefetch_primary_key?(table_name = nil) 58: false 59: end
Override to return the quoted table name if the database needs it
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 69 69: def quote_table_name(name) 70: name 71: 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 116 116: def raw_connection 117: @connection 118: 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 88 88: def reconnect! 89: @active = true 90: 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 vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 99 99: def requires_reloading? 100: false 101: 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 49 49: def supports_count_distinct? 50: true 51: 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 43 43: def supports_migrations? 44: false 45: 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 105 105: def verify!(timeout) 106: now = Time.now.to_i 107: if (now - @last_verification) > timeout 108: reconnect! unless active? 109: @last_verification = now 110: end 111: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 153 153: def format_log_entry(message, dump = nil) 154: if ActiveRecord::Base.colorize_logging 155: if @@row_even 156: @@row_even = false 157: message_color, dump_color = "4;36;1", "0;1" 158: else 159: @@row_even = true 160: message_color, dump_color = "4;35;1", "0" 161: end 162: 163: log_entry = " \e[#{message_color}m#{message}\e[0m " 164: log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump 165: log_entry 166: else 167: "%s %s" % [message, dump] 168: end 169: end
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb, line 128 128: def log(sql, name) 129: if block_given? 130: if @logger and @logger.debug? 131: result = nil 132: seconds = Benchmark.realtime { result = yield } 133: @runtime += seconds 134: log_info(sql, name, seconds) 135: result 136: else 137: yield 138: end 139: else 140: log_info(sql, name, 0) 141: nil 142: end 143: rescue Exception => e 144: # Log message and raise exception. 145: # Set last_verification to 0, so that connection gets verified 146: # upon reentering the request loop 147: @last_verification = 0 148: message = "#{e.class.name}: #{e.message}: #{sql}" 149: log_info(message, name, 0) 150: raise ActiveRecord::StatementInvalid, message 151: end