Set the columns to set in the dataset when the dataset fetches rows. Argument types supported:
Set the hashes to yield by execute when retrieving rows. Argument types supported:
Yield no rows
Always yield a single row with this hash
Yield separately for each hash in this array
First retrieval gets the first value in the array, second gets the second value, etc.
Called with the select SQL query, uses the value returned, which should be a hash or array of hashes.
Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.
Set the number of rows to return from update or delete. Argument types supported:
Return 0 for all updates and deletes
Used for all updates and deletes
First update/delete gets the first value in the array, second gets the second value, etc.
Called with the update/delete SQL query, uses the value returned.
Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.
Mock the server version, useful when using the shared adapters
Set the autogenerated primary key integer to be returned when running an insert query. Argument types supported:
Return nil for all inserts
Starting integer for next insert, with futher inserts getting an incremented value
First insert gets the first value in the array, second gets the second value, etc.
Called with the insert SQL query, uses the value returned
Should be an Exception subclass, will create a new instance an raise it wrapped in a DatabaseError.
# File lib/sequel/adapters/mock.rb, line 49 def autoid=(v) @autoid = case v when Integer i = v - 1 proc{@mutex.synchronize{i+=1}} else v end end
Return a related Connection option connecting to the given shard.
# File lib/sequel/adapters/mock.rb, line 101 def connect(server) Connection.new(self, server, server_opts(server)) end
# File lib/sequel/adapters/mock.rb, line 105 def disconnect_connection(c) end
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 111 def execute(sql, opts=OPTS, &block) synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} end
Store the sql used, and return the value of the numrows method.
# File lib/sequel/adapters/mock.rb, line 117 def execute_dui(sql, opts=OPTS) execute(sql, opts.merge(:meth=>:numrows)) end
Store the sql used, and return the value of the autoid method.
# File lib/sequel/adapters/mock.rb, line 122 def execute_insert(sql, opts=OPTS) execute(sql, opts.merge(:meth=>:autoid)) end
Enable use of savepoints.
# File lib/sequel/adapters/mock.rb, line 137 def supports_savepoints? shared_adapter? ? super : true end
# File lib/sequel/adapters/mock.rb, line 143 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_connection_yield(sql, c){} unless opts[:log] == false @mutex.synchronize{@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 if ds @mutex.synchronize do v = ds.autoid if v.is_a?(Integer) ds.send(:cache_set, :_autoid, v + 1) end v end end || _nextres(@autoid, sql, nil) end end rescue => e raise_error(e) end end
# File lib/sequel/adapters/mock.rb, line 175 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, @mutex.synchronize{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 @fetch attribute: #{v.inspect}" end when nil # nothing else raise Error, "Invalid @fetch attribute: #{f.inspect}" end end
# File lib/sequel/adapters/mock.rb, line 205 def _nextres(v, sql, default) case v when Integer v when Array v.empty? ? default : _nextres(@mutex.synchronize{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
# File lib/sequel/adapters/mock.rb, line 226 def _numrows(sql, v) _nextres(v, sql, 0) end
Additional options supported:
Call autoid= with the value
Call columns= with the value
Call fetch= with the value
Call numrows= with the value
A module the object is extended with.
The array to store the SQL queries in.
# File lib/sequel/adapters/mock.rb, line 238 def adapter_initialize opts = @opts @mutex = Mutex.new @sqls = opts[:sqls] || [] @shared_adapter = false case db_type = opts[:host] when String, Symbol db_type = db_type.to_sym unless mod = Sequel.synchronize{SHARED_ADAPTER_MAP[db_type]} begin require "sequel/adapters/shared/#{db_type}" rescue LoadError else mod = Sequel.synchronize{SHARED_ADAPTER_MAP[db_type]} end end if mod @shared_adapter = true extend(mod::DatabaseMethods) extend_datasets(mod::DatasetMethods) if mod.respond_to?(:mock_adapter_setup) mod.mock_adapter_setup(self) end end end unless @shared_adapter extend UnmodifiedIdentifiers::DatabaseMethods extend_datasets UnmodifiedIdentifiers::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 end
# File lib/sequel/adapters/mock.rb, line 279 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, @mutex.synchronize{cs.shift}) end end when Proc ds.columns(*cs.call(sql)) when nil # nothing else raise Error, "Invalid @columns attribute: #{cs.inspect}" end end
# File lib/sequel/adapters/mock.rb, line 298 def dataset_class_default Dataset end
# File lib/sequel/adapters/mock.rb, line 302 def quote_identifiers_default shared_adapter? ? super : false end