module Sequel::Plugins::PgArrayAssociations::ClassMethods

Public Instance Methods

many_to_pg_array(name, opts=OPTS, &block) click to toggle source

Create a #many_to_pg_array association, for the case where the associated table contains the array with foreign keys pointing to the current table. See associate for options.

# File lib/sequel/plugins/pg_array_associations.rb, line 286
def many_to_pg_array(name, opts=OPTS, &block)
  associate(:many_to_pg_array, name, opts, &block)
end
pg_array_to_many(name, opts=OPTS, &block) click to toggle source

Create a #pg_array_to_many association, for the case where the current table contains the array with foreign keys pointing to the associated table. See associate for options.

# File lib/sequel/plugins/pg_array_associations.rb, line 293
def pg_array_to_many(name, opts=OPTS, &block)
  associate(:pg_array_to_many, name, opts, &block)
end

Private Instance Methods

def_many_to_pg_array(opts) click to toggle source

Setup the many_to_pg_array-specific datasets, eager loaders, and modification methods.

# File lib/sequel/plugins/pg_array_associations.rb, line 300
def def_many_to_pg_array(opts)
  name = opts[:name]
  model = self
  pk = opts[:eager_loader_key] = opts[:primary_key] ||= model.primary_key
  raise(Error, "no primary key specified for #{inspect}") unless pk
  opts[:key] = opts.default_key unless opts.has_key?(:key)
  key = opts[:key]
  key_column = opts[:key_column] ||= opts[:key]
  opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
  opts[:dataset] ||= lambda do
    opts.associated_dataset.where(Sequel.pg_array_op(opts.predicate_key).contains(Sequel.pg_array([get_column_value(pk)], opts.array_type)))
  end
  opts[:eager_loader] ||= proc do |eo|
    id_map = eo[:id_map]

    eager_load_results(opts, eo.merge(:loader=>false)) do |assoc_record|
      if pks ||= assoc_record.get_column_value(key)
        pks.each do |pkv|
          next unless objects = id_map[pkv]
          objects.each do |object| 
            object.associations[name].push(assoc_record)
          end
        end
      end
    end
  end

  join_type = opts[:graph_join_type]
  select = opts[:graph_select]
  opts[:cartesian_product_number] ||= 1

  if opts.include?(:graph_only_conditions)
    conditions = opts[:graph_only_conditions]
    graph_block = opts[:graph_block]
  else
    conditions = opts[:graph_conditions]
    conditions = nil if conditions.empty?
    graph_block = proc do |j, lj, js|
      Sequel.pg_array_op(Sequel.deep_qualify(j, key_column)).contains([Sequel.deep_qualify(lj, opts.primary_key)])
    end

    if orig_graph_block = opts[:graph_block]
      pg_array_graph_block = graph_block
      graph_block = proc do |j, lj, js|
        Sequel.&(orig_graph_block.call(j,lj,js), pg_array_graph_block.call(j, lj, js))
      end
    end
  end

  opts[:eager_grapher] ||= proc do |eo|
    ds = eo[:self]
    ds = ds.graph(eager_graph_dataset(opts, eo), conditions, eo.merge(:select=>select, :join_type=>eo[:join_type]||join_type, :qualify=>:deep, :from_self_alias=>eo[:from_self_alias]), &graph_block)
    ds
  end

  return if opts[:read_only]

  save_opts = {:validate=>opts[:validate]}
  save_opts[:raise_on_failure] = opts[:raise_on_save_failure] != false

  opts[:adder] ||= proc do |o|
    if array = o.get_column_value(key)
      array << get_column_value(pk)
    else
      o.set_column_value("#{key}=", Sequel.pg_array([get_column_value(pk)], opts.array_type))
    end
    o.save(save_opts)
  end
  
  opts[:remover] ||= proc do |o|
    if (array = o.get_column_value(key)) && !array.empty?
      array.delete(get_column_value(pk))
      o.save(save_opts)
    end
  end

  opts[:clearer] ||= proc do
    opts.associated_dataset.where(Sequel.pg_array_op(key).contains([get_column_value(pk)])).update(key=>Sequel.function(:array_remove, key, get_column_value(pk)))
  end
end
def_pg_array_to_many(opts) click to toggle source

Setup the pg_array_to_many-specific datasets, eager loaders, and modification methods.

# File lib/sequel/plugins/pg_array_associations.rb, line 382
def def_pg_array_to_many(opts)
  name = opts[:name]
  opts[:key] = opts.default_key unless opts.has_key?(:key)
  key = opts[:key]
  key_column = opts[:key_column] ||= key
  opts[:eager_loader_key] = nil
  opts[:after_load].unshift(:array_uniq!) if opts[:uniq]
  opts[:dataset] ||= lambda do
    opts.associated_dataset.where(opts.predicate_key=>get_column_value(key).to_a)
  end
  opts[:eager_loader] ||= proc do |eo|
    rows = eo[:rows]
    id_map = {}
    pkm = opts.primary_key_method

    rows.each do |object|
      if associated_pks = object.get_column_value(key)
        associated_pks.each do |apk|
          (id_map[apk] ||= []) << object
        end
      end
    end

    eager_load_results(opts, eo.merge(:id_map=>id_map)) do |assoc_record|
      if objects = id_map[assoc_record.get_column_value(pkm)]
        objects.each do |object| 
          object.associations[name].push(assoc_record)
        end
      end
    end
  end

  join_type = opts[:graph_join_type]
  select = opts[:graph_select]
  opts[:cartesian_product_number] ||= 1

  if opts.include?(:graph_only_conditions)
    conditions = opts[:graph_only_conditions]
    graph_block = opts[:graph_block]
  else
    conditions = opts[:graph_conditions]
    conditions = nil if conditions.empty?
    graph_block = proc do |j, lj, js|
      Sequel.pg_array_op(Sequel.deep_qualify(lj, key_column)).contains([Sequel.deep_qualify(j, opts.primary_key)])
    end

    if orig_graph_block = opts[:graph_block]
      pg_array_graph_block = graph_block
      graph_block = proc do |j, lj, js|
        Sequel.&(orig_graph_block.call(j,lj,js), pg_array_graph_block.call(j, lj, js))
      end
    end
  end

  opts[:eager_grapher] ||= proc do |eo|
    ds = eo[:self]
    ds = ds.graph(eager_graph_dataset(opts, eo), conditions, eo.merge(:select=>select, :join_type=>eo[:join_type]||join_type, :qualify=>:deep, :from_self_alias=>eo[:from_self_alias]), &graph_block)
    ds
  end

  return if opts[:read_only]

  save_opts = {:validate=>opts[:validate]}
  save_opts[:raise_on_failure] = opts[:raise_on_save_failure] != false

  if opts[:save_after_modify]
    save_after_modify = proc do |obj|
      obj.save(save_opts)
    end
  end

  opts[:adder] ||= proc do |o|
    opk = o.get_column_value(opts.primary_key) 
    if array = get_column_value(key)
      modified!(key)
      array << opk
    else
      set_column_value("#{key}=", Sequel.pg_array([opk], opts.array_type))
    end
    save_after_modify.call(self) if save_after_modify
  end
  
  opts[:remover] ||= proc do |o|
    if (array = get_column_value(key)) && !array.empty?
      modified!(key)
      array.delete(o.get_column_value(opts.primary_key))
      save_after_modify.call(self) if save_after_modify
    end
  end

  opts[:clearer] ||= proc do
    if (array = get_column_value(key)) && !array.empty?
      modified!(key)
      array.clear
      save_after_modify.call(self) if save_after_modify
    end
  end
end