slyce 0.0.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +3 -0
  3. data/bin/slyce +95 -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: 91edeaa8cb6e4b9558e048a8e7ad12930d5bfef263a81f910097f22ff8bd4d80
4
+ data.tar.gz: 4f88700e316728eec0302629b681960c7d83a0bb822bc7633266fda61046ef78
5
5
  SHA512:
6
- metadata.gz: f1fe6b3108944d002323ec744bfe46570484d4640e30f8f268b2c9b249e3bfcc1c2ad75f70ef6116b87989228ff3d53edc1c928031cb9f0200ae34434001e0d6
7
- data.tar.gz: 33867ad0dea4a0b41b22830dcf25199d8e29a4049c02d6a2f91547058c51a6bebc62bf88386755252fb0985bf444aceedeadbaa50f82492dcc760cbe50160797
6
+ metadata.gz: 918dc893d4ed97407780cce69a732ae4d447d95ce2e5d0a7c7f3716da23926fade500c1d6b5572d5badaec2d18e43651ea55b99e1cd319f20e564b144045f056
7
+ data.tar.gz: 23e29b06c3c0f59cb8dc19eeff412cbf77590014d81aca62ef7aca18d961a1c0184b1e9efbcbe0376d9e54a3b82810046df7e818c3a6c64679787b90b87653f2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/bin/slyce ADDED
@@ -0,0 +1,95 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ STDOUT.sync = true
4
+
5
+ VERSION="0.5.0"
6
+
7
+ require "bundler/setup"
8
+ require "mysql2"
9
+ require "optparse"
10
+
11
+ dbas = nil
12
+ tabl = nil
13
+
14
+ OptionParser.new.instance_eval do
15
+ @banner = "usage: #{program_name} [options] <database> <table>"
16
+
17
+ on "-c", "--columns" , "Display column names and quit"
18
+ on "-h", "--help" , "Show help and command usage" do Kernel.abort to_s; end
19
+ on "-s", "--show <count>" , "Show this many values", Integer
20
+ on "-v", "--version" , "Show version number" do Kernel.abort "#{program_name} #{VERSION}"; end
21
+ on "-x", "--extract <col1,col2,...>", "Comma separated list of columns to extract"
22
+
23
+ self
24
+ end.parse!(into: opts={}) rescue abort($!.message)
25
+
26
+ show = opts[:show]
27
+ keep = opts[:extract].to_s.downcase.split(",")
28
+
29
+ dbas ||= ARGV.shift or Kernel.abort "no database given"
30
+ tabl ||= ARGV.shift or Kernel.abort "no table given"
31
+
32
+ # ==[ Helpers ]==
33
+
34
+ class Mysql2::Client
35
+ def query!(stmt, *args, **, &)
36
+ puts "\n==[ SQL statement ]==\n\n", stmt, ";"
37
+ query(stmt, *args, **, &)
38
+ end
39
+ end
40
+
41
+ def display(name, data, show, uniq, tots)
42
+ seen = data.inject(0) {|seen, coun| seen += coun[0] }
43
+ wide = tots.to_s.size
44
+ fill = " " * wide
45
+ line = "=" * name.size
46
+
47
+ puts "\n#{fill} #{name}\n#{fill} #{line}\n"
48
+ data.each {|cnt, val| puts "%*d %s" % [wide, cnt, val || "NULL"] }
49
+ puts "#{fill} -----\n"
50
+ puts "%*d shown (top #{show})" % [wide, seen] if show
51
+ puts "%*d total (all #{uniq})" % [wide, tots]
52
+ end
53
+
54
+ # ==[ Let 'er rip! ]==
55
+
56
+ conn = Mysql2::Client.new(database: dbas, as: :array)
57
+ resu = conn.query("select * from `#{tabl}` limit 0")
58
+ cols = resu.fields
59
+ keep = keep.empty? ? cols : keep & cols
60
+
61
+ if opts[:columns]
62
+ puts cols
63
+ exit
64
+ end
65
+
66
+ keep.each do |name|
67
+
68
+ # data summary
69
+ stmt = show ? "limit #{show}" : ""
70
+ data = conn.query(<<~"" + stmt).to_a
71
+ select
72
+ count(*) as cnt,
73
+ `#{name}` as val
74
+ from
75
+ `#{tabl}`
76
+ group by
77
+ val
78
+ order by
79
+ cnt desc
80
+
81
+ uniq = conn.query(<<~"").to_a[0][0]
82
+ select
83
+ count(distinct(`#{name}`))
84
+ from
85
+ `#{tabl}`
86
+
87
+ tots = conn.query(<<~"").to_a[0][0]
88
+ select
89
+ count(`#{name}`)
90
+ from
91
+ `#{tabl}`
92
+
93
+ display(name, data, show, uniq, tots)
94
+
95
+ 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.0
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