Class Index [+]

Quicksearch

Sequel::Mock::Database

Database class for Sequel’s mock adapter.

Constants

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 Class Methods

new(opts={}) click to toggle source

Additional options supported:

:autoid

Call # with the value

:columns

Call # with the value

:fetch

Call # with the value

:numrows

Call # 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 127
127:       def initialize(opts={})
128:         super
129:         opts = @opts
130:         if mod_name = SHARED_ADAPTERS[opts[:host]]
131:           @shared_adapter = true
132:           require "sequel/adapters/shared/#{opts[:host]}"
133:           extend Sequel.const_get(mod_name)::DatabaseMethods
134:           extend_datasets Sequel.const_get(mod_name)::DatasetMethods
135:           if pr = SHARED_ADAPTER_SETUP[opts[:host]]
136:             pr.call(self)
137:           end
138:         end
139:         self.autoid = opts[:autoid]
140:         self.columns = opts[:columns]
141:         self.fetch = opts[:fetch]
142:         self.numrows = opts[:numrows]
143:         extend(opts[:extend]) if opts[:extend]
144:         @sqls = opts[:sqls] || []
145:       end

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
148:       def connect(server)
149:         Connection.new(self, server, server_opts(server))
150:       end
execute(sql, opts={}, &block) click to toggle source

Store the sql used for later retrieval with #, and return the appropriate value using either the #, #, or # methods.

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

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

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

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

     # File lib/sequel/adapters/mock.rb, line 166
166:       def execute_insert(sql, opts={})
167:         execute(sql, opts.merge(:meth=>:autoid))
168:       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 172
172:       def sqls
173:         s = @sqls.dup
174:         @sqls.clear
175:         s
176:       end
supports_savepoints?() click to toggle source

Enable use of savepoints.

     # File lib/sequel/adapters/mock.rb, line 179
179:       def supports_savepoints?
180:         shared_adapter? ? super : true
181:       end

Private Instance Methods

_autoid(sql, v, ds=nil) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 185
185:       def _autoid(sql, v, ds=nil)
186:         case v
187:         when Integer
188:           if ds
189:             ds.autoid += 1 if ds.autoid.is_a?(Integer)
190:           else
191:             @autoid += 1
192:           end
193:           v
194:         else
195:           _nextres(v, sql, nil)
196:         end
197:       end
_execute(c, sql, opts={}, &block) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 199
199:       def _execute(c, sql, opts={}, &block)
200:         sql += " -- args: #{opts[:arguments].inspect}" if opts[:arguments]
201:         sql += " -- #{@opts[:append]}" if @opts[:append]
202:         sql += " -- #{c.server.is_a?(Symbol) ? c.server : c.server.inspect}" if c.server != :default
203:         log_info(sql) unless opts[:log] == false
204:         @sqls << sql 
205: 
206:         ds = opts[:dataset]
207:         begin
208:           if block
209:             columns(ds, sql) if ds
210:             _fetch(sql, (ds._fetch if ds) || @fetch, &block)
211:           elsif meth = opts[:meth]
212:             if meth == :numrows
213:               _numrows(sql, (ds.numrows if ds) || @numrows)
214:             else
215:               v = ds.autoid if ds
216:               _autoid(sql, v || @autoid, (ds if v))
217:             end
218:           end
219:         rescue => e
220:           raise_error(e)
221:         end
222:       end
_fetch(sql, f, &block) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 224
224:       def _fetch(sql, f, &block)
225:         case f
226:         when Hash
227:           yield f.dup
228:         when Array
229:           if f.all?{|h| h.is_a?(Hash)}
230:             f.each{|h| yield h.dup}
231:           else
232:             _fetch(sql, f.shift, &block)
233:           end
234:         when Proc
235:           h = f.call(sql)
236:           if h.is_a?(Hash)
237:             yield h.dup
238:           elsif h
239:             h.each{|h1| yield h1.dup}
240:           end
241:         when Class
242:           if f < Exception
243:             raise f
244:           else
245:             raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
246:           end
247:         when nil
248:           # nothing
249:         else
250:           raise Error, "Invalid @fetch attribute: #{f.inspect}"
251:         end
252:       end
_nextres(v, sql, default) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 254
254:       def _nextres(v, sql, default)
255:         case v
256:         when Integer
257:           v
258:         when Array
259:           v.empty? ? default : _nextres(v.shift, sql, default)
260:         when Proc
261:           v.call(sql)
262:         when Class
263:           if v < Exception
264:             raise v
265:           else
266:             raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
267:           end
268:         when nil
269:           default
270:         else
271:           raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
272:         end
273:       end
_numrows(sql, v) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 275
275:       def _numrows(sql, v)
276:         _nextres(v, sql, 0)
277:       end
columns(ds, sql, cs=@columns) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 279
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, 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
disconnect_connection(c) click to toggle source
     # File lib/sequel/adapters/mock.rb, line 298
298:       def disconnect_connection(c)
299:       end
identifier_input_method_default() click to toggle source
     # File lib/sequel/adapters/mock.rb, line 305
305:       def identifier_input_method_default
306:         shared_adapter? ? super : nil
307:       end
identifier_output_method_default() click to toggle source
     # File lib/sequel/adapters/mock.rb, line 309
309:       def identifier_output_method_default
310:         shared_adapter? ? super : nil
311:       end
quote_identifiers_default() click to toggle source
     # File lib/sequel/adapters/mock.rb, line 301
301:       def quote_identifiers_default
302:         shared_adapter? ? super : false
303:       end
shared_adapter?() click to toggle source
     # File lib/sequel/adapters/mock.rb, line 313
313:       def shared_adapter?
314:         @shared_adapter
315:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.