/activerecord/lib/active_record/querying.rb

https://github.com/mickeyreiss/rails · Ruby · 67 lines · 32 code · 5 blank · 30 comment · 1 complexity · df162eb4d9b03412b455d83e1c3b4423 MD5 · raw file

  1. module ActiveRecord
  2. module Querying
  3. delegate :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :to => :all
  4. delegate :first_or_create, :first_or_create!, :first_or_initialize, :to => :all
  5. delegate :find_or_create_by, :find_or_create_by!, :find_or_initialize_by, :to => :all
  6. delegate :find_by, :find_by!, :to => :all
  7. delegate :destroy, :destroy_all, :delete, :delete_all, :update, :update_all, :to => :all
  8. delegate :find_each, :find_in_batches, :to => :all
  9. delegate :select, :group, :order, :except, :reorder, :limit, :offset, :joins,
  10. :where, :preload, :eager_load, :includes, :from, :lock, :readonly,
  11. :having, :create_with, :uniq, :references, :none, :to => :all
  12. delegate :count, :average, :minimum, :maximum, :sum, :calculate, :pluck, :ids, :to => :all
  13. # Executes a custom SQL query against your database and returns all the results. The results will
  14. # be returned as an array with columns requested encapsulated as attributes of the model you call
  15. # this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in
  16. # a Product object with the attributes you specified in the SQL query.
  17. #
  18. # If you call a complicated SQL query which spans multiple tables the columns specified by the
  19. # SELECT will be attributes of the model, whether or not they are columns of the corresponding
  20. # table.
  21. #
  22. # The +sql+ parameter is a full SQL query as a string. It will be called as is, there will be
  23. # no database agnostic conversions performed. This should be a last resort because using, for example,
  24. # MySQL specific terms will lock you to using that particular database engine or require you to
  25. # change your call if you switch engines.
  26. #
  27. # # A simple SQL query spanning multiple tables
  28. # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
  29. # # => [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
  30. #
  31. # # You can use the same string replacement techniques as you can with ActiveRecord#find
  32. # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
  33. # # => [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...]
  34. def find_by_sql(sql, binds = [])
  35. logging_query_plan do
  36. result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
  37. column_types = {}
  38. if result_set.respond_to? :column_types
  39. column_types = result_set.column_types
  40. else
  41. ActiveSupport::Deprecation.warn "the object returned from `select_all` must respond to `column_types`"
  42. end
  43. result_set.map { |record| instantiate(record, column_types) }
  44. end
  45. end
  46. # Returns the result of an SQL statement that should only include a COUNT(*) in the SELECT part.
  47. # The use of this method should be restricted to complicated SQL queries that can't be executed
  48. # using the ActiveRecord::Calculations class methods. Look into those before using this.
  49. #
  50. # ==== Parameters
  51. #
  52. # * +sql+ - An SQL statement which should return a count query from the database, see the example below.
  53. #
  54. # Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
  55. def count_by_sql(sql)
  56. logging_query_plan do
  57. sql = sanitize_conditions(sql)
  58. connection.select_value(sql, "#{name} Count").to_i
  59. end
  60. end
  61. end
  62. end