Class: Sequel::Mock::Database
- Defined in:
- lib/sequel/adapters/mock.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
writeonly
Set the columns to set in the dataset when the dataset fetches rows.
-
#fetch ⇒ Object
writeonly
Set the hashes to yield by execute when retrieving rows.
-
#numrows ⇒ Object
writeonly
Set the number of rows to return from update or delete.
-
#server_version ⇒ Object
Mock the server version, useful when using the shared adapters.
Instance Method Summary collapse
-
#autoid=(v) ⇒ Object
Set the autogenerated primary key integer to be returned when running an insert query.
-
#connect(server) ⇒ Object
Return a related Connection option connecting to the given shard.
- #disconnect_connection(c) ⇒ Object
-
#execute(sql, opts = OPTS, &block) ⇒ Object
(also: #execute_ddl)
Store the sql used for later retrieval with #sqls, and return the appropriate value using either the #autoid, #fetch, or #numrows methods.
-
#execute_dui(sql, opts = OPTS) ⇒ Object
Store the sql used, and return the value of the #numrows method.
-
#execute_insert(sql, opts = OPTS) ⇒ Object
Store the sql used, and return the value of the #autoid method.
-
#sqls ⇒ Object
Return all stored SQL queries, and clear the cache of SQL queries.
-
#supports_savepoints? ⇒ Boolean
Enable use of savepoints.
Instance Attribute Details
#columns=(value) ⇒ Object
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
67 68 69 |
# File 'lib/sequel/adapters/mock.rb', line 67 def columns=(value) @columns = value end |
#fetch=(value) ⇒ Object (writeonly)
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.
82 83 84 |
# File 'lib/sequel/adapters/mock.rb', line 82 def fetch=(value) @fetch = value end |
#numrows=(value) ⇒ Object (writeonly)
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.
95 96 97 |
# File 'lib/sequel/adapters/mock.rb', line 95 def numrows=(value) @numrows = value end |
#server_version ⇒ Object
Mock the server version, useful when using the shared adapters
98 99 100 |
# File 'lib/sequel/adapters/mock.rb', line 98 def server_version @server_version end |
Instance Method Details
#autoid=(v) ⇒ Object
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.
49 50 51 52 53 54 55 56 57 |
# 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 |
#connect(server) ⇒ Object
Return a related Connection option connecting to the given shard.
101 102 103 |
# File 'lib/sequel/adapters/mock.rb', line 101 def connect(server) Connection.new(self, server, server_opts(server)) end |
#disconnect_connection(c) ⇒ Object
105 106 |
# File 'lib/sequel/adapters/mock.rb', line 105 def disconnect_connection(c) end |
#execute(sql, opts = OPTS, &block) ⇒ Object Also known as: execute_ddl
Store the sql used for later retrieval with #sqls, and return the appropriate value using either the #autoid, #fetch, or #numrows methods.
111 112 113 |
# File 'lib/sequel/adapters/mock.rb', line 111 def execute(sql, opts=OPTS, &block) synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} end |
#execute_dui(sql, opts = OPTS) ⇒ Object
Store the sql used, and return the value of the #numrows method.
117 118 119 |
# File 'lib/sequel/adapters/mock.rb', line 117 def execute_dui(sql, opts=OPTS) execute(sql, opts.merge(:meth=>:numrows)) end |
#execute_insert(sql, opts = OPTS) ⇒ Object
Store the sql used, and return the value of the #autoid method.
122 123 124 |
# File 'lib/sequel/adapters/mock.rb', line 122 def execute_insert(sql, opts=OPTS) execute(sql, opts.merge(:meth=>:autoid)) end |
#sqls ⇒ Object
Return all stored SQL queries, and clear the cache of SQL queries.
128 129 130 131 132 133 134 |
# File 'lib/sequel/adapters/mock.rb', line 128 def sqls @mutex.synchronize do s = @sqls.dup @sqls.clear s end end |
#supports_savepoints? ⇒ Boolean
Enable use of savepoints.
137 138 139 |
# File 'lib/sequel/adapters/mock.rb', line 137 def supports_savepoints? shared_adapter? ? super : true end |