class ActiveRecord::ConnectionAdapters::NullDBAdapter

Constants

TableDefinition

Public Class Methods

insinuate_into_spec(config) click to toggle source

A convenience method for integratinginto RSpec. See README for example of use.

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 5
def self.insinuate_into_spec(config)
  config.before :all do
    ActiveRecord::Base.establish_connection(:adapter => :nulldb)
  end

  config.after :all do
    ActiveRecord::Base.establish_connection(:test)
  end
end
new(config={}) click to toggle source

Recognized options:

:schema

path to the schema file, relative to Rails.root

Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 18
def initialize(config={})
  @log            = StringIO.new
  @logger         = Logger.new(@log)
  @last_unique_id = 0
  @tables         = {'schema_info' => new_table_definition(nil)}
  @indexes        = Hash.new { |hash, key| hash[key] = [] }
  @schema_path    = config.fetch(:schema){ "db/schema.rb" }
  @config         = config.merge(:adapter => :nulldb)
  super(nil, @logger)
  @visitor = Arel::Visitors::ToSql.new self if defined?(Arel::Visitors::ToSql)
end

Public Instance Methods

adapter_name() click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 49
def adapter_name
  "NullDB"
end
add_fk_constraint(*args) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 107
def add_fk_constraint(*args)
  # NOOP
end
add_index(table_name, column_names, options = {}) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 69
def add_index(table_name, column_names, options = {})
  column_names = Array.wrap(column_names).map(&:to_s)
  index_name, index_type, ignore = add_index_options(table_name, column_names, options)
  @indexes[table_name] << IndexDefinition.new(table_name, index_name, (index_type == 'UNIQUE'), column_names, [], [])
end
add_index_options(table_name, column_name, options = {}) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 76
def add_index_options(table_name, column_name, options = {})
  column_names = Array.wrap(column_name)
  index_name   = index_name(table_name, :column => column_names)

  if Hash === options # legacy support, since this param was a string
    index_type = options[:unique] ? "UNIQUE" : ""
    index_name = options[:name].to_s if options.key?(:name)
  else
    index_type = options
  end

  if index_name.length > index_name_length
    raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' is too long; the limit is #{index_name_length} characters"
  end
  if index_name_exists?(table_name, index_name, false)
    raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' already exists"
  end
  index_columns = quoted_columns_for_index(column_names, options).join(", ")

  [index_name, index_type, index_columns]
end
add_pk_constraint(*args) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 111
def add_pk_constraint(*args)
  # NOOP
end
checkpoint!() click to toggle source

Inserts a checkpoint in the log. See also execution_log_since_checkpoint.

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 45
def checkpoint!
  self.execution_log << Checkpoint.new
end
columns(table_name, name = nil) click to toggle source

Retrieve table columns as defined by the schema

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 121
def columns(table_name, name = nil)
  if @tables.size <= 1
    ActiveRecord::Migration.verbose = false
    schema_path = if Pathname(@schema_path).absolute?
                    @schema_path
                  else
                    File.join(NullDB.configuration.project_root, @schema_path)
                  end
    Kernel.load(schema_path)
  end

  takes_cast_type = defined?(ActiveRecord::Type::Value)

  if table = @tables[table_name]
    table.columns.map do |col_def|
      if takes_cast_type
        ActiveRecord::ConnectionAdapters::NullDBAdapter::Column.new(
          col_def.name.to_s,
          col_def.default,
          lookup_cast_type(col_def.type),
          col_def.type,
          col_def.null
        )
      else
        ActiveRecord::ConnectionAdapters::NullDBAdapter::Column.new(
          col_def.name.to_s,
          col_def.default,
          col_def.type,
          col_def.null
        )
      end
    end
  else
    []
  end
end
create(statement, name = nil, primary_key = nil, object_id = nil, sequence_name = nil, binds = [])
Alias for: insert
create_table(table_name, options = {}) { |table_definition| ... } click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 57
def create_table(table_name, options = {})
  table_definition = new_table_definition(self, table_name, options.delete(:temporary), options)

  unless options[:id] == false
    table_definition.primary_key(options[:primary_key] || "id")
  end

  yield table_definition if block_given?

  @tables[table_name] = table_definition
