Added statev to sql transform script
[libfirm] / ir / be / test / statev_sql.py
1 #! /usr/bin/env python
2
3 import sys
4 import fileinput
5 import os
6 import re
7
8 from optparse import OptionParser
9
10 def ev_create_stmt(heads):
11         create = 'create table if not exists ev (id int, time int, timeev int'
12         for x in heads:
13                 create += (', %s real' % (x,))
14         create += ');'
15         return create
16
17 def ctx_create_stmt(heads):
18         create = 'create table if not exists ctx (id int unique on conflict ignore'
19         for x in heads:
20                 create += (', %s text' % (x,))
21         create += ');'
22         return create
23
24 def find_heads(file):
25         ctx_heads = set()
26         ev_heads = set()
27         for line in fileinput.input(file):
28                 items = re.split('\s+', line)
29                 if items[0] == 'E':
30                         ev_heads.add(items[2])
31                 elif items[0] == 'P':
32                         ctx_heads.add(items[2])
33         return (ev_heads, ctx_heads)
34
35 def fill_tables(file):
36         keystack = []
37         valstack = []
38         for line in fileinput.input(file):
39                 items = re.split('\s+', line)
40                 op    = items[0]
41                 id    = items[1]
42                 if op == 'P':
43                         key   = items[2]
44                         val   = items[3]
45                         keystack.append(key)
46                         valstack.append(val)
47                         stmt = 'insert into ctx (id'
48                         for x in keystack:
49                                 stmt += ', ' + x
50                         stmt += ') values (' + str(int(id, 16))
51                         for x in valstack:
52                                 stmt += ', \'' + x + '\''
53                         stmt += ');'
54                 elif op == 'O':
55                         keystack.pop()
56                         valstack.pop()
57                         stmt = ''
58                 elif op == 'E':
59                         key   = items[2]
60                         val   = items[3]
61                         time   = items[4]
62                         timeev = items[5]
63                         t      = (id, val, time, timeev)
64                         stmt   = 'insert into ev (id, %s, time, timeev) values (%d, %s, %s, %s);' % (key, int(id, 16), val, time, timeev)
65
66                 if stmt:
67                         print stmt
68
69
70 def main():
71         parser = OptionParser('usage: %prog [options] input')
72         parser.add_option("-c", "--create", action="store_true")
73         (options, args) = parser.parse_args()
74
75         if len(args) < 1:
76                 parser.print_help()
77                 sys.exit(1)
78
79         file = args[0]
80
81         if not os.path.isfile(file):
82                 print "cannot find input file %s" % (file, )
83                 sys.exit(2)
84
85         (ev_heads, ctx_heads) = find_heads(file)
86
87         if options.create:
88                 print ev_create_stmt(ev_heads)
89                 print ctx_create_stmt(ctx_heads)
90
91         else:
92                 print 'begin transaction;'
93                 fill_tables(file)
94                 print 'commit;'
95
96
97 if __name__ == "__main__":
98         main()