Sha256: a7956fe8b0f7c0b5f38f149661412bbb34408d78de7b05d3e36907f78ddf8af2

Contents?: true

Size: 1.83 KB

Versions: 13

Compression:

Stored size: 1.83 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module Sqlserver
      module Showplan
        class PrinterTable
          cattr_accessor :max_column_width, :cell_padding
          self.max_column_width = 50
          self.cell_padding = 1

          attr_reader :result

          def initialize(result)
            @result = result
          end

          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

          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

          def build_separator
            '+' + @widths.map { |w| '-' * (w + (cell_padding * 2)) }.join('+') + '+'
          end

          def build_cells(items)
            cells = []
            items.each_with_index do |item, i|
              cells << cast_item(item).ljust(@widths[i])
            end
            "| #{cells.join(' | ')} |"
          end

          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
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-4.1.8 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.0.4 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.7 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.0.3 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.6 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.0.2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.5 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.4 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.0.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.1.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-4.0.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb