Skip to content

Commit cff61bb

Browse files
committed
Avoid formatting of psql commands (fixes andialbrecht#469).
1 parent fcbccb8 commit cff61bb

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Bug Fixes
2727
* Fix issue with strip_comments causing a syntax error (issue425, by fredyw).
2828
* Fix formatting on INSERT which caused staircase effect on values (issue329,
2929
by fredyw).
30+
* Avoid formatting of psql commands (issue469).
3031

3132
Internal Changes
3233

sqlparse/keywords.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def is_keyword(value):
4343
(r'%(\(\w+\))?s', tokens.Name.Placeholder),
4444
(r'(?<!\w)[$:?]\w+', tokens.Name.Placeholder),
4545

46+
(r'\\\w+', tokens.Command),
47+
4648
# FIXME(andi): VALUES shouldn't be listed here
4749
# see https://github.com/andialbrecht/sqlparse/pull/64
4850
# IN is special, it may be followed by a parenthesis, but

sqlparse/sql.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,3 +625,7 @@ class Operation(TokenList):
625625

626626
class Values(TokenList):
627627
"""Grouping of values"""
628+
629+
630+
class Command(TokenList):
631+
"""Grouping of CLI commands."""

sqlparse/tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __repr__(self):
5555

5656
# Generic types for non-source code
5757
Generic = Token.Generic
58+
Command = Generic.Command
5859

5960
# String and some others are not direct children of Token.
6061
# alias them:
@@ -66,4 +67,3 @@ def __repr__(self):
6667
DML = Keyword.DML
6768
DDL = Keyword.DDL
6869
CTE = Keyword.CTE
69-
Command = Keyword.Command

tests/test_regressions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,3 +366,10 @@ def test_issue322_concurrently_is_keyword():
366366
def test_issue359_index_error_assignments(s):
367367
sqlparse.parse(s)
368368
sqlparse.format(s, strip_comments=True)
369+
370+
371+
def test_issue469_copy_as_psql_command():
372+
formatted = sqlparse.format(
373+
'\\copy select * from foo',
374+
keyword_case='upper', identifier_case='capitalize')
375+
assert formatted == '\\copy SELECT * FROM Foo'

tests/test_tokenize.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,9 @@ def test_parse_order_by():
195195
p = sqlparse.parse('ORDER BY')[0]
196196
assert len(p.tokens) == 1
197197
assert p.tokens[0].ttype is T.Keyword
198+
199+
200+
def test_cli_commands():
201+
p = sqlparse.parse('\\copy')[0]
202+
assert len(p.tokens) == 1
203+
assert p.tokens[0].ttype == T.Command

0 commit comments

Comments
 (0)