Skip to content

Commit

Permalink
Support more placeholders (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored Jun 14, 2022
1 parent 705a77b commit 41de3a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sqltree/tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,26 @@ def tokenize(sql: str, dialect: Dialect) -> Iterable[Token]:
if next_char is not None and next_char.isalpha():
token_type = TokenType.placeholder
text = "%" + _consume_identifier(pi)
elif next_char == "(":
token_type = TokenType.placeholder
pi.next()
text = "%("
text += _consume_identifier(pi)
next_char = pi.next()
if next_char != ")":
raise TokenizeError(f"expected ')', got {next_char!r}")
text += ")"
text += _consume_identifier(pi)
elif next_char == "%":
pi.next()
token_type = TokenType.punctuation
text = "%%"
else:
token_type = TokenType.punctuation
text = "%"
elif char == "?":
token_type = TokenType.placeholder
text = char
elif char in starting_char_to_continuations:
token_type = TokenType.punctuation
continuations = starting_char_to_continuations[char]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,12 @@ def test_create_table() -> None:
def test_rename_tables() -> None:
assert format("rename table x to y") == "RENAME TABLE x TO y\n"
assert format("rename table x to y, z to w") == "RENAME TABLE x TO y, z TO w\n"


def test_placeholder() -> None:
assert format("select * from x where x = ?") == "SELECT *\nFROM x\nWHERE x = ?\n"
assert format("select * from x where x = %s") == "SELECT *\nFROM x\nWHERE x = %s\n"
assert (
format("select * from x where x = %(x)s")
== "SELECT *\nFROM x\nWHERE x = %(x)s\n"
)

0 comments on commit 41de3a9

Please sign in to comment.