-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_decode_to_case
executable file
·531 lines (431 loc) · 11.9 KB
/
convert_decode_to_case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env ruby
# encoding: utf-8
require 'optparse'
require "stringio"
class ParseError < StandardError; end
options = {}
opts = OptionParser.new
opts.banner = "Usage: convert_decode_to_case [options]"
opts.on("-h" , "--help" , "Display usage information") do |h|
options[:help] = true
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-p", "--parallel" , "Process all input files in parallel" ) do |n|
options[:jobs] = true
end
opts.on("-i INPUT" , "--input-dir=INPUT", "Read from INPUT, may be - for STDIN (default), a single file or the path to a directory containing multiple *.sql files to be processsed") do |d|
options[:input] = d
end
opts.on("-o DEST" , "--output-destination=DEST", "Write output to DEST, if omitted STDOUT will be used if input is STDIN or a single file, if input is a directory a directory must be used as output destination") do |d|
options[:output] = d
end
opts.on("-f","--force" , "Overwrite existing output files") do |f|
options[:force] = true
end
class SearchType
SEARCHTYPES = [ :default , :string , :comment , :parentheses , :newline , :column_alias ]
def initialize()
@searchstack = []
add(:default)
end
def add(type)
if !SEARCHTYPES.include?(type)
raise TypeError, "Invalid search type"
end
@searchstack.push type
end
def current
@searchstack.last
end
def remove
if @searchstack.count == 1
raise RunTimeError, "Last searchtype cant be removed"
end
@searchstack.pop
end
def to_s
current.to_s
end
end
class DecodeParser
def initialize(str)
@strio = StringIO.new str
if @strio.length < 6
raise ParseError, "Unable to parse, not enough input, length = (#{str.length characters})"
end
@parsed = false
@lineno = 1
end
def self.new_from_file(path)
parser = allocate
parser.initialize_from_file(path)
parser
end
def initialize_from_file(path)
@strio = StringIO.new File.open(path).read
if @strio.length < 6
raise ParseError, "Unable to parse, not enough input , filesize = (#{str.length characters})"
end
@parsed = false
@lineno = 1
end
def to_case
parse unless @parsed
@strio.string
end
def write_file(path)
File.open(path,"w").write(to_case)
end
private
def readc(curbuf,outbuf)
if !@strio.eof?
c = @strio.getc
if curbuf.length < 6
curbuf << c
return curbuf,outbuf
else
outbuf << curbuf[0]
nbuf = curbuf[1..-1]
curbuf = nbuf << c
return curbuf,outbuf
end
elsif curbuf.length > 1
outbuf << curbuf[0]
nbuf = curbuf[1..-1]
curbuf = nbuf
return curbuf,outbuf
else
raise ParseError, "Can't read past eof."
return
end
end
def parse
curbuf = ""
outbuf = StringIO.new
loop do
decode_found = false
searchtype = SearchType.new
curpos = 0
comment_count = 0
curbuf = ""
outbuf = StringIO.new
@strio.rewind
5.times { curbuf,outbuf = readc(curbuf,outbuf) }
while curbuf.length > 1
curbuf,outbuf = readc(curbuf,outbuf)
@lineno += 1 if curbuf[0] == "\n"
case searchtype.current
when :default
if curbuf.downcase == "decode"
decode_found = true
decode_parentheses = parentheses_match()
tokens = tokenize(decode_parentheses)
case_stmt = tokens_to_case(tokens)
outbuf << case_stmt
curbuf = ""
while (curbuf.length < 6) && !@strio.eof?
curbuf << @strio.getc
end
else
if comment_line?(curbuf)
searchtype.add :newline
elsif comment_start?(curbuf)
comment_count = 1
searchtype.add :comment
elsif string_start?(curbuf)
searchtype.add :string
elsif column_alias?(curbuf)
searchtype.add :column_alias
end
end
when :newline
if curbuf[0] == "\n"
searchtype.remove
end
when :comment
if comment_start?(curbuf)
comment_count+=1
elsif comment_end?(curbuf)
comment_count-=1
if comment_count == 0
searchtype.remove
end
end
when :string
if string_start?(curbuf)
in_str = false
curbuf,outbuf = readc(curbuf,outbuf)
while !@strio.eof? && string_start?(curbuf)
in_str = !in_str
curbuf,outbuf = readc(curbuf,outbuf)
end
if !in_str
searchtype.remove
end
end
when :column_alias
if curbuf[0] == ','
searchtype.remove
elsif comment_start?(curbuf)
comment_count = 1
searchtype.add :comment
end
end
end
if decode_found
outbuf << curbuf
@strio = outbuf
else
break
end
end
outbuf << curbuf
@strio = outbuf
end
def tokens_to_case(tokens)
compare = tokens.shift
str = " CASE "
while tokens.count > 1
equals , returnval = tokens.shift , tokens.shift
if equals.match /\s*null\s*/i
str += "WHEN #{compare} IS NULL THEN #{returnval} "
else
str += "WHEN #{compare} = #{equals} THEN #{returnval} "
end
end
if tokens.count == 1
str += "ELSE #{tokens.shift} END "
else
str += "END "
end
end
def parentheses_match()
curpos = 0
parentheses_found = false
parentheses_startpos = 0
curbuf = ""
outbuf = ""
comment_count = 0
parentheses_count = 0
searchtype = SearchType.new
#Keep one more character in curbuf for look-ahead
curbuf << @strio.getc
while !@strio.eof?
curbuf << @strio.getc
if parentheses_found
outbuf << curbuf[curpos]
end
case searchtype.current
when :default
if comment_start?(curbuf,curpos)
comment_count = 1
searchtype.add :comment
elsif comment_line?(curbuf,curpos)
searchtype.add :newline
elsif lparen?(curbuf,curpos)
parentheses_count += 1
parentheses_found = true
elsif rparen?(curbuf,curpos)
parentheses_count -= 1
if parentheses_count == 0 && parentheses_found
#Return look-ahead char to @strio since it hasn't consumed by this method
@strio.ungetc curbuf[-1]
#Last char would be matching parenthises, remove from outbuf before returning
outbuf.chop!
return outbuf
end
elsif string_start?(curbuf,curpos)
searchtype.add :string
end
when :newline
if curbuf[curpos] == "\n"
searchtype.remove
end
when :comment
if comment_start?(curbuf,curpos)
comment_count += 1
elsif comment_end?(curbuf,curpos)
comment_count -= 1
if comment_count == 0
searchtype.remove
end
end
when :string
if string_start?(curbuf,curpos)
in_str = false
while string_start?(curbuf,curpos+1) && !@strio.eof?
in_str = !in_str
curpos += 1
curbuf << @strio.getc
outbuf << curbuf[curpos]
end
if !in_str
searchtype.remove
end
end
end
curpos += 1
end
raise ParseError, "Unable to find end parentheses at line number #{@lineno}"
end
def tokenize(str)
tokens = []
curpos = 0
token_startpos = 0
searchtype = SearchType.new
parentheses_count = 0
comment_count = 0
begin
case searchtype.current
when :default
if str[curpos] == ',' # Token delimiter
tokens << str[token_startpos..curpos-1]
token_startpos = curpos+1
elsif comment_line?(str,curpos)
searchtype.add :newline
elsif comment_start?(str,curpos)
comment_count = 1
searchtype.add :comment
elsif string_start?(str,curpos)
searchtype.add :string
elsif lparen?(str,curpos)
parentheses_count = 1
searchtype.add :parentheses
end
when :parentheses
if lparen?(str,curpos)
parentheses_count += 1
elsif rparen?(str,curpos)
parentheses_count -= 1
if parentheses_count == 0
searchtype.remove
end
elsif string_start?(str,curpos)
searchtype.add :string
elsif comment_line?(str,curpos)
searchtype.add :newline
elsif comment_start?(str,curpos)
searchtype.add :comment
end
when :newline
if str[curpos] == "\n"
searchtype.remove
end
when :comment
if comment_start?(str,curpos)
comment_count += 1
end
if comment_end?(str,curpos)
comment_count -= 1
if comment_count == 0
searchtype.remove
end
end
when :string
if string_start?(str,curpos)
in_str = false
while string_start?(str,curpos+1)
in_str = !in_str
curpos += 1
end
if !in_str
searchtype.remove
end
end
end
curpos += 1
end while curpos < str.length
if tokens.count == 0
raise ParseError, "Unable to tokenize at line number #{@lineno}"
end
tokens << str[token_startpos..curpos-1]
end
def string_start?(str,pos=0)
str[pos] == "'" || str[pos] =='"'
end
def build_case_statement(tokens)
end
def comment_start?(str,pos=0)
str[pos] == '/' && str[pos+1] == '*'
end
def comment_end?(str,pos=0)
str[pos] == '*' && str[pos+1] == '/'
end
def comment_line?(str,pos=0)
str[pos] == '-' && str[pos+1] == '-'
end
def lparen?(str,pos=0)
str[pos] == '('
end
def rparen?(str,pos=0)
str[pos] == ')'
end
def column_alias?(str,pos=0)
str.downcase[0..3] == ' as '
end
end
begin
opts.parse!()
rescue OptionParser::InvalidOption => e
puts e
puts opts
exit(1)
end
if options[:help]
puts opts
exit(0)
end
if options[:input].nil? or options[:input] == "-"
d = DecodeParser.new(STDIN.read)
elsif File.exists?(options[:input]) && !Dir.exists?(options[:input])
d = DecodeParser.new_from_file options[:input]
elsif Dir.exists? options[:input]
filenames = Dir.glob(options[:input] + "/*.sql" )
if filenames.count == 0
puts "No input *.sql files in #{options[:input]}"
exit(1)
end
if options[:output].nil? || !Dir.exists?(options[:output])
puts "Invalid output directory: #{options[:output]}"
exit(1)
end
exists = false
filenames.each {|f| exists = true if File.exists?(options[:output] + "/" + File.basename(f) ) }
if exists && !options[:force]
puts "Output files exists, use -f to overwrite"
exit(1)
end
d = filenames.map do |file|
{
:filename => file ,
:decoder => DecodeParser.new_from_file(file)
}
end
elsif Dir.exists?(options[:input]) && ( options[:output].nil? || !Dir.exists?(options[:output]) )
puts "Output directory invalid"
exit(1)
end
if d.is_a? Array
procs = []
d.each do |asdf|
if options[:jobs].nil?
asdf[:decoder].write_file( options[:output] + "/" + File.basename(asdf[:filename]) )
else
procs << Process.fork { puts "Starting background job" ; asdf[:decoder].write_file( options[:output] + "/" + File.basename(asdf[:filename]) ) }
end
end
if !options[:jobs].nil?
procs.each { |p| Process.waitpid p }
end
else
if options[:output].nil? || options[:output] == "-"
puts d.to_case
elsif File.exists?(options[:output]) && options[:force].nil?
puts "Output file aldready exists, use -f to overwrite"
exit(1)
else
File.open(options[:output],"w").write d.to_case
end
end