PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/asset_packager/ruby/1.8/gems/activerecord-3.0.0/lib/active_record/relation/finder_methods.rb

https://github.com/Granit/Contactbook
Ruby | 355 lines | 206 code | 29 blank | 120 comment | 20 complexity | 671af8387dbf9deef6d17056cb8ef451 MD5 | raw file
  1. require 'active_support/core_ext/object/blank'
  2. require 'active_support/core_ext/hash/indifferent_access'
  3. module ActiveRecord
  4. module FinderMethods
  5. # Find operates with four different retrieval approaches:
  6. #
  7. # * Find by id - This can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]).
  8. # If no record can be found for all of the listed ids, then RecordNotFound will be raised.
  9. # * Find first - This will return the first record matched by the options used. These options can either be specific
  10. # conditions or merely an order. If no record can be matched, +nil+ is returned. Use
  11. # <tt>Model.find(:first, *args)</tt> or its shortcut <tt>Model.first(*args)</tt>.
  12. # * Find last - This will return the last record matched by the options used. These options can either be specific
  13. # conditions or merely an order. If no record can be matched, +nil+ is returned. Use
  14. # <tt>Model.find(:last, *args)</tt> or its shortcut <tt>Model.last(*args)</tt>.
  15. # * Find all - This will return all the records matched by the options used.
  16. # If no records are found, an empty array is returned. Use
  17. # <tt>Model.find(:all, *args)</tt> or its shortcut <tt>Model.all(*args)</tt>.
  18. #
  19. # All approaches accept an options hash as their last parameter.
  20. #
  21. # ==== Parameters
  22. #
  23. # * <tt>:conditions</tt> - An SQL fragment like "administrator = 1", <tt>[ "user_name = ?", username ]</tt>,
  24. # or <tt>["user_name = :user_name", { :user_name => user_name }]</tt>. See conditions in the intro.
  25. # * <tt>:order</tt> - An SQL fragment like "created_at DESC, name".
  26. # * <tt>:group</tt> - An attribute name by which the result should be grouped. Uses the <tt>GROUP BY</tt> SQL-clause.
  27. # * <tt>:having</tt> - Combined with +:group+ this can be used to filter the records that a
  28. # <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
  29. # * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
  30. # * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5,
  31. # it would skip rows 0 through 4.
  32. # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed),
  33. # named associations in the same form used for the <tt>:include</tt> option, which will perform an
  34. # <tt>INNER JOIN</tt> on the associated table(s),
  35. # or an array containing a mixture of both strings and named associations.
  36. # If the value is a string, then the records will be returned read-only since they will
  37. # have attributes that do not correspond to the table's columns.
  38. # Pass <tt>:readonly => false</tt> to override.
  39. # * <tt>:include</tt> - Names associations that should be loaded alongside. The symbols named refer
  40. # to already defined associations. See eager loading under Associations.
  41. # * <tt>:select</tt> - By default, this is "*" as in "SELECT * FROM", but can be changed if you,
  42. # for example, want to do a join but not include the joined columns. Takes a string with the SELECT SQL fragment (e.g. "id, name").
  43. # * <tt>:from</tt> - By default, this is the table name of the class, but can be changed
  44. # to an alternate table name (or even the name of a database view).
  45. # * <tt>:readonly</tt> - Mark the returned records read-only so they cannot be saved or updated.
  46. # * <tt>:lock</tt> - An SQL fragment like "FOR UPDATE" or "LOCK IN SHARE MODE".
  47. # <tt>:lock => true</tt> gives connection's default exclusive lock, usually "FOR UPDATE".
  48. #
  49. # ==== Examples
  50. #
  51. # # find by id
  52. # Person.find(1) # returns the object for ID = 1
  53. # Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
  54. # Person.find([7, 17]) # returns an array for objects with IDs in (7, 17)
  55. # Person.find([1]) # returns an array for the object with ID = 1
  56. # Person.find(1, :conditions => "administrator = 1", :order => "created_on DESC")
  57. #
  58. # Note that returned records may not be in the same order as the ids you
  59. # provide since database rows are unordered. Give an explicit <tt>:order</tt>
  60. # to ensure the results are sorted.
  61. #
  62. # ==== Examples
  63. #
  64. # # find first
  65. # Person.find(:first) # returns the first object fetched by SELECT * FROM people
  66. # Person.find(:first, :conditions => [ "user_name = ?", user_name])
  67. # Person.find(:first, :conditions => [ "user_name = :u", { :u => user_name }])
  68. # Person.find(:first, :order => "created_on DESC", :offset => 5)
  69. #
  70. # # find last
  71. # Person.find(:last) # returns the last object fetched by SELECT * FROM people
  72. # Person.find(:last, :conditions => [ "user_name = ?", user_name])
  73. # Person.find(:last, :order => "created_on DESC", :offset => 5)
  74. #
  75. # # find all
  76. # Person.find(:all) # returns an array of objects for all the rows fetched by SELECT * FROM people
  77. # Person.find(:all, :conditions => [ "category IN (?)", categories], :limit => 50)
  78. # Person.find(:all, :conditions => { :friends => ["Bob", "Steve", "Fred"] }
  79. # Person.find(:all, :offset => 10, :limit => 10)
  80. # Person.find(:all, :include => [ :account, :friends ])
  81. # Person.find(:all, :group => "category")
  82. #
  83. # Example for find with a lock: Imagine two concurrent transactions:
  84. # each will read <tt>person.visits == 2</tt>, add 1 to it, and save, resulting
  85. # in two saves of <tt>person.visits = 3</tt>. By locking the row, the second
  86. # transaction has to wait until the first is finished; we get the
  87. # expected <tt>person.visits == 4</tt>.
  88. #
  89. # Person.transaction do
  90. # person = Person.find(1, :lock => true)
  91. # person.visits += 1
  92. # person.save!
  93. # end
  94. def find(*args)
  95. return to_a.find { |*block_args| yield(*block_args) } if block_given?
  96. options = args.extract_options!
  97. if options.present?
  98. apply_finder_options(options).find(*args)
  99. else
  100. case args.first
  101. when :first, :last, :all
  102. send(args.first)
  103. else
  104. find_with_ids(*args)
  105. end
  106. end
  107. end
  108. # A convenience wrapper for <tt>find(:first, *args)</tt>. You can pass in all the
  109. # same arguments to this method as you can to <tt>find(:first)</tt>.
  110. def first(*args)
  111. if args.any?
  112. if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
  113. to_a.first(*args)
  114. else
  115. apply_finder_options(args.first).first
  116. end
  117. else
  118. find_first
  119. end
  120. end
  121. # A convenience wrapper for <tt>find(:last, *args)</tt>. You can pass in all the
  122. # same arguments to this method as you can to <tt>find(:last)</tt>.
  123. def last(*args)
  124. if args.any?
  125. if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
  126. to_a.last(*args)
  127. else
  128. apply_finder_options(args.first).last
  129. end
  130. else
  131. find_last
  132. end
  133. end
  134. # A convenience wrapper for <tt>find(:all, *args)</tt>. You can pass in all the
  135. # same arguments to this method as you can to <tt>find(:all)</tt>.
  136. def all(*args)
  137. args.any? ? apply_finder_options(args.first).to_a : to_a
  138. end
  139. # Returns true if a record exists in the table that matches the +id+ or
  140. # conditions given, or false otherwise. The argument can take five forms:
  141. #
  142. # * Integer - Finds the record with this primary key.
  143. # * String - Finds the record with a primary key corresponding to this
  144. # string (such as <tt>'5'</tt>).
  145. # * Array - Finds the record that matches these +find+-style conditions
  146. # (such as <tt>['color = ?', 'red']</tt>).
  147. # * Hash - Finds the record that matches these +find+-style conditions
  148. # (such as <tt>{:color => 'red'}</tt>).
  149. # * No args - Returns false if the table is empty, true otherwise.
  150. #
  151. # For more information about specifying conditions as a Hash or Array,
  152. # see the Conditions section in the introduction to ActiveRecord::Base.
  153. #
  154. # Note: You can't pass in a condition as a string (like <tt>name =
  155. # 'Jamie'</tt>), since it would be sanitized and then queried against
  156. # the primary key column, like <tt>id = 'name = \'Jamie\''</tt>.
  157. #
  158. # ==== Examples
  159. # Person.exists?(5)
  160. # Person.exists?('5')
  161. # Person.exists?(:name => "David")
  162. # Person.exists?(['name LIKE ?', "%#{query}%"])
  163. # Person.exists?
  164. def exists?(id = nil)
  165. id = id.id if ActiveRecord::Base === id
  166. case id
  167. when Array, Hash
  168. where(id).exists?
  169. else
  170. relation = select(primary_key).limit(1)
  171. relation = relation.where(primary_key.eq(id)) if id
  172. relation.first ? true : false
  173. end
  174. end
  175. protected
  176. def find_with_associations
  177. including = (@eager_load_values + @includes_values).uniq
  178. join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, nil)
  179. rows = construct_relation_for_association_find(join_dependency).to_a
  180. join_dependency.instantiate(rows)
  181. rescue ThrowResult
  182. []
  183. end
  184. def construct_relation_for_association_calculations
  185. including = (@eager_load_values + @includes_values).uniq
  186. join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, including, arel.joins(arel))
  187. relation = except(:includes, :eager_load, :preload)
  188. apply_join_dependency(relation, join_dependency)
  189. end
  190. def construct_relation_for_association_find(join_dependency)
  191. relation = except(:includes, :eager_load, :preload, :select).select(column_aliases(join_dependency))
  192. apply_join_dependency(relation, join_dependency)
  193. end
  194. def apply_join_dependency(relation, join_dependency)
  195. for association in join_dependency.join_associations
  196. relation = association.join_relation(relation)
  197. end
  198. limitable_reflections = using_limitable_reflections?(join_dependency.reflections)
  199. if !limitable_reflections && relation.limit_value
  200. limited_id_condition = construct_limited_ids_condition(relation.except(:select))
  201. relation = relation.where(limited_id_condition)
  202. end
  203. relation = relation.except(:limit, :offset) unless limitable_reflections
  204. relation
  205. end
  206. def construct_limited_ids_condition(relation)
  207. orders = relation.order_values.join(", ")
  208. values = @klass.connection.distinct("#{@klass.connection.quote_table_name @klass.table_name}.#{@klass.primary_key}", orders)
  209. ids_array = relation.select(values).collect {|row| row[@klass.primary_key]}
  210. ids_array.empty? ? raise(ThrowResult) : primary_key.in(ids_array)
  211. end
  212. def find_by_attributes(match, attributes, *args)
  213. conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h}
  214. result = where(conditions).send(match.finder)
  215. if match.bang? && result.blank?
  216. raise RecordNotFound, "Couldn't find #{@klass.name} with #{conditions.to_a.collect {|p| p.join(' = ')}.join(', ')}"
  217. else
  218. result
  219. end
  220. end
  221. def find_or_instantiator_by_attributes(match, attributes, *args)
  222. protected_attributes_for_create, unprotected_attributes_for_create = {}, {}
  223. args.each_with_index do |arg, i|
  224. if arg.is_a?(Hash)
  225. protected_attributes_for_create = args[i].with_indifferent_access
  226. else
  227. unprotected_attributes_for_create[attributes[i]] = args[i]
  228. end
  229. end
  230. conditions = (protected_attributes_for_create.merge(unprotected_attributes_for_create)).slice(*attributes).symbolize_keys
  231. record = where(conditions).first
  232. unless record
  233. record = @klass.new do |r|
  234. r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty?
  235. r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty?
  236. end
  237. yield(record) if block_given?
  238. record.save if match.instantiator == :create
  239. end
  240. record
  241. end
  242. def find_with_ids(*ids)
  243. return to_a.find { |*block_args| yield(*block_args) } if block_given?
  244. expects_array = ids.first.kind_of?(Array)
  245. return ids.first if expects_array && ids.first.empty?
  246. ids = ids.flatten.compact.uniq
  247. case ids.size
  248. when 0
  249. raise RecordNotFound, "Couldn't find #{@klass.name} without an ID"
  250. when 1
  251. result = find_one(ids.first)
  252. expects_array ? [ result ] : result
  253. else
  254. find_some(ids)
  255. end
  256. end
  257. def find_one(id)
  258. id = id.id if ActiveRecord::Base === id
  259. record = where(primary_key.eq(id)).first
  260. unless record
  261. conditions = arel.wheres.map { |x| x.value }.join(', ')
  262. conditions = " [WHERE #{conditions}]" if conditions.present?
  263. raise RecordNotFound, "Couldn't find #{@klass.name} with ID=#{id}#{conditions}"
  264. end
  265. record
  266. end
  267. def find_some(ids)
  268. result = where(primary_key.in(ids)).all
  269. expected_size =
  270. if @limit_value && ids.size > @limit_value
  271. @limit_value
  272. else
  273. ids.size
  274. end
  275. # 11 ids with limit 3, offset 9 should give 2 results.
  276. if @offset_value && (ids.size - @offset_value < expected_size)
  277. expected_size = ids.size - @offset_value
  278. end
  279. if result.size == expected_size
  280. result
  281. else
  282. conditions = arel.wheres.map { |x| x.value }.join(', ')
  283. conditions = " [WHERE #{conditions}]" if conditions.present?
  284. error = "Couldn't find all #{@klass.name.pluralize} with IDs "
  285. error << "(#{ids.join(", ")})#{conditions} (found #{result.size} results, but was looking for #{expected_size})"
  286. raise RecordNotFound, error
  287. end
  288. end
  289. def find_first
  290. if loaded?
  291. @records.first
  292. else
  293. @first ||= limit(1).to_a[0]
  294. end
  295. end
  296. def find_last
  297. if loaded?
  298. @records.last
  299. else
  300. @last ||= reverse_order.limit(1).to_a[0]
  301. end
  302. end
  303. def column_aliases(join_dependency)
  304. join_dependency.joins.collect{|join| join.column_names_with_alias.collect{|column_name, aliased_name|
  305. "#{connection.quote_table_name join.aliased_table_name}.#{connection.quote_column_name column_name} AS #{aliased_name}"}}.flatten.join(", ")
  306. end
  307. def using_limitable_reflections?(reflections)
  308. reflections.none? { |r| r.collection? }
  309. end
  310. end
  311. end