Class: SQLite3::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlite_print_table.rb

Instance Method Summary collapse

Instance Method Details

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sqlite_print_table.rb', line 5

def print_table(sql_command_string, options={})
  # dependent on #prepare, #columns, #each methods from sqlite3 gem

  raise ArgumentError.new("WARNING: This method only allows SQL Queries that start with 'SELECT'") unless sql_command_string.split[0].upcase == "SELECT"
  data = self.prepare(sql_command_string)
  columns = data.columns
  columns_length = columns.length

  width_tolerance = options[:width_tolerance] || 2
  raise ArgumentError.new("WARNING: width_tolerance option must be an integer greater than 0") unless width_tolerance.is_a?(Integer) && width_tolerance > 0

  guide_tolerance = options[:guide_tolerance] || 3
  raise ArgumentError.new("WARNING: guide_tolerance option must be an integer greater than 0") unless guide_tolerance.is_a?(Integer) && guide_tolerance > 0

  raise ArgumentError.new("WARNING: guide_preference option must be a boolean") unless options[:guides] == nil || options[:guides] == false || options[:guides] == true
  guide_preference = options[:guide_preference] == false ? false : true

  raise ArgumentError.new("WARNING: margin option must be an integer greater than 0") unless options[:margin] == nil || options[:margin].is_a?(Integer) && options[:margin] > 0
  left_margin = " " * (options[:margin] || 3)

  header_count = columns.map { |header| header.to_s.length }

  storage = data.map { |row|
    header_count.length.times do |index|
      width = row[index].to_s.length
      header_count[index] = width if width > header_count[index]
    end
    row
  }

  header_line = columns.map.with_index {|column, index| " " + column.to_s.upcase.ljust(header_count[index] + 1) + " "}.join("|")

  puts ""
  puts left_margin + header_line
  puts left_margin + "-" * header_line.length

  storage.each do |row|
    puts left_margin + row.map.with_index { |this_column, index| 
      this_column_value = this_column.to_s
      padding = columns_length > guide_tolerance && this_column_value.length < header_count[index] - width_tolerance && guide_preference ? "_" : " "
      formatted_column = this_column ? this_column_value + " " : "NIL "
      " " + formatted_column.ljust(header_count[index] + 1, padding) + " "
    }.join("|")
  end
  puts ""
  "Table Printed: (#{storage.length} lines)"
end