Class: Sequel::Mock::Database

Inherits:
Database show all
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::COLUMN_DEFINITION_ORDER, Database::COLUMN_SCHEMA_DATETIME_TYPES, Database::COLUMN_SCHEMA_STRING_TYPES, Database::COMBINABLE_ALTER_TABLE_OPS, Database::COMMA_SEPARATOR, Database::CURRENT_TIMESTAMP_RE, Database::DEFAULT_DATABASE_ERROR_REGEXPS, Database::DEFAULT_JOIN_TABLE_COLUMN_OPTIONS, Database::DEFAULT_STRING_COLUMN_SIZE, Database::EXTENSIONS, Database::NOT_NULL, Database::NULL, Database::OPTS, Database::PRIMARY_KEY, Database::SCHEMA_TYPE_CLASSES, 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

Attributes inherited from Database

#cache_schema, #dataset_class, #default_string_column_size, #log_connection_info, #log_warn_duration, #loggers, #opts, #pool, #prepared_statements, #sql_log_level, #timezone, #transaction_isolation_level

Instance Method Summary collapse

Methods inherited from Database

#<<, #[], adapter_class, adapter_scheme, #adapter_scheme, #add_column, #add_index, #add_servers, #after_commit, after_initialize, #after_rollback, #alter_table, #alter_table_generator, #call, #cast_type_literal, connect, #create_join_table, #create_join_table!, #create_join_table?, #create_or_replace_view, #create_table, #create_table!, #create_table?, #create_table_generator, #create_view, #database_type, #dataset, #disconnect, #drop_column, #drop_index, #drop_join_table, #drop_table, #drop_table?, #drop_view, #each_server, #extend_datasets, extension, #extension, #fetch, #freeze, #from, #from_application_timestamp, #get, #global_index_namespace?, #in_transaction?, #initialize, #initialize_copy, #inspect, #literal, #literal_symbol, #literal_symbol_set, load_adapter, #log_connection_yield, #log_exception, #log_info, #log_yield, #logger=, #prepared_statement, #quote_identifier, register_extension, #remove_servers, #rename_column, #rename_table, #rollback_checker, #run, run_after_initialize, #schema, #schema_type_class, #select, #serial_primary_key_options, #servers, #set_column_default, #set_column_type, #set_prepared_statement, set_shared_adapter_scheme, #sharded?, single_threaded, single_threaded=, #single_threaded?, #supports_create_table_if_not_exists?, #supports_deferrable_constraints?, #supports_deferrable_foreign_key_constraints?, #supports_drop_table_if_exists?, #supports_foreign_key_parsing?, #supports_index_parsing?, #supports_partial_indexes?, #supports_prepared_transactions?, #supports_savepoints_in_prepared_transactions?, #supports_schema_parsing?, #supports_table_listing?, #supports_transaction_isolation_levels?, #supports_transactional_ddl?, #supports_view_listing?, #supports_views_with_check_option?, #supports_views_with_local_check_option?, #synchronize, #table_exists?, #test_connection, #to_application_timestamp, #transaction, #typecast_value, #uri, #url, #valid_connection?

Methods included from Sequel::Metaprogramming

#meta_def

Constructor Details

This class inherits a constructor from Sequel::Database

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



69
70
71
# File 'lib/sequel/adapters/mock.rb', line 69

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.



84
85
86
# File 'lib/sequel/adapters/mock.rb', line 84

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.



97
98
99
# File 'lib/sequel/adapters/mock.rb', line 97

def numrows=(value)
  @numrows = value
end

#server_versionObject

Mock the server version, useful when using the shared adapters



100
101
102
# File 'lib/sequel/adapters/mock.rb', line 100

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.



51
52
53
54
55
56
57
58
59
# File 'lib/sequel/adapters/mock.rb', line 51

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.



103
104
105
# File 'lib/sequel/adapters/mock.rb', line 103

def connect(server)
  Connection.new(self, server, server_opts(server))
end

#disconnect_connection(c) ⇒ Object



107
108
# File 'lib/sequel/adapters/mock.rb', line 107

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.



113
114
115
# File 'lib/sequel/adapters/mock.rb', line 113

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.



119
120
121
# File 'lib/sequel/adapters/mock.rb', line 119

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.



124
125
126
# File 'lib/sequel/adapters/mock.rb', line 124

def execute_insert(sql, opts=OPTS)
  execute(sql, opts.merge(:meth=>:autoid))
end

#sqlsObject

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



130
131
132
133
134
135
136
# File 'lib/sequel/adapters/mock.rb', line 130

def sqls
  @mutex.synchronize do
    s = @sqls.dup
    @sqls.clear
    s
  end
end

#supports_savepoints?Boolean

Enable use of savepoints.

Returns:

  • (Boolean)


139
140
141
# File 'lib/sequel/adapters/mock.rb', line 139

def supports_savepoints?
  shared_adapter? ? super : true
end