From 4e23143af0de33779298fdd210a4a5123315a29c Mon Sep 17 00:00:00 2001 From: Sebastian Hack Date: Fri, 17 Aug 2007 11:38:52 +0000 Subject: [PATCH] Talk to sqlite3 directly changed options [r15558] --- ir/be/test/statev_sql.py | 75 ++++++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/ir/be/test/statev_sql.py b/ir/be/test/statev_sql.py index 327948513..36b25fce1 100755 --- a/ir/be/test/statev_sql.py +++ b/ir/be/test/statev_sql.py @@ -4,6 +4,8 @@ import sys import fileinput import os import re +import sha +import sqlite3 from optparse import OptionParser @@ -32,72 +34,95 @@ def find_heads(file): ctx_heads.add(items[2]) return (ev_heads, ctx_heads) -def fill_tables(file): - keystack = [] +def fill_tables(conn, file): + curr_id = 0 + ids = dict() valstack = [] + keystack = [] for line in fileinput.input(file): items = re.split('\s+', line) op = items[0] id = items[1] + if op == 'P': key = items[2] val = items[3] keystack.append(key) valstack.append(val) + + dig = sha.new() + for i in xrange(0,len(keystack)): + dig.update(".") + dig.update(keystack[i]) + dig.update("=") + dig.update(valstack[i]) + + hash = dig.digest() + if hash in ids: + id = ids[hash] + else: + id = curr_id + ids[hash] = curr_id + curr_id = curr_id + 1 + stmt = 'insert into ctx (id' for x in keystack: stmt += ', ' + x - stmt += ') values (' + str(int(id, 16)) + stmt += ') values (' + str(id) for x in valstack: stmt += ', \'' + x + '\'' stmt += ');' + conn.execute(stmt) + elif op == 'O': keystack.pop() valstack.pop() - stmt = '' + elif op == 'E': key = items[2] val = items[3] time = items[4] timeev = items[5] t = (id, val, time, timeev) - stmt = 'insert into ev (id, %s, time, timeev) values (%d, %s, %s, %s);' % (key, int(id, 16), val, time, timeev) - - if stmt: - print stmt + stmt = 'insert into ev (id, %s, time, timeev) values (?, ?, ?, ?)' % (key,) + conn.execute(stmt, (id, val, time, timeev)) def main(): - parser = OptionParser('usage: %prog [options] input') - parser.add_option("-s", "--schema", action="store_true", help="emit only the schema definitions") - parser.add_option("-e", "--events", action="store_true", help="emit only the event information") + parser = OptionParser('usage: %prog [options] ') + parser.add_option("-c", "--clean", action="store_true", help="clean existing data base before adding data") (options, args) = parser.parse_args() - if len(args) < 1: + if len(args) < 2: parser.print_help() sys.exit(1) - file = args[0] + db = args[0] + file = args[1] if not os.path.isfile(file): print "cannot find input file %s" % (file, ) sys.exit(2) - (ev_heads, ctx_heads) = find_heads(file) + have_to_clean = 0 + if os.path.isfile(db): + if options.clean: + have_to_clean = 1 + else: + print "database %s already exists (use different name or use -c)" % (db, ) + sys.exit(3) - if not options.schema and not options.events: - options.schema = 1 - options.events = 1 - - if options.schema: - print ev_create_stmt(ev_heads) - print ctx_create_stmt(ctx_heads) + (ev_heads, ctx_heads) = find_heads(file) - if options.events: - print 'begin transaction;' - fill_tables(file) - print 'commit;' + conn = sqlite3.connect(db) + if have_to_clean: + conn.execute("drop table ctx") + conn.execute("drop table ev") + conn.execute(ev_create_stmt(ev_heads)) + conn.execute(ctx_create_stmt(ctx_heads)) + fill_tables(conn, file) + conn.commit() if __name__ == "__main__": main() -- 2.20.1