simple-sql 0.5.13 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/simple/sql/connection/base.rb +7 -4
- data/lib/simple/sql/helpers/printer.rb +50 -28
- data/lib/simple/sql/version.rb +1 -1
- data/simple-sql.gemspec +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c2cf9ed788b6ea018998b2a6e97d0a6144fe223f86234812387a86eaf65d2485
|
4
|
+
data.tar.gz: d722f3cfb44f0029ec51ccd2d54dbbe609fd6036b57431dc2c2481d8496ecf50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c94eaa1fe5651b0355ff6c614bd26a51a6f33d3b5c6f6ca5d832883d570e8e2434f63dc73a5f7e138f3d6eb9eef1ab3b43b5e137af282a55a21c38b8f7a8f09
|
7
|
+
data.tar.gz: 6e25c1fbc93fd05964d6bf3aae02df55047bf67f90ff19e0fed2ccbf327cbafa0871913591c58d4bc9169069c2299f89005ebc1f3192767d694f7a3fc81d54cd
|
@@ -61,11 +61,14 @@ class Simple::SQL::Connection
|
|
61
61
|
end
|
62
62
|
|
63
63
|
# Runs a query and prints the results via "table_print"
|
64
|
-
def print(sql, *args,
|
65
|
-
|
64
|
+
def print(sql, *args, io: STDOUT, width: :auto)
|
65
|
+
if sql.is_a?(Array) && args.empty?
|
66
|
+
records = sql
|
67
|
+
else
|
68
|
+
records = all sql, *args, into: Hash
|
69
|
+
end
|
66
70
|
|
67
|
-
records
|
68
|
-
Simple::SQL::Helpers::Printer.print records
|
71
|
+
Simple::SQL::Helpers::Printer.print(records, width: width, io: io)
|
69
72
|
records
|
70
73
|
end
|
71
74
|
|
@@ -1,56 +1,78 @@
|
|
1
|
-
# rubocop:disable Metrics/AbcSize
|
1
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
2
2
|
|
3
3
|
# private
|
4
4
|
module Simple::SQL::Helpers::Printer
|
5
5
|
extend self
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
ROW_SEPARATOR = " | "
|
8
|
+
|
9
|
+
def self.print(records, io: STDOUT, width: :auto)
|
10
|
+
# check args
|
11
|
+
|
12
|
+
return if records.empty?
|
13
|
+
return if records.first.keys.empty?
|
14
|
+
|
15
|
+
if width == :auto && io.isatty
|
16
|
+
width = `tput cols`.to_i
|
12
17
|
end
|
18
|
+
width = nil if width && width <= 0
|
19
|
+
|
20
|
+
# prepare printing
|
21
|
+
|
22
|
+
rows = materialize_rows(records)
|
23
|
+
column_widths = calculate_column_widths(rows)
|
24
|
+
column_widths = optimize_column_widths(column_widths, width, rows.first.length) if width
|
25
|
+
|
26
|
+
# print
|
27
|
+
|
28
|
+
print_records(rows, io, column_widths)
|
13
29
|
end
|
14
30
|
|
15
|
-
|
16
|
-
return if records.empty?
|
31
|
+
private
|
17
32
|
|
33
|
+
def materialize_rows(records)
|
18
34
|
keys = records.first.keys
|
19
|
-
return if keys.empty?
|
20
35
|
|
21
36
|
rows = []
|
22
|
-
rows << keys
|
23
|
-
|
37
|
+
rows << keys.map(&:to_s)
|
24
38
|
records.each do |rec|
|
25
39
|
rows << rec.values_at(*keys).map(&:to_s)
|
26
40
|
end
|
41
|
+
rows
|
42
|
+
end
|
27
43
|
|
28
|
-
|
44
|
+
def calculate_column_widths(rows)
|
45
|
+
rows.inject([0] * rows.first.length) do |ary, row|
|
29
46
|
ary.zip(row.map(&:length)).map(&:max)
|
30
47
|
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def optimize_column_widths(column_widths, width, column_count)
|
51
|
+
required_width = column_widths.sum + column_count * ROW_SEPARATOR.length
|
52
|
+
overflow = required_width - width
|
53
|
+
return column_widths if overflow <= 0
|
54
|
+
|
55
|
+
# TODO: Use d'hondt with a minimum percentage for a fairer distribution
|
56
|
+
# The following is just a quick hack...
|
57
|
+
overflow += 40
|
58
|
+
|
59
|
+
column_widths.map do |col_width|
|
60
|
+
(col_width - overflow * col_width * 1.0 / required_width).to_i
|
61
|
+
end
|
62
|
+
end
|
31
63
|
|
64
|
+
def print_records(rows, io, column_widths)
|
32
65
|
rows.each_with_index do |row, idx|
|
33
|
-
parts = row.zip(
|
34
|
-
"
|
66
|
+
parts = row.zip(column_widths).map do |value, col_width|
|
67
|
+
s = "%-#{col_width}s " % value
|
68
|
+
s[0..col_width]
|
35
69
|
end
|
36
70
|
|
37
|
-
|
71
|
+
io.puts parts.join(ROW_SEPARATOR)
|
38
72
|
|
39
73
|
if idx == 0
|
40
|
-
|
74
|
+
io.puts parts.join(ROW_SEPARATOR).gsub(/[^|]/, "-")
|
41
75
|
end
|
42
76
|
end
|
43
77
|
end
|
44
|
-
|
45
|
-
def table_print?
|
46
|
-
load_table_print unless instance_variable_defined? :@table_print
|
47
|
-
@table_print
|
48
|
-
end
|
49
|
-
|
50
|
-
def load_table_print
|
51
|
-
require "table_print"
|
52
|
-
@table_print = true
|
53
|
-
rescue LoadError
|
54
|
-
@table_print = false
|
55
|
-
end
|
56
78
|
end
|
data/lib/simple/sql/version.rb
CHANGED
data/simple-sql.gemspec
CHANGED
@@ -43,7 +43,6 @@ Gem::Specification.new do |gem|
|
|
43
43
|
gem.add_development_dependency 'rubocop', '~> 0.61.1'
|
44
44
|
gem.add_development_dependency 'database_cleaner', '~> 1'
|
45
45
|
gem.add_development_dependency 'simplecov', '~> 0'
|
46
|
-
gem.add_development_dependency 'awesome_print', '~> 0'
|
47
46
|
|
48
47
|
gem.add_development_dependency 'memory_profiler', '~> 0.9.12'
|
49
48
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- radiospiel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-06-
|
12
|
+
date: 2019-06-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg_array_parser
|
@@ -165,20 +165,6 @@ dependencies:
|
|
165
165
|
- - "~>"
|
166
166
|
- !ruby/object:Gem::Version
|
167
167
|
version: '0'
|
168
|
-
- !ruby/object:Gem::Dependency
|
169
|
-
name: awesome_print
|
170
|
-
requirement: !ruby/object:Gem::Requirement
|
171
|
-
requirements:
|
172
|
-
- - "~>"
|
173
|
-
- !ruby/object:Gem::Version
|
174
|
-
version: '0'
|
175
|
-
type: :development
|
176
|
-
prerelease: false
|
177
|
-
version_requirements: !ruby/object:Gem::Requirement
|
178
|
-
requirements:
|
179
|
-
- - "~>"
|
180
|
-
- !ruby/object:Gem::Version
|
181
|
-
version: '0'
|
182
168
|
- !ruby/object:Gem::Dependency
|
183
169
|
name: memory_profiler
|
184
170
|
requirement: !ruby/object:Gem::Requirement
|