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 6, 2024
1 parent 2b6ff37 commit e1cb78d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions rdfproxy/utils/sparql_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit e1cb78d

Please sign in to comment.