home · contact · privacy
Add database connection, read and write Days through them.
[plomtask] / plomtask / db.py
index b7291dba4245852097f7dc3146e9b1d6d355bbcf..50eb737cbfa3924eb3f393dd5440ed08fce0b8d9 100644 (file)
@@ -40,3 +40,23 @@ class DatabaseFile:
                     diff_msg = Differ().compare(retrieved_schema.splitlines(),
                                                 stored_schema.splitlines())
                     raise HandledException(msg_err + '\n'.join(diff_msg))
+
+
+class DatabaseConnection:
+    """A single connection to the database."""
+
+    def __init__(self, db_file: DatabaseFile):
+        self.file = db_file
+        self.conn = sql_connect(self.file.path)
+
+    def commit(self):
+        """Commit SQL transaction."""
+        self.conn.commit()
+
+    def exec(self, code: str, inputs: tuple = ()):
+        """Add commands to SQL transaction."""
+        return self.conn.execute(code, inputs)
+
+    def close(self):
+        """Close DB connection."""
+        self.conn.close()