diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-13 07:43:35 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2007-01-13 07:43:35 +0000 |
commit | 937eeab103a14bb4db61ec1841ad57e01636bf21 (patch) | |
tree | 79868e71896f763f7df5f84ba1f10dc4d5c5dc11 /activerecord/lib/active_record/connection_adapters | |
parent | 7b07baac27120edb22a9b9f80c53a3b22d292b46 (diff) | |
download | rails-937eeab103a14bb4db61ec1841ad57e01636bf21.tar.gz rails-937eeab103a14bb4db61ec1841ad57e01636bf21.tar.bz2 rails-937eeab103a14bb4db61ec1841ad57e01636bf21.zip |
Oracle: correctly perform eager finds with :limit and :order. Closes #7021.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5919 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/oracle_adapter.rb | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb b/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb index def2a69256..53cf2a439a 100644 --- a/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb @@ -459,22 +459,29 @@ begin def distinct(columns, order_by) return "DISTINCT #{columns}" if order_by.blank? - # construct a clean list of column names from the ORDER BY clause, removing - # any asc/desc modifiers - order_columns = order_by.split(',').collect! { |s| s.split.first } - order_columns.delete_if &:blank? - - # simplify the ORDER BY to just use positional syntax, to avoid the complexity of - # having to create valid column aliases for the FIRST_VALUE columns - order_by.replace(((offset=columns.count(',')+2) .. offset+order_by.count(',')).to_a * ", ") - # 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.map! { |c| "FIRST_VALUE(#{c}) OVER (PARTITION BY #{columns} ORDER BY #{c})" } + 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 + private def select(sql, name = nil) |