class Mondrian::OLAP::Result

Constants

AXIS_SYMBOLS
AXIS_SYMBOL_TO_NUMBER
QUERY_TIMING_CUMULATIVE_REGEXP

Attributes

profiling_handler[R]
raw_cell_set[R]
total_duration[R]

Public Class Methods

java_to_ruby_value(value, column_type = nil) click to toggle source
# File lib/mondrian/olap/result.rb, line 555
def self.java_to_ruby_value(value, column_type = nil)
  case value
  when Numeric, String
    value
  when Java::JavaMath::BigDecimal
    BigDecimal(value.to_s)
  when Java::JavaSql::Clob
    clob_to_string(value)
  else
    value
  end
end
new(connection, raw_cell_set, options = {}) click to toggle source
# File lib/mondrian/olap/result.rb, line 6
def initialize(connection, raw_cell_set, options = {})
  @connection = connection
  @raw_cell_set = raw_cell_set
  @profiling_handler = options[:profiling_handler]
  @total_duration = options[:total_duration]
end

Private Class Methods

clob_to_string(value) click to toggle source
# File lib/mondrian/olap/result.rb, line 570
def self.clob_to_string(value)
  if reader = value.getCharacterStream
    buffered_reader = Java::JavaIo::BufferedReader.new(reader)
    result = []
    while str = buffered_reader.readLine
      result << str
    end
    result.join("\n")
  end
ensure
  if buffered_reader
    buffered_reader.close
  elsif reader
    reader.close
  end
end

Public Instance Methods

axes_count() click to toggle source
# File lib/mondrian/olap/result.rb, line 15
def axes_count
  axes.length
end
axis_full_names() click to toggle source
# File lib/mondrian/olap/result.rb, line 23
def axis_full_names
  @axis_full_names ||= axis_positions(:getUniqueName)
end
axis_members() click to toggle source
# File lib/mondrian/olap/result.rb, line 27
def axis_members
  @axis_members ||= axis_positions(:to_member)
end
axis_names() click to toggle source
# File lib/mondrian/olap/result.rb, line 19
def axis_names
  @axis_names ||= axis_positions(:getName)
end
drill_through(params = {}) click to toggle source

Specify drill through cell position, for example, as

:row => 0, :cell => 1

Specify max returned rows with :max_rows parameter Specify returned fields (as list of MDX levels and measures) with :return parameter Specify measures which at least one should not be empty (NULL) with :nonempty parameter

# File lib/mondrian/olap/result.rb, line 143
def drill_through(params = {})
  Error.wrap_native_exception do
    cell_params = []
    axes_count.times do |i|
      axis_symbol = AXIS_SYMBOLS[i]
      raise ArgumentError, "missing position #{axis_symbol.inspect}" unless axis_position = params[axis_symbol]
      cell_params << Java::JavaLang::Integer.new(axis_position)
    end
    raw_cell = @raw_cell_set.getCell(cell_params)
    DrillThrough.from_raw_cell(raw_cell, params.merge(role_name: @connection.role_name))
  end
end
formatted_values(*axes_sequence) click to toggle source
# File lib/mondrian/olap/result.rb, line 50
def formatted_values(*axes_sequence)
  values_using(:getFormattedValue, axes_sequence)
end
profiling_mark_full(name, duration) click to toggle source
# File lib/mondrian/olap/result.rb, line 126
def profiling_mark_full(name, duration)
  profiling_timing && profiling_timing.markFull(name, duration)
end
profiling_plan() click to toggle source
# File lib/mondrian/olap/result.rb, line 110
def profiling_plan
  if profiling_handler
    @raw_cell_set.close
    if plan = profiling_handler.plan
      plan.gsub("\r\n", "\n")
    end
  end
end
profiling_timing() click to toggle source
# File lib/mondrian/olap/result.rb, line 119
def profiling_timing
  if profiling_handler
    @raw_cell_set.close
    profiling_handler.timing
  end
end
profiling_timing_string() click to toggle source
# File lib/mondrian/olap/result.rb, line 132
def profiling_timing_string
  if profiling_timing && (timing_string = profiling_timing.toString)
    timing_string.gsub("\r\n", "\n").sub(QUERY_TIMING_CUMULATIVE_REGEXP, '')
  end
end
to_html(options = {}) click to toggle source

format results in simple HTML table

# File lib/mondrian/olap/result.rb, line 64
def to_html(options = {})
  case axes_count
  when 1
    builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |doc|
      doc.table do
        doc.tr do
          column_full_names.each do |column_full_name|
            column_full_name = column_full_name.join(',') if column_full_name.is_a?(Array)
            doc.th column_full_name, :align => 'right'
          end
        end
        doc.tr do
          (options[:formatted] ? formatted_values : values).each do |value|
            doc.td value, :align => 'right'
          end
        end
      end
    end
    builder.doc.to_html
  when 2
    builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |doc|
      doc.table do
        doc.tr do
          doc.th
          column_full_names.each do |column_full_name|
            column_full_name = column_full_name.join(',') if column_full_name.is_a?(Array)
            doc.th column_full_name, :align => 'right'
          end
        end
        (options[:formatted] ? formatted_values : values).each_with_index do |row, i|
          doc.tr do
            row_full_name = row_full_names[i].is_a?(Array) ? row_full_names[i].join(',') : row_full_names[i]
            doc.th row_full_name, :align => 'left'
            row.each do |cell|
              doc.td cell, :align => 'right'
            end
          end
        end
      end
    end
    builder.doc.to_html
  else
    raise ArgumentError, "just columns and rows axes are supported"
  end
end
values(*axes_sequence) click to toggle source
# File lib/mondrian/olap/result.rb, line 46
def values(*axes_sequence)
  values_using(:getValue, axes_sequence)
end
values_using(values_method, axes_sequence = []) click to toggle source
# File lib/mondrian/olap/result.rb, line 54
def values_using(values_method, axes_sequence = [])
  if axes_sequence.empty?
    axes_sequence = (0...axes_count).to_a.reverse
  elsif axes_sequence.size != axes_count
    raise ArgumentError, "axes sequence size is not equal to result axes count"
  end
  recursive_values(values_method, axes_sequence, 0)
end

Private Instance Methods

axes() click to toggle source
# File lib/mondrian/olap/result.rb, line 587
def axes
  @axes ||= @raw_cell_set.getAxes
end
axis_positions(map_method, join_with = false) click to toggle source
# File lib/mondrian/olap/result.rb, line 591
def axis_positions(map_method, join_with = false)
  axes.map do |axis|
    axis.getPositions.map do |position|
      names = position.getMembers.map do |member|
        if map_method == :to_member
          Member.new(member)
        else
          member.send(map_method)
        end
      end
      if names.size == 1
        names[0]
      elsif join_with
        names.join(join_with)
      else
        names
      end
    end
  end
end
recursive_values(value_method, axes_sequence, current_index, cell_params = []) click to toggle source
# File lib/mondrian/olap/result.rb, line 620
def recursive_values(value_method, axes_sequence, current_index, cell_params = [])
  if axis_number = axes_sequence[current_index]
    axis_number = AXIS_SYMBOL_TO_NUMBER[axis_number] if axis_number.is_a?(Symbol)
    positions_size = axes[axis_number].getPositions.size
    (0...positions_size).map do |i|
      cell_params[axis_number] = Java::JavaLang::Integer.new(i)
      recursive_values(value_method, axes_sequence, current_index + 1, cell_params)
    end
  else
    self.class.java_to_ruby_value(@raw_cell_set.getCell(cell_params).send(value_method))
  end
end