class Sequel::Mock::Database

Attributes

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

autoid=(v) click to toggle source

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.

   # File lib/sequel/adapters/mock.rb
49 def autoid=(v)
50   @autoid = case v
51   when Integer
52     i = v - 1
53     proc{@mutex.synchronize{i+=1}}
54   else
55     v
56   end
57 end
connect(server) click to toggle source

Return a related Connection option connecting to the given shard.

    # File lib/sequel/adapters/mock.rb
101 def connect(server)
102   Connection.new(self, server, server_opts(server))
103 end
disconnect_connection(c) click to toggle source
    # File lib/sequel/adapters/mock.rb
105 def disconnect_connection(c)
106 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
111 def execute(sql, opts=OPTS, &block)
112   synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} 
113 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
117 def execute_dui(sql, opts=OPTS)
118   execute(sql, opts.merge(:meth=>:numrows))
119 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
122 def execute_insert(sql, opts=OPTS)
123   execute(sql, opts.merge(:meth=>:autoid))
124 end
sqls() click to toggle source

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

    # File lib/sequel/adapters/mock.rb
128 def sqls
129   @mutex.synchronize do
130     s = @sqls.dup
131     @sqls.clear
132     s
133   end
134 end
supports_savepoints?() click to toggle source

Enable use of savepoints.

Calls superclass method Sequel::Database#supports_savepoints?
    # File lib/sequel/adapters/mock.rb
137 def supports_savepoints?
138   shared_adapter? ? super : true
139 end

Private Instance Methods

_execute(c, sql, opts=OPTS, &block) click to toggle source
    # File lib/sequel/adapters/mock.rb
143 def _execute(c, sql, opts=OPTS, &block)
144   sql += " -- args: #{opts[:arguments].inspect}" if opts[:arguments]
145   sql += " -- #{@opts[:append]}" if @opts[:append]
146   sql += " -- #{c.server.is_a?(Symbol) ? c.server : c.server.inspect}" if c.server != :default
147   log_connection_yield(sql, c){} unless opts[:log] == false
148   @mutex.synchronize{@sqls << sql}
149 
150   ds = opts[:dataset]
151   begin
152     if block
153       columns(ds, sql) if ds
154       _fetch(sql, (ds._fetch if ds) || @fetch, &block)
155     elsif meth = opts[:meth]
156       if meth == :numrows
157         _numrows(sql, (ds.numrows if ds) || @numrows)
158       else
159         if ds
160           @mutex.synchronize do
161             v = ds.autoid
162             if v.is_a?(Integer)
163               ds.send(:cache_set, :_autoid, v + 1)
164             end
165             v
166           end
167         end || _nextres(@autoid, sql, nil)
168       end
169     end
170   rescue => e
171     raise_error(e)
172   end
173 end
_fetch(sql, f) { |dup| ... } click to toggle source
    # File lib/sequel/adapters/mock.rb
175 def _fetch(sql, f, &block)
176   case f
177   when Hash
178     yield f.dup
179   when Array
180     if f.all?{|h| h.is_a?(Hash)}
181       f.each{|h| yield h.dup}
182     else
183       _fetch(sql, @mutex.synchronize{f.shift}, &block)
184     end
185   when Proc
186     h = f.call(sql)
187     if h.is_a?(Hash)
188       yield h.dup
189     elsif h
190       h.each{|h1| yield h1.dup}
191     end
192   when Class
193     if f < Exception
194       raise f
195     else
196       raise Error, "Invalid @fetch attribute: #{v.inspect}"
197     end
198   when nil
199     # nothing
200   else
201     raise Error, "Invalid @fetch attribute: #{f.inspect}"
202   end
203 end
_nextres(v, sql, default) click to toggle source
    # File lib/sequel/adapters/mock.rb
205 def _nextres(v, sql, default)
206   case v
207   when Integer
208     v
209   when Array
210     v.empty? ? default : _nextres(@mutex.synchronize{v.shift}, sql, default)
211   when Proc
212     v.call(sql)
213   when Class
214     if v < Exception
215       raise v
216     else
217       raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
218     end
219   when nil
220     default
221   else
222     raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
223   end
224 end
_numrows(sql, v) click to toggle source
    # File lib/sequel/adapters/mock.rb
226 def _numrows(sql, v)
227   _nextres(v, sql, 0)
228 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
238 def adapter_initialize
239   opts = @opts
240   @mutex = Mutex.new
241   @sqls = opts[:sqls] || []
242   @shared_adapter = false
243 
244   case db_type = opts[:host] 
245   when String, Symbol
246     db_type = db_type.to_sym
247     unless mod = Sequel.synchronize{SHARED_ADAPTER_MAP[db_type]}
248       begin
249         require "sequel/adapters/shared/#{db_type}"
250       rescue LoadError
251       else
252         mod = Sequel.synchronize{SHARED_ADAPTER_MAP[db_type]}
253       end
254     end
255 
256     if mod
257       @shared_adapter = true
258       extend(mod::DatabaseMethods)
259       extend_datasets(mod::DatasetMethods)
260       if mod.respond_to?(:mock_adapter_setup)
261         mod.mock_adapter_setup(self)
262       end
263     end
264   end
265 
266   unless @shared_adapter
267     extend UnmodifiedIdentifiers::DatabaseMethods
268     extend_datasets UnmodifiedIdentifiers::DatasetMethods
269   end
270 
271   self.autoid = opts[:autoid]
272   self.columns = opts[:columns]
273   self.fetch = opts[:fetch]
274   self.numrows = opts[:numrows]
275   extend(opts[:extend]) if opts[:extend]
276   sqls
277 end
columns(ds, sql, cs=@columns) click to toggle source
    # File lib/sequel/adapters/mock.rb
279 def columns(ds, sql, cs=@columns)
280   case cs
281   when Array
282     unless cs.empty?
283       if cs.all?{|c| c.is_a?(Symbol)}
284         ds.columns(*cs)
285       else
286         columns(ds, sql, @mutex.synchronize{cs.shift})
287       end
288     end
289   when Proc
290     ds.columns(*cs.call(sql))
291   when nil
292     # nothing
293   else
294     raise Error, "Invalid @columns attribute: #{cs.inspect}"
295   end
296 end
dataset_class_default() click to toggle source
    # File lib/sequel/adapters/mock.rb
298 def dataset_class_default
299   Dataset
300 end
quote_identifiers_default() click to toggle source
    # File lib/sequel/adapters/mock.rb
302 def quote_identifiers_default
303   shared_adapter? ? super : false
304 end
shared_adapter?() click to toggle source
    # File lib/sequel/adapters/mock.rb
306 def shared_adapter?
307   @shared_adapter
308 end