class Sequel::Mock::Database

Database class for Sequel's mock adapter.

Constants

DatasetClass
SHARED_ADAPTERS

Map of database type names to module names, used for handling mock adapters for specific database types.

SHARED_ADAPTER_SETUP

Procs to run for specific database types to get the mock adapter to work with the shared adapter

Attributes

autoid[W]

Set the autogenerated primary key integer to be returned when running an insert query. Argument types supported:

nil

Return nil for all inserts

Integer

Starting integer for next insert, with futher inserts getting an incremented value

Array

First insert gets the first value in the array, second gets the second value, etc.

Proc

Called with the insert SQL query, uses the value returned

Class

Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.

columns[W]

Set the columns to set in the dataset when the dataset fetches rows. Argument types supported:

nil

Set no columns

Array of Symbols: Used for all datasets Array (otherwise): First retrieval gets the first value in the

array, second gets the second value, etc.
Proc

Called with the select SQL query, uses the value returned, which should be an array of symbols

fetch[W]

Set the hashes to yield by execute when retrieving rows. Argument types supported:

nil

Yield no rows

Hash

Always yield a single row with this hash

Array of Hashes

Yield separately for each hash in this array

Array (otherwise)

First retrieval gets the first value in the array, second gets the second value, etc.

Proc

Called with the select SQL query, uses the value returned, which should be a hash or array of hashes.

Class

Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.

numrows[W]

Set the number of rows to return from update or delete. Argument types supported:

nil

Return 0 for all updates and deletes

Integer

Used for all updates and deletes

Array

First update/delete gets the first value in the array, second gets the second value, etc.

Proc

Called with the update/delete SQL query, uses the value returned.

Class

Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.

server_version[RW]

Mock the server version, useful when using the shared adapters

Public Instance Methods

connect(server) click to toggle source

Return a related Connection option connecting to the given shard.

# File lib/sequel/adapters/mock.rb, line 148
def connect(server)
  Connection.new(self, server, server_opts(server))
end
disconnect_connection(c) click to toggle source
# File lib/sequel/adapters/mock.rb, line 152
def disconnect_connection(c)
end
execute(sql, opts=OPTS, &block) click to toggle source

Store the sql used for later retrieval with sqls, and return the appropriate value using either the autoid, fetch, or numrows methods.

# File lib/sequel/adapters/mock.rb, line 158
def execute(sql, opts=OPTS, &block)
  synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} 
end
Also aliased as: execute_ddl
execute_ddl(sql, opts=OPTS, &block)
Alias for: execute
execute_dui(sql, opts=OPTS) click to toggle source

Store the sql used, and return the value of the numrows method.

# File lib/sequel/adapters/mock.rb, line 164
def execute_dui(sql, opts=OPTS)
  execute(sql, opts.merge(:meth=>:numrows))
end
execute_insert(sql, opts=OPTS) click to toggle source

Store the sql used, and return the value of the autoid method.

# File lib/sequel/adapters/mock.rb, line 169
def execute_insert(sql, opts=OPTS)
  execute(sql, opts.merge(:meth=>:autoid))
end
sqls() click to toggle source

Return all stored SQL queries, and clear the cache of SQL queries.

# File lib/sequel/adapters/mock.rb, line 175
def sqls
  s = @sqls.dup
  @sqls.clear
  s
end
supports_savepoints?() click to toggle source

Enable use of savepoints.

Calls superclass method Sequel::Database#supports_savepoints?
# File lib/sequel/adapters/mock.rb, line 182
def supports_savepoints?
  shared_adapter? ? super : true
end

Private Instance Methods

_autoid(sql, v, ds=nil) click to toggle source
# File lib/sequel/adapters/mock.rb, line 188
def _autoid(sql, v, ds=nil)
  case v
  when Integer
    if ds
      ds.autoid += 1 if ds.autoid.is_a?(Integer)
    else
      @autoid += 1
    end
    v
  else
    _nextres(v, sql, nil)
  end
