/activerecord/lib/active_record/querying.rb

https://github.com/bradediger/rails · Ruby · 64 lines · 29 code · 4 blank · 31 comment · 1 complexity · 52b74ab25b1501f33de6ffeaedda1170 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, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly,
  11. :having, :create_with, :uniq, :distinct, :references, :none, :unscope, to: :all
  12. delegate :count, :average, :minimum, :maximum, :sum, :calculate, to: :all
  13. delegate :pluck, :ids, to: :all
  14. # Executes a custom SQL query against your database and returns all the results. The results will
  15. # be returned as an array with columns requested encapsulated as attributes of the model you call
  16. # this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in
  17. # a +Product+ object with the attributes you specified in the SQL query.
  18. #
  19. # If you call a complicated SQL query which spans multiple tables the columns specified by the
  20. # SELECT will be attributes of the model, whether or not they are columns of the corresponding
  21. # table.
  22. #
  23. # The +sql+ parameter is a full SQL query as a string. It will be called as is, there will be
  24. # no database agnostic conversions performed. This should be a last resort because using, for example,
  25. # MySQL specific terms will lock you to using that particular database engine or require you to
  26. # change your call if you switch engines.
  27. #
  28. # # A simple SQL query spanning multiple tables
  29. # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
  30. # # => [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...]
  31. #
  32. # You can use the same string replacement techniques as you can with <tt>ActiveRecord::QueryMethods#where</tt>:
  33. #
  34. # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
  35. # Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
  36. def find_by_sql(sql, binds = [])
  37. result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds)
  38. column_types = {}
  39. if result_set.respond_to? :column_types
  40. column_types = result_set.column_types
  41. else
  42. ActiveSupport::Deprecation.warn "the object returned from `select_all` must respond to `column_types`"
  43. end
  44. result_set.map { |record| instantiate(record, column_types) }
  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. sql = sanitize_conditions(sql)
  57. connection.select_value(sql, "#{name} Count").to_i
  58. end
  59. end
  60. end