sqlconv 0.9.2 → 1.0

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/sqlconv +51 -38
  3. data/sqlconv.gemspec +1 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f5a4cff886763224e9216e3f42be20d97ecb9dd96a9bcb23be71d2cc5553276
4
- data.tar.gz: df12b1041887989c6782ed1e2a64ce4f47f09b31f40130cc1cb8833e1bd8b95f
3
+ metadata.gz: 953b48afedc6e52ef6430f3d1bc1b66d6d652dfdc33c494b5950a961f157b6ec
4
+ data.tar.gz: 6d6d791ba8c0611245143d0198a9ef8a2be00f84e2b5faf1d01c16a3426826e3
5
5
  SHA512:
6
- metadata.gz: 36e69fad7422741a99aeb03b28541eec399875c45993d1c7b86ca6bdccf27ad9d04d5aa751cde238c0e8f9fb303e1fbd009e8d82c0915116f568f25eff0ea290
7
- data.tar.gz: 4c49fa603ecb629a4fc6c3c0bcb366ca9ac45bbb87d515ed866550e03dc9e4cf79d3d52cc82f115c2ec664e30db5d742e5a848bd2966818dd98453b88babb56c
6
+ metadata.gz: 9a9cfa9b993be6d3ffbeadd9deef4e6c5af05933a1d680e0e4975a335530d11029066f7b26cd76d7e2466bc809d16a15f98164fc60664ec86dab67f423d0184f
7
+ data.tar.gz: 2cbe842f8898108c0b627151f6ae5014df61b5d6ea5f3d31300f11067563739bd96520cfec746d670e4951a7d0e7ee38ba55c7aed99a110e1deec4048d8d02c7
data/bin/sqlconv CHANGED
@@ -61,10 +61,31 @@ def grok(want)
61
61
  end or raise "invalid selector '#{want}'"
62
62
  end
63
63
 
64
+ # display table output
65
+ def table(cols, rows)
66
+ cols.is_a?(Array) && cols.size > 0 or return
67
+ rows.is_a?(Array) && rows.size > 0 or return
68
+ join = " | "
69
+ both = [cols] + rows
70
+ flip = both.transpose
71
+ wide = flip.map {|row| row.map {|col| col.to_s.size }.max }
72
+ pict = wide.map {|len| "%-#{len}.#{len}s" }.join(join)
73
+ pict = [join, pict, join].join.strip
74
+ line = (pict % ([""] * cols.size)).tr("| ", "+-")
75
+ seen = -1
76
+ puts "", line
77
+ both.each do |vals|
78
+ puts pict % vals
79
+ puts line if (seen += 1) == 0
80
+ end
81
+ puts line, "#{seen} rows displayed", ""
82
+ end
83
+
64
84
  # convert the insert statements
65
85
  def conv(tab1, map1, tab2, map2, mode, dump)
66
86
  data = StringScanner.new("")
67
87
  need = grok(map1)
88
+ rows = nil
68
89
  cols = nil
69
90
  len1 = nil
70
91
  len2 = nil
@@ -86,6 +107,8 @@ def conv(tab1, map1, tab2, map2, mode, dump)
86
107
  # if needed, output pipes header
87
108
  if mode == "pipes" && map2
88
109
  puts map2.gsub(',','|')
110
+ elsif mode == "table"
111
+ rows = []
89
112
  end
90
113
 
91
114
  # process each line
@@ -109,7 +132,7 @@ def conv(tab1, map1, tab2, map2, mode, dump)
109
132
  puts sep, "| col | %-*.*s |" % [max, max, 'data'], sep
110
133
  len1.times do |pos|
111
134
  val = cols[pos]
112
- val[max-3..] = '...' if val.size > max
135
+ val[max-3..-1] = '...' if val.size > max
113
136
  puts "| %3d | %-*.*s | " % [pos + 1, max, max, val]
114
137
  end
115
138
  puts sep
@@ -171,21 +194,31 @@ def conv(tab1, map1, tab2, map2, mode, dump)
171
194
  cols or exit
