Skip to content

Commit

Permalink
fix: correct regex pattern for SELECT clause extraction
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lu-pl committed Nov 8, 2024
1 parent 2b6ff37 commit cb15e14
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,21 @@

def replace_query_select_clause(query: str, repl: str) -> str:
"""Replace the SELECT clause of a query with repl."""
if re.search(r"select\s.+", query, re.I) is None:
pattern: re.Pattern = re.compile(
r"select\s+.*?(?=\s+where)", flags=re.IGNORECASE | re.DOTALL
)

if re.search(pattern=pattern, string=query) is None:
raise Exception("Unable to obtain SELECT clause.")

count_query = re.sub(
pattern=r"select\s.+",
modified_query = re.sub(
pattern=pattern,
repl=repl,
string=query,
count=1,
flags=re.I,
)

return count_query
return modified_query


def construct_count_query(query: str, model: type[_TModelInstance]) -> str:
Expand Down

0 comments on commit cb15e14

Please sign in to comment.