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 308 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 315 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 322 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] if opts[:uniq] opts[:after_load] ||= [] opts[:after_load].unshift(:array_uniq!) end 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] eo = Hash[eo] eo[:loader] = false eager_load_results(opts, eo) do |assoc_record| if pks = assoc_record.get_column_value(key) pks.each do |pkv| id_map[pkv].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), &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 unless opts.has_key?(:adder) 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 end unless opts.has_key?(:remover) 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 end unless opts.has_key?(:clearer) opts[:clearer] = proc do pk_value = get_column_value(pk) db_type = opts.array_type opts.associated_dataset.where(Sequel.pg_array_op(key).contains(Sequel.pg_array([pk_value], db_type))).update(key=>Sequel.function(:array_remove, key, Sequel.cast(pk_value, db_type))) end 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 416 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 if opts[:uniq] opts[:after_load] ||= [] opts[:after_load].unshift(:array_uniq!) end 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 Sequel.synchronize_with(eo[:mutex]) do rows.each do |object| if associated_pks = object.get_column_value(key) associated_pks.each do |apk| (id_map[apk] ||= []) << object end end end end eo = Hash[eo] eo[:id_map] = id_map eager_load_results(opts, eo) 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), &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 unless opts.has_key?(:adder) 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 end unless opts.has_key?(:remover) 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 end unless opts.has_key?(:clearer) 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 end