From bdad1e3919aabdac8d8fa619c7277573c28c0436 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Wed, 31 Jan 2024 17:55:40 +0100
Subject: [PATCH] ENH: add function that escapes characters

---
 CHANGELOG.md               |  1 +
 src/linkahead/apiutils.py  | 19 +++++++++++++++++++
 unittests/test_apiutils.py | 18 +++++++++++++-----
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b2cb3685..7fccf7c3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
   which allows to identify property values with each other in case that one is
   an id and the other is an Entity with this id. Default is ``False``, so no
   change to the default behavior.
+* apiutils.escape_quoted_text for escaping text in queries.
 
 ### Changed ###
 
diff --git a/src/linkahead/apiutils.py b/src/linkahead/apiutils.py
index 39f97fcd..be6b2d82 100644
--- a/src/linkahead/apiutils.py
+++ b/src/linkahead/apiutils.py
@@ -623,3 +623,22 @@ def _same_id_as_resolved_entity(this, that):
     if not isinstance(this, Entity) and isinstance(that, Entity):
         return that.id is not None and that.id == this
     return this == that
+
+
+def escape_quoted_text(text: str) -> str:
+    """The characters `\` and `*` need to be escaped if used in quoted expressions in the query
+    language.
+
+    This function return the given string where the characters `\` and `*` are escaped by a `\`.
+
+    Parameters
+    ----------
+    text : str
+        The text to be escaped
+
+    Returns
+    -------
+    str
+        The escaped text
+    """
+    return text.replace('\\', r'\\').replace('*', r'\*')
diff --git a/unittests/test_apiutils.py b/unittests/test_apiutils.py
index 549312c3..b9f5cd0f 100644
--- a/unittests/test_apiutils.py
+++ b/unittests/test_apiutils.py
@@ -26,13 +26,13 @@
 # A. Schlemmer, 02/2018
 
 
-import pytest
 import linkahead as db
 import linkahead.apiutils
-from linkahead.apiutils import (apply_to_ids, compare_entities, create_id_query,
-                                empty_diff, EntityMergeConflictError,
-                                resolve_reference, merge_entities)
-
+import pytest
+from linkahead.apiutils import (EntityMergeConflictError, apply_to_ids,
+                                compare_entities, create_id_query, empty_diff,
+                                escape_quoted_text, merge_entities,
+                                resolve_reference)
 from linkahead.common.models import SPECIAL_ATTRIBUTES
 
 
@@ -633,3 +633,11 @@ def test_merge_id_with_resolved_entity():
     merge_entities(recA, recB, merge_id_with_resolved_entity=True)
     assert recA.get_property(rtname).value == [ref_id, ref_id*2]
     assert recA.get_property(rtname).value == recB.get_property(rtname).value
+
+
+
+def test_escape_quoted_text():
+    assert escape_quoted_text("bla") == "bla"
+    assert escape_quoted_text("bl\\a") == "bl\\\\a"
+    assert escape_quoted_text("bl*a") == "bl\\*a"
+    assert escape_quoted_text("bl*ab\\\\lab\\*labla") == "bl\\*ab\\\\\\\\lab\\\\\\*labla"
-- 
GitLab