module Sequel::Model::DatasetMethods
DatasetMethods contains methods that all model datasets have.
Public Instance Methods
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
# File lib/sequel/model/base.rb, line 2030 def [](*args) if args.length == 1 && (i = args[0]) && i.is_a?(Integer) with_pk(i) else super end end
This allows you to call as_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.as_hash # SELECT * FROM artists # => {1=>#<Artist {:id=>1, ...}>, # 2=>#<Artist {:id=>2, ...}>, # ...}
# File lib/sequel/model/base.rb, line 2089 def as_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
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) # ...
# File lib/sequel/model/base.rb, line 2047 def destroy pr = proc{all(&:destroy).length} model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call end
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
# File lib/sequel/model/base.rb, line 2057 def last(*a, &block) if ds = _primary_key_order ds.last(*a, &block) else super end end
The model class associated with this dataset
Artist.dataset.model # => Artist
# File lib/sequel/model/base.rb, line 2022 def model @opts[:model] end
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 # ...
# File lib/sequel/model/base.rb, line 2073 def paged_each(*a, &block) if ds = _primary_key_order ds.paged_each(*a, &block) else super end end
Alias of #as_hash for backwards compatibility.
# File lib/sequel/model/base.rb, line 2099 def to_hash(*a) as_hash(*a) end
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
# File lib/sequel/model/base.rb, line 2113 def with_pk(pk) if pk && (loader = _with_pk_loader) loader.first(*pk) else first(model.qualified_primary_key_hash(pk)) end end
Same as #with_pk, but raises NoMatchingRow instead of returning nil if no row matches.
# File lib/sequel/model/base.rb, line 2123 def with_pk!(pk) with_pk(pk) || raise(NoMatchingRow.new(self)) end
Private Instance Methods
If the dataset is not already ordered, and the model has a primary key, return a clone ordered by the primary key.
# File lib/sequel/model/base.rb, line 2131 def _primary_key_order if @opts[:order].nil? && model && (pk = model.primary_key) cached_dataset(:_pk_order_ds){order(*pk)} end end
A cached placeholder literalizer, if one exists for the current dataset.
# File lib/sequel/model/base.rb, line 2138 def _with_pk_loader cached_placeholder_literalizer(:_with_pk_loader) do |pl| table = model.table_name cond = case primary_key = model.primary_key when Array primary_key.map{|key| [SQL::QualifiedIdentifier.new(table, key), pl.arg]} when Symbol {SQL::QualifiedIdentifier.new(table, primary_key)=>pl.arg} else raise(Error, "#{model} does not have a primary key") end where(cond).limit(1) end end
# File lib/sequel/model/base.rb, line 2154 def non_sql_option?(key) super || key == :model end