module Sequel::Model::DatasetMethods
DatasetMethods contains methods that all model datasets have.
Public Instance Methods
Source
# File lib/sequel/model/base.rb 2287 def [](*args) 2288 if args.length == 1 && (i = args[0]) && i.is_a?(Integer) 2289 with_pk(i) 2290 else 2291 super 2292 end 2293 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 2347 def as_hash(key_column=nil, value_column=nil, opts=OPTS) 2348 if key_column 2349 super 2350 else 2351 raise(Sequel::Error, "No primary key for model") unless model && (pk = model.primary_key) 2352 super(pk, value_column, opts) 2353 end 2354 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 2304 def destroy 2305 @db.transaction(:server=>opts[:server], :skip_transaction=>model.use_transactions == false) do 2306 all(&:destroy).length 2307 end 2308 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 2315 def last(*a, &block) 2316 if ds = _primary_key_order 2317 ds.last(*a, &block) 2318 else 2319 super 2320 end 2321 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 2279 def model 2280 @opts[:model] 2281 end
The model class associated with this dataset
Artist.dataset.model # => Artist
Source
# File lib/sequel/model/base.rb 2331 def paged_each(*a, &block) 2332 if ds = _primary_key_order 2333 ds.paged_each(*a, &block) 2334 else 2335 super 2336 end 2337 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 2357 def to_hash(*a) 2358 as_hash(*a) 2359 end
Alias of as_hash for backwards compatibility.
Source
# File lib/sequel/model/base.rb 2371 def with_pk(pk) 2372 if pk && (loader = _with_pk_loader) 2373 loader.first(*pk) 2374 else 2375 first(model.qualified_primary_key_hash(pk)) 2376 end 2377 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 2381 def with_pk!(pk) 2382 with_pk(pk) || raise(NoMatchingRow.new(self)) 2383 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 2389 def _force_primary_key_order 2390 cached_dataset(:_pk_order_ds){order(*unambiguous_primary_key)} 2391 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 2395 def _primary_key_order 2396 if @opts[:order].nil? && model && model.primary_key 2397 _force_primary_key_order 2398 end 2399 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 2402 def _with_pk_loader 2403 cached_placeholder_literalizer(:_with_pk_loader) do |pl| 2404 table = model.table_name 2405 cond = case primary_key = model.primary_key 2406 when Array 2407 primary_key.map{|key| [SQL::QualifiedIdentifier.new(table, key), pl.arg]} 2408 when Symbol 2409 {SQL::QualifiedIdentifier.new(table, primary_key)=>pl.arg} 2410 else 2411 raise(Error, "#{model} does not have a primary key") 2412 end 2413 2414 where(cond).limit(1) 2415 end 2416 end
A cached placeholder literalizer, if one exists for the current dataset.
Source
# File lib/sequel/model/base.rb 2427 def non_sql_option?(key) 2428 super || key == :model 2429 end
Source
# File lib/sequel/model/base.rb 2419 def unambiguous_primary_key 2420 if joined_dataset? 2421 model.qualified_primary_key 2422 else 2423 model.primary_key 2424 end 2425 end
The primary key for the dataset’s model, qualified if the dataset is joined.