module Sequel::Model::DatasetMethods

DatasetMethods contains methods that all model datasets have.

Public Instance Methods

[](*args) click to toggle source

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
Calls superclass method
     # File lib/sequel/model/base.rb
2178 def [](*args)
2179   if args.length == 1 && (i = args[0]) && i.is_a?(Integer)
2180     with_pk(i)
2181   else
2182     super
2183   end
2184 end
as_hash(key_column=nil, value_column=nil, opts=OPTS) click to toggle source

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, ...}>,
#     ...}
Calls superclass method
     # File lib/sequel/model/base.rb
2238 def as_hash(key_column=nil, value_column=nil, opts=OPTS)
2239   if key_column
2240     super
2241   else
2242     raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key)
2243     super(pk, value_column, opts) 
2244   end
2245 end
destroy() click to toggle source

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
2195 def destroy
2196   @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do
2197     all(&:destroy).length
2198   end
2199 end
last(*a, &block) click to toggle source

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
Calls superclass method
     # File lib/sequel/model/base.rb
2206 def last(*a, &block)
2207   if ds = _primary_key_order
2208     ds.last(*a, &block)
2209   else
2210     super
2211   end
2212 end
model() click to toggle source

The model class associated with this dataset

Artist.dataset.model # => Artist
     # File lib/sequel/model/base.rb
2170 def model
2171   @opts[:model]
2172 end
paged_each(*a, &block) click to toggle source

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
# ...
Calls superclass method
     # File lib/sequel/model/base.rb
2222 def paged_each(*a, &block)
2223   if ds = _primary_key_order
2224     ds.paged_each(*a, &block)
2225   else
2226     super
2227   end
2228 end
to_hash(*a) click to toggle source

Alias of as_hash for backwards compatibility.

     # File lib/sequel/model/base.rb
2248 def to_hash(*a)
2249   as_hash(*a)
2250 end
with_pk(pk) click to toggle source

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
2262 def with_pk(pk)
2263   if pk && (loader = _with_pk_loader)
2264     loader.first(*pk)
2265   else
2266     first(model.qualified_primary_key_hash(pk))
2267   end
2268 end
with_pk!(pk) click to toggle source

Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.

     # File lib/sequel/model/base.rb
2272 def with_pk!(pk)
2273   with_pk(pk) || raise(NoMatchingRow.new(self))
2274 end

Private Instance Methods

_force_primary_key_order() click to toggle source

Return the dataset ordered by the model’s primary key. This should not be used if the model does not have a primary key.

     # File lib/sequel/model/base.rb
2280 def _force_primary_key_order
2281   cached_dataset(:_pk_order_ds){order(*unambiguous_primary_key)}
2282 end
_primary_key_order() click to toggle source

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
2286 def _primary_key_order
2287   if @opts[:order].nil? && model && model.primary_key
2288     _force_primary_key_order
2289   end
2290 end
_with_pk_loader() click to toggle source

A cached placeholder literalizer, if one exists for the current dataset.

     # File lib/sequel/model/base.rb
2293 def _with_pk_loader
2294   cached_placeholder_literalizer(:_with_pk_loader) do |pl|
2295     table = model.table_name
2296     cond = case primary_key = model.primary_key
2297     when Array
2298       primary_key.map{|key| [SQL::QualifiedIdentifier.new(table, key), pl.arg]}
2299     when Symbol
2300       {SQL::QualifiedIdentifier.new(table, primary_key)=>pl.arg}
2301     else
2302       raise(Error, "#{model} does not have a primary key")
2303     end
2304 
2305     where(cond).limit(1)
2306   end
2307 end
non_sql_option?(key) click to toggle source
Calls superclass method
     # File lib/sequel/model/base.rb
2318 def non_sql_option?(key)
2319   super || key == :model
2320 end
unambiguous_primary_key() click to toggle source

The primary key for the dataset’s model, qualified if the dataset is joined.

     # File lib/sequel/model/base.rb
2310 def unambiguous_primary_key
2311   if joined_dataset?
2312     model.qualified_primary_key
2313   else
2314     model.primary_key
2315   end
2316 end