-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr2l.py
688 lines (613 loc) · 20.2 KB
/
r2l.py
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#!/usr/bin/python3
import sys
import re
from os import listdir
from os.path import isfile, join
# To Do:
# Currently am auto-generating `col_<<column name>>` as a var
# Should instead:
# register column names that don't have an alias used in SELECT queries
# and occurence of that column name being used outside of a query is replaced with col_<<column name>> :)
#
#Convert the print statements to utl_file.put_line() statements
stack = []
selectvars_type = []
sep = '''
--------------------------------------------------------------------------------
'''
head = """set scan off
CREATE OR REPLACE PACKAGE pkz_name IS
{}END pkz_name;
/
SHOW ERRORS;
SET SCAN ON;
WHENEVER SQLERROR CONTINUE;
DROP PUBLIC SYNONYM pkz_name;
WHENEVER SQLERROR EXIT ROLLBACK;
CREATE PUBLIC SYNONYM pkz_name FOR pkz_name;
WHENEVER SQLERROR CONTINUE;
START gurgrtb pkz_name;
WHENEVER SQLERROR EXIT ROLLBACK;
"""
foot = sep + """END pkz_name;
/
show errors
grant execute on pkz_name to public;
"""
sqrkeywords = [
'print ',
'display ',
'add ',
'subtract ',
'let ',
'move ',
'do ',
'if ',
'write ',
'evaluate ']
# We'll save this regex for later :)
writepad = re.compile(':\\d+$')
# prntloc = re.compile('\([\+\-]*\d*\,*\d+\,\d+\)')
#
# def r2lprint(s):
# for match in prntloc.finditer(s):
# pass
#
# # match has the last value
# fmt = match.group().strip().split('--')[0].split(' edit ')
# s = r2lline(s[0:match.span()[0]])
#
#
# return "{}rpad(to_char({}), {})".format(indent,s[0:t.span()[0]])
def removeproc(s, proc):
print("removing " + proc)
proc_start = s.find('\nPROCEDURE ' + proc)
proc_end = s.find('\nEND ' + proc) + 6 + len(proc)
return s[0:proc_start] + s[proc_end:]
def deadprocremoval(s):
# gather all of the procedure names
all_procedures = []
i = 0
while i >= 0:
i = s.find('\nPROCEDURE P_', i)
if i >= 0:
i += 11
next_parens = s.find('(', i)
next_is = s.find(' IS', i)
if next_parens < next_is:
all_procedures += [s[i:next_parens]]
i = next_parens
else:
all_procedures += [s[i:next_is]]
i = next_is
# Now check for the appearance of of the procedures
l = s.split('\n')
all_procedures_swap = []
do_loop = True
while do_loop:
do_loop = False
for i in all_procedures:
if i != 'P_Main':
found = False
for j in l:
if j.strip().startswith(i) and j.strip()[len(i)].lower() not in 'abcdefghijklmnopqrstuvwxyz_':
found = True
break
if found:
all_procedures_swap += [i]
else:
s = removeproc(s, i)
do_loop = True
if do_loop:
do_loop = False
for i in all_procedures_swap:
found = False
for j in l:
if j.strip().startswith(i) and j.strip()[len(i)].lower() not in 'abcdefghijklmnopqrstuvwxyz_':
found = True
break
if found:
all_procedures += [i]
else:
s = removeproc(s, i[1:])
do_loop = True
return s
def decomment(s):
i = s.find('--')
if i >= 0:
comment = ' ' + s[i:].strip()
s = s[0:i].strip()
else:
comment = ''
s = s.strip()
return (s, comment)
def snake_to_camel(s):
if s=='':
return s
t = s[0].upper()
k = 1
while k<len(s):
u = False
while s[k] in ['-', '_'] and k<len(s)-1:
k += 1
u = True
if u:
t += s[k].upper()
else:
t += s[k].lower()
k += 1
return t
def r2lwrite(s):
if s.strip() == '':
return s
k = 0
while s[k] in ' \t':
k += 1
indent = s[0:k]
s = r2lline(s[k:])
t = writepad.search(s)
if not t:
if s.startswith('__num_'):
return "{}to_char({})".format(indent,s.split('--')[0].strip())
else:
return indent + s
if s.startswith('__num_'):
return "{}rpad(to_char({}), {})".format(indent,s[0:t.span()[0]],t.group()[1:])
else:
return "{}rpad({}, {})".format(indent,s[0:t.span()[0]],t.group()[1:])
def col_dealias(s):
k = 0
while s[k] in ' \t':
k += 1
indent = s[0:k]
s = s.strip().rstrip(',').rstrip()
if s.startswith('from'):
return ('', '', '', False)
for i in sqrkeywords:
if s.startswith(i):
return ('','','',False)
s = '__col_'+s.strip();
# now work backwards in s until an '&' is encountered
k = len(s) - 6
while not s.startswith('__col_', k):
k -= 1
if k == 0:
# then we don't have an alias!
col = s[6:]
alias = 'col_' + col
else:
col = s[6:k]
alias = s[k+2:]
return (indent, col.strip(), alias.strip(), True)
def r2lvar_rename(s):
t = ''
k = 0
while k<len(s):
m = True
for i in selectvars_type:
if s.startswith(i,k):
t += 'col_' + i
k += len(i)
m = False
if m:
t += s[k]
k += 1
return t
def r2lline(s):
global stack, constants
if s.strip() == '':
return s
# calculate the indent
k = 0
while s[k] in [' ','\t']:
k += 1
indent = s[0:k]
s = s.strip()
if s.startswith('let '):
s = s[4:].split('=')
t = ''
for i in s[1:]:
t += i + '='
(t, comment) = decomment(t[0:-1])
return "{}{}:={};{}".format(indent,s[0],t,comment)
elif s.startswith('input '):
return indent+'&'+s[6:]
elif s.startswith('do '):
return indent+'P_'+snake_to_camel(s[3:])+';'
elif s.startswith('add '):
#add x to y ==> y := y + x
s = s[4:].split(' to ')
t = s[1]
for i in s[2:]:
t += ' to ' + i
(t, comment) = decomment(t)
return "{}{} := {} + {};{}".format(indent,t,t,s[0].strip(), comment)
elif s.startswith('subtract '):
#subtract x from y ==> y := y - x
(s, comment) = decomment(s)
s = s[9:].split(' from ')
t = s[1]
for i in s[2:]:
t += ' from ' + i
return "{}{} := {} - {};{}".format(indent,t,t,s[0].strip(), comment)
elif s == 'begin-report':
stack += ['P_Main']
return sep+"PROCEDURE P_Main IS\nBEGIN"
elif s == 'end-report':
return "END P_Main;"
elif s.startswith('begin-heading'):
stack += ['P_PrintHeading']
return sep+"PROCEDURE P_PrintHeading IS\nBEGIN"
elif s == 'end-heading':
return "END P_PrintHeading;"
elif s.startswith('begin-procedure'):
s = 'P_'+snake_to_camel(s[16:])
stack += [s]
return "{}PROCEDURE {} IS\nBEGIN".format(sep,s)
elif s.startswith('end-procedure'):
return "END {};".format(stack[-1])
elif s.startswith('while '):
(s, comment) = decomment(s[6:])
return "{}WHILE {} LOOP{}".format(indent, s, comment)
elif s.startswith('if '):
(s, comment) = decomment(s[3:])
return "{}IF {} THEN{}".format(indent, s, comment)
elif s == 'end-while':
return indent+'END LOOP;'
elif s.startswith('end-if'):
return indent+'END IF;'+s[6:]
elif s.startswith('begin-select'):
stack += [s[6:].upper(),True]
return ''
elif s == 'new-page':
s = "P_PrintHeading;"
elif s.startswith('move '):
s = s[5:].split(' to ')
t = s[1].strip().split(' ')
if t[0].startswith('__num_'):
return "{}{} := to_number({});".format(indent,t[0].strip(),s[0].strip())
else:
t += ['']
t[1] = t[1].strip()
if t[1] != '':
return "{}{} := to_char({}, '{}');".format(indent,t[0].strip(),s[0].strip(),t[1].strip())
else:
return "{}{} := to_char({});".format(indent,t[0].strip(),s[0].strip())
elif s.startswith('display '):
(s, comment) = decomment(s[8:])
if s.endswith('noline'):
return "{}DBMS_OUTPUT.PUT({});{}".format(indent, s[0:-6].strip(), comment)
else:
return "{}DBMS_OUTPUT.PUT_LINE({});{}".format(indent, s, comment)
elif s.startswith('open '):
if ' as ' not in s:
return s
else:
s = s[5:].split(' as ')
return "{}file_{} := UTL_FILE.FOPEN('{}', {}, 'w');".format(indent,s[1].split(' ')[0],'FILE_DIR',s[0])
elif s.startswith('close '):
return "{}UTL_FILE.FCLOSE(file_{});".format(indent, s[6:])
elif s in 'end-sql':
return ';'
elif s in ['commit']:
return "{}{};".format(indent, s, ';')
elif s in ['begin-setup', 'end-setup', 'begin-sql']:
return ''
elif s.startswith('page-size'):
s = s[9:].strip().split(' ')
constants += [["page height", int(s[0])], ["page width", int(s[1])]]
return ''
elif s.startswith('__num_define'):
s = s[12:].strip().replace('\t', ' ').split(' ')
constants += [[s[0], s[-1]]]
return ''
return indent+s
def low(s):
t = ''
k = 0
stck = ['']
dolow = True
while k<len(s):
i = s[k]
if i == "'":
if stck[-1] == i:
stck.pop()
if len(stck) == 1:
dolow = True
else:
stck += [i]
dolow = False
if dolow:
if i == '!':
if s[k+1] == '=':
t += '<>'
k += 1
else:
t += '--'
while s[k+1]!='\n':
k += 1
t += s[k]
elif i == '&':
t += '__col_'
elif i == '#':
t += '__num_'
elif i == '$':
t += '__var_'
else:
t += s[k].lower()
elif s[k] == '&':
t += "' || chr(38) || '"
else:
t += s[k]
k += 1
return t
def r2l(s):
def r2l_write():
nonlocal i, s, k
i = i.strip()
idx = i.lower().index('from')
out = '\tutl_file.put_line(file_{},'.format(i[6:idx].strip())
out += r2lwrite(i[idx + 4:])
while s[k+1].strip() == '':
k += 1
k += 1
i = s[k].strip()
while i.startswith("'") or i.startswith('__num_') or i.startswith('__col_') or i.startswith('__var_'):
out += ' ||\n\t\t\t'
out += r2lwrite(s[k])
k += 1
i = s[k].strip()
return out + ');\n'
def r2l_evaluate():
nonlocal i, s, k
(v, out) = decomment(i.strip()[9:])
k += 1
cond = []
first = True
while not s[k].strip().lower().startswith('end-evaluate'):
i = s[k]
if i.strip().lower().startswith('when '):
(i, comment) = decomment(i)
#indent
indent = i.lower().split('when')[0]
cond += ["{} = {}".format(v, i[i.index('=') + 1:].strip())]
elif i.strip().lower().startswith('when-other'):
# treat this like an ELSE
# if there are any conditions queued, discard them
out += "{}ELSE\n".format(indent)
cond = []
else:
if len(cond) > 0:
if first:
out += "{}IF {}".format(indent, cond[0])
first = False
else:
out += "{}ELSIF {}".format(indent, cond[0])
for j in cond[1:]:
out += ' OR {}'.format(j)
out += ' THEN\n'
cond = []
out += r2lline(i) + '\n'
k += 1
k += 1
i = s[k]
return out + '{}END IF;\n'.format(indent)
global stack, selectvars_type, constants
constants = []
stack = []
selectvars = []
selectvars_i = []
selectvars_type = []
cursors = ''
curse_cnt = 0
index = 0
s = low(s)
out = ''
s = s.split('\n')
k = 0
# We'll read any comments at the top of the file and keep them there
comment = ''
while s[k].startswith('--') or s[k].strip() == '':
comment += s[k]+'\n'
k += 1
while k<len(s):
i = s[k]
while i.strip().startswith('write'):
out += r2l_write()
if i.strip().startswith('evaluate '):
out += r2l_evaluate()
if len(stack) == 0:
out += r2lline(r2lvar_rename(i)) + '\n'
elif stack[-1] != True:
out += r2lline(i) + '\n'
else:
# here we are in a select statement that we have to parse
# Note! This doesn't handle comments or trailing whitespace!
curse = ''
while i == '':
k += 1
i = s[k]
cont_loop = True
prev_col = ''
prev_alias = ''
prev_indent = ''
while cont_loop:
# read backwards to determine the output name, if any
i = i.split('!')[0]
if i.strip()!='':
(indent, col, alias, cont_loop) = col_dealias(i)
if cont_loop:
if col == '':
prev_alias = alias
if prev_col != '':
if prev_alias == 'col_' + prev_col:
curse += "\t\t{}{},\n".format(prev_indent, prev_col)
else:
curse += "\t\t{}{}\t\t{},\n".format(prev_indent, prev_col, prev_alias[4:])
selectvars += [prev_alias]
selectvars_i += [index]
selectvars_type += [prev_col]
k += 1
i = s[k]
while i == '':
k += 1
i = s[k]
else:
k += 1
i = s[k]
prev_col = col
prev_alias = alias
prev_indent = indent
else:
k += 1
i = s[k]
if prev_col != '':
if prev_alias == 'col_' + prev_col:
curse += "\t\t{}{},\n".format(prev_indent, prev_col)
else:
curse += "\t\t{}{}\t\t{},\n".format(prev_indent, prev_col, prev_alias[4:])
selectvars += [prev_alias]
selectvars_i += [index]
selectvars_type += [prev_col]
curse = curse.rstrip(',\n \t') + '\n'
work = ''
while not i.strip().startswith("from"):
if i.strip().startswith('write'):
work += r2l_write()
elif i.strip().startswith('evaluate '):
work += r2l_evaluate()
else:
work += r2lline(i) + '\n'
k += 1
i = s[k]
logic = ''
while "end-select" not in i:
logic += '\n\t' + r2lline(i)
k += 1
i = s[k]
logic = logic.replace('and ', 'AND ')
logic = logic.replace('or ', 'OR ')
logic = logic.replace('from ', 'FROM ')
logic = logic.replace('where ', 'WHERE ')
stack.pop()
if work != '':
cname = "Cursor{}".format(curse_cnt)
cursors += "{}CURSOR {} IS\n\t{}\n{}{};\n".format(sep, cname, stack.pop(), curse, logic)
curse_cnt += 1
out += 'FOR i{} IN {} LOOP\n\t{}\nEND LOOP;\n'.format(index,cname,work.strip())
index+=1
else:
#otherwise, we have a select into
out += stack.pop()
for i in curse.split('\n'):
out += '\n\t\t'+i.strip().split('\t')[0] + ','
while out != out.rstrip().rstrip(','):
out = out.rstrip().rstrip(',')
out += "\nINTO"
j = 0
while selectvars_i[j] != index:
j += 1
i = j
while i<len(selectvars_i)-1:
out += '\n\t\t'+selectvars[i]+','
i += 1
out += '\n\t\t'+selectvars[i]
selectvars = selectvars[0:j]
selectvars_i = selectvars_i[0:j]
selectvars_type = selectvars_type[0:j]
out += logic+';\n'
k += 1
# out = out.replace('select','SELECT')
# out = out.replace('where','WHERE')
# out = out.replace('and','AND')
# out = out.replace('or','OR')
# combine the selectvar variables so that we can sort
vars = sep + 'CREATE OR REPLACE PACKAGE BODY pkz_name IS\n'
h = -1
w = -1
for i in constants:
if i[0] == 'page height':
h = i[1]
elif i[0] == 'page width':
w = i[1]
break
vars += "\t{}\t\t\tNUMBER:={};\n".format("max_lines", h)
vars += "\t{}\t\t\tNUMBER:={};\n".format("report_width", w)
vars += "\t{}\t\t\tNUMBER:={};\n".format("page_num", 1)
vars += "\t{}\t\t\tNUMBER;\n".format("line_num")
vars += """ cr_file UTL_FILE.FILE_TYPE;
current_date_dmy VARCHAR2(11);
current_time VARCHAR2(8);
inst_name gubinst.gubinst_name%TYPE;
program_name VARCHAR2(15);
run_msg VARCHAR2(60);
rpt_msg VARCHAR2(50);
file_name VARCHAR2(50);
report_title VARCHAR2(100);\n--\n"""
k = 0
while k < len(selectvars):
var = selectvars[k]
typ = selectvars_type[k]
if '__' + var in out:
out = out.replace('__' + var, "i{}.{}".format(selectvars_i[k],var[4:]))
substr = " := i{}.{};".format(selectvars_i[k],var[4:])
i = out.find(substr)
while i >= 0:
n = i
while out[n] in ' \t':
n -= 1
while out[n] in '_.' or '0'<=out[n]<='9' or 'A'<=out[n]<='Z' or 'a'<=out[n]<='z':
n -= 1
if " {} ".format(out[n:i].strip()) not in vars:
vars += "\t{}\t\t\t{}.{}%TYPE;\n".format(out[n:i].strip(), typ.split('_')[0], typ)
i = out.find(substr, i + 1)
k += 1
defs = ''
for i in stack:
defs += "\tPROCEDURE {};\n".format(i)
out += """-------------------------------------------------------------------------------
PROCEDURE P_PrintLine(s VARCHAR2) IS
BEGIN
utl_file.put_line (cr_file, rtrim(substr(s, 1, report_width)));
line_num := line_num + 1;
IF line_num >= max_lines THEN
P_PrintHeading;
END IF;
END P_PrintLine;
"""
s = comment+head.format(defs)+vars+cursors+out+foot
s = s.replace('__col_', 'col_')
# s = s.replace('__num_', '')
# s = s.replace('__var_', '')
s = s.replace('__num_', 'num_')
s = s.replace('__var_', 'var_')
s = s.replace('current-line', '__line_num')
s = s.replace('\n;', ';')
s = s.replace(" = ''", ' IS NULL')
s = s.replace("<> ''", 'IS NOT NULL')
while '\n\n\n--' in s:
s = s.replace('\n\n\n--', '\n\n--')
while 'BEGIN\n\n' in s:
s = s.replace('BEGIN\n\n', 'BEGIN\n')
while '\n\nEND' in s:
s = s.replace('\n\nEND', '\nEND')
for i in constants:
s = s.replace('{'+i[0]+'}', str(i[1]))
return deadprocremoval(s)
k = 1
while k<len(sys.argv):
fin = sys.argv[k]
f = open(fin, 'r')
s = f.read()
f.close()
s = r2l(s)
if k+1 < len(sys.argv):
fout = sys.argv[k+1]
s = s.replace('pkz_name', fout.replace('/','\\').split('\\')[-1].split('.')[0])
print("{} ==> {}".format(fin,fout))
f = open(fout,'w')
f.write(s)
f.close()
else:
print(s)
k += 2