Module: Sequel::Model::DatasetMethods
- Defined in:
- lib/sequel/model/base.rb
Overview
Dataset methods are methods that the model class extends its dataset with in the call to set_dataset.
Instance Attribute Summary collapse
-
#model ⇒ Object
The model class associated with this dataset.
Instance Method Summary collapse
-
#[](*args) ⇒ Object
Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument.
-
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object.
-
#graph(table, *args, &block) ⇒ Object
Allow Sequel::Model classes to be used as dataset arguments when graphing:.
-
#insert_sql(*values) ⇒ Object
Handle Sequel::Model instances when inserting, using the model instance’s values for the insert, unless the model instance can be used directly in SQL.
-
#join_table(type, table, *args, &block) ⇒ Object
Allow Sequel::Model classes to be used as table name arguments in dataset join methods:.
-
#last(*a, &block) ⇒ Object
If there is no order already defined on this dataset, order it by the primary key and call last.
-
#paged_each(*a, &block) ⇒ Object
If there is no order already defined on this dataset, order it by the primary key and call paged_each.
-
#to_hash(key_column = nil, value_column = nil, opts = OPTS) ⇒ Object
This allows you to call
to_hashwithout any arguments, which will result in a hash with the primary key value being the key and the model object being the value. -
#with_pk(pk) ⇒ Object
Given a primary key value, return the first record in the dataset with that primary key value.
-
#with_pk!(pk) ⇒ Object
Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.
Instance Attribute Details
#model ⇒ Object
The model class associated with this dataset
Artist.dataset.model # => Artist
2372 2373 2374 |
# File 'lib/sequel/model/base.rb', line 2372 def model @model end |
Instance Method Details
#[](*args) ⇒ Object
Assume if a single integer is given that it is a lookup by primary key, and call with_pk with the argument.
Artist.dataset[1] # SELECT * FROM artists WHERE (id = 1) LIMIT 1
2378 2379 2380 2381 2382 2383 2384 |
# File 'lib/sequel/model/base.rb', line 2378 def [](*args) if args.length == 1 && (i = args.at(0)) && i.is_a?(Integer) with_pk(i) else super end end |
#destroy ⇒ Object
Destroy each row in the dataset by instantiating it and then calling destroy on the resulting model object. This isn’t as fast as deleting the dataset, which does a single SQL call, but this runs any destroy hooks on each object in the dataset.
Artist.dataset.destroy
# DELETE FROM artists WHERE (id = 1)
# DELETE FROM artists WHERE (id = 2)
# ...
2395 2396 2397 2398 |
# File 'lib/sequel/model/base.rb', line 2395 def destroy pr = proc{all(&:destroy).length} model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call end |
#graph(table, *args, &block) ⇒ Object
Allow Sequel::Model classes to be used as dataset arguments when graphing:
Artist.graph(Album, :artist_id=>id)
# SELECT artists.id, artists.name, albums.id AS albums_id, albums.artist_id, albums.name AS albums_name
# FROM artists LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)
2405 2406 2407 2408 2409 2410 2411 |
# File 'lib/sequel/model/base.rb', line 2405 def graph(table, *args, &block) if table.is_a?(Class) && table < Sequel::Model super(table.dataset, *args, &block) else super end end |
#insert_sql(*values) ⇒ Object
Handle Sequel::Model instances when inserting, using the model instance’s values for the insert, unless the model instance can be used directly in SQL.
Album.insert(Album.load(:name=>'A'))
# INSERT INTO albums (name) VALUES ('A')
2419 2420 2421 2422 2423 2424 2425 |
# File 'lib/sequel/model/base.rb', line 2419 def insert_sql(*values) if values.size == 1 && (v = values.at(0)).is_a?(Sequel::Model) && !v.respond_to?(:sql_literal_append) super(v.to_hash) else super end end |
#join_table(type, table, *args, &block) ⇒ Object
Allow Sequel::Model classes to be used as table name arguments in dataset join methods:
Artist.join(Album, :artist_id=>id)
# SELECT * FROM artists INNER JOIN albums ON (albums.artist_id = artists.id)
2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 |
# File 'lib/sequel/model/base.rb', line 2432 def join_table(type, table, *args, &block) if table.is_a?(Class) && table < Sequel::Model if table.dataset.simple_select_all? super(type, table.table_name, *args, &block) else super(type, table.dataset, *args, &block) end else super end end |
#last(*a, &block) ⇒ Object
If there is no order already defined on this dataset, order it by the primary key and call last.
Album.last
# SELECT * FROM albums ORDER BY id DESC LIMIT 1
2449 2450 2451 2452 2453 2454 2455 |
# File 'lib/sequel/model/base.rb', line 2449 def last(*a, &block) if opts[:order].nil? && model && (pk = model.primary_key) order(*pk).last(*a, &block) else super end end |
#paged_each(*a, &block) ⇒ Object
If there is no order already defined on this dataset, order it by the primary key and call paged_each.
Album.paged_each{|row| }
# SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 0
# SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 1000
# SELECT * FROM albums ORDER BY id LIMIT 1000 OFFSET 2000
# ...
2465 2466 2467 2468 2469 2470 2471 |
# File 'lib/sequel/model/base.rb', line 2465 def paged_each(*a, &block) if opts[:order].nil? && model && (pk = model.primary_key) order(*pk).paged_each(*a, &block) else super end end |
#to_hash(key_column = nil, value_column = nil, opts = OPTS) ⇒ Object
This allows you to call to_hash without any arguments, which will result in a hash with the primary key value being the key and the model object being the value.
Artist.dataset.to_hash # SELECT * FROM artists
# => {1=>#<Artist {:id=>1, ...}>,
# 2=>#<Artist {:id=>2, ...}>,
# ...}
2481 2482 2483 2484 2485 2486 2487 2488 |
# File 'lib/sequel/model/base.rb', line 2481 def to_hash(key_column=nil, value_column=nil, opts=OPTS) if key_column super else raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) super(pk, value_column, opts) end end |
#with_pk(pk) ⇒ Object
Given a primary key value, return the first record in the dataset with that primary key value. If no records matches, returns nil.
# Single primary key
Artist.dataset.with_pk(1)
# SELECT * FROM artists WHERE (artists.id = 1) LIMIT 1
# Composite primary key
Artist.dataset.with_pk([1, 2])
# SELECT * FROM artists WHERE ((artists.id1 = 1) AND (artists.id2 = 2)) LIMIT 1
2500 2501 2502 |
# File 'lib/sequel/model/base.rb', line 2500 def with_pk(pk) first(model.qualified_primary_key_hash(pk)) end |
#with_pk!(pk) ⇒ Object
Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.
2506 2507 2508 |
# File 'lib/sequel/model/base.rb', line 2506 def with_pk!(pk) with_pk(pk) || raise(NoMatchingRow.new(self)) end |