Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more placeholders #53

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)