From acf133de4cc482ba8c4954179757e301f52c751a Mon Sep 17 00:00:00 2001 From: John Nduli Date: Tue, 24 Sep 2024 09:45:30 +0300 Subject: fix: rdf and sql queries for fetching rif data SQL queries fail because we get bytes instead of strings which cannot be decoded. We decode results from DictCursor to fix this. For RDF, we cast `?symbol` to a string. --- gn3/db/rdf/wiki.py | 2 +- gn3/db/wiki.py | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'gn3/db') diff --git a/gn3/db/rdf/wiki.py b/gn3/db/rdf/wiki.py index 4464eed..b2b301a 100644 --- a/gn3/db/rdf/wiki.py +++ b/gn3/db/rdf/wiki.py @@ -86,7 +86,7 @@ CONSTRUCT { rdf:type gnc:GNWikiEntry ; dct:identifier ?id_ ; dct:created ?createTime . - FILTER ( LCASE(?symbol) = LCASE('$symbol') ) . + FILTER ( LCASE(STR(?symbol)) = LCASE("$symbol") ) . { SELECT (MAX(?vers) AS ?max) ?id_ WHERE { ?comment dct:identifier ?id_ ; diff --git a/gn3/db/wiki.py b/gn3/db/wiki.py index c3c68e0..4721a69 100644 --- a/gn3/db/wiki.py +++ b/gn3/db/wiki.py @@ -9,6 +9,16 @@ class MissingDBDataException(Exception): """Error due to DB missing some data""" +def _decode_dict(result: dict): + new_result = {} + for k, v in result.items(): + if isinstance(v, bytes): + new_result[k] = v.decode() + else: + new_result[k] = v + return new_result + + def get_latest_comment(connection, comment_id: int) -> int: """ Latest comment is one with the highest versionId """ cursor = connection.cursor(DictCursor) @@ -20,7 +30,7 @@ def get_latest_comment(connection, comment_id: int) -> int: ORDER BY versionId DESC LIMIT 1; """ cursor.execute(query, (str(comment_id),)) - result = cursor.fetchone() + result = _decode_dict(cursor.fetchone()) if (pubmed_ids := result.get("pubmed_ids")) is None: pubmed_ids = "" result["pubmed_ids"] = [x.strip() for x in pubmed_ids.split()] @@ -31,7 +41,7 @@ def get_latest_comment(connection, comment_id: int) -> int: """ cursor.execute(categories_query, (str(comment_id), result["version"])) - categories = cursor.fetchall() + categories = [_decode_dict(x) for x in cursor.fetchall()] result["categories"] = [x["Name"] for x in categories] return result -- cgit v1.2.3