slyce 1.3.1 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/slyce +36 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ba003aec5b58e63630c2b5fc40437ecfc841e8b2c2239fea6fba2cc785687d8
4
- data.tar.gz: 3713c8ea0c95383fad5151012553a4f22fba837925d19839bcef5abadba1b2d6
3
+ metadata.gz: 00d9ddd3a6d353a87ede364090e7b534adeb9f0f7c2bc64c374db8dcf9e181d9
4
+ data.tar.gz: 2b22b3ee7f996cda2c5770af031fc8fa65db652d54b70bd16bcea03247ce9d3d
5
5
  SHA512:
6
- metadata.gz: e93706e9acd8ce49b35d1c47c19cffb388ab3e77dbe1cdeff9f1e817c3b89d5b854edbafb1422951d82b4a395c63882cf6f267c4bff772597a830ea13cc98c42
7
- data.tar.gz: cec5303f23cd5e5370c349e566f1afed2d5a7f3a6b094aede4ba7c851aeaf2ef76f58adfe1148660b3e852bfea4923ade8dab40ae73e9c656560f55a210f05d6
6
+ metadata.gz: d91171715f18698c3ced8c1f7a0a7eca87d28766a2b99e7272950760cc21f3cc6eab626f57f45d5adbd2e6b33590c04e56f6d55a8427241ecc31ff45d7506d74
7
+ data.tar.gz: 3ea0f1d215bb9bb0a4c6cc70b7e579e61a7e7167950f83ec48e27bbb88f987df571da4b0bf31ee772ef3214768fa0b2df43f0d20af6dd80effa809a462a7846c
data/bin/slyce CHANGED
@@ -11,7 +11,7 @@ dbas = nil
11
11
  tabl = nil
12
12
 
13
13
  OptionParser.new.instance_eval do
14
- @version = "1.3.1"
14
+ @version = "1.3.3"
15
15
  @banner = "usage: #{program_name} [options] <database> <table>"
16
16
 
17
17
  on "--csv" , "Output comma separated values"
@@ -19,6 +19,7 @@ OptionParser.new.instance_eval do
19
19
  on "--tsv" , "Output tab separated values"
20
20
  on "-a", "--ascii" , "Convert data to ASCII using AnyAscii"
21
21
  on "-c", "--columns" , "Display column names and quit"
22
+ on "-d", "--dump" , "Dump database schema and quit"
22
23
  on "-h", "--help" , "Show help and command usage" do Kernel.abort to_s; end
23
24
  on "-n", "--natural" , "Sort naturally, not numerically"
24
25
  on "-r", "--rows <count>" , "Rows of data to show", Integer
@@ -26,7 +27,7 @@ OptionParser.new.instance_eval do
26
27
  on "-t", "--tables" , "Display table names and quit"
27
28
  on "-v", "--version" , "Show version number" do Kernel.abort "#{program_name} #{@version}"; end
28
29
  on "-w", "--where <cond>" , "Where clause (eg - 'age>50 and state='AZ')"
29
- on "-x", "--extract <a,b,c...>" , "Comma separated list of columns (or tables) to extract"
30
+ on "-x", "--extract <a,b,c...>" , "Comma separated list of columns or tables to extract"
30
31
 
31
32
  self
32
33
  end.parse!(into: opts={}) rescue abort($!.message)
@@ -37,11 +38,12 @@ xtsv = opts[:tsv]
37
38
  xprt = xcsv || xpsv || xtsv and require "censive"
38
39
 
39
40
  asky = opts[:ascii ] and require "any_ascii"
40
- filt = opts[:where ] and filt = "where\n #{filt}"
41
- hide = opts[:suppress]
41
+ dump = opts[:dump]
42
+ want = opts[:extract ].to_s.downcase.split(",")
42
43
  natu = opts[:natural ]
43
44
  show = opts[:rows ]
44
- want = opts[:extract].to_s.downcase.split(",")
45
+ hide = opts[:suppress]
46
+ filt = opts[:where ] and filt = "where\n #{filt}"
45
47
 
46
48
  dbas ||= ARGV.shift or abort "no database given"
47
49
  tabl ||= ARGV.shift or opts[:tables] or !want.empty? or abort "no table given"
@@ -77,7 +79,9 @@ end
77
79
  # ==[ Let 'er rip! ]==
78
80
 
79
81
  # get database connection
80
- if dbas.include?("/")
82
+ if !dbas.include?("/")
83
+ conf = { database: dbas }
84
+ else
81
85
  dbas = $' if dbas =~ %r|^mysql://| # drop mysql:// prefix, if present
82
86
  auth, dbas = dbas.split("/", 2)
83
87
  if auth =~ /^(?:(\w+)(?::([^@]+))?@)?(?:([^:]+)?(?::(\d+))?)$/
@@ -92,17 +96,38 @@ if dbas.include?("/")
92
96
  else
93
97
  abort "invalid database value #{dbas.inspect}"
94
98
  end
95
- else
96
- conf = { database: dbas }
97
99
  end
98
100
 
99
101
  conn = Mysql2::Client.new(**conf, as: :array)
100
102
 
101
- # get table names
102
- if tabl.nil? || opts[:tables]
103
+ # dump database schema or show table names
104
+ if tabl.nil? || opts[:tables] || opts[:dump]
105
+
106
+ # get table names
103
107
  resu = conn.sql("show tables")
104
108
  tbls = resu.to_a.flatten
105
- want = (want.empty? || opts[:tables]) ? tbls : want.select {|e| tbls.include?(e) }
109
+ want = (want.empty? || opts[:tables] || opts[:dump]) ? tbls : want.select {|e| tbls.include?(e) }
110
+
111
+ # dump database schema
112
+ if opts[:dump]
113
+ pict = "%Y-%m-%dT%H:%M:%S%z"
114
+ puts "-- Dump of `#{dbas}` database on #{Time.now.strftime(pict)}\n\n"
115
+ puts "set foreign_key_checks=0;\n\n" unless want.empty?
116
+ tail = []
117
+ want.each do |name|
118
+ text = conn.sql("show create table `#{name}`").to_a.flatten[1] + ";\n\n"
119
+ if text =~ /^create table/i
120
+ puts text
121
+ elsif text.gsub!(/^(create ).*?(?=view)/i, '\1')
122
+ tail << text
123
+ end
124
+ end
125
+ puts tail.join unless tail.empty?
126
+ puts "set foreign_key_checks=1;\n\n" unless want.empty?
127
+ exit
128
+ end
129
+
130
+ # show table names
106
131
  puts want
107
132
  exit
108
133
  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: 1.3.1
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve