2009-07-21 19:21:03 -04:00
|
|
|
module ActiveRecord
|
|
|
|
class Relation
|
2009-08-18 07:27:37 -04:00
|
|
|
delegate :to_sql, :to => :relation
|
2009-12-28 05:46:21 -05:00
|
|
|
delegate :length, :collect, :map, :each, :all?, :to => :to_a
|
2009-12-27 15:03:20 -05:00
|
|
|
attr_reader :relation, :klass, :associations_to_preload, :eager_load_associations
|
2009-07-21 19:21:03 -04:00
|
|
|
|
2009-12-28 08:42:15 -05:00
|
|
|
include RelationalCalculations
|
2009-12-25 16:23:10 -05:00
|
|
|
def initialize(klass, relation, readonly = false, preload = [], eager_load = [])
|
2009-08-18 13:22:38 -04:00
|
|
|
@klass, @relation = klass, relation
|
2009-12-25 16:23:10 -05:00
|
|
|
@readonly = readonly
|
|
|
|
@associations_to_preload = preload
|
|
|
|
@eager_load_associations = eager_load
|
2009-12-26 04:37:00 -05:00
|
|
|
@loaded = false
|
2009-09-01 14:36:09 -04:00
|
|
|
end
|
|
|
|
|
2009-12-27 14:38:34 -05:00
|
|
|
def merge(r)
|
2009-12-28 03:54:52 -05:00
|
|
|
raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
|
|
|
|
|
2009-12-27 14:38:34 -05:00
|
|
|
joins(r.relation.joins(r.relation)).
|
|
|
|
group(r.send(:group_clauses).join(', ')).
|
|
|
|
order(r.send(:order_clauses).join(', ')).
|
|
|
|
where(r.send(:where_clause)).
|
|
|
|
limit(r.taken).
|
|
|
|
offset(r.skipped).
|
2009-12-27 15:03:20 -05:00
|
|
|
select(r.send(:select_clauses).join(', ')).
|
|
|
|
eager_load(r.eager_load_associations).
|
2009-12-27 16:49:31 -05:00
|
|
|
preload(r.associations_to_preload).
|
2009-12-28 15:12:53 -05:00
|
|
|
from(r.send(:sources).present? ? r.send(:from_clauses) : nil)
|
2009-12-27 14:38:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
alias :& :merge
|
|
|
|
|
2009-12-26 04:10:45 -05:00
|
|
|
def preload(*associations)
|
2009-12-26 03:56:02 -05:00
|
|
|
create_new_relation(@relation, @readonly, @associations_to_preload + Array.wrap(associations))
|
2009-10-05 14:24:08 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 04:10:45 -05:00
|
|
|
def eager_load(*associations)
|
2009-12-26 03:56:02 -05:00
|
|
|
create_new_relation(@relation, @readonly, @associations_to_preload, @eager_load_associations + Array.wrap(associations))
|
2009-08-27 19:03:46 -04:00
|
|
|
end
|
|
|
|
|
2009-12-28 03:50:54 -05:00
|
|
|
def readonly(status = true)
|
|
|
|
status.nil? ? create_new_relation : create_new_relation(@relation, status)
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def select(selects)
|
2009-12-28 03:50:54 -05:00
|
|
|
if selects.present?
|
|
|
|
frozen = @relation.joins(relation).present? ? false : @readonly
|
|
|
|
create_new_relation(@relation.project(selects), frozen)
|
|
|
|
else
|
|
|
|
create_new_relation
|
|
|
|
end
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-12-27 07:07:36 -05:00
|
|
|
def from(from)
|
2009-12-27 16:49:31 -05:00
|
|
|
from.present? ? create_new_relation(@relation.from(from)) : create_new_relation
|
2009-12-27 07:07:36 -05:00
|
|
|
end
|
|
|
|
|
2009-12-28 13:37:46 -05:00
|
|
|
def having(*args)
|
|
|
|
return create_new_relation if args.blank?
|
|
|
|
|
|
|
|
if [String, Hash, Array].include?(args.first.class)
|
|
|
|
havings = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
|
|
|
|
else
|
|
|
|
havings = args.first
|
|
|
|
end
|
|
|
|
|
|
|
|
create_new_relation(@relation.having(havings))
|
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def group(groups)
|
2009-12-27 14:38:34 -05:00
|
|
|
groups.present? ? create_new_relation(@relation.group(groups)) : create_new_relation
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def order(orders)
|
2009-12-27 14:38:34 -05:00
|
|
|
orders.present? ? create_new_relation(@relation.order(orders)) : create_new_relation
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-12-28 02:03:35 -05:00
|
|
|
def lock(locks = true)
|
|
|
|
case locks
|
|
|
|
when String
|
|
|
|
create_new_relation(@relation.lock(locks))
|
|
|
|
when TrueClass, NilClass
|
|
|
|
create_new_relation(@relation.lock)
|
|
|
|
else
|
|
|
|
create_new_relation
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 08:34:30 -05:00
|
|
|
def reverse_order
|
|
|
|
relation = create_new_relation
|
|
|
|
relation.instance_variable_set(:@orders, nil)
|
|
|
|
|
|
|
|
order_clause = @relation.send(:order_clauses).join(', ')
|
|
|
|
if order_clause.present?
|
|
|
|
relation.order(reverse_sql_order(order_clause))
|
|
|
|
else
|
|
|
|
relation.order("#{@klass.table_name}.#{@klass.primary_key} DESC")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def limit(limits)
|
2009-12-27 14:38:34 -05:00
|
|
|
limits.present? ? create_new_relation(@relation.take(limits)) : create_new_relation
|
2009-08-18 07:27:37 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def offset(offsets)
|
2009-12-27 14:38:34 -05:00
|
|
|
offsets.present? ? create_new_relation(@relation.skip(offsets)) : create_new_relation
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def on(join)
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.on(join))
|
2009-08-18 06:50:11 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 13:10:03 -04:00
|
|
|
def joins(join, join_type = nil)
|
2009-12-27 12:59:38 -05:00
|
|
|
return create_new_relation if join.blank?
|
2009-12-26 13:41:31 -05:00
|
|
|
|
|
|
|
join_relation = case join
|
|
|
|
when String
|
|
|
|
@relation.join(join)
|
|
|
|
when Hash, Array, Symbol
|
|
|
|
if @klass.send(:array_of_strings?, join)
|
|
|
|
@relation.join(join.join(' '))
|
2009-12-25 17:37:50 -05:00
|
|
|
else
|
2009-12-26 13:41:31 -05:00
|
|
|
@relation.join(@klass.send(:build_association_joins, join))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@relation.join(join, join_type)
|
2009-08-14 11:33:05 -04:00
|
|
|
end
|
2009-12-26 13:41:31 -05:00
|
|
|
|
2009-12-28 03:50:54 -05:00
|
|
|
create_new_relation(join_relation, true)
|
2009-07-31 15:08:22 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 02:27:34 -05:00
|
|
|
def where(*args)
|
2009-12-27 08:52:18 -05:00
|
|
|
return create_new_relation if args.blank?
|
|
|
|
|
2009-12-26 02:27:34 -05:00
|
|
|
if [String, Hash, Array].include?(args.first.class)
|
|
|
|
conditions = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
|
|
|
|
else
|
|
|
|
conditions = args.first
|
|
|
|
end
|
|
|
|
|
2009-12-25 17:37:50 -05:00
|
|
|
create_new_relation(@relation.where(conditions))
|
2009-07-31 15:08:22 -04:00
|
|
|
end
|
|
|
|
|
2009-08-18 15:35:33 -04:00
|
|
|
def respond_to?(method)
|
2009-11-10 14:00:50 -05:00
|
|
|
@relation.respond_to?(method) || Array.method_defined?(method) || super
|
2009-08-18 15:35:33 -04:00
|
|
|
end
|
|
|
|
|
2009-12-26 04:37:00 -05:00
|
|
|
def to_a
|
|
|
|
return @records if loaded?
|
|
|
|
|
|
|
|
@records = if @eager_load_associations.any?
|
|
|
|
catch :invalid_query do
|
|
|
|
return @klass.send(:find_with_associations, {
|
|
|
|
:select => @relation.send(:select_clauses).join(', '),
|
|
|
|
:joins => @relation.joins(relation),
|
|
|
|
:group => @relation.send(:group_clauses).join(', '),
|
|
|
|
:order => @relation.send(:order_clauses).join(', '),
|
2009-12-27 05:44:04 -05:00
|
|
|
:conditions => where_clause,
|
2009-12-26 04:37:00 -05:00
|
|
|
:limit => @relation.taken,
|
2009-12-27 06:46:20 -05:00
|
|
|
:offset => @relation.skipped,
|
2009-12-28 15:12:53 -05:00
|
|
|
:from => (@relation.send(:from_clauses) if @relation.send(:sources).present?)
|
2009-12-26 04:37:00 -05:00
|
|
|
},
|
|
|
|
ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, @eager_load_associations, nil))
|
|
|
|
end
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
@klass.find_by_sql(@relation.to_sql)
|
|
|
|
end
|
|
|
|
|
|
|
|
@associations_to_preload.each {|associations| @klass.send(:preload_associations, @records, associations) }
|
|
|
|
@records.each { |record| record.readonly! } if @readonly
|
|
|
|
|
|
|
|
@loaded = true
|
|
|
|
@records
|
|
|
|
end
|
|
|
|
|
|
|
|
alias all to_a
|
|
|
|
|
2009-12-27 05:44:04 -05:00
|
|
|
def find(*ids, &block)
|
|
|
|
return to_a.find(&block) if block_given?
|
|
|
|
|
|
|
|
expects_array = ids.first.kind_of?(Array)
|
|
|
|
return ids.first if expects_array && ids.first.empty?
|
|
|
|
|
|
|
|
ids = ids.flatten.compact.uniq
|
|
|
|
|
|
|
|
case ids.size
|
|
|
|
when 0
|
|
|
|
raise RecordNotFound, "Couldn't find #{@klass.name} without an ID"
|
|
|
|
when 1
|
|
|
|
result = find_one(ids.first)
|
|
|
|
expects_array ? [ result ] : result
|
|
|
|
else
|
|
|
|
find_some(ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 07:30:49 -05:00
|
|
|
def exists?(id = nil)
|
|
|
|
relation = select("#{@klass.quoted_table_name}.#{@klass.primary_key}").limit(1)
|
|
|
|
relation = relation.where(@klass.primary_key => id) if id
|
|
|
|
relation.first ? true : false
|
|
|
|
end
|
|
|
|
|
2009-12-26 04:37:00 -05:00
|
|
|
def first
|
|
|
|
if loaded?
|
|
|
|
@records.first
|
|
|
|
else
|
|
|
|
@first ||= limit(1).to_a[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 08:34:30 -05:00
|
|
|
def last
|
|
|
|
if loaded?
|
|
|
|
@records.last
|
|
|
|
else
|
|
|
|
@last ||= reverse_order.limit(1).to_a[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 09:05:55 -05:00
|
|
|
def destroy_all
|
|
|
|
to_a.each {|object| object.destroy}
|
|
|
|
reset
|
|
|
|
end
|
|
|
|
|
2009-12-26 04:37:00 -05:00
|
|
|
def loaded?
|
|
|
|
@loaded
|
|
|
|
end
|
|
|
|
|
2009-12-26 04:58:23 -05:00
|
|
|
def reload
|
|
|
|
@loaded = false
|
2009-12-27 09:05:55 -05:00
|
|
|
reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
@first = @last = nil
|
|
|
|
@records = []
|
2009-12-26 04:58:23 -05:00
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2009-12-27 02:45:00 -05:00
|
|
|
protected
|
2009-12-25 16:23:10 -05:00
|
|
|
|
|
|
|
def method_missing(method, *args, &block)
|
|
|
|
if @relation.respond_to?(method)
|
|
|
|
@relation.send(method, *args, &block)
|
|
|
|
elsif Array.method_defined?(method)
|
|
|
|
to_a.send(method, *args, &block)
|
2009-12-26 16:55:29 -05:00
|
|
|
elsif match = DynamicFinderMatch.match(method)
|
|
|
|
attributes = match.attribute_names
|
|
|
|
super unless @klass.send(:all_attributes_exists?, attributes)
|
|
|
|
|
|
|
|
if match.finder?
|
2009-12-27 02:45:00 -05:00
|
|
|
find_by_attributes(match, attributes, *args)
|
2009-12-27 03:58:19 -05:00
|
|
|
elsif match.instantiator?
|
|
|
|
find_or_instantiator_by_attributes(match, attributes, *args, &block)
|
2009-12-26 16:55:29 -05:00
|
|
|
end
|
2009-12-25 16:23:10 -05:00
|
|
|
else
|
|
|
|
super
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
2009-12-25 16:23:10 -05:00
|
|
|
end
|
|
|
|
|
2009-12-27 02:45:00 -05:00
|
|
|
def find_by_attributes(match, attributes, *args)
|
|
|
|
conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h}
|
|
|
|
result = where(conditions).send(match.finder)
|
|
|
|
|
|
|
|
if match.bang? && result.blank?
|
|
|
|
raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"
|
|
|
|
else
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 03:58:19 -05:00
|
|
|
def find_or_instantiator_by_attributes(match, attributes, *args)
|
|
|
|
guard_protected_attributes = false
|
|
|
|
|
|
|
|
if args[0].is_a?(Hash)
|
|
|
|
guard_protected_attributes = true
|
2009-12-27 04:16:38 -05:00
|
|
|
attributes_for_create = args[0].with_indifferent_access
|
|
|
|
conditions = attributes_for_create.slice(*attributes).symbolize_keys
|
|
|
|
else
|
|
|
|
attributes_for_create = conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h}
|
2009-12-27 03:58:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
record = where(conditions).first
|
|
|
|
|
|
|
|
unless record
|
|
|
|
record = @klass.new { |r| r.send(:attributes=, attributes_for_create, guard_protected_attributes) }
|
|
|
|
yield(record) if block_given?
|
|
|
|
record.save if match.instantiator == :create
|
|
|
|
end
|
|
|
|
|
|
|
|
record
|
|
|
|
end
|
|
|
|
|
2009-12-27 05:44:04 -05:00
|
|
|
def find_one(id)
|
|
|
|
record = where(@klass.primary_key => id).first
|
|
|
|
|
|
|
|
unless record
|
|
|
|
conditions = where_clause(', ')
|
|
|
|
conditions = " [WHERE #{conditions}]" if conditions.present?
|
|
|
|
raise RecordNotFound, "Couldn't find #{@klass.name} with ID=#{id}#{conditions}"
|
|
|
|
end
|
|
|
|
|
|
|
|
record
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_some(ids)
|
|
|
|
result = where(@klass.primary_key => ids).all
|
|
|
|
|
|
|
|
expected_size =
|
|
|
|
if @relation.taken && ids.size > @relation.taken
|
|
|
|
@relation.taken
|
|
|
|
else
|
|
|
|
ids.size
|
|
|
|
end
|
|
|
|
|
|
|
|
# 11 ids with limit 3, offset 9 should give 2 results.
|
|
|
|
if @relation.skipped && (ids.size - @relation.skipped < expected_size)
|
|
|
|
expected_size = ids.size - @relation.skipped
|
|
|
|
end
|
|
|
|
|
|
|
|
if result.size == expected_size
|
|
|
|
result
|
|
|
|
else
|
|
|
|
conditions = where_clause(', ')
|
|
|
|
conditions = " [WHERE #{conditions}]" if conditions.present?
|
|
|
|
|
|
|
|
error = "Couldn't find all #{@klass.name.pluralize} with IDs "
|
|
|
|
error << "(#{ids.join(", ")})#{conditions} (found #{result.size} results, but was looking for #{expected_size})"
|
|
|
|
raise RecordNotFound, error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-27 07:07:36 -05:00
|
|
|
def create_new_relation(relation = @relation, readonly = @readonly, preload = @associations_to_preload, eager_load = @eager_load_associations)
|
2009-12-27 16:49:31 -05:00
|
|
|
self.class.new(@klass, relation, readonly, preload, eager_load)
|
2009-12-25 16:23:10 -05:00
|
|
|
end
|
|
|
|
|
2009-12-27 05:44:04 -05:00
|
|
|
def where_clause(join_string = "\n\tAND ")
|
|
|
|
@relation.send(:where_clauses).join(join_string)
|
|
|
|
end
|
2009-12-27 08:34:30 -05:00
|
|
|
|
|
|
|
def reverse_sql_order(order_query)
|
|
|
|
order_query.to_s.split(/,/).each { |s|
|
|
|
|
if s.match(/\s(asc|ASC)$/)
|
|
|
|
s.gsub!(/\s(asc|ASC)$/, ' DESC')
|
|
|
|
elsif s.match(/\s(desc|DESC)$/)
|
|
|
|
s.gsub!(/\s(desc|DESC)$/, ' ASC')
|
|
|
|
else
|
|
|
|
s.concat(' DESC')
|
|
|
|
end
|
|
|
|
}.join(',')
|
|
|
|
end
|
|
|
|
|
2009-07-21 19:21:03 -04:00
|
|
|
end
|
|
|
|
end
|