Skip to content
Snippets Groups Projects
Commit bdad1e39 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

ENH: add function that escapes characters

parent f9695ed8
No related branches found
No related tags found
2 merge requests!130Release v0.14.0,!125F escape
Pipeline #46630 passed with warnings
...@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ...@@ -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 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 an id and the other is an Entity with this id. Default is ``False``, so no
change to the default behavior. change to the default behavior.
* apiutils.escape_quoted_text for escaping text in queries.
### Changed ### ### Changed ###
......
...@@ -623,3 +623,22 @@ def _same_id_as_resolved_entity(this, that): ...@@ -623,3 +623,22 @@ def _same_id_as_resolved_entity(this, that):
if not isinstance(this, Entity) and isinstance(that, Entity): if not isinstance(this, Entity) and isinstance(that, Entity):
return that.id is not None and that.id == this return that.id is not None and that.id == this
return this == that 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'\*')
...@@ -26,13 +26,13 @@ ...@@ -26,13 +26,13 @@
# A. Schlemmer, 02/2018 # A. Schlemmer, 02/2018
import pytest
import linkahead as db import linkahead as db
import linkahead.apiutils import linkahead.apiutils
from linkahead.apiutils import (apply_to_ids, compare_entities, create_id_query, import pytest
empty_diff, EntityMergeConflictError, from linkahead.apiutils import (EntityMergeConflictError, apply_to_ids,
resolve_reference, merge_entities) compare_entities, create_id_query, empty_diff,
escape_quoted_text, merge_entities,
resolve_reference)
from linkahead.common.models import SPECIAL_ATTRIBUTES from linkahead.common.models import SPECIAL_ATTRIBUTES
...@@ -633,3 +633,11 @@ def test_merge_id_with_resolved_entity(): ...@@ -633,3 +633,11 @@ def test_merge_id_with_resolved_entity():
merge_entities(recA, recB, merge_id_with_resolved_entity=True) 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 == [ref_id, ref_id*2]
assert recA.get_property(rtname).value == recB.get_property(rtname).value 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"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment