2021-06-08 12:37:10 +02:00
|
|
|
import sqlite3
|
|
|
|
|
2021-07-31 20:49:38 +02:00
|
|
|
from fsfwgen.core import get_console_logger
|
|
|
|
|
|
|
|
LOGGER = get_console_logger()
|
|
|
|
|
2021-06-08 12:37:10 +02:00
|
|
|
|
|
|
|
class SqlWriter:
|
|
|
|
def __init__(self, db_filename: str):
|
|
|
|
self.filename = db_filename
|
|
|
|
self.conn = sqlite3.connect(self.filename)
|
|
|
|
|
|
|
|
def open(self, sql_creation_command: str):
|
2021-07-31 20:49:38 +02:00
|
|
|
LOGGER.info(f'SQL Writer: Opening {self.filename}')
|
2021-06-08 12:37:10 +02:00
|
|
|
self.conn.execute(sql_creation_command)
|
|
|
|
|
|
|
|
def delete(self, sql_deletion_command):
|
2021-07-31 20:49:38 +02:00
|
|
|
LOGGER.info("SQL Writer: Deleting SQL table")
|
2021-06-08 12:37:10 +02:00
|
|
|
self.conn.execute(sql_deletion_command)
|
|
|
|
|
|
|
|
def write_entries(self, sql_insertion_command, current_entry):
|
|
|
|
cur = self.conn.cursor()
|
|
|
|
cur.execute(sql_insertion_command, current_entry)
|
|
|
|
return cur.lastrowid
|
|
|
|
|
|
|
|
def commit(self):
|
2021-07-31 20:49:38 +02:00
|
|
|
LOGGER.info("SQL Writer: Commiting SQL table")
|
2021-06-08 12:37:10 +02:00
|
|
|
self.conn.commit()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.conn.close()
|
|
|
|
|
2021-07-31 20:49:38 +02:00
|
|
|
def sql_writing_helper(
|
|
|
|
self, creation_cmd, insertion_cmd, mib_table: dict, deletion_cmd: str = ""
|
|
|
|
):
|
2021-06-08 12:37:10 +02:00
|
|
|
if deletion_cmd != "":
|
|
|
|
self.delete(deletion_cmd)
|
|
|
|
self.open(creation_cmd)
|
|
|
|
for i in mib_table:
|
|
|
|
self.write_entries(insertion_cmd, mib_table[i])
|
|
|
|
self.commit()
|
|
|
|
self.close()
|