end
delete(statement, name=nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 194
def delete(statement, name=nil, binds = [])
  with_entry_point(:delete) do
    super(statement, name).length
  end
end
exec_query(statement, name = 'SQL', binds = []) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 168
def exec_query(statement, name = 'SQL', binds = [])
  self.execution_log << Statement.new(entry_point, statement)
  EmptyResult.new
end
execute(statement, name = nil) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 163
def execute(statement, name = nil)
  self.execution_log << Statement.new(entry_point, statement)
  NullObject.new
end
execution_log() click to toggle source

A log of every statement that has been “executed” by this connection adapter instance.

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 32
def execution_log
  (@execution_log ||= [])
end
execution_log_since_checkpoint() click to toggle source

A log of every statement that has been “executed” since the last time checkpoint! was called, or since the connection was created.

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 38
def execution_log_since_checkpoint
  checkpoint_index = @execution_log.rindex(Checkpoint.new)
  checkpoint_index = checkpoint_index ? checkpoint_index + 1 : 0
  @execution_log[(checkpoint_index..-1)]
end
index_name_exists?(table_name, index_name, default) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 100
def index_name_exists?(table_name, index_name, default)
  return default unless respond_to?(:indexes)
  index_name = index_name.to_s
  indexes(table_name).detect { |i| i.name == index_name }
end
indexes(table_name, name = nil) click to toggle source

Retrieve table indexes as defined by the schema

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 159
def indexes(table_name, name = nil)
  @indexes[table_name]
end
insert(statement, name = nil, primary_key = nil, object_id = nil, sequence_name = nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 179
def insert(statement, name = nil, primary_key = nil, object_id = nil, sequence_name = nil, binds = [])
  (object_id || next_unique_id).tap do
    with_entry_point(:insert) do
      super(statement, name, primary_key, object_id, sequence_name)
    end
  end
end
Also aliased as: create
primary_key(table_name) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 224
def primary_key(table_name)
  columns(table_name).detect { |col| col.sql_type == :primary_key }.try(:name)
end
select_all(statement, name=nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 200
def select_all(statement, name=nil, binds = [])
  with_entry_point(:select_all) do
    super(statement, name)
  end
end
select_one(statement, name=nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 206
def select_one(statement, name=nil, binds = [])
  with_entry_point(:select_one) do
    super(statement, name)
  end
end
select_rows(statement, name = nil, binds = []) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 173
def select_rows(statement, name = nil, binds = [])
  [].tap do
    self.execution_log << Statement.new(entry_point, statement)
  end
end
select_value(statement, name=nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 212
def select_value(statement, name=nil, binds = [])
  with_entry_point(:select_value) do
    super(statement, name)
  end
end
select_values(statement, name=nil) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 218
def select_values(statement, name=nil)
  with_entry_point(:select_values) do
    super(statement, name)
  end
end
supports_migrations?() click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 53
def supports_migrations?
  true
end
tables() click to toggle source

Retrieve the table names defined by the schema

# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 116
def tables
  @tables.keys.map(&:to_s)
end
update(statement, name=nil, binds = []) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 188
def update(statement, name=nil, binds = [])
  with_entry_point(:update) do
    super(statement, name)
  end
end

Protected Instance Methods

select(statement, name = nil, binds = []) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 230
def select(statement, name = nil, binds = [])
  EmptyResult.new.tap do |r|
    r.columns = columns_for(name)
    self.execution_log << Statement.new(entry_point, statement)
  end
end

Private Instance Methods

columns_for(table_name) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 239
def columns_for(table_name)
  table_def = @tables[table_name]
  table_def ? table_def.columns : []
end
entry_point() click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 258
def entry_point
  Thread.current[:entry_point]
end
new_table_definition(adapter = nil, table_name = nil, is_temporary = nil, options = {}) click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 272
def new_table_definition(adapter = nil, table_name = nil, is_temporary = nil, options = {})
  case ::ActiveRecord::VERSION::MAJOR
  when 4
    TableDefinition.new(native_database_types, table_name, is_temporary, options)
  when 2,3
    TableDefinition.new(adapter)
  else
    raise "Unsupported ActiveRecord version #{::ActiveRecord::VERSION::STRING}"
  end
end
next_unique_id() click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 244
def next_unique_id
  @last_unique_id += 1
end
with_entry_point(method) { || ... } click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 248
def with_entry_point(method)
  if entry_point.nil?
    with_thread_local_variable(:entry_point, method) do
      yield
    end
  else
    yield
  end
end
with_thread_local_variable(name, value) { || ... } click to toggle source
# File lib/active_record/connection_adapters/nulldb_adapter/core.rb, line 262
def with_thread_local_variable(name, value)
  old_value = Thread.current[name]
  Thread.current[name] = value
  begin
    yield
  ensure
    Thread.current[name] = old_value
  end
end