Class: ActiveRecord::ConnectionAdapters::NullDBAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/active_record/connection_adapters/nulldb_adapter/table_definition.rb,
lib/active_record/connection_adapters/nulldb_adapter/index_definition.rb,
lib/active_record/connection_adapters/nulldb_adapter/configuration.rb,
lib/active_record/connection_adapters/nulldb_adapter/empty_result.rb,
lib/active_record/connection_adapters/nulldb_adapter/null_object.rb,
lib/active_record/connection_adapters/nulldb_adapter/checkpoint.rb,
lib/active_record/connection_adapters/nulldb_adapter/statement.rb,
lib/active_record/connection_adapters/nulldb_adapter/column.rb,
lib/active_record/connection_adapters/nulldb_adapter/core.rb

Defined Under Namespace

Classes: Checkpoint, Column, Configuration, EmptyResult, IndexDefinition, NullObject, Statement

Constant Summary collapse

TableDefinition =
ActiveRecord::ConnectionAdapters::TableDefinition

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ NullDBAdapter

Recognized options:

:schema

path to the schema file, relative to Rails.root



18
19
20
21
22
23
24
25
26
27
28
# 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

Class Method Details

.insinuate_into_spec(config) ⇒ Object

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



5
6
7
8
9
10
11
12
13
# 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

Instance Method Details

#adapter_nameObject



49
50
51
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 49

def adapter_name
  "NullDB"
end

#add_fk_constraint(*args) ⇒ Object



107
108
109
# 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 = {}) ⇒ Object



69
70
71
72
73
# 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 = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



111
112
113
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 111

def add_pk_constraint(*args)
  # NOOP
end

#checkpoint!Object

Inserts a checkpoint in the log. See also #execution_log_since_checkpoint.



45
46
47
# 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) ⇒ Object

Retrieve table columns as defined by the schema



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 125

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

  if table = @tables[table_name]
    table.columns.map do |col_def|
      ActiveRecord::ConnectionAdapters::NullDBAdapter::Column.new(*new_column_arguments(col_def))
    end
  else
    []
  end
end

#create_table(table_name, options = {}) {|table_definition| ... } ⇒ Object

Yields:

  • (table_definition)


57
58
59
60
61
62
63
64
65
66
67
# 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 = []) ⇒ Object



181
182
183
184
185
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 181

def delete(statement, name=nil, binds = [])
  with_entry_point(:delete) do
    super(statement, name).size
  end
end

#enable_extensionObject



115
116
117
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 115

def enable_extension(*)
  # NOOP
end

#exec_query(statement, name = 'SQL', binds = []) ⇒ Object



155
156
157
158
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 155

def exec_query(statement, name = 'SQL', binds = [])
  self.execution_log << Statement.new(entry_point, statement)
  EmptyResult.new
end

#execute(statement, name = nil) ⇒ Object



150
151
152
153
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 150

def execute(statement, name = nil)
  self.execution_log << Statement.new(entry_point, statement)
  NullObject.new
end

#execution_logObject

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



32
33
34
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 32

def execution_log
  (@execution_log ||= [])
end

#execution_log_since_checkpointObject

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



38
39
40
41
42
# 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) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
# 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) ⇒ Object

Retrieve table indexes as defined by the schema



146
147
148
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 146

def indexes(table_name, name = nil)
  @indexes[table_name]
end

#insert(statement, name = nil, primary_key = nil, object_id = nil, sequence_name = nil, binds = []) ⇒ Object Also known as: create



166
167
168
169
170
171
172
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 166

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

#primary_key(table_name) ⇒ Object



211
212
213
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 211

def primary_key(table_name)
  columns(table_name).detect { |col| col.sql_type == :primary_key }.try(:name)
end

#select_all(statement, name = nil, binds = []) ⇒ Object



187
188
189
190
191
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 187

def select_all(statement, name=nil, binds = [])
  with_entry_point(:select_all) do
    super(statement, name)
  end
end

#select_one(statement, name = nil, binds = []) ⇒ Object



193
194
195
196
197
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 193

def select_one(statement, name=nil, binds = [])
  with_entry_point(:select_one) do
    super(statement, name)
  end
end

#select_rows(statement, name = nil, binds = []) ⇒ Object



160
161
162
163
164
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 160

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 = []) ⇒ Object



199
200
201
202
203
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 199

def select_value(statement, name=nil, binds = [])
  with_entry_point(:select_value) do
    super(statement, name)
  end
end

#select_values(statement, name = nil) ⇒ Object



205
206
207
208
209
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 205

def select_values(statement, name=nil)
  with_entry_point(:select_values) do
    super(statement, name)
  end
end

#supports_migrations?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 53

def supports_migrations?
  true
end

#tablesObject

Retrieve the table names defined by the schema



120
121
122
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 120

def tables
  @tables.keys.map(&:to_s)
end

#update(statement, name = nil, binds = []) ⇒ Object



175
176
177
178
179
# File 'lib/active_record/connection_adapters/nulldb_adapter/core.rb', line 175

def update(statement, name=nil, binds = [])
  with_entry_point(:update) do
    super(statement, name)
  end
end