Skip to content

Commit

Permalink
feat #198: support psycopg placeholders (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
tconbeer authored Aug 5, 2022
1 parent 5a1ecfd commit cf44cb9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Features

- sqlfmt now supports the psycopg placeholders `%s` and `%(name)s` ([#198](https://github.com/tconbeer/sqlfmt/issues/198) - thank you [@snorkysnark](https://github.com/snorkysnark)!)

### Formatting Changes + Bug Fixes

- sqlfmt now standardizes whitespace inside word tokens ([#201](https://github.com/tconbeer/sqlfmt/issues/201))
Expand Down
21 changes: 13 additions & 8 deletions src/sqlfmt/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ def __init__(self) -> None:
actions.add_node_to_buffer, token_type=TokenType.COLON
),
),
Rule(
name="other_identifiers",
priority=907,
pattern=group(
r"@\w+", # stages
r"\$\d+", # pg placeholders
r"%(\([^%()]+\))?s", # psycopg placeholders
),
action=partial(
actions.add_node_to_buffer, token_type=TokenType.NAME
),
),
Rule(
name="operator",
priority=910,
Expand Down Expand Up @@ -249,6 +261,7 @@ def __init__(self) -> None:
r"-\|-",
r"[*+?]?\?", # regex greedy/non-greedy, also ?
r"!!", # negate text match
r"%%", # psycopg escaped mod operator
r"[+\-*/%&|^=<>:#!]=?", # singles
),
action=partial(
Expand Down Expand Up @@ -410,14 +423,6 @@ def __init__(self) -> None:
+ group(r"\W", r"$"),
action=actions.handle_set_operator,
),
Rule(
name="other_identifiers",
priority=4000,
pattern=group(r"@\w+", r"\$\d+"),
action=partial(
actions.add_node_to_buffer, token_type=TokenType.NAME
),
),
Rule(
name="name",
priority=5000,
Expand Down
11 changes: 11 additions & 0 deletions tests/data/unformatted/119_psycopg_placeholders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SELECT image_data FROM images WHERE id = %s;
SELECT image_data, dividend %% divisor as escaped_mod_operator FROM images WHERE id in (%(one)s, %(two)s, %(three)s, %(f!o^u+r)s);
)))))__SQLFMT_OUTPUT__(((((
select image_data
from images
where id = %s
;
select image_data, dividend %% divisor as escaped_mod_operator
from images
where id in (%(one)s, %(two)s, %(three)s, %(f!o^u+r)s)
;
1 change: 1 addition & 0 deletions tests/functional_tests/test_general_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"unformatted/116_chained_booleans.sql",
"unformatted/117_whitespace_in_tokens.sql",
"unformatted/118_within_group.sql",
"unformatted/119_psycopg_placeholders.sql",
"unformatted/200_base_model.sql",
"unformatted/201_basic_snapshot.sql",
"unformatted/202_unpivot_macro.sql",
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_tests/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ def test_rule_props_are_unique(self, dialect: Polyglot) -> None:
("main", "operator", "+"),
("main", "operator", "-"),
("main", "operator", "/"),
("main", "operator", "%"),
("main", "operator", "%%"),
("main", "operator", "<>"),
("main", "operator", "||"),
("main", "operator", "=>"),
Expand Down Expand Up @@ -183,6 +185,9 @@ def test_rule_props_are_unique(self, dialect: Polyglot) -> None:
("main", "name", "replace"),
("main", "other_identifiers", "$2"),
("main", "other_identifiers", "@my_unquoted_stage"),
("main", "other_identifiers", "%s"),
("main", "other_identifiers", "%(name)s"),
("main", "other_identifiers", "%(anything! else!)s"),
("main", "newline", "\n"),
("jinja", "jinja_comment", "{# my comment #}"),
("jinja", "jinja_comment", "{#-my comment -#}"),
Expand Down

0 comments on commit cf44cb9

Please sign in to comment.