implement own compare method

pull/6827/head
Konstantin Haase 14 years ago
parent 2850f1bd72
commit 182a4c111a
  1. 11
      lib/report/query_utils.rb
  2. 3
      lib/report/result.rb
  3. 2
      lib/report/table.rb

@ -1,4 +1,6 @@
module Report::QueryUtils module Report::QueryUtils
Infinity = 1.0/0
alias singleton_class metaclass unless respond_to? :singleton_class alias singleton_class metaclass unless respond_to? :singleton_class
delegate :quoted_false, :quoted_true, :to => "engine.reporting_connection" delegate :quoted_false, :quoted_true, :to => "engine.reporting_connection"
@ -217,6 +219,15 @@ module Report::QueryUtils
Report::QueryUtils.cache Report::QueryUtils.cache
end end
def compare(first, second)
first = Array(first).flatten
second = Array(second).flatten
first.zip second do |a, b|
return (a <=> b) || (a == Infinity ? 1 : -1) if a != b
end
second.size > first.size ? -1 : 0
end
def mysql? def mysql?
[:mysql, :mysql2].include? adapter_name.to_s.downcase.to_sym [:mysql, :mysql2].include? adapter_name.to_s.downcase.to_sym
end end

@ -106,7 +106,6 @@ class Report::Result
def set_key(index = []) def set_key(index = [])
self.key = index.map { |k| map_field(k, fields[k]) } self.key = index.map { |k| map_field(k, fields[k]) }
end end
end end
class DirectResult < Base class DirectResult < Base
@ -155,7 +154,7 @@ class Report::Result
def sort!(force = false) def sort!(force = false)
return false if @sorted and not force return false if @sorted and not force
values.sort! { |a,b| a.key <=> b.key } values.sort! { |a,b| compare a.key, b.key }
values.each { |e| e.sort! force } values.each { |e| e.sort! force }
@sorted = true @sorted = true
end end

@ -78,7 +78,7 @@ class Report::Table
@indexes ||= begin @indexes ||= begin
indexes = Hash.new { |h,k| h[k] = Set.new } indexes = Hash.new { |h,k| h[k] = Set.new }
query.each_direct_result { |result| [:row, :column].each { |t| indexes[t] << fields_from(result, t) } } query.each_direct_result { |result| [:row, :column].each { |t| indexes[t] << fields_from(result, t) } }
indexes.keys.each { |k| indexes[k] = indexes[k].sort { |x, y| x <=> y } } indexes.keys.each { |k| indexes[k] = indexes[k].sort { |x, y| compare x, y } }
indexes indexes
end end
@indexes[type] @indexes[type]

Loading…
Cancel
Save