Module: Sequel::Model::DatasetMethods

Defined in:
lib/sequel/model/base.rb

Overview

DatasetMethods contains methods that all model datasets have.

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

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


2567
2568
2569
2570
2571
2572
2573
# File 'lib/sequel/model/base.rb', line 2567

def [](*args)
  if args.length == 1 && (i = args[0]) && i.is_a?(Integer)
    with_pk(i)
  else
    super
  end
end

#as_hash(key_column = nil, value_column = nil, opts = OPTS) ⇒ Object

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, ...}>,
#     ...}


2673
2674
2675
2676
2677
2678
2679
2680
# File 'lib/sequel/model/base.rb', line 2673

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

#destroyObject

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)
# ...


2584
2585
2586
2587
# File 'lib/sequel/model/base.rb', line 2584

def destroy
  pr = proc{all(&:destroy).length}
  model.use_transactions ? @db.transaction(:server=>opts[:server], &pr) : pr.call
end

#graph(table, *args, &block) ⇒ Object

Allow Sequel::Model classes to be used as dataset arguments when graphing:

Artist.graph(Album, :artist_id=>id)
# SELECT artists.id, artists.name, albums.id AS albums_id, albums.artist_id, albums.name AS albums_name
# FROM artists LEFT OUTER JOIN albums ON (albums.artist_id = artists.id)


2594
2595
2596
2597
2598
2599
2600
2601
# File 'lib/sequel/model/base.rb', line 2594

def graph(table, *args, &block)
  if table.is_a?(Class) && table < Sequel::Model
    Sequel::Deprecation.deprecate("Passing Sequel::Model class as first argument to Sequel::Dataset#graph", "Pass the model's dataset as the first argument instead")
    super(table.dataset, *args, &block)
  else
    super
  end
end

#insert_sql(*values) ⇒ Object

Handle Sequel::Model instances when inserting, using the model instance's values for the insert, unless the model instance can be used directly in SQL.

Album.insert(Album.load(:name=>'A'))
# INSERT INTO albums (name) VALUES ('A')


2609
2610
2611
2612
2613
2614
2615
2616
# File 'lib/sequel/model/base.rb', line 2609

def insert_sql(*values)
  if values.size == 1 && (v = values[0]).is_a?(Sequel::Model) && !v.respond_to?(:sql_literal_append)
    Sequel::Deprecation.deprecate("Passing Sequel::Model instance argument to Sequel::Dataset#insert", "Pass model_instance.values or model_instance.to_hash as the argument instead")
    super(v.to_hash)
  else
    super
  end
end

#join_table(type, table, *args, &block) ⇒ Object

Allow Sequel::Model classes to be used as table name arguments in dataset join methods:

Artist.join(Album, :artist_id=>id)
# SELECT * FROM artists INNER JOIN albums ON (albums.artist_id = artists.id)


2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
# File 'lib/sequel/model/base.rb', line 2623

def join_table(type, table, *args, &block)
  if table.is_a?(Class) && table < Sequel::Model
    Sequel::Deprecation.deprecate("Passing Sequel::Model class to a dataset join method", "Pass the model's table name or dataset as the first argument instead")
    if table.dataset.simple_select_all?
      super(type, table.table_name, *args, &block)
    else
      super(type, table.dataset, *args, &block)
    end
  else
    super
  end
end

#last(*a, &block) ⇒ Object

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


2641
2642
2643
2644
2645
2646
2647
# File 'lib/sequel/model/base.rb', line 2641

def last(*a, &block)
  if ds = _primary_key_order
    ds.last(*a, &block)
  else
    super
  end
end

#modelObject

The model class associated with this dataset

Artist.dataset.model # => Artist


2559
2560
2561
# File 'lib/sequel/model/base.rb', line 2559

def model
  @opts[:model]
end

#paged_each(*a, &block) ⇒ Object

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
# ...


2657
2658
2659
2660
2661
2662
2663
# File 'lib/sequel/model/base.rb', line 2657

def paged_each(*a, &block)
  if ds = _primary_key_order
    ds.paged_each(*a, &block)
  else
    super
  end
end

#to_hash(*a) ⇒ Object

Alias of as_hash for backwards compatibility.



2683
2684
2685
# File 'lib/sequel/model/base.rb', line 2683

def to_hash(*a)
  as_hash(*a)
end

#where_all(cond, &block) ⇒ Object

Return an array of all rows matching the given filter condition, also yielding each row to the given block. Basically the same as where(cond).all(&block), except it can be optimized to not create an intermediate dataset.

Artist.where_all(:id=>[1,2,3])
# SELECT * FROM artists WHERE (id IN (1, 2, 3))


2693
2694
2695
2696
2697
2698
2699
# File 'lib/sequel/model/base.rb', line 2693

def where_all(cond, &block)
  if loader = _model_where_loader
    loader.all(filter_expr(cond), &block)
  else
    where(cond).all(&block)
  end
end

#where_each(cond, &block) ⇒ Object

Iterate over all rows matching the given filter condition, yielding each row to the given block. Basically the same as where(cond).each(&block), except it can be optimized to not create an intermediate dataset.

Artist.where_each(:id=>[1,2,3]){|row| p row}
# SELECT * FROM artists WHERE (id IN (1, 2, 3))


2707
2708
2709
2710
2711
2712
2713
# File 'lib/sequel/model/base.rb', line 2707

def where_each(cond, &block)
  if loader = _model_where_loader
    loader.each(filter_expr(cond), &block)
  else
    where(cond).each(&block)
  end
end

#where_single_value(cond) ⇒ Object

Filter the datasets using the given filter condition, then return a single value. This assumes that the dataset has already been setup to limit the selection to a single column. Basically the same as where(cond).single_value, except it can be optimized to not create an intermediate dataset.

Artist.select(:name).where_single_value(:id=>1)
# SELECT name FROM artists WHERE (id = 1) LIMIT 1


2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
# File 'lib/sequel/model/base.rb', line 2722

def where_single_value(cond)
  if loader = cached_placeholder_literalizer(:_model_where_single_value_loader) do |pl|
      single_value_ds.where(pl.arg)
    end

    loader.get(filter_expr(cond))
  else
    where(cond).single_value
  end
end

#with_pk(pk) ⇒ Object

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


2743
2744
2745
2746
2747
2748
2749
# File 'lib/sequel/model/base.rb', line 2743

def with_pk(pk)
  if pk && (loader = _with_pk_loader)
    loader.first(*pk)
  else
    first(model.qualified_primary_key_hash(pk))
  end
end

#with_pk!(pk) ⇒ Object

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



2753
2754
2755
# File 'lib/sequel/model/base.rb', line 2753

def with_pk!(pk)
  with_pk(pk) || raise(NoMatchingRow.new(self))
end