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 2108 def [](*args) 2109 if args.length == 1 && (i = args[0]) && i.is_a?(Integer) 2110 with_pk(i) 2111 else 2112 super 2113 end 2114 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 2167 def as_hash(key_column=nil, value_column=nil, opts=OPTS) 2168 if key_column 2169 super 2170 else 2171 raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) 2172 super(pk, value_column, opts) 2173 end 2174 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 2125 def destroy 2126 pr = proc{all(&:destroy).length} 2127 model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call 2128 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 2135 def last(*a, &block) 2136 if ds = _primary_key_order 2137 ds.last(*a, &block) 2138 else 2139 super 2140 end 2141 end
The model class associated with this dataset
Artist.dataset.model # => Artist
# File lib/sequel/model/base.rb 2100 def model 2101 @opts[:model] 2102 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 2151 def paged_each(*a, &block) 2152 if ds = _primary_key_order 2153 ds.paged_each(*a, &block) 2154 else 2155 super 2156 end 2157 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/model/base.rb 2177 def to_hash(*a) 2178 as_hash(*a) 2179 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 2191 def with_pk(pk) 2192 if pk && (loader = _with_pk_loader) 2193 loader.first(*pk) 2194 else 2195 first(model.qualified_primary_key_hash(pk)) 2196 end 2197 end
Same as with_pk
, but raises NoMatchingRow
instead of returning nil if no row matches.
# File lib/sequel/model/base.rb 2201 def with_pk!(pk) 2202 with_pk(pk) || raise(NoMatchingRow.new(self)) 2203 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 2209 def _primary_key_order 2210 if @opts[:order].nil? && model && (pk = model.primary_key) 2211 cached_dataset(:_pk_order_ds){order(*pk)} 2212 end 2213 end
A cached placeholder literalizer, if one exists for the current dataset.
# File lib/sequel/model/base.rb 2216 def _with_pk_loader 2217 cached_placeholder_literalizer(:_with_pk_loader) do |pl| 2218 table = model.table_name 2219 cond = case primary_key = model.primary_key 2220 when Array 2221 primary_key.map{|key| [SQL::QualifiedIdentifier.new(table, key), pl.arg]} 2222 when Symbol 2223 {SQL::QualifiedIdentifier.new(table, primary_key)=>pl.arg} 2224 else 2225 raise(Error, "#{model} does not have a primary key") 2226 end 2227 2228 where(cond).limit(1) 2229 end 2230 end
# File lib/sequel/model/base.rb 2232 def non_sql_option?(key) 2233 super || key == :model 2234 end