Skip to content

Commit

Permalink
Add support for some of the JSON operators (fixes #682).
Browse files Browse the repository at this point in the history
  • Loading branch information
andialbrecht committed Mar 16, 2024
1 parent 5bb129d commit 6b05583
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Enhancements:
Some database backends love statements without semicolon (issue742).
* Support TypedLiterals in get_parameters (pr649, by Khrol).
* Improve splitting of Transact SQL when using GO keyword (issue762).
* Support for some JSON operators (issue682).

Bug Fixes

Expand Down
2 changes: 2 additions & 0 deletions sqlparse/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
# but the match isn't a keyword.
(r'\w[$#\w]*', PROCESS_AS_KEYWORD),
(r'[;:()\[\],\.]', tokens.Punctuation),
# JSON operators
(r'(\->>?|#>>?|@>|<@|\?\|?|\?&|\-|#\-)', tokens.Operator),
(r'[<>=~!]+', tokens.Operator.Comparison),
(r'[+/@#%^&|^-]+', tokens.Operator),
]
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,17 @@ def test_configurable_regex():
for t in tokens
if t.ttype not in sqlparse.tokens.Whitespace
)[4] == (sqlparse.tokens.Keyword, "zorder by")


@pytest.mark.parametrize('sql', [
'->', '->>', '#>', '#>>',
'@>', '<@',
# leaving ? out for now, they're somehow ambiguous as placeholders
# '?', '?|', '?&',
'||', '-', '#-'
])
def test_json_operators(sql):
p = sqlparse.parse(sql)
assert len(p) == 1
assert len(p[0].tokens) == 1
assert p[0].tokens[0].ttype == sqlparse.tokens.Operator

0 comments on commit 6b05583

Please sign in to comment.