Process the stream
(self, stream)
| 93 | return 0 |
| 94 | |
| 95 | def process(self, stream): |
| 96 | """Process the stream""" |
| 97 | EOS_TTYPE = T.Whitespace, T.Comment.Single |
| 98 | |
| 99 | # Run over all stream tokens |
| 100 | for ttype, value in stream: |
| 101 | # Yield token if we finished a statement and there's no whitespaces |
| 102 | # It will count newline token as a non whitespace. In this context |
| 103 | # whitespace ignores newlines. |
| 104 | # why don't multi line comments also count? |
| 105 | if self.consume_ws and ttype not in EOS_TTYPE: |
| 106 | yield sql.Statement(self.tokens) |
| 107 | |
| 108 | # Reset filter and prepare to process next statement |
| 109 | self._reset() |
| 110 | |
| 111 | # Change current split level (increase, decrease or remain equal) |
| 112 | self.level += self._change_splitlevel(ttype, value) |
| 113 | |
| 114 | # Append the token to the current statement |
| 115 | self.tokens.append(sql.Token(ttype, value)) |
| 116 | |
| 117 | # Check if we get the end of a statement |
| 118 | # Issue762: Allow GO (or "GO 2") as statement splitter. |
| 119 | # When implementing a language toggle, it's not only to add |
| 120 | # keywords it's also to change some rules, like this splitting |
| 121 | # rule. |
| 122 | # Issue809: Ignore semicolons inside BEGIN...END blocks, but handle |
| 123 | # standalone BEGIN; as a transaction statement |
| 124 | if ttype is T.Punctuation and value == ';': |
| 125 | # If we just saw BEGIN; then this is a transaction BEGIN, |
| 126 | # not a BEGIN...END block, so decrement depth |
| 127 | if self._seen_begin: |
| 128 | self._begin_depth = max(0, self._begin_depth - 1) |
| 129 | self._seen_begin = False |
| 130 | # Split on semicolon if not inside a BEGIN...END block |
| 131 | if self.level <= 0 and self._begin_depth == 0: |
| 132 | self.consume_ws = True |
| 133 | elif ttype is T.Keyword and value.split()[0] == 'GO': |
| 134 | self.consume_ws = True |
| 135 | elif (ttype not in (T.Whitespace, T.Newline, T.Comment.Single, |
| 136 | T.Comment.Multiline) |
| 137 | and not (ttype is T.Keyword and value.upper() == 'BEGIN')): |
| 138 | # Reset _seen_begin if we see a non-whitespace, non-comment |
| 139 | # token but not for BEGIN itself (which just set the flag) |
| 140 | self._seen_begin = False |
| 141 | |
| 142 | # Yield pending statement (if any) |
| 143 | if self.tokens and not all(t.is_whitespace for t in self.tokens): |
| 144 | yield sql.Statement(self.tokens) |
no test coverage detected