3
3
Sequel's dataset layer is mostly structured as an DSL, so it often obscures
4
4
what actual objects are being used. For example, you don't usually create
5
5
Sequel objects by calling #new on the object's class (other than Sequel::Model
6
subclasses). However, just as almost everything in ruby is an object, all
6
instances). However, just as almost everything in ruby is an object, all
7
7
the methods you call in Sequel deal with objects behind the scenes.
9
9
There are five main types of Sequel-specific objects that you deal with in
204
204
ruby class, it will generally use a Sequel::SQL::Expression subclass to represent that
207
Some of the examples below show examples that require the {core_extensions extension}[link:files/doc/core_extensions_rdoc.html].
207
209
=== Sequel::LiteralString
209
211
Sequel::LiteralString is not actually a Sequel::SQL::Expression subclass. It is
256
258
The following shortcuts exist for creating Sequel::SQL::ValueList objects:
258
260
Sequel.value_list([[1, 2], [3, 4]])
259
[[1, 2], [3, 4]].sql_value_list
261
[[1, 2], [3, 4]].sql_value_list # core_extensions extension
261
263
=== Sequel::SQL::Identifier
280
283
The following shortcuts exist for creating Sequel::SQL::QualifiedIdentifier objects:
285
Sequel.expr(:table__column)
282
286
Sequel.qualify(:table, :column)
283
:column.qualify(:table)
287
:column.qualify(:table) # core_extensions extension
285
289
=== Sequel::SQL::AliasedExpression
292
296
The following shortcuts exist for creating Sequel::SQL::AliasedExpression objects:
298
Sequel.expr(:column___alias)
294
299
Sequel.as(:column, :alias)
300
:column.as(:alias) # core_extensions extension
297
302
=== Sequel::SQL::ComplexExpression
309
314
There are many shortcuts for creating Sequel::SQL::ComplexExpression objects:
311
316
Sequel.or(:col1, :col2)
317
:col1 | :col2 # core_extensions extension
314
319
Sequel.+(:column, 2)
320
:column + 2 # core_extensions extension
317
322
Sequel.join([:column, 'b'])
323
:column + 'b' # core_extensions extension
320
325
=== Sequel::SQL::CaseExpression
339
344
Sequel.case({2=>1}, 0, :a)
340
345
Sequel.case({{:a=>2}=>1}, 0)
347
{2=>1}.case(0, :a) # core_extensions extension
348
{{:a=>2}=>1}.case(0) # core_extensions extension
345
350
=== Sequel::SQL::Cast
373
378
Sequel::SQL::ColumnAll.new(:table) # "table".*
375
The following shortcut exists for creating Sequel::SQL::Cast objects:
380
The following shortcut exists for creating Sequel::SQL::ColumnAll objects:
382
Sequel.expr(:table).*
383
:table.* # core_extensions extension
379
385
=== Sequel::SQL::Constant
390
396
DB[:table].insert(:time=>Sequel::CURRENT_TIMESTAMP)
398
=== Sequel::SQL::DelayedEvaluation
400
Sequel::SQL::DelayedEvaluation objects represent an evaluation that is delayed
401
until query literalization.
403
Sequel::SQL::DelayedEvaluation.new(proc{some_model.updated_at})
405
The following shortcut exists for creating Sequel::SQL::DelayedEvaluation
408
Sequel.delay{some_model.updated_at}
410
Note how Sequel.delay requires a block, while Sequel::SQL::DelayedEvaluation.new
411
accepts a generic callable object.
413
Let's say you wanted a dataset for the number of objects greater than some
414
attribute of another object:
416
ds = DB[:table].where{updated_at > some_model.updated_at}
418
The problem with the above query is that it evaluates "some_model.updated_at"
419
statically, so if you change some_model.updated_at later, it won't affect this
420
dataset. You can use Sequel.delay to fix this:
422
ds = DB[:table].where{updated_at > Sequel.delay{some_model.updated_at}}
424
This will evaluate "some_model.updated_at" every time you literalize the
425
dataset (usually every time it is executed).
392
427
=== Sequel::SQL::Function
394
429
Sequel::SQL::Function objects represents database function calls, which take a function
399
434
The following shortcuts exist for creating Sequel::SQL::Function objects:
401
436
Sequel.function(:func, :a, 2)
402
:func.sql_function(:a, 2)
437
:func.sql_function(:a, 2) # core_extensions extension
404
439
=== Sequel::SQL::JoinClause
436
471
Sequel.lit(':b = :v', :b=>:a, :v=>1)
437
472
Sequel.lit(['', ' = '], :a, 1)
440
':b = :v'.lit(:b=>:a, :v=>1)
474
'? = ?'.lit(:a, 1) # core_extensions extension
475
':b = :v'.lit(:b=>:a, :v=>1) # core_extensions extension
442
477
=== Sequel::SQL::OrderedExpression
461
496
Sequel.asc(:a, :nulls=>:first)
462
497
Sequel.desc(:a, :nulls=>:last)
466
:a.asc(:nulls=>:first)
467
:a.desc(:nulls=>:last)
499
:a.asc # core_extensions extension
500
:a.desc # core_extensions extension
501
:a.asc(:nulls=>:first) # core_extensions extension
502
:a.desc(:nulls=>:last) # core_extensions extension
469
504
=== Sequel::SQL::Subscript
471
506
Sequel::SQL::Subscript objects represent SQL database array access. They take an
472
expression and an array of indexes:
507
expression and an array of indexes (or a range for an SQL array slice):
474
509
Sequel::SQL::Subscript.new(:a, [1]) # "a"[1]
475
510
Sequel::SQL::Subscript.new(:a, [1, 2]) # "a"[1, 2]
511
Sequel::SQL::Subscript.new(:a, [1..2]) # "a"[1:2]
477
513
The following shortcuts exist for creating Sequel::SQL::Subscript objects:
479
515
Sequel.subscript(:a, 1)
480
516
Sequel.subscript(:a, 1, 2)
517
Sequel.subscript(:a, 1..2)
483
:a.sql_subscript(1, 2)
519
:a.sql_subscript(1) # core_extensions extension
520
:a.sql_subscript(1, 2) # core_extensions extension
521
:a.sql_subscript(1..2) # core_extensions extension
485
523
=== Sequel::SQL::VirtualRow
487
525
Sequel::SQL::VirtualRow is a BasicObject subclass that is the backbone behind the
488
526
block expression support:
490
DB[:table].filter{a < 1}
528
DB[:table].where{a < 1}
492
In the above code, the block is instance-evaled instead a VirtualRow instance.
530
In the above code, the block is instance-evaled inside a VirtualRow instance.
494
532
These objects are usually not instantiated manually. See the
495
533
{Virtual Row Guide}[link:files/doc/virtual_rows_rdoc.html] for details.