class ArJdbc::MSSQL::ExplainSupport::PrinterTable

@private

Attributes

result[R]

Public Class Methods

new(result) click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 44
def initialize(result)
  @result = result
end

Public Instance Methods

pp() click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 48
def pp
  @widths = compute_column_widths
  @separator = build_separator
  pp = []
  pp << @separator
  pp << build_cells(result.columns)
  pp << @separator
  result.rows.each do |row|
    pp << build_cells(row)
  end
  pp << @separator
  pp.join("\n") << "\n"
end

Private Instance Methods

build_cells(items) click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 79
def build_cells(items)
  cells = []
  items.each_with_index do |item, i|
    cells << cast_item(item).ljust(@widths[i])
  end
  "| #{cells.join(' | ')} |"
end
build_separator() click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 75
def build_separator
  '+' << @widths.map {|w| '-' * (w + (cell_padding * 2))}.join('+') << '+'
end
cast_item(item) click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 87
def cast_item(item)
  case item
  when NilClass then 'NULL'
  when Float then item.to_s.to(9)
  else item.to_s.truncate(max_column_width)
  end
end
compute_column_widths() click to toggle source
# File lib/arjdbc/mssql/explain_support.rb, line 64
def compute_column_widths
  [].tap do |computed_widths|
    result.columns.each_with_index do |column, i|
      cells_in_column = [column] + result.rows.map { |r| cast_item(r[i]) }
      computed_width = cells_in_column.map(&:length).max
      final_width = computed_width > max_column_width ? max_column_width : computed_width
      computed_widths << final_width
    end
  end
end