If a prepared statement can be used to load the associated objects, execute it to retrieve them. Otherwise, fall back to the default implementation.
# File lib/sequel/plugins/prepared_statements_associations.rb, line 73 def _load_associated_objects(opts, dynamic_opts={}) if !opts.can_have_associated_objects?(self) || dynamic_opts[:callback] || (ps = opts[:prepared_statement]) == false super else if bv = association_bound_variables(opts) (ps || association_prepared_statement(opts)).call(bv) else super end end end
Return a bound variable hash that maps the keys in ks
(qualified by the table
) to the values of the results of
sending the methods in vs
.
# File lib/sequel/plugins/prepared_statements_associations.rb, line 41 def association_bound_variable_hash(table, ks, vs) Hash[*ks.zip(vs).map{|k, v| [:"#{table}.#{k}", send(v)]}.flatten] end
Given an association reflection, return a bound variable hash for the given association for this instance's values.
# File lib/sequel/plugins/prepared_statements_associations.rb, line 47 def association_bound_variables(opts) case opts[:type] when :many_to_one association_bound_variable_hash(opts.associated_class.table_name, opts.primary_keys, opts[:keys]) when :one_to_many association_bound_variable_hash(opts.associated_class.table_name, opts[:keys], opts[:primary_keys]) when :many_to_many association_bound_variable_hash(opts.join_table_alias, opts[:left_keys], opts[:left_primary_keys]) when :many_through_many association_bound_variable_hash(opts.final_reverse_edge[:alias], Array(opts[:left_key]), opts[:left_primary_keys]) end end
Given an association reflection, return and cache a prepared statement for this association such that, given appropriate bound variables, the prepared statement will work correctly for any instance.
# File lib/sequel/plugins/prepared_statements_associations.rb, line 63 def association_prepared_statement(opts) opts.send(:cached_fetch, :prepared_statement) do ps = _associated_dataset(opts, {}).unbind.first.prepare(opts.returns_array? ? :select : :first, :"smpsap_#{NEXT.call}") ps.log_sql = true ps end end