slyce 0.0.1 → 0.5.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/bin/slyce +97 -0
  4. data/slyce.gemspec +15 -0
  5. metadata +6 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92e960044291870d8bfeea13c27e582055aa20a4dd3b39b1503dac850b289c06
4
- data.tar.gz: de623540d7d8629966198b5fe20ff5742c0e850efc99644363954f4d8b9415c7
3
+ metadata.gz: 2c731b8535e6d17c332c7e9e72934343d11e8be440f99fb8476dd6c0083c8676
4
+ data.tar.gz: c65100a6813cfa59aaf2711d26c2ac2280ba803232fb04675541aebda58de8db
5
5
  SHA512:
6
- metadata.gz: f1fe6b3108944d002323ec744bfe46570484d4640e30f8f268b2c9b249e3bfcc1c2ad75f70ef6116b87989228ff3d53edc1c928031cb9f0200ae34434001e0d6
7
- data.tar.gz: 33867ad0dea4a0b41b22830dcf25199d8e29a4049c02d6a2f91547058c51a6bebc62bf88386755252fb0985bf444aceedeadbaa50f82492dcc760cbe50160797
6
+ metadata.gz: aa404be4b51dd23be004502356d98f045248f017bf4cb2a86196a60c6d7b6268327f2559cff0be5e1b1e312c5982319edc84d48695600c724b9804e3460247ec
7
+ data.tar.gz: 5bd8fbc834b2668e8f333092fa825b525015482fa07820b6045949b4a1b2dbeb4fe8049077a1cc569f6994f70ff1cabeea4381e7a78ab93d25a8e26830c0e57a
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/bin/slyce ADDED
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ STDOUT.sync = true
4
+
5
+ VERSION="0.5.1"
6
+
7
+ require "bundler/setup"
8
+ require "mysql2"
9
+ require "optparse"
10
+
11
+ trap("INT" ) { abort "\n" }
12
+
13
+ dbas = nil
14
+ tabl = nil
15
+
16
+ OptionParser.new.instance_eval do
17
+ @banner = "usage: #{program_name} [options] <database> <table>"
18
+
19
+ on "-c", "--columns" , "Display column names and quit"
20
+ on "-h", "--help" , "Show help and command usage" do Kernel.abort to_s; end
21
+ on "-s", "--show <count>" , "Show this many values", Integer
22
+ on "-v", "--version" , "Show version number" do Kernel.abort "#{program_name} #{VERSION}"; end
23
+ on "-x", "--extract <col1,col2,...>", "Comma separated list of columns to extract"
24
+
25
+ self
26
+ end.parse!(into: opts={}) rescue abort($!.message)
27
+
28
+ show = opts[:show]
29
+ keep = opts[:extract].to_s.downcase.split(",")
30
+
31
+ dbas ||= ARGV.shift or Kernel.abort "no database given"
32
+ tabl ||= ARGV.shift or Kernel.abort "no table given"
33
+
34
+ # ==[ Helpers ]==
35
+
36
+ class Mysql2::Client
37
+ def query!(stmt, *args, **, &)
38
+ puts "\n==[ SQL statement ]==\n\n", stmt, ";"
39
+ query(stmt, *args, **, &)
40
+ end
41
+ end
42
+
43
+ def display(name, data, show, uniq, tots)
44
+ seen = data.inject(0) {|seen, coun| seen += coun[0] }
45
+ wide = tots.to_s.size
46
+ fill = " " * wide
47
+ line = "=" * name.size
48
+
49
+ puts "\n#{fill} #{name}\n#{fill} #{line}\n"
50
+ data.each {|cnt, val| puts "%*d %s" % [wide, cnt, val || "NULL"] }
51
+ puts "#{fill} -----\n"
52
+ puts "%*d shown (top #{show})" % [wide, seen] if show
53
+ puts "%*d total (all #{uniq})" % [wide, tots]
54
+ end
55
+
56
+ # ==[ Let 'er rip! ]==
57
+
58
+ conn = Mysql2::Client.new(database: dbas, as: :array)
59
+ resu = conn.query("select * from `#{tabl}` limit 0")
60
+ cols = resu.fields
61
+ keep = keep.empty? ? cols : keep & cols
62
+
63
+ if opts[:columns]
64
+ puts cols
65
+ exit
66
+ end
67
+
68
+ keep.each do |name|
69
+
70
+ # data summary
71
+ stmt = show ? "limit #{show}" : ""
72
+ data = conn.query(<<~"" + stmt).to_a
73
+ select
74
+ count(*) as cnt,
75
+ `#{name}` as val
76
+ from
77
+ `#{tabl}`
78
+ group by
79
+ val
80
+ order by
81
+ cnt desc
82
+
83
+ uniq = conn.query(<<~"").to_a[0][0]
84
+ select
85
+ count(distinct(`#{name}`))
86
+ from
87
+ `#{tabl}`
88
+
89
+ tots = conn.query(<<~"").to_a[0][0]
90
+ select
91
+ count(`#{name}`)
92
+ from
93
+ `#{tabl}`
94
+
95
+ display(name, data, show, uniq, tots)
96
+
97
+ end
data/slyce.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "slyce"
5
+ s.version = `grep '^VERSION' bin/slyce | cut -f 2 -d '"'`
6
+ s.author = "Steve Shreeve"
7
+ s.email = "steve.shreeve@gmail.com"
8
+ s.summary =
9
+ s.description = "Ruby utility to show data statistics for MySQL databases"
10
+ s.homepage = "https://github.com/shreeve/slyce"
11
+ s.license = "MIT"
12
+ s.files = `git ls-files`.split("\n") - %w[.gitignore]
13
+ s.executables = `cd bin && git ls-files .`.split("\n")
14
+ s.add_runtime_dependency "mysql2", "~> 0.5"
15
+ 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.0.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve
@@ -26,12 +26,16 @@ dependencies:
26
26
  version: '0.5'
27
27
  description: Ruby utility to show data statistics for MySQL databases
28
28
  email: steve.shreeve@gmail.com
29
- executables: []
29
+ executables:
30
+ - slyce
30
31
  extensions: []
31
32
  extra_rdoc_files: []
32
33
  files:
34
+ - Gemfile
33
35
  - LICENSE
34
36
  - README.md
37
+ - bin/slyce
38
+ - slyce.gemspec
35
39
  homepage: https://github.com/shreeve/slyce
36
40
  licenses:
37
41
  - MIT