172
195
  end
173
196
 
174
- # output insert statement
175
- if mode == "pipes"
197
+ # generate output
198
+ if mode == "pipes" || mode == "table"
176
199
  ours.map! {|e| e == 'NULL' ? '' : e !~ /\A['"]/ ? e : e[1..-2].gsub("|","~").gsub("''", "'")}
200
+ end
201
+ if mode == "pipes"
177
202
  puts ours * "|"
203
+ elsif mode == "table"
204
+ rows << ours.dup
178
205
  else
179
206
  puts [pref, ours * ",", ");"].join
180
207
  end
181
208
  end
209
+
210
+ # output table
211
+ if mode == "table"
212
+ cols = map2 ? map2.split(',') : rows[0].size.times.map {|i| "col#{i+1}"}
213
+ table cols, rows
214
+ end
182
215
  end
183
216
 
184
217
  # ==[ invoke the cli ]==
185
218
 
186
219
  argv = -1
187
220
  ARGV.size.times do
188
- if ARGV[argv += 1] == "-p"
221
+ if ARGV[argv += 1] == "-x"
189
222
  begin
190
223
  require plugin = ARGV[argv += 1]
191
224
  rescue LoadError
@@ -195,15 +228,11 @@ ARGV.size.times do
195
228
  end
196
229
  end
197
230
 
198
- if ARGV.delete "-r"
199
- mode = "replace"
200
- elsif ARGV.delete "--pipes"
201
- mode = "pipes"
202
- end
203
-
204
- if ARGV.delete "-s"
205
- mode = "show"
206
- end
231
+ # mutually exclusive options
232
+ if ARGV.delete "-s" then mode = "show"
233
+ elsif ARGV.delete "-r" then mode = "replace"
234
+ elsif ARGV.delete "-p" then mode = "pipes"
235
+ elsif ARGV.delete "-t" then mode = "table"; end
207
236
 
208
237
  if ARGV.shift =~ /^([a-z][-\w]*):?(.+)?$/
209
238
  tab1 = $1
@@ -219,30 +248,14 @@ if ARGV.size > 0 and !File.exists?(ARGV.first)
219
248
  end
220
249
 
221
250
  tab1 or die [
222
- "Usage: #{File.basename $0}",
223
- "[-p <plugin1.rb> [-p <plugin2.rb>]...]",
224
- "[--pipes] (to output pipe separated values instead of SQL",
225
- "[-r] (to use 'replace into' instead of 'insert into')",
226
- "[-s] (to show column indexes and values for the first row)",
227
- "<src_table>(:[sel1,sel2,...])",
228
- "[dst_table][:][col1,col2,...] file"
229
- ] * ' '
251
+ "Usage: #{File.basename $0} <options> " +
252
+ "<src_table>(:[sel1,sel2,...]) " +
253
+ "[dst_table][:][col1,col2,...] file",
254
+ " -p (output pipe separated values instead of SQL",
255
+ " -r (use 'replace into' instead of 'insert into')",
256
+ " -s (show column indexes and values for the first row)",
257
+ " -t (display output as a formatted table)",
258
+ " -x <plugin1.rb> [-x <plugin2.rb>]...]",
259
+ ] * "\n"
230
260
 
231
261
  conv tab1, map1, tab2 || tab1, map2, mode, ARGF
232
-
233
- __END__
234
-
235
- # show opt parsing
236
- p [:tab1, tab1]
237
- p [:map1, map1]
238
- p [:tab2, tab2]
239
- p [:map2, map2]
240
- p [:argv, ARGV]
241
- exit
242
-
243
- # values for testing
244
- tab1 = 'adonis_schema'
245
- map1 = nil
246
- tab2 = nil
247
- map2 = nil
248
- ARGV.push 'db1.sql'
data/sqlconv.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "sqlconv"
5
- s.version = "0.9.2"
5
+ s.version = "1.0"
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
8
  s.summary = "Handy utility to massage MySQL dump files"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlconv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Shreeve