diff options
| author | Andi Albrecht <albrecht.andi@gmail.com> | 2016-03-07 11:20:47 +0100 |
|---|---|---|
| committer | Andi Albrecht <albrecht.andi@gmail.com> | 2016-03-07 11:20:47 +0100 |
| commit | 8f39d3367e8878e5f9a09b0166e3f0f73626e67d (patch) | |
| tree | 9c941e82db9ce322b909241a2a9de4357be79374 | |
| parent | 56e72ace0e6dcc7b4faac9fe9a3de29371d07932 (diff) | |
| parent | f516b66a0e254af510b6b8a18510aad922d69701 (diff) | |
| download | sqlparse-8f39d3367e8878e5f9a09b0166e3f0f73626e67d.tar.gz | |
Merge pull request #228 from compareasiagroup/master
Fix for #227 (get_type() doesn't work for queries that use WITH)
| -rw-r--r-- | sqlparse/keywords.py | 2 | ||||
| -rw-r--r-- | sqlparse/sql.py | 12 | ||||
| -rw-r--r-- | sqlparse/tokens.py | 1 | ||||
| -rw-r--r-- | tests/test_regressions.py | 13 |
4 files changed, 27 insertions, 1 deletions
diff --git a/sqlparse/keywords.py b/sqlparse/keywords.py index b6c4246..dd08be0 100644 --- a/sqlparse/keywords.py +++ b/sqlparse/keywords.py @@ -486,7 +486,7 @@ KEYWORDS = { 'VOLATILE': tokens.Keyword, 'WHENEVER': tokens.Keyword, - 'WITH': tokens.Keyword, + 'WITH': tokens.Keyword.CTE, 'WITHOUT': tokens.Keyword, 'WORK': tokens.Keyword, 'WRITE': tokens.Keyword, diff --git a/sqlparse/sql.py b/sqlparse/sql.py index c1111bb..9c0497a 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -487,6 +487,18 @@ class Statement(TokenList): elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL): return first_token.normalized + elif first_token.ttype == T.Keyword.CTE: + # The WITH keyword should be followed by either an Identifier or + # an IdentifierList containing the CTE definitions; the actual + # DML keyword (e.g. SELECT, INSERT) will follow next. + idents = self.token_next(self.token_index(first_token), skip_ws=True) + if isinstance(idents, (Identifier, IdentifierList)): + dml_keyword = self.token_next(self.token_index(idents), skip_ws=True) + if dml_keyword.ttype == T.Keyword.DML: + return dml_keyword.normalized + # Hmm, probably invalid syntax, so return unknown. + return 'UNKNOWN' + return 'UNKNOWN' diff --git a/sqlparse/tokens.py b/sqlparse/tokens.py index 01a9b89..98fa8a6 100644 --- a/sqlparse/tokens.py +++ b/sqlparse/tokens.py @@ -75,6 +75,7 @@ Token.Number = Number # SQL specific tokens DML = Keyword.DML DDL = Keyword.DDL +CTE = Keyword.CTE Command = Keyword.Command Group = Token.Group diff --git a/tests/test_regressions.py b/tests/test_regressions.py index ca7dd5b..c66a42d 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -291,3 +291,16 @@ def test_issue212_py2unicode(): def test_issue213_leadingws(): sql = " select * from foo" assert sqlparse.format(sql, strip_whitespace=True) == "select * from foo" + + +def test_issue227_gettype_cte(): + select_stmt = sqlparse.parse('SELECT 1, 2, 3 FROM foo;') + assert select_stmt[0].get_type() == 'SELECT' + with_stmt = sqlparse.parse('WITH foo AS (SELECT 1, 2, 3) SELECT * FROM foo;') + assert with_stmt[0].get_type() == 'SELECT' + with2_stmt = sqlparse.parse(''' + WITH foo AS (SELECT 1 AS abc, 2 AS def), + bar AS (SELECT * FROM something WHERE x > 1) + INSERT INTO elsewhere SELECT * FROM foo JOIN bar; + ''') + assert with2_stmt[0].get_type() == 'INSERT' |
