Class: Sequel::Mock::Database
- Defined in:
- lib/sequel/adapters/mock.rb
Overview
Database class for Sequel’s mock adapter.
Constant Summary collapse
- DatasetClass =
self
Constants inherited from Database
Database::ADAPTERS, Database::AUTOINCREMENT, Database::CASCADE, Database::COLUMN_DEFINITION_ORDER, Database::COMMA_SEPARATOR, Database::MSSQL_DEFAULT_RE, Database::MYSQL_TIMESTAMP_RE, Database::NOT_NULL, Database::NO_ACTION, Database::NULL, Database::POSTGRES_DEFAULT_RE, Database::PRIMARY_KEY, Database::RESTRICT, Database::SET_DEFAULT, Database::SET_NULL, Database::SQL_BEGIN, Database::SQL_COMMIT, Database::SQL_RELEASE_SAVEPOINT, Database::SQL_ROLLBACK, Database::SQL_ROLLBACK_TO_SAVEPOINT, Database::SQL_SAVEPOINT, Database::STRING_DEFAULT_RE, Database::TEMPORARY, Database::TRANSACTION_BEGIN, Database::TRANSACTION_COMMIT, Database::TRANSACTION_ISOLATION_LEVELS, Database::TRANSACTION_ROLLBACK, Database::UNDERSCORE, Database::UNIQUE, Database::UNSIGNED
Instance Attribute Summary collapse
-
#autoid ⇒ Object
writeonly
Set the autogenerated primary key integer to be returned when running an insert query.
-
#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.
Attributes inherited from Database
#dataset_class, #default_schema, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level
Instance Method Summary collapse
-
#connect(server) ⇒ Object
Return a related Connection option connecting to the given shard.
-
#execute(sql, 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 = {}) ⇒ Object
Store the sql used, and return the value of the #numrows method.
-
#execute_insert(sql, opts = {}) ⇒ Object
Store the sql used, and return the value of the #autoid method.
-
#initialize(opts = {}) ⇒ Database
constructor
Additional options supported:.
-
#sqls ⇒ Object
Return all stored SQL queries, and clear the cache of SQL queries.
-
#supports_savepoints? ⇒ Boolean
Enable use of savepoints.
Methods inherited from Database
#<<, #[], adapter_class, #adapter_scheme, adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, #after_rollback, #alter_table, #call, #cast_type_literal, connect, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_table, #drop_view, #dump_indexes_migration, #dump_schema_migration, #dump_table_schema, #each_server, #extend_datasets, #fetch, #from, #from_application_timestamp, #get, #identifier_input_method, identifier_input_method, #identifier_input_method=, identifier_input_method=, identifier_output_method, #identifier_output_method, #identifier_output_method=, identifier_output_method=, #in_transaction?, #indexes, #inspect, #literal, #log_info, #log_yield, #logger=, #query, quote_identifiers=, #quote_identifiers=, #quote_identifiers?, #remove_servers, #rename_column, #rename_table, #run, #schema, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_prepared_transactions?, #supports_transaction_isolation_levels?, #synchronize, #table_exists?, #tables, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #views
Methods included from Sequel::Metaprogramming
Constructor Details
#initialize(opts = {}) ⇒ Database
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.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/sequel/adapters/mock.rb', line 95 def initialize(opts={}) super opts = @opts 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 |
Instance Attribute Details
#autoid=(value) ⇒ Object (writeonly)
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.
47 48 49 |
# File 'lib/sequel/adapters/mock.rb', line 47 def autoid=(value) @autoid = value end |
#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
57 58 59 |
# File 'lib/sequel/adapters/mock.rb', line 57 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.
72 73 74 |
# File 'lib/sequel/adapters/mock.rb', line 72 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.
85 86 87 |
# File 'lib/sequel/adapters/mock.rb', line 85 def numrows=(value) @numrows = value end |
Instance Method Details
#connect(server) ⇒ Object
Return a related Connection option connecting to the given shard.
107 108 109 |
# File 'lib/sequel/adapters/mock.rb', line 107 def connect(server) Connection.new(self, server, server_opts(server)) end |
#execute(sql, 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.
114 115 116 |
# File 'lib/sequel/adapters/mock.rb', line 114 def execute(sql, opts={}, &block) synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)} end |
#execute_dui(sql, opts = {}) ⇒ Object
Store the sql used, and return the value of the #numrows method.
120 121 122 |
# File 'lib/sequel/adapters/mock.rb', line 120 def execute_dui(sql, opts={}) execute(sql, opts.merge(:meth=>:numrows)) end |
#execute_insert(sql, opts = {}) ⇒ Object
Store the sql used, and return the value of the #autoid method.
125 126 127 |
# File 'lib/sequel/adapters/mock.rb', line 125 def execute_insert(sql, opts={}) execute(sql, opts.merge(:meth=>:autoid)) end |
#sqls ⇒ Object
Return all stored SQL queries, and clear the cache of SQL queries.
131 132 133 134 135 |
# File 'lib/sequel/adapters/mock.rb', line 131 def sqls s = @sqls.dup @sqls.clear s end |
#supports_savepoints? ⇒ Boolean
Enable use of savepoints.
138 139 140 |
# File 'lib/sequel/adapters/mock.rb', line 138 def supports_savepoints? true end |