class Sequel::Mock::Database

Database class for Sequel’s mock adapter.

def _autoid(sql, v, ds=nil)

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

def _execute(c, sql, opts={}, &block)

def _execute(c, sql, 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)
  @sqls << sql 
  ds = opts[:dataset]
  begin
    if block
      columns(ds, sql) if ds
      _fetch(sql, ds._fetch || @fetch, &block)
    elsif meth = opts[:meth]
      if meth == :numrows
        _numrows(sql, ds.numrows || @numrows)
      else
        v = ds.autoid
        _autoid(sql, v || @autoid, (ds if v))
      end
    end
  rescue => e
    raise_error(e)
  end
end

def _fetch(sql, f, &block)

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

def _nextres(v, sql, default)

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

def _numrows(sql, v)

def _numrows(sql, v)
  _nextres(v, sql, 0)
end

def columns(ds, sql, cs=@columns)

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

def connect(server)

Return a related Connection option connecting to the given shard.
def connect(server)
  Connection.new(self, server, server_opts(server))
end

def disconnect_connection(c)

def disconnect_connection(c)
end

def execute(sql, opts={}, &block)

#numrows methods.
the appropriate value using either the #autoid, #fetch, or
Store the sql used for later retrieval with #sqls, and return
def execute(sql, opts={}, &block)
  synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} 
end

def execute_dui(sql, opts={})

Store the sql used, and return the value of the #numrows method.
def execute_dui(sql, opts={})
  execute(sql, opts.merge(:meth=>:numrows))
end

def execute_insert(sql, opts={})

Store the sql used, and return the value of the #autoid method.
def execute_insert(sql, opts={})
  execute(sql, opts.merge(:meth=>:autoid))
end

def identifier_input_method_default

def identifier_input_method_default
  shared_adapter? ? super : nil
end

def identifier_output_method_default

def identifier_output_method_default
  shared_adapter? ? super : nil
end

def initialize(opts={})

:sqls :: The array to store the SQL queries in.
:extend :: A module the object is extended with.
:numrows :: Call #numrows= with the value
:fetch :: Call #fetch= with the value
:columns :: Call #columns= with the value
:autoid :: Call #autoid= with the value

Additional options supported:
def initialize(opts={})
  super
  opts = @opts
  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
  end
  self.autoid = opts[:autoid]
  self.columns = opts[:columns]
  self.fetch = opts[:fetch]
  self.numrows = opts[:numrows]
  extend(opts[:extend]) if opts[:extend]
  @sqls = opts[:sqls] || []
end

def log_connection_execute(c, sql)

def log_connection_execute(c, sql)
  c.execute(sql)
end

def quote_identifiers_default

def quote_identifiers_default
  shared_adapter? ? super : false
end

def shared_adapter?

def shared_adapter?
  @shared_adapter
end

def sqls

of SQL queries.
Return all stored SQL queries, and clear the cache
def sqls
  s = @sqls.dup
  @sqls.clear
  s
end

def supports_savepoints?

Enable use of savepoints.
def supports_savepoints?
  shared_adapter? ? super : true
end