Add CRUD operations for project info table

This commit is contained in:
D. Berge
2020-09-10 20:39:06 +02:00
parent 3b5b200f08
commit d192eb3668

View File

@@ -544,3 +544,60 @@ class Datastore:
self.maybe_commit()
# We do not commit if we've been passed a cursor, instead
# we assume that we are in the middle of a transaction
def add_info(self, key, value, cursor = None):
"""
Add an item of information to the project
"""
if cursor is None:
cur = self.conn.cursor()
else:
cur = cursor
qry = """
INSERT INTO info (key, value)
VALUES(%s, %s)
ON CONFLICT (key) DO UPDATE
SET value = EXCLUDED.value;
"""
cur.execute(qry, (key, value))
if cursor is None:
self.maybe_commit()
# We do not commit if we've been passed a cursor, instead
# we assume that we are in the middle of a transaction
def get_info(self, key, cursor = None):
"""
Retrieve an item of information from the project
"""
if cursor is None:
cur = self.conn.cursor()
else:
cur = cursor
qry = "SELECT value FROM info WHERE key = %s;"
cur.execute(qry, (key,))
res = cur.fetchone()
if cursor is None:
self.maybe_commit()
# We do not commit if we've been passed a cursor, instead
# we assume that we are in the middle of a transaction
return res
def del_info(self, key, cursor = None):
"""
Remove a an item of information from the project
"""
if cursor is None:
cur = self.conn.cursor()
else:
cur = cursor
qry = "DELETE FROM info WHERE key = %s;"
cur.execute(qry, (key,))
if cursor is None:
self.maybe_commit()
# We do not commit if we've been passed a cursor, instead
# we assume that we are in the middle of a transaction