Add function to delete data by file hash

This commit is contained in:
D. Berge
2020-09-06 13:38:37 +02:00
parent 3e949f6185
commit c6285e881e

View File

@@ -128,6 +128,22 @@ class Datastore:
# We do not commit if we've been passed a cursor, instead
# we assume that we are in the middle of a transaction
def del_hash(self, hash, cursor = None):
"""
Remove a hash from a survey's `file` table.
"""
if cursor is None:
cur = self.conn.cursor()
else:
cur = cursor
qry = "DELETE FROM files WHERE hash = %s;"
cur.execute(qry, (hash,))
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 list_files(self, cursor = None):
"""
List all files known to a survey.
@@ -236,16 +252,7 @@ class Datastore:
incr = records[0]["point_number"] <= records[-1]["point_number"]
# Start by deleting any online data we may have for this sequence
# FIXME Factor this out into its own function
qry = """
DELETE
FROM raw_lines rl
USING raw_lines_files rlf
WHERE
rl.sequence = rlf.sequence
AND rlf.hash = '*online*'
AND rl.sequence = %s;
"""
self.del_hash("*online*", cursor)
qry = """
INSERT INTO raw_lines (sequence, line, remarks, ntbp, incr)
@@ -354,18 +361,7 @@ class Datastore:
incr = p111.point_number(records[0]) <= p111.point_number(records[-1])
# Start by deleting any online data we may have for this sequence
# FIXME Factor this out into its own function
qry = """
DELETE
FROM raw_lines rl
USING raw_lines_files rlf
WHERE
rl.sequence = rlf.sequence
AND rlf.hash = '*online*'
AND rl.sequence = %s;
"""
cursor.execute(qry, (fileinfo["sequence"],))
self.del_hash("*online*", cursor)
qry = """
INSERT INTO raw_lines (sequence, line, remarks, ntbp, incr)