end
_execute(c, sql, opts=OPTS, &block) click to toggle source
# File lib/sequel/adapters/mock.rb, line 202
def _execute(c, sql, opts=OPTS, &block)
  sql += " -- args: #{opts[:arguments].inspect}" if opts[:arguments]
  sql += " -- #{@opts[:append]}" if @opts[:append]
  sql += " -- #{c.server.is_a?(Symbol) ? c.server : c.server.inspect}" if c.server != :default
  log_info(sql) unless opts[:log] == false
  @sqls << sql 

  ds = opts[:dataset]
  begin
    if block
      columns(ds, sql) if ds
      _fetch(sql, (ds._fetch if ds) || @fetch, &block)
    elsif meth = opts[:meth]
      if meth == :numrows
        _numrows(sql, (ds.numrows if ds) || @numrows)
      else
        v = ds.autoid if ds
        _autoid(sql, v || @autoid, (ds if v))
      end
    end
  rescue => e
    raise_error(e)
  end
end
_fetch(sql, f) { |dup| ... } click to toggle source
# File lib/sequel/adapters/mock.rb, line 227
def _fetch(sql, f, &block)
  case f
  when Hash
    yield f.dup
  when Array
    if f.all?{|h| h.is_a?(Hash)}
      f.each{|h| yield h.dup}
    else
      _fetch(sql, f.shift, &block)
    end
  when Proc
    h = f.call(sql)
    if h.is_a?(Hash)
      yield h.dup
    elsif h
      h.each{|h1| yield h1.dup}
    end
  when Class
    if f < Exception
      raise f
    else
      raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
    end
  when nil
    # nothing
  else
    raise Error, "Invalid @fetch attribute: #{f.inspect}"
  end
end
_nextres(v, sql, default) click to toggle source
# File lib/sequel/adapters/mock.rb, line 257
def _nextres(v, sql, default)
  case v
  when Integer
    v
  when Array
    v.empty? ? default : _nextres(v.shift, sql, default)
  when Proc
    v.call(sql)
  when Class
    if v < Exception
      raise v
    else
      raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
    end
  when nil
    default
  else
    raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
  end
end
_numrows(sql, v) click to toggle source
# File lib/sequel/adapters/mock.rb, line 278
def _numrows(sql, v)
  _nextres(v, sql, 0)
end
adapter_initialize() click to toggle source

Additional options supported:

:autoid

Call autoid= with the value

:columns

Call columns= with the value

:fetch

Call fetch= with the value

:numrows

Call numrows= with the value

:extend

A module the object is extended with.

:sqls

The array to store the SQL queries in.

# File lib/sequel/adapters/mock.rb, line 290
def adapter_initialize
  opts = @opts
  @sqls = opts[:sqls] || []
  if mod_name = SHARED_ADAPTERS[opts[:host]]
    @shared_adapter = true
    require "sequel/adapters/shared/#{opts[:host]}"
    extend Sequel.const_get(mod_name)::DatabaseMethods
    extend_datasets Sequel.const_get(mod_name)::DatasetMethods
    if pr = SHARED_ADAPTER_SETUP[opts[:host]]
      pr.call(self)
    end
  else
    @shared_adapter = false
  end
  self.autoid = opts[:autoid]
  self.columns = opts[:columns]
  self.fetch = opts[:fetch]
  self.numrows = opts[:numrows]
  extend(opts[:extend]) if opts[:extend]
  sqls
end
columns(ds, sql, cs=@columns) click to toggle source
# File lib/sequel/adapters/mock.rb, line 312
def columns(ds, sql, cs=@columns)
  case cs
  when Array
    unless cs.empty?
      if cs.all?{|c| c.is_a?(Symbol)}
        ds.columns(*cs)
      else
        columns(ds, sql, cs.shift)
      end
    end
  when Proc
    ds.columns(*cs.call(sql))
  when nil
    # nothing
  else
    raise Error, "Invalid @columns attribute: #{cs.inspect}"
  end
end
identifier_input_method_default() click to toggle source
# File lib/sequel/adapters/mock.rb, line 335
def identifier_input_method_default
  shared_adapter? ? super : nil
end
identifier_output_method_default() click to toggle source
# File lib/sequel/adapters/mock.rb, line 339
def identifier_output_method_default
  shared_adapter? ? super : nil
end
quote_identifiers_default() click to toggle source
# File lib/sequel/adapters/mock.rb, line 331
def quote_identifiers_default
  shared_adapter? ? super : false
end
shared_adapter?() click to toggle source
# File lib/sequel/adapters/mock.rb, line 343
def shared_adapter?
  @shared_adapter
end