|
|
@ -15,7 +15,7 @@ class SQLitedb:
|
|
|
|
db = ""
|
|
|
|
db = ""
|
|
|
|
cursor = None
|
|
|
|
cursor = None
|
|
|
|
connection = None
|
|
|
|
connection = None
|
|
|
|
lastrow = None
|
|
|
|
last_result = None
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, dbpath: str):
|
|
|
|
def __init__(self, dbpath: str):
|
|
|
|
db = dbpath
|
|
|
|
db = dbpath
|
|
|
@ -29,37 +29,56 @@ class SQLitedb:
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
def __del__(self):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.connection.commit()
|
|
|
|
self.connection.close()
|
|
|
|
self.connection.close()
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print("Couldn't gracefully close db: %s" % e, file=STDERR)
|
|
|
|
print("Couldn't gracefully close db: %s" % e, file=STDERR)
|
|
|
|
|
|
|
|
|
|
|
|
def query(self, qq: str):
|
|
|
|
def query(self, qq: str) -> list:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.cursor.execute(qq)
|
|
|
|
self.cursor.execute(qq)
|
|
|
|
self.lastrow = self.cursor.fetchall()
|
|
|
|
self.last_result = self.cursor.fetchall()
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
|
|
|
|
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
|
|
|
|
self.lastrow = []
|
|
|
|
self.last_result = []
|
|
|
|
return self.lastrow
|
|
|
|
return self.last_result
|
|
|
|
|
|
|
|
|
|
|
|
# sometimes we need the cursor for safety reasons, for example does sqlite3 all the security related
|
|
|
|
# sometimes we need the cursor for safety reasons, for example does sqlite3 all the security related
|
|
|
|
# escaoing in supplied strings for us, when we deliver it to con.execute in the second argument as a tuple
|
|
|
|
# escaoing in supplied strings for us, when we deliver it to con.execute in the second argument as a tuple
|
|
|
|
def getCursor(self):
|
|
|
|
def getCursor(self) -> sqlite3:
|
|
|
|
return self.cursor
|
|
|
|
return self.cursor
|
|
|
|
|
|
|
|
|
|
|
|
# we could try to utilise that ourselfs in a function. Be c a r e f u l, these values in the tuple MUST HAVE
|
|
|
|
# we could try to utilise that ourselfs in a function. Be c a r e f u l, these values in the tuple MUST HAVE
|
|
|
|
# THE RIGHT TYPE
|
|
|
|
# THE RIGHT TYPE
|
|
|
|
def safequery(self, qq: str, deliver: tuple):
|
|
|
|
def safequery(self, qq: str, deliver: tuple) -> list:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.cursor.execute(qq, deliver)
|
|
|
|
self.cursor.execute(qq, deliver)
|
|
|
|
self.lastrow = self.cursor.fetchall()
|
|
|
|
self.last_result = self.cursor.fetchall()
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
|
|
|
|
print("Couldn't execute query %s, exception: %s" % (qq, e), file=STDERR)
|
|
|
|
self.lastrow = []
|
|
|
|
self.last_result = []
|
|
|
|
except TypeError as e:
|
|
|
|
except TypeError as e:
|
|
|
|
print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=STDERR)
|
|
|
|
print("Types in given tuple doesnt match to execute query \"%s\": %s" % (qq, e), file=STDERR)
|
|
|
|
self.lastrow = []
|
|
|
|
self.last_result = []
|
|
|
|
return self.lastrow
|
|
|
|
return self.last_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def removeApplicantFromDB(self, userid: int) -> bool:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.last_result = self.cursor.execute("DELETE FROM `applications` WHERE id = ? ", [userid])
|
|
|
|
|
|
|
|
self.connection.commit()
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not delete user with id: {userid}, exception in DB: {e}") # @TODO LOGGING FFS
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def removeApplicantFromDBperUsername(self, username: str) -> bool:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
self.last_result = self.cursor.execute("DELETE FROM `applications` WHERE username = ?", [username])
|
|
|
|
|
|
|
|
self.connection.commit()
|
|
|
|
|
|
|
|
except sqlite3.Error as e:
|
|
|
|
|
|
|
|
print(f"Could not delete user {username}, exception in DB: {e}") # @TODO LOGGING
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|