~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to doc/object_model.rdoc

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
8
8
 
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
205
205
concept.
206
206
 
 
207
Some of the examples below show examples that require the {core_extensions extension}[link:files/doc/core_extensions_rdoc.html].
 
208
 
207
209
=== Sequel::LiteralString
208
210
 
209
211
Sequel::LiteralString is not actually a Sequel::SQL::Expression subclass.  It is
215
217
The following shortcuts exist for creating Sequel::LiteralString objects:
216
218
 
217
219
  Sequel.lit("co'de")
218
 
  "co'de".lit
 
220
  "co'de".lit # core_extensions extension
219
221
 
220
222
=== Sequel::SQL::Blob
221
223
 
228
230
The following shortcuts exist for creating Sequel::SQL::Blob objects:
229
231
 
230
232
  Sequel.blob("blob")
231
 
  "blob".to_sequel_blob
 
233
  "blob".to_sequel_blob  # core_extensions extension
232
234
 
233
235
=== Sequel::SQLTime
234
236
 
256
258
The following shortcuts exist for creating Sequel::SQL::ValueList objects:
257
259
 
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
260
262
 
261
263
=== Sequel::SQL::Identifier
262
264
 
268
270
 
269
271
The following shortcuts exist for creating Sequel::SQL::Identifier objects:
270
272
 
 
273
  Sequel.expr(:column)
271
274
  Sequel.identifier(:col__umn)
272
 
  :col__umn.identifier
 
275
  :col__umn.identifier # core_extensions extension
273
276
 
274
277
=== Sequel::SQL::QualifiedIdentifier
275
278
 
279
282
 
280
283
The following shortcuts exist for creating Sequel::SQL::QualifiedIdentifier objects:
281
284
 
 
285
  Sequel.expr(:table__column)
282
286
  Sequel.qualify(:table, :column)
283
 
  :column.qualify(:table)
 
287
  :column.qualify(:table) # core_extensions extension
284
288
 
285
289
=== Sequel::SQL::AliasedExpression
286
290
 
291
295
 
292
296
The following shortcuts exist for creating Sequel::SQL::AliasedExpression objects:
293
297
 
 
298
  Sequel.expr(:column___alias)
294
299
  Sequel.as(:column, :alias)
295
 
  :column.as(:alias)
 
300
  :column.as(:alias) # core_extensions extension
296
301
  
297
302
=== Sequel::SQL::ComplexExpression
298
303
 
309
314
There are many shortcuts for creating Sequel::SQL::ComplexExpression objects:
310
315
 
311
316
  Sequel.or(:col1, :col2)
312
 
  :col1 | :col2
 
317
  :col1 | :col2 # core_extensions extension
313
318
 
314
319
  Sequel.+(:column, 2)
315
 
  :column + 2
 
320
  :column + 2 # core_extensions extension
316
321
 
317
322
  Sequel.join([:column, 'b'])
318
 
  :column + 'b'
 
323
  :column + 'b' # core_extensions extension
319
324
 
320
325
=== Sequel::SQL::CaseExpression
321
326
 
339
344
  Sequel.case({2=>1}, 0, :a)
340
345
  Sequel.case({{:a=>2}=>1}, 0)
341
346
 
342
 
  {2=>1}.case(0, :a)
343
 
  {{:a=>2}=>1}.case(0)
 
347
  {2=>1}.case(0, :a) # core_extensions extension
 
348
  {{:a=>2}=>1}.case(0) # core_extensions extension
344
349
 
345
350
=== Sequel::SQL::Cast
346
351
 
360
365
  Sequel.cast(:a, String)
361
366
  Sequel.cast(:a, :int4)
362
367
 
363
 
  :a.cast(String)
364
 
  :a.cast(:int4)
 
368
  :a.cast(String) # core_extensions extension
 
369
  :a.cast(:int4) # core_extensions extension
365
370
 
366
371
=== Sequel::SQL::ColumnAll
367
372
 
372
377
 
373
378
  Sequel::SQL::ColumnAll.new(:table) # "table".*
374
379
 
375
 
The following shortcut exists for creating Sequel::SQL::Cast objects:
 
380
The following shortcut exists for creating Sequel::SQL::ColumnAll objects:
376
381
 
377
 
  :table.*
 
382
  Sequel.expr(:table).*
 
383
  :table.* # core_extensions extension
378
384
 
379
385
=== Sequel::SQL::Constant
380
386
 
389
395
 
390
396
  DB[:table].insert(:time=>Sequel::CURRENT_TIMESTAMP)
391
397
 
 
398
=== Sequel::SQL::DelayedEvaluation
 
399
 
 
400
Sequel::SQL::DelayedEvaluation objects represent an evaluation that is delayed
 
401
until query literalization.
 
402
 
 
403
  Sequel::SQL::DelayedEvaluation.new(proc{some_model.updated_at})
 
404
 
 
405
The following shortcut exists for creating Sequel::SQL::DelayedEvaluation
 
406
objects:
 
407
 
 
408
  Sequel.delay{some_model.updated_at}
 
409
 
 
410
Note how Sequel.delay requires a block, while Sequel::SQL::DelayedEvaluation.new
 
411
accepts a generic callable object.
 
412
 
 
413
Let's say you wanted a dataset for the number of objects greater than some
 
414
attribute of another object:
 
415
 
 
416
  ds = DB[:table].where{updated_at > some_model.updated_at}
 
417
 
 
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:
 
421
 
 
422
  ds = DB[:table].where{updated_at > Sequel.delay{some_model.updated_at}}
 
423
 
 
424
This will evaluate "some_model.updated_at" every time you literalize the
 
425
dataset (usually every time it is executed).
 
426
 
392
427
=== Sequel::SQL::Function
393
428
 
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:
400
435
 
401
436
  Sequel.function(:func, :a, 2)
402
 
  :func.sql_function(:a, 2)
 
437
  :func.sql_function(:a, 2) # core_extensions extension
403
438
 
404
439
=== Sequel::SQL::JoinClause
405
440
 
436
471
  Sequel.lit(':b = :v', :b=>:a, :v=>1)
437
472
  Sequel.lit(['', ' = '], :a, 1)
438
473
 
439
 
  '? = ?'.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
441
476
 
442
477
=== Sequel::SQL::OrderedExpression
443
478
 
461
496
  Sequel.asc(:a, :nulls=>:first)
462
497
  Sequel.desc(:a, :nulls=>:last)
463
498
 
464
 
  :a.asc
465
 
  :a.desc
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
468
503
 
469
504
=== Sequel::SQL::Subscript
470
505
 
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):
473
508
 
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]
476
512
 
477
513
The following shortcuts exist for creating Sequel::SQL::Subscript objects:
478
514
 
479
515
  Sequel.subscript(:a, 1)
480
516
  Sequel.subscript(:a, 1, 2)
 
517
  Sequel.subscript(:a, 1..2)
481
518
 
482
 
  :a.sql_subscript(1)
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
484
522
  
485
523
=== Sequel::SQL::VirtualRow
486
524
 
487
525
Sequel::SQL::VirtualRow is a BasicObject subclass that is the backbone behind the
488
526
block expression support:
489
527
 
490
 
  DB[:table].filter{a < 1}
 
528
  DB[:table].where{a < 1}
491
529
 
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.
493
531
 
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.