Returns the type of a statement. The returned value is a string holding an upper-cased reprint of the first DML or DDL keyword. If the first token in this group isn't a DML or DDL keyword "UNKNOWN" is returned. Whitespaces and comments at the beginning of the statem
(self)
| 404 | """Represents a SQL statement.""" |
| 405 | |
| 406 | def get_type(self): |
| 407 | """Returns the type of a statement. |
| 408 | |
| 409 | The returned value is a string holding an upper-cased reprint of |
| 410 | the first DML or DDL keyword. If the first token in this group |
| 411 | isn't a DML or DDL keyword "UNKNOWN" is returned. |
| 412 | |
| 413 | Whitespaces and comments at the beginning of the statement |
| 414 | are ignored. |
| 415 | """ |
| 416 | token = self.token_first(skip_cm=True) |
| 417 | if token is None: |
| 418 | # An "empty" statement that either has not tokens at all |
| 419 | # or only whitespace tokens. |
| 420 | return 'UNKNOWN' |
| 421 | |
| 422 | elif token.ttype in (T.Keyword.DML, T.Keyword.DDL): |
| 423 | return token.normalized |
| 424 | |
| 425 | elif token.ttype == T.Keyword.CTE: |
| 426 | # The WITH keyword should be followed by either an Identifier or |
| 427 | # an IdentifierList containing the CTE definitions; the actual |
| 428 | # DML keyword (e.g. SELECT, INSERT) will follow next. |
| 429 | tidx = self.token_index(token) |
| 430 | while tidx is not None: |
| 431 | tidx, token = self.token_next(tidx, skip_ws=True) |
| 432 | if isinstance(token, (Identifier, IdentifierList)): |
| 433 | tidx, token = self.token_next(tidx, skip_ws=True) |
| 434 | |
| 435 | if token is not None \ |
| 436 | and token.ttype == T.Keyword.DML: |
| 437 | return token.normalized |
| 438 | |
| 439 | # Hmm, probably invalid syntax, so return unknown. |
| 440 | return 'UNKNOWN' |
| 441 | |
| 442 | |
| 443 | class Identifier(NameAliasMixin, TokenList): |