Oracle needs to override the behavior of distinct for the JDBC Adaptor
----------------------------------------------------------------------

                 Key: JRUBY-1344
                 URL: http://jira.codehaus.org/browse/JRUBY-1344
             Project: JRuby
          Issue Type: Bug
          Components: ActiveRecord-JDBC
    Affects Versions: JRuby 1.0.1
            Reporter: Matthew Field
            Assignee: Thomas E Enebo


Oracle cannot sort by columns that are not included in a query when there is a 
distinct.  It needs to use the FIRST_VALUE function on the ordered columns.  
See the comment in the normal rails oracle adaptor for the distinct method.  We 
need to add something like the following.

def distinct(columns, order_by)
  return "DISTINCT #{columns}" if order_by.blank?

  # construct a valid DISTINCT clause, ie. one that includes the ORDER BY 
columns, using
  # FIRST_VALUE such that the inclusion of these columns doesn't invalidate the 
DISTINCT
  order_columns = order_by.split(',').map { |s| s.strip }.reject(&:blank?)
  order_columns = order_columns.zip((0...order_columns.size).to_a).map do |c, i|
    "FIRST_VALUE(#{c.split.first}) OVER (PARTITION BY #{columns} ORDER BY #{c}) 
AS alias_#{i}__"
  end
  sql = "DISTINCT #{columns}, "
  sql << order_columns * ", "
end

       
# ORDER BY clause for the passed order option.
#
# Uses column aliases as defined by #distinct.
def add_order_by_for_association_limiting!(sql, options)
  return sql if options[:order].blank?
  order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?)
  order.map! {|s| $1 if s =~ / (.*)/}
  order = order.zip((0...order.size).to_a).map { |s,i| "alias_#{i}__ #{s}" 
}.join(', ')
  sql << "ORDER BY #{order}"
end

 

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to