2019-06-27 10:37:23 +00:00
|
|
|
import sqlite3
|
|
|
|
import os
|
|
|
|
|
|
|
|
class Database:
|
|
|
|
def __init__(self, args):
|
|
|
|
filename = ":memory:" if args.db is None else args.db
|
|
|
|
|
2019-09-09 13:29:15 +00:00
|
|
|
if args.new_db and os.path.exists(filename):
|
2019-06-27 10:37:23 +00:00
|
|
|
os.remove(filename)
|
|
|
|
|
|
|
|
self.new = not os.path.exists(filename)
|
|
|
|
self.db = sqlite3.connect(filename)
|
2019-08-21 10:16:10 +00:00
|
|
|
|
|
|
|
self.init("CREATE TABLE StepsDone ( step varchar(32) )")
|
|
|
|
self.commit()
|
2019-06-27 10:37:23 +00:00
|
|
|
|
|
|
|
def execute(self, *args, **kwargs):
|
|
|
|
return self.db.execute(*args, **kwargs)
|
|
|
|
|
|
|
|
def init(self, *args, **kwargs):
|
|
|
|
# same as execute, only skipped if not a new database file
|
|
|
|
if self.new:
|
|
|
|
return self.execute(*args, **kwargs)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self.db.commit()
|
2019-08-21 10:57:42 +00:00
|
|
|
|
|
|
|
def is_step_done(self, step_name):
|
|
|
|
wc_done = self.db.execute("SELECT count(*) FROM StepsDone WHERE step=?", (step_name, )).fetchone()
|
|
|
|
return wc_done[0] != 0
|
|
|
|
|
|
|
|
def step_is_done(self, step_name):
|
|
|
|
self.db.execute("INSERT INTO StepsDone (step) VALUES (?)", (step_name, ))
|
|
|
|
self.db.commit()
|
|
|
|
|