Sha256: 7d741c9c2b29d2a7054185646d00d63d98002014cbf52fd3adf236ae21abe34d

Contents?: true

Size: 1.91 KB

Versions: 22

Compression:

Stored size: 1.91 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

22 entries across 22 versions & 2 rubygems

Version Path
activerecord-sqlserver-adapter-3.2.18 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.17 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.16 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.15 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.14 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.13 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
sic-activerecord-sqlserver-adapter-4.0.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.12 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.11 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.10 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.9 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.8 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.7 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.6 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.5 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.4 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.3 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-3.2.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb