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={})
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)
= columns.map { || .to_s.length }
storage = data.map { |row|
.length.times do |index|
width = row[index].to_s.length
[index] = width if width > [index]
end
row
}
= columns.map.with_index {|column, index| " " + column.to_s.upcase.ljust([index] + 1) + " "}.join("|")
puts ""
puts left_margin +
puts left_margin + "-" * .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 < [index] - width_tolerance && guide_preference ? "_" : " "
formatted_column = this_column ? this_column_value + " " : "NIL "
" " + formatted_column.ljust([index] + 1, padding) + " "
}.join("|")
end
puts ""
"Table Printed: (#{storage.length} lines)"
end
|