Skip to content
Snippets Groups Projects
Verified Commit e9f222e8 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT: caosdb/linkahead and new escape function

parent db8d0b3a
Branches
Tags
1 merge request!160STY: styling
Pipeline #47124 passed with warnings
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
""" """
Crawl a file structure using a yaml cfood definition and synchronize Crawl a file structure using a yaml cfood definition and synchronize
the acuired data with CaosDB. the acuired data with LinkAhead.
""" """
from __future__ import annotations from __future__ import annotations
...@@ -44,18 +44,18 @@ from datetime import datetime ...@@ -44,18 +44,18 @@ from datetime import datetime
from enum import Enum from enum import Enum
from typing import Any, Optional, Union from typing import Any, Optional, Union
import caosdb as db import linkahead as db
import yaml import yaml
from caosadvancedtools.cache import UpdateCache from caosadvancedtools.cache import UpdateCache
from caosadvancedtools.crawler import Crawler as OldCrawler from caosadvancedtools.crawler import Crawler as OldCrawler
from caosadvancedtools.serverside.helper import send_mail from caosadvancedtools.serverside.helper import send_mail
from caosadvancedtools.utils import create_entity_link from caosadvancedtools.utils import create_entity_link
from caosdb.apiutils import (EntityMergeConflictError, compare_entities, from linkahead.apiutils import (EntityMergeConflictError, compare_entities,
merge_entities) merge_entities)
from caosdb.cached import cache_clear, cached_get_entity_by from linkahead.cached import cache_clear, cached_get_entity_by
from caosdb.exceptions import EmptyUniqueQueryError from linkahead.exceptions import EmptyUniqueQueryError
from linkahead.common.datatype import get_list_datatype, is_reference from linkahead.common.datatype import get_list_datatype, is_reference
from linkahead.utils.escape import escape_quoted_text from linkahead.utils.escape import escape_squoted_text
from .config import get_config_setting from .config import get_config_setting
from .converters import Converter, ConverterValidationError from .converters import Converter, ConverterValidationError
...@@ -900,8 +900,8 @@ class Crawler(object): ...@@ -900,8 +900,8 @@ class Crawler(object):
@staticmethod @staticmethod
def _get_property_id_for_datatype(rtname: str, name: str): def _get_property_id_for_datatype(rtname: str, name: str):
return cached_get_entity_by( return cached_get_entity_by(
query=f"FIND Entity '{escape_quoted_text(rtname)}' " query=f"FIND Entity '{escape_squoted_text(rtname)}' "
f"with name='{escape_quoted_text(name)}'").id f"with name='{escape_squoted_text(name)}'").id
@staticmethod @staticmethod
def replace_name_with_referenced_entity_id(prop: db.Property): def replace_name_with_referenced_entity_id(prop: db.Property):
......
...@@ -45,13 +45,13 @@ from importlib_resources import files ...@@ -45,13 +45,13 @@ from importlib_resources import files
from jsonschema import validate from jsonschema import validate
from typing import Any, Optional, Type, Union from typing import Any, Optional, Type, Union
import caosdb as db import linkahead as db
from caosadvancedtools.cache import UpdateCache, Cache from caosadvancedtools.cache import UpdateCache, Cache
from caosadvancedtools.crawler import Crawler as OldCrawler from caosadvancedtools.crawler import Crawler as OldCrawler
from caosdb.apiutils import (compare_entities, EntityMergeConflictError, from linkahead.apiutils import (compare_entities, EntityMergeConflictError,
merge_entities) merge_entities)
from caosdb.common.datatype import is_reference from linkahead.common.datatype import is_reference
from .converters import Converter, DirectoryConverter, ConverterValidationError from .converters import Converter, DirectoryConverter, ConverterValidationError
......
...@@ -34,8 +34,8 @@ from typing import Any ...@@ -34,8 +34,8 @@ from typing import Any
import linkahead as db import linkahead as db
import yaml import yaml
from caosdb.cached import cached_get_entity_by, cached_query from linkahead.cached import cached_get_entity_by, cached_query
from caosdb.utils.escape import escape_quoted_text from linkahead.utils.escape import escape_squoted_text
from .identifiable import Identifiable from .identifiable import Identifiable
from .utils import has_parent from .utils import has_parent
...@@ -46,22 +46,24 @@ logger = logging.getLogger(__name__) ...@@ -46,22 +46,24 @@ logger = logging.getLogger(__name__)
def get_children_of_rt(rtname): def get_children_of_rt(rtname):
"""Supply the name of a recordtype. This name and the name of all children RTs are returned in """Supply the name of a recordtype. This name and the name of all children RTs are returned in
a list""" a list"""
escaped = escape_quoted_text(rtname) escaped = escape_squoted_text(rtname)
return [p.name for p in cached_query(f"FIND RECORDTYPE '{escaped}'")] return [p.name for p in cached_query(f"FIND RECORDTYPE '{escaped}'")]
def convert_value(value: Any): def convert_value(value: Any) -> str:
""" Returns a string representation of the value that is suitable """ Return a string representation of the value suitable for the search query.
to be used in the query
looking for the identified record. This is for search queries looking for the identified record.
Parameters Parameters
---------- ----------
value : Any type, the value that shall be returned and potentially converted. value: Any
The value to be converted.
Returns Returns
------- -------
out : the string reprensentation of the value out: str
the string reprensentation of the value.
""" """
...@@ -72,7 +74,7 @@ def convert_value(value: Any): ...@@ -72,7 +74,7 @@ def convert_value(value: Any):
elif isinstance(value, bool): elif isinstance(value, bool):
return str(value).upper() return str(value).upper()
elif isinstance(value, str): elif isinstance(value, str):
return escape_quoted_text(value) return escape_squoted_text(value)
else: else:
return str(value) return str(value)
...@@ -115,7 +117,7 @@ identifiabel, identifiable and identified record) for a Record. ...@@ -115,7 +117,7 @@ identifiabel, identifiable and identified record) for a Record.
query_string = "FIND RECORD " query_string = "FIND RECORD "
if ident.record_type is not None: if ident.record_type is not None:
escaped_rt = escape_quoted_text(ident.record_type) escaped_rt = escape_squoted_text(ident.record_type)
query_string += f"'{escaped_rt}'" query_string += f"'{escaped_rt}'"
for ref in ident.backrefs: for ref in ident.backrefs:
eid = ref eid = ref
...@@ -126,7 +128,7 @@ identifiabel, identifiable and identified record) for a Record. ...@@ -126,7 +128,7 @@ identifiabel, identifiable and identified record) for a Record.
query_string += " WITH " query_string += " WITH "
if ident.name is not None: if ident.name is not None:
query_string += "name='{}'".format(escape_quoted_text(ident.name)) query_string += "name='{}'".format(escape_squoted_text(ident.name))
if len(ident.properties) > 0: if len(ident.properties) > 0:
query_string += " AND " query_string += " AND "
...@@ -142,10 +144,10 @@ identifiabel, identifiable and identified record) for a Record. ...@@ -142,10 +144,10 @@ identifiabel, identifiable and identified record) for a Record.
query_string = "" query_string = ""
for pname, pvalue in entity.properties.items(): for pname, pvalue in entity.properties.items():
if pvalue is None: if pvalue is None:
query_string += "'" + escape_quoted_text(pname) + "' IS NULL AND " query_string += "'" + escape_squoted_text(pname) + "' IS NULL AND "
elif isinstance(pvalue, list): elif isinstance(pvalue, list):
for v in pvalue: for v in pvalue:
query_string += ("'" + escape_quoted_text(pname) + "'='" + query_string += ("'" + escape_squoted_text(pname) + "'='" +
convert_value(v) + "' AND ") convert_value(v) + "' AND ")
# TODO: (for review) # TODO: (for review)
...@@ -159,7 +161,7 @@ identifiabel, identifiable and identified record) for a Record. ...@@ -159,7 +161,7 @@ identifiabel, identifiable and identified record) for a Record.
# IdentifiableAdapter.create_property_query(p.value) + # IdentifiableAdapter.create_property_query(p.value) +
# ") AND ") # ") AND ")
else: else:
query_string += ("'" + escape_quoted_text(pname) + "'='" + query_string += ("'" + escape_squoted_text(pname) + "'='" +
convert_value(pvalue) + "' AND ") convert_value(pvalue) + "' AND ")
# remove the last AND # remove the last AND
return query_string[:-4] return query_string[:-4]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment