Guest User

Untitled

a guest
Jun 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. # We need to handle this in Arel:
  2.  
  3. # schema_statements.rb
  4.  
  5. # SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
  6. # Both PostgreSQL and Oracle overrides this for custom DISTINCT syntax.
  7. #
  8. # distinct("posts.id", "posts.created_at desc")
  9. def distinct(columns, order_by)
  10. "DISTINCT #{columns}"
  11. end
  12.  
  13. # ORDER BY clause for the passed order option.
  14. # PostgreSQL overrides this due to its stricter standards compliance.
  15. def add_order_by_for_association_limiting!(sql, options)
  16. sql << " ORDER BY #{options[:order]}"
  17. end
  18.  
  19. # postsgresql_adapter.rb
  20. # Returns a SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.
  21. #
  22. # PostgreSQL requires the ORDER BY columns in the select list for distinct queries, and
  23. # requires that the ORDER BY include the distinct column.
  24. #
  25. # distinct("posts.id", "posts.created_at desc")
  26. def distinct(columns, order_by) #:nodoc:
  27. return "DISTINCT #{columns}" if order_by.blank?
  28.  
  29. # Construct a clean list of column names from the ORDER BY clause, removing
  30. # any ASC/DESC modifiers
  31. order_columns = order_by.split(',').collect { |s| s.split.first }
  32. order_columns.delete_if &:blank?
  33. order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" }
  34.  
  35. # Return a DISTINCT ON() clause that's distinct on the columns we want but includes
  36. # all the required columns for the ORDER BY to work properly.
  37. sql = "DISTINCT ON (#{columns}) #{columns}, "
  38. sql << order_columns * ', '
  39. end
  40.  
  41. # Returns an ORDER BY clause for the passed order option.
  42. #
  43. # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this
  44. # by wrapping the +sql+ string as a sub-select and ordering in that query.
  45. def add_order_by_for_association_limiting!(sql, options) #:nodoc:
  46. return sql if options[:order].blank?
  47.  
  48. order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?)
  49. order.map! { |s| 'DESC' if s =~ /\bdesc$/i }
  50. order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{s}" }.join(', ')
  51.  
  52. sql.replace "SELECT * FROM (#{sql}) AS id_list ORDER BY #{order}"
  53. end
  54.  
  55.  
  56. # And this:
  57.  
  58. # database_statements.rb
  59. def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
  60. "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})"
  61. end
  62.  
  63. # MysqlAdapter - mysql_adapter.rb
  64. def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
  65. where_sql
  66. end
Add Comment
Please, Sign In to add comment