Skip to content

Commit

Permalink
build regex pattern once
Browse files Browse the repository at this point in the history
  • Loading branch information
imryche committed Jun 30, 2024
1 parent 22d22b8 commit c280221
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions litequery/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ class Query:
def parse_queries(path):
with open(path) as f:
content = f.read()

raw_queries = re.findall(r"-- name: (.+)\n([\s\S]*?);", content)

queries = []
op_pattern = "|".join("\\" + "\\".join(list(op.value)) for op in Op if op.value)
pattern = rf"^([a-z_][a-z0-9_-]*)({op_pattern})?$"
for query_name, sql in raw_queries:
op_pattern = "|".join("\\" + "\\".join(list(op.value)) for op in Op if op.value)
pattern = rf"^([a-z_][a-z0-9_-]*)({op_pattern})?$"
match = re.match(pattern, query_name)
if not match:
raise NameError(f'Invalid query name: "{query_name}"')
Expand All @@ -37,6 +39,7 @@ def parse_queries(path):
args = re.findall(r":(\w+)", sql)
query = Query(name=query_name, sql=sql, args=args, op=op)
queries.append(query)

return queries


Expand Down

0 comments on commit c280221

Please sign in to comment.