Skip to content

Commit 7c514ba

Browse files
committed
Handle whitespace in literals and identifiers
As the whitespace has meaning within a literal/identifier.
1 parent 9f7be32 commit 7c514ba

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎src/sql_tstring/__init__.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def _print_node(
151151
node: Element,
152152
placeholders: list | None = None,
153153
dialect: str = "sql",
154+
strip: bool = True,
154155
) -> str:
155156
if placeholders is None:
156157
placeholders = []
@@ -201,10 +202,13 @@ def _print_node(
201202
placeholders.append(None)
202203
result = f"${len(placeholders)}" if dialect == "asyncpg" else "?"
203204
case Literal():
204-
value = "".join(_print_node(part, placeholders, dialect) for part in node.parts)
205+
value = "".join(_print_node(part, placeholders, dialect, False) for part in node.parts)
205206
result = f"'{value}'"
206207

207-
return result.strip()
208+
if strip:
209+
return result.strip()
210+
else:
211+
return result
208212

209213

210214
def _replace_placeholders(

‎src/sql_tstring/parser.py‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,14 @@ def _parse_string(
410410
current_node: Node,
411411
statements: list[Statement],
412412
) -> Node:
413-
tokens = [part.strip() for part in SPLIT_RE.split(raw) if part.strip() != ""]
413+
tokens = []
414+
for part in SPLIT_RE.split(raw):
415+
if part == "":
416+
continue
417+
elif part.strip() == "":
418+
tokens.append(part)
419+
else:
420+
tokens.append(part.strip())
414421
index = 0
415422
while index < len(tokens):
416423
raw_current_token = tokens[index]
@@ -424,6 +431,8 @@ def _parse_string(
424431
)
425432
else:
426433
current_node.parts.append(Part(parent=current_node, text=raw_current_token))
434+
elif raw_current_token.strip() == "":
435+
consumed = 1
427436
elif isinstance(current_node, (Function, Group)):
428437
if current_token == ")":
429438
group_or_function = _find_node(current_node, (Function, Group))
@@ -522,9 +531,12 @@ def _parse_clause(
522531
index = 0
523532
clause_entry = CLAUSES
524533
text = ""
525-
while index < len(tokens) and tokens[index].lower() in clause_entry:
526-
clause_entry = cast(ClauseDictionary, clause_entry[tokens[index].lower()])
527-
text = f"{text} {tokens[index]}".strip()
534+
while index < len(tokens) and (
535+
tokens[index].strip() == "" or tokens[index].lower() in clause_entry
536+
):
537+
if tokens[index].strip() != "":
538+
clause_entry = cast(ClauseDictionary, clause_entry[tokens[index].lower()])
539+
text = f"{text} {tokens[index]}".strip()
528540
index += 1
529541

530542
if isinstance(current_node, (Function, Group)):

‎tests/test_parsing.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,13 @@ def test_nested() -> None:
124124
def test_opening_parenthesis() -> None:
125125
query, _ = sql("(SELECT x FROM y) UNION ALL (SELECT z FROM y)", locals())
126126
assert query == "(SELECT x FROM y) UNION ALL (SELECT z FROM y)"
127+
128+
129+
def test_literal() -> None:
130+
query, _ = sql("SELECT 'ABC DEF' FROM y", locals())
131+
assert query == "SELECT 'ABC DEF' FROM y"
132+
133+
134+
def test_identifier() -> None:
135+
query, _ = sql("""SELECT "ABC DEF" FROM y""", locals())
136+
assert query == """SELECT "ABC DEF" FROM y"""

0 commit comments

Comments
 (0)