summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@gmail.com>2019-03-11 12:45:11 +0100
committerAndi Albrecht <albrecht.andi@gmail.com>2019-03-11 12:45:11 +0100
commitcff61bb93a080c1becbf2c6cd33911bc206cf587 (patch)
tree3683be35ea59d970be8ff0f46c77151615405636
parentfcbccb849b56f29fb7f3ddb958d3e10279f8d939 (diff)
downloadsqlparse-cff61bb93a080c1becbf2c6cd33911bc206cf587.tar.gz
Avoid formatting of psql commands (fixes #469).
-rw-r--r--CHANGELOG1
-rw-r--r--sqlparse/keywords.py2
-rw-r--r--sqlparse/sql.py4
-rw-r--r--sqlparse/tokens.py2
-rw-r--r--tests/test_regressions.py7
-rw-r--r--tests/test_tokenize.py6
6 files changed, 21 insertions, 1 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 78b3901..1351fb2 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -27,6 +27,7 @@ Bug Fixes
* Fix issue with strip_comments causing a syntax error (issue425, by fredyw).
* Fix formatting on INSERT which caused staircase effect on values (issue329,
by fredyw).
+* Avoid formatting of psql commands (issue469).
Internal Changes
diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py
index ad85368..602051a 100644
--- a/sqlparse/keywords.py
+++ b/sqlparse/keywords.py
@@ -43,6 +43,8 @@ SQL_REGEX = {
(r'%(\(\w+\))?s', tokens.Name.Placeholder),
(r'(?<!\w)[$:?]\w+', tokens.Name.Placeholder),
+ (r'\\\w+', tokens.Command),
+
# FIXME(andi): VALUES shouldn't be listed here
# see https://github.com/andialbrecht/sqlparse/pull/64
# IN is special, it may be followed by a parenthesis, but
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index d9702ca..a752555 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -625,3 +625,7 @@ class Operation(TokenList):
class Values(TokenList):
"""Grouping of values"""
+
+
+class Command(TokenList):
+ """Grouping of CLI commands."""
diff --git a/sqlparse/tokens.py b/sqlparse/tokens.py
index 41e3742..eefc0b4 100644
--- a/sqlparse/tokens.py
+++ b/sqlparse/tokens.py
@@ -55,6 +55,7 @@ Assignment = Token.Assignment
# Generic types for non-source code
Generic = Token.Generic
+Command = Generic.Command
# String and some others are not direct children of Token.
# alias them:
@@ -66,4 +67,3 @@ Token.Number = Number
DML = Keyword.DML
DDL = Keyword.DDL
CTE = Keyword.CTE
-Command = Keyword.Command
diff --git a/tests/test_regressions.py b/tests/test_regressions.py
index 73bc504..1d52ea7 100644
--- a/tests/test_regressions.py
+++ b/tests/test_regressions.py
@@ -366,3 +366,10 @@ def test_issue322_concurrently_is_keyword():
def test_issue359_index_error_assignments(s):
sqlparse.parse(s)
sqlparse.format(s, strip_comments=True)
+
+
+def test_issue469_copy_as_psql_command():
+ formatted = sqlparse.format(
+ '\\copy select * from foo',
+ keyword_case='upper', identifier_case='capitalize')
+ assert formatted == '\\copy SELECT * FROM Foo'
diff --git a/tests/test_tokenize.py b/tests/test_tokenize.py
index 23a5157..fcd1102 100644
--- a/tests/test_tokenize.py
+++ b/tests/test_tokenize.py
@@ -195,3 +195,9 @@ def test_parse_order_by():
p = sqlparse.parse('ORDER BY')[0]
assert len(p.tokens) == 1
assert p.tokens[0].ttype is T.Keyword
+
+
+def test_cli_commands():
+ p = sqlparse.parse('\\copy')[0]
+ assert len(p.tokens) == 1
+ assert p.tokens[0].ttype == T.Command