slyce 0.9.2 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/slyce +14 -10
  3. data/bin/slyce3 +114 -0
  4. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73484428602b9a69afae319a88b9b0045cb13b3d17e3f24a9532b73a35c53a28
4
- data.tar.gz: 243242d2a638ad262b6e34c0b7c5d3541eac62048ed24a81d9c1d93a6d91b55c
3
+ metadata.gz: 81d5ddf505bb1b8f75770447e2c6015a3cf2c1ce2003b543cbce2c3364b8c9cf
4
+ data.tar.gz: 2b4ae78ed3f654619e9b2d01e1d50a2cdc561f0e541416cb8739400fa8980e1a
5
5
  SHA512:
6
- metadata.gz: 1f636b19580ca0797aa86325d5b0d24e23caa9c6e92d3a35716882a1d0c69b9708fbb5554c982d8f1f096801248e2ce9f79562b11b46eac50b76fd93747fdd69
7
- data.tar.gz: eb0ffa3c6b948a0d0d6c0bb05b16881f4c628eebbf9de63c5bc052df80e0b661a91cb195a1ff1449afcf004a006b19e76e43a0ee279e4993615fd45d2d48bae3
6
+ metadata.gz: 90657cc538cfaff2540321d9fe3862593d1136820ca02a5c604d5994c495f5d91164554a1de2b142b131dca37309423cda926ed4abb12a6a941d93a3ff6c2f7c
7
+ data.tar.gz: 59eb8d3217d38f71972dff57fcf331746a866c8fa5c5cda60d69eeb96de7bb778aa8ac0e3b6d013f6d59e0bbcebfe03191423a244fc97654df7359f6e0665291
data/bin/slyce CHANGED
@@ -1,8 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- STDOUT.sync = true
3
+ VERSION="0.9.5"
4
4
 
5
- VERSION="0.9.2"
5
+ STDOUT.sync = true
6
6
 
7
7
  require "mysql2"
8
8
  require "optparse"
@@ -27,19 +27,23 @@ OptionParser.new.instance_eval do
27
27
  end.parse!(into: opts={}) rescue abort($!.message)
28
28
 
29
29
  abcd = opts[:alpha]
30
+ filt = opts[:where] and filt = "where\n #{filt}"
30
31
  show = opts[:show]
31
32
  want = opts[:extract].to_s.downcase.split(",")
32
- filt = opts[:where] and filt = "where\n #{filt}"
33
33
 
34
- dbas ||= ARGV.shift or Kernel.abort "no database given"
35
- tabl ||= ARGV.shift or Kernel.abort "no table given"
34
+ dbas ||= ARGV.shift or abort "no database given"
35
+ tabl ||= ARGV.shift or abort "no table given"
36
36
 
37
37
  # ==[ Helpers ]==
38
38
 
39
39
  class Mysql2::Client
40
- def query!(stmt, *args, **, &)
40
+ def sql(...)
41
+ query(...)
42
+ end
43
+
44
+ def sql!(stmt, *args, **, &)
41
45
  puts "\n==[ SQL statement ]==\n\n", stmt.strip, ";"
42
- query(stmt, *args, **, &)
46
+ sql(stmt, *args, **, &)
43
47
  end
44
48
  end
45
49
 
@@ -72,7 +76,7 @@ end
72
76
  want.each do |name|
73
77
  sort = abcd ? "" : "cnt desc,"
74
78
  stmt = show ? "limit #{show}" : ""
75
- data = conn.query(<<~"" + stmt).to_a
79
+ data = conn.sql(<<~"" + stmt).to_a
76
80
  select
77
81
  count(*) as cnt,
78
82
  `#{name}` as val
@@ -87,14 +91,14 @@ want.each do |name|
87
91
  -if(regexp_like(`#{name}`, '^\\\\d'), regexp_instr(`#{name}`, '[^\\\\d]'), null) desc,
88
92
  `#{name}` is null, `#{name}`
89
93
 
90
- uniq = conn.query(<<~"").to_a[0][0]
94
+ uniq = conn.sql(<<~"").to_a[0][0]
91
95
  select
92
96
  count(distinct(ifnull(`#{name}`,0)))
93
97
  from
94
98
  `#{tabl}`
95
99
  #{filt}
96
100
 
97
- tots = conn.query(<<~"").to_a[0][0]
101
+ tots = conn.sql(<<~"").to_a[0][0]
98
102
  select
99
103
  count(ifnull(`#{name}`,0))
100
104
  from
