diff options
author | Shikanime Deva <deva.shikanime@protonmail.com> | 2021-07-19 13:56:30 +0200 |
---|---|---|
committer | William Phetsinorath <william.phetsinorath@shikanime.studio> | 2023-01-04 18:36:15 +0100 |
commit | dd9d5b91d7aa30e4a000d5370f09dc99378891dc (patch) | |
tree | 2ec6ee1d648ab0a2d17bf23e68bfe000e5146d0e /sqlparse | |
parent | 907fb496f90f2719095a1f01fe24db1e5c0e15a8 (diff) | |
download | sqlparse-dd9d5b91d7aa30e4a000d5370f09dc99378891dc.tar.gz |
Fix get_type with comments between WITH keyword
Diffstat (limited to 'sqlparse')
-rw-r--r-- | sqlparse/sql.py | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 586cd21..1ccfbdb 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -413,27 +413,28 @@ class Statement(TokenList): Whitespaces and comments at the beginning of the statement are ignored. """ - first_token = self.token_first(skip_cm=True) - if first_token is None: + token = self.token_first(skip_cm=True) + if token is None: # An "empty" statement that either has not tokens at all # or only whitespace tokens. return 'UNKNOWN' - elif first_token.ttype in (T.Keyword.DML, T.Keyword.DDL): - return first_token.normalized + elif token.ttype in (T.Keyword.DML, T.Keyword.DDL): + return token.normalized - elif first_token.ttype == T.Keyword.CTE: + elif 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. - fidx = self.token_index(first_token) - tidx, token = self.token_next(fidx, skip_ws=True) - if isinstance(token, (Identifier, IdentifierList)): - _, dml_keyword = self.token_next(tidx, skip_ws=True) - - if dml_keyword is not None \ - and dml_keyword.ttype == T.Keyword.DML: - return dml_keyword.normalized + tidx = self.token_index(token) + while tidx is not None: + tidx, token = self.token_next(tidx, skip_ws=True) + if isinstance(token, (Identifier, IdentifierList)): + tidx, token = self.token_next(tidx, skip_ws=True) + + if token is not None \ + and token.ttype == T.Keyword.DML: + return token.normalized # Hmm, probably invalid syntax, so return unknown. return 'UNKNOWN' |