module Sequel::Model::DatasetMethods
DatasetMethods contains methods that all model datasets have.
Public Instance Methods
Source
# File lib/sequel/model/base.rb 2193 def [](*args) 2194 if args.length == 1 && (i = args[0]) && i.is_a?(Integer) 2195 with_pk(i) 2196 else 2197 super 2198 end 2199 end
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
Source
# File lib/sequel/model/base.rb 2253 def as_hash(key_column=nil, value_column=nil, opts=OPTS) 2254 if key_column 2255 super 2256 else 2257 raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) 2258 super(pk, value_column, opts) 2259 end 2260 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, ...}>, # ...}
Source
# File lib/sequel/model/base.rb 2210 def destroy 2211 @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do 2212 all(&:destroy).length 2213 end 2214 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) # ...
Source
# File lib/sequel/model/base.rb 2221 def last(*a, &block) 2222 if ds = _primary_key_order 2223 ds.last(*a, &block) 2224 else 2225 super 2226 end 2227 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
Source
# File lib/sequel/model/base.rb 2185 def model 2186 @opts[:model] 2187 end
The model class associated with this dataset
Artist.dataset.model # => Artist
Source
# File lib/sequel/model/base.rb 2237 def paged_each(*a, &block) 2238 if ds = _primary_key_order 2239 ds.paged_each(*a, &block) 2240 else 2241 super 2242 end 2243 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 # ...
Source
# File lib/sequel/model/base.rb 2263 def to_hash(*a) 2264 as_hash(*a) 2265 end
Alias of as_hash for backwards compatibility.
Source
# File lib/sequel/model/base.rb 2277 def with_pk(pk) 2278 if pk && (loader = _with_pk_loader) 2279 loader.first(*pk) 2280 else 2281 first(model.qualified_primary_key_hash(pk)) 2282 end 2283 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
Source
# File lib/sequel/model/base.rb 2287 def with_pk!(pk) 2288 with_pk(pk) || raise(NoMatchingRow.new(self)) 2289 end
Same as with_pk, but raises NoMatchingRow instead of returning nil if no row matches.
Private Instance Methods
Source
# File lib/sequel/model/base.rb 2295 def _force_primary_key_order 2296 cached_dataset(:_pk_order_ds){order(*unambiguous_primary_key)} 2297 end
Return the dataset ordered by the model’s primary key. This should not be used if the model does not have a primary key.
Source
# File lib/sequel/model/base.rb 2301 def _primary_key_order 2302 if @opts[:order].nil? && model && model.primary_key 2303 _force_primary_key_order 2304 end 2305 end
If the dataset is not already ordered, and the model has a primary key, return a clone ordered by the primary key.
Source
# File lib/sequel/model/base.rb 2308 def _with_pk_loader 2309 cached_placeholder_literalizer(:_with_pk_loader) do |pl| 2310 table = model.table_name 2311 cond = case primary_key = model.primary_key 2312 when Array 2313 primary_key.map{|key| [SQL::QualifiedIdentifier.new(table, key), pl.arg]} 2314 when Symbol 2315 {SQL::QualifiedIdentifier.new(table, primary_key)=>pl.arg} 2316 else 2317 raise(Error, "#{model} does not have a primary key") 2318 end 2319 2320 where(cond).limit(1) 2321 end 2322 end
A cached placeholder literalizer, if one exists for the current dataset.
Source
# File lib/sequel/model/base.rb 2333 def non_sql_option?(key) 2334 super || key == :model 2335 end
Source
# File lib/sequel/model/base.rb 2325 def unambiguous_primary_key 2326 if joined_dataset? 2327 model.qualified_primary_key 2328 else 2329 model.primary_key 2330 end 2331 end
The primary key for the dataset’s model, qualified if the dataset is joined.