From e1cb78d5a1e5bea14041a52af88d1ee1e80be7cf Mon Sep 17 00:00:00 2001 From: Lukas Plank Date: Mon, 28 Oct 2024 14:01:19 +0100 Subject: [PATCH] fix: correct regex pattern for SELECT clause extraction The new regex non-greedily matches everything after "select" and before "where". "[\s\S]*?" basically means ".*?" but is more general because it also matches linebreaks without the re.DOTALL flag. See https://docs.python.org/3/library/re.html#re.DOTALL. Fixes #122. --- rdfproxy/utils/sparql_utils.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/rdfproxy/utils/sparql_utils.py b/rdfproxy/utils/sparql_utils.py index b76cb45..799f8d4 100644 --- a/rdfproxy/utils/sparql_utils.py +++ b/rdfproxy/utils/sparql_utils.py @@ -20,21 +20,21 @@ """) -def replace_query_select_clause(query: str, repl: str) -> str: +def replace_query_select_clause(query: str, repl: str): """Replace the SELECT clause of a query with repl.""" - if re.search(r"select\s.+", query, re.I) is None: + pattern = r"(select\s+[\s\S]*?)(?=\s+where)" + + if re.search(pattern=pattern, string=query, flags=re.IGNORECASE) is None: raise Exception("Unable to obtain SELECT clause.") - count_query = re.sub( - pattern=r"select\s.+", + return re.sub( + pattern=pattern, repl=repl, string=query, count=1, - flags=re.I, + flags=re.IGNORECASE, ) - return count_query - def construct_count_query(query: str, model: type[_TModelInstance]) -> str: """Construct a generic count query from a SELECT query."""