data/bin/slyce3 ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # NOTE: Requires the 'regexp' sqlite3 extension from https://github.com/nalgeon/sqlean
4
+
5
+ VERSION="0.9.5"
6
+
7
+ STDOUT.sync = true
8
+
9
+ require "extralite"
10
+ require "optparse"
11
+
12
+ trap("INT" ) { abort "\n" }
13
+
14
+ dbas = nil
15
+ tabl = nil
16
+
17
+ OptionParser.new.instance_eval do
18
+ @banner = "usage: #{program_name} [options] <database> <table>"
19
+
20
+ on "-a", "--alpha" , "Sort alphabetically, not numerically"
21
+ on "-c", "--columns" , "Display column names and quit"
22
+ on "-h", "--help" , "Show help and command usage" do Kernel.abort to_s; end
23
+ on "-r", "--regexp <path>" , "Path to the sqlean/regexp extension"
24
+ on "-s", "--show <count>" , "Show this many values", Integer
25
+ on "-v", "--version" , "Show version number" do Kernel.abort "#{program_name} #{VERSION}"; end
26
+ on "-w", "--where <cond>" , "Filter rows (eg - 'age=33,Name=/[BW]ill/i,color=\"\"'"
27
+ on "-x", "--extract <col1,col2,...>", "Comma separated list of columns to extract"
28
+
29
+ self
30
+ end.parse!(into: opts={regexp: "regexp"}) rescue abort($!.message)
31
+
32
+ abcd = opts[:alpha]
33
+ filt = opts[:where] and filt = "where\n #{filt}"
34
+ regx = opts[:regexp]; File.exist?(regx) or abort "no regexp extension found at '#{regx}'"
35
+ show = opts[:show]
36
+ want = opts[:extract].to_s.downcase.split(",")
37
+
38
+ dbas ||= ARGV.shift or abort "no database given"
39
+ tabl ||= ARGV.shift or abort "no table given"
40
+
41
+ # ==[ Helpers ]==
42
+
43
+ class Extralite::Database
44
+ def sql(...)
45
+ query_ary(...)
46
+ end
47
+
48
+ def sql!(stmt, *args, **, &)
49
+ puts "\n==[ SQL statement ]==\n\n", stmt.strip, ";"
50
+ sql(stmt, *args, **, &)
51
+ end
52
+ end
53
+
54
+ def display(name, data, show, uniq, tots)
55
+ seen = data.inject(0) {|seen, coun| seen += coun[0] }
56
+ rows = [data.size, seen].min
57
+ wide = tots.to_s.size
58
+ fill = " " * wide
59
+ line = "=" * name.size
60
+
61
+ puts "\n#{fill} #{name}\n#{fill} #{line}\n"
62
+ data.each {|cnt, val| puts "%*d %s" % [wide, cnt, val || "NULL"] }
63
+ puts "#{fill} -----\n"
64
+ puts "%*d shown (top %d)" % [wide, rows, rows] if show
65
+ puts "%*d total (all %d)" % [wide, tots, uniq]
66
+ end
67
+
68
+ # ==[ Let 'er rip! ]==
69
+
70
+ conn = Extralite::Database.new(dbas)
71
+ resu = conn.load_extension(regx)
72
+ cols = conn.columns("select * from `#{tabl}` limit 0").map(&:to_s)
73
+ want = want.empty? ? cols : want & cols
74
+
75
+ if opts[:columns]
76
+ puts cols
77
+ exit
78
+ end
79
+
80
+ want.each do |name|
81
+ sort = abcd ? "" : "cnt desc,"
82
+ stmt = show ? "limit #{show}" : ""
83
+ data = conn.sql(<<~"" + stmt).to_a
84
+ select
85
+ count(*) as cnt,
86
+ `#{name}` as val
87
+ from
88
+ `#{tabl}`
89
+ #{filt}
90
+ group by
91
+ val
92
+ order by #{sort}
93
+ -iif(regexp_like(`#{name}`, '^[-+]?((0|([1-9]\\d*)(\\.\\d*)?)|((0|([1-9]\\d*))\\.\\d+))$'), `#{name}` + 0, null) desc,
94
+ -iif(regexp_like(`#{name}`, '^0\\d+$'), length(`#{name}`), null) desc,
95
+ -iif(regexp_like(`#{name}`, '^\\d'), length(regexp_substr(`#{name}`, '^\\d+')), null) desc,
96
+ `#{name}` is null, `#{name}`
97
+ collate nocase
98
+
99
+ uniq = conn.sql(<<~"").to_a[0][0]
100
+ select
101
+ count(distinct(ifnull(`#{name}`,0)))
102
+ from
103
+ `#{tabl}`
104
+ #{filt}
105
+
106
+ tots = conn.sql(<<~"").to_a[0][0]
107
+ select
108
+ count(ifnull(`#{name}`,0))
109
+ from
110
+ `#{tabl}`
111
+ #{filt}
112
+
113
+ display(name, data, show, uniq, tots)
114
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slyce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
@@ -28,6 +28,7 @@ description: Ruby utility to show data statistics for MySQL databases
28
28
  email: steve.shreeve@gmail.com
29
29
  executables:
30
30
  - slyce
31
+ - slyce3
31
32
  extensions: []
32
33
  extra_rdoc_files: []
33
34
  files:
@@ -35,6 +36,7 @@ files:
35
36
  - LICENSE
36
37
  - README.md
37
38
  - bin/slyce
39
+ - bin/slyce3
38
40
  - slyce.gemspec
39
41
  homepage: https://github.com/shreeve/slyce
40
42
  licenses: