Sha256: a23098b0b382975db0ebecf2b789719217ca4dc361ba588f231fe8093227a75a

Contents?: true

Size: 1.86 KB

Versions: 38

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

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

38 entries across 38 versions & 1 rubygems

Version Path
activerecord-sqlserver-adapter-8.0.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.2.2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.9 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.8 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.2.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.2.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.7 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.6 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.5 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.4 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.0.7 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.3 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.0 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.0.6 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.0.rc2 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.0.rc1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.0.5.1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb
activerecord-sqlserver-adapter-7.1.0.beta1 lib/active_record/connection_adapters/sqlserver/showplan/printer_table.rb