/usr/lib/ruby/vendor_ruby/sequel/adapters/mock.rb is in ruby-sequel 4.15.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | module Sequel
module Mock
# Connection class for Sequel's mock adapter.
class Connection
# Sequel::Mock::Database object that created this connection
attr_reader :db
# Shard this connection operates on, when using Sequel's
# sharding support (always :default for databases not using
# sharding).
attr_reader :server
# The specific database options for this connection.
attr_reader :opts
# Store the db, server, and opts.
def initialize(db, server, opts)
@db = db
@server = server
@opts = opts
end
# Delegate to the db's #_execute method.
def execute(sql)
@db.send(:_execute, self, sql, :log=>false)
end
end
# Database class for Sequel's mock adapter.
class Database < Sequel::Database
set_adapter_scheme :mock
# Map of database type names to module names, used for handling
# mock adapters for specific database types.
SHARED_ADAPTERS = {
'access'=>'Access',
'cubrid'=>'Cubrid',
'db2'=>'DB2',
'firebird'=>'Firebird',
'fdbsql'=>'Fdbsql',
'informix'=>'Informix',
'mssql'=>'MSSQL',
'mysql'=>'MySQL',
'oracle'=>'Oracle',
'postgres'=>'Postgres',
'sqlanywhere'=>'SqlAnywhere',
'sqlite'=>'SQLite'
}
# Procs to run for specific database types to get the mock adapter
# to work with the shared adapter
SHARED_ADAPTER_SETUP = {
'postgres' => lambda do |db|
db.instance_eval do
@server_version = 90400
initialize_postgres_adapter
end
db.extend(Module.new do
def bound_variable_arg(arg, conn)
arg
end
def primary_key(table)
:id
end
end)
end,
'oracle' => lambda do |db|
db.instance_eval do
@primary_key_sequences = {}
end
end,
'mysql' => lambda do |db|
db.instance_eval do
@server_version = 50617
end
end,
'mssql' => lambda do |db|
db.instance_eval do
@server_version = 11000000
end
end,
'sqlite' => lambda do |db|
db.instance_eval do
@sqlite_version = 30804
end
end
}
# 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.
attr_writer :autoid
# 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
attr_writer :columns
# 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.
attr_writer :fetch
# 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.
attr_writer :numrows
# Mock the server version, useful when using the shared adapters
attr_accessor :server_version
# Return a related Connection option connecting to the given shard.
def connect(server)
Connection.new(self, server, server_opts(server))
end
def disconnect_connection(c)
end
# Store the sql used for later retrieval with #sqls, and return
# the appropriate value using either the #autoid, #fetch, or
# #numrows methods.
def execute(sql, opts=OPTS, &block)
synchronize(opts[:server]){|c| _execute(c, sql, opts, &block)}
end
alias execute_ddl execute
# Store the sql used, and return the value of the #numrows method.
def execute_dui(sql, opts=OPTS)
execute(sql, opts.merge(:meth=>:numrows))
end
# Store the sql used, and return the value of the #autoid method.
def execute_insert(sql, opts=OPTS)
execute(sql, opts.merge(:meth=>:autoid))
end
# Return all stored SQL queries, and clear the cache
# of SQL queries.
def sqls
s = @sqls.dup
@sqls.clear
s
end
# Enable use of savepoints.
def supports_savepoints?
shared_adapter? ? super : true
end
private
def _autoid(sql, v, ds=nil)
case v
when Integer
if ds
ds.autoid += 1 if ds.autoid.is_a?(Integer)
else
@autoid += 1
end
v
else
_nextres(v, sql, nil)
end
end
def _execute(c, sql, opts=OPTS, &block)
sql += " -- args: #{opts[:arguments].inspect}" if opts[:arguments]
sql += " -- #{@opts[:append]}" if @opts[:append]
sql += " -- #{c.server.is_a?(Symbol) ? c.server : c.server.inspect}" if c.server != :default
log_info(sql) unless opts[:log] == false
@sqls << sql
ds = opts[:dataset]
begin
if block
columns(ds, sql) if ds
_fetch(sql, (ds._fetch if ds) || @fetch, &block)
elsif meth = opts[:meth]
if meth == :numrows
_numrows(sql, (ds.numrows if ds) || @numrows)
else
v = ds.autoid if ds
_autoid(sql, v || @autoid, (ds if v))
end
end
rescue => e
raise_error(e)
end
end
def _fetch(sql, f, &block)
case f
when Hash
yield f.dup
when Array
if f.all?{|h| h.is_a?(Hash)}
f.each{|h| yield h.dup}
else
_fetch(sql, f.shift, &block)
end
when Proc
h = f.call(sql)
if h.is_a?(Hash)
yield h.dup
elsif h
h.each{|h1| yield h1.dup}
end
when Class
if f < Exception
raise f
else
raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
end
when nil
# nothing
else
raise Error, "Invalid @fetch attribute: #{f.inspect}"
end
end
def _nextres(v, sql, default)
case v
when Integer
v
when Array
v.empty? ? default : _nextres(v.shift, sql, default)
when Proc
v.call(sql)
when Class
if v < Exception
raise v
else
raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
end
when nil
default
else
raise Error, "Invalid @autoid/@numrows attribute: #{v.inspect}"
end
end
def _numrows(sql, v)
_nextres(v, sql, 0)
end
# 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.
def adapter_initialize
opts = @opts
@sqls = opts[:sqls] || []
if mod_name = SHARED_ADAPTERS[opts[:host]]
@shared_adapter = true
require "sequel/adapters/shared/#{opts[:host]}"
extend Sequel.const_get(mod_name)::DatabaseMethods
extend_datasets Sequel.const_get(mod_name)::DatasetMethods
if pr = SHARED_ADAPTER_SETUP[opts[:host]]
pr.call(self)
end
else
@shared_adapter = false
end
self.autoid = opts[:autoid]
self.columns = opts[:columns]
self.fetch = opts[:fetch]
self.numrows = opts[:numrows]
extend(opts[:extend]) if opts[:extend]
sqls
end
def columns(ds, sql, cs=@columns)
case cs
when Array
unless cs.empty?
if cs.all?{|c| c.is_a?(Symbol)}
ds.columns(*cs)
else
columns(ds, sql, cs.shift)
end
end
when Proc
ds.columns(*cs.call(sql))
when nil
# nothing
else
raise Error, "Invalid @columns attribute: #{cs.inspect}"
end
end
def quote_identifiers_default
shared_adapter? ? super : false
end
def identifier_input_method_default
shared_adapter? ? super : nil
end
def identifier_output_method_default
shared_adapter? ? super : nil
end
def shared_adapter?
@shared_adapter
end
end
class Dataset < Sequel::Dataset
Database::DatasetClass = self
# Override the databases's autoid setting for this dataset
attr_accessor :autoid
# Override the databases's fetch setting for this dataset
attr_accessor :_fetch
# Override the databases's numrows setting for this dataset
attr_accessor :numrows
# If arguments are provided, use them to set the columns
# for this dataset and return self. Otherwise, use the
# default Sequel behavior and return the columns.
def columns(*cs)
if cs.empty?
super
else
@columns = cs
self
end
end
def fetch_rows(sql, &block)
execute(sql, &block)
end
private
def execute(sql, opts=OPTS, &block)
super(sql, opts.merge(:dataset=>self), &block)
end
def execute_dui(sql, opts=OPTS, &block)
super(sql, opts.merge(:dataset=>self), &block)
end
def execute_insert(sql, opts=OPTS, &block)
super(sql, opts.merge(:dataset=>self), &block)
end
end
end
end
|