Example #1
0
def Tokens2Unicode(stream):
    result = ""

    for _, value in stream:
        result += u(value)

    return result
Example #2
0
    def _split_kwds(self, tlist):
        split_words = ('FROM', 'STRAIGHT_JOIN$', 'JOIN$', 'AND', 'OR',
                       'GROUP', 'ORDER', 'UNION', 'VALUES',
                       'SET', 'BETWEEN', 'EXCEPT', 'HAVING')

        def _next_token(i):
            t = tlist.token_next_match(i, T.Keyword, split_words,
                                       regex=True)
            if t and t.value.upper() == 'BETWEEN':
                t = _next_token(tlist.token_index(t) + 1)
                if t and t.value.upper() == 'AND':
                    t = _next_token(tlist.token_index(t) + 1)
            return t

        idx = 0
        token = _next_token(idx)
        added = set()
        while token:
            prev = tlist.token_prev(tlist.token_index(token), False)
            offset = 1
            if prev and prev.is_whitespace() and prev not in added:
                tlist.tokens.pop(tlist.token_index(prev))
                offset += 1
            uprev = u(prev)
            if (prev and (uprev.endswith('\n') or uprev.endswith('\r'))):
                nl = tlist.token_next(token)
            else:
                nl = self.nl()
                added.add(nl)
                tlist.insert_before(token, nl)
                offset += 1
            token = _next_token(tlist.token_index(nl) + offset)
Example #3
0
 def get_typecast(self):
     """Returns the typecast or ``None`` of this object as a string."""
     marker = self.token_next_match(0, T.Punctuation, '::')
     if marker is None:
         return None
     next_ = self.token_next(self.token_index(marker), False)
     if next_ is None:
         return None
     return u(next_)
Example #4
0
def split(sql, encoding=None):
    """Split *sql* into single statements.

    :param sql: A string containting one or more SQL statements.
    :param encoding: The encoding of the statement (optional).
    :returns: A list of strings.
    """
    stack = engine.FilterStack()
    stack.split_statements = True
    return [u(stmt).strip() for stmt in stack.run(sql, encoding)]
Example #5
0
    def process(self, stack, stmt):
        self.count += 1
        if self.count > 1:
            varname = '%s%d' % (self.varname, self.count)
        else:
            varname = self.varname

        has_nl = len(u(stmt).strip().splitlines()) > 1
        stmt.tokens = self._process(stmt.tokens, varname, has_nl)
        return stmt
Example #6
0
 def process(self, stack, stmt):
     if isinstance(stmt, sql.Statement):
         self._curr_stmt = stmt
     self._process(stmt)
     if isinstance(stmt, sql.Statement):
         if self._last_stmt is not None:
             if u(self._last_stmt).endswith('\n'):
                 nl = '\n'
             else:
                 nl = '\n\n'
             stmt.tokens.insert(
                 0, sql.Token(T.Whitespace, nl))
         if self._last_stmt != stmt:
             self._last_stmt = stmt
Example #7
0
 def _process(self, stack, group, stream):
     for token in stream:
         if token.is_whitespace() and '\n' in token.value:
             if token.value.endswith('\n'):
                 self.line = ''
             else:
                 self.line = token.value.splitlines()[-1]
         elif (token.is_group()
               and token.__class__ not in self.keep_together):
             token.tokens = self._process(stack, token, token.tokens)
         else:
             val = u(token)
             if len(self.line) + len(val) > self.width:
                 match = re.search('^ +', self.line)
                 if match is not None:
                     indent = match.group()
                 else:
                     indent = ''
                 yield sql.Token(T.Whitespace, '\n%s' % indent)
                 self.line = indent
             self.line += val
         yield token
Example #8
0
 def _get_repr_value(self):
     raw = u(self)
     if len(raw) > 7:
         raw = raw[:6] + u'...'
     return re.sub('\s+', ' ', raw)
Example #9
0
 def __str__(self):
     if sys.version_info[0] == 3:
         return self.value
     else:
         return u(self).encode('utf-8')
Example #10
0
 def _to_string(self):
     if sys.version_info[0] == 3:
         return ''.join(x.value for x in self.flatten())
     else:
         return ''.join(u(x) for x in self.flatten())
Example #11
0
 def process(self, stack, stmt):
     raw = u(stmt)
     lines = split_unquoted_newlines(raw)
     res = '\n'.join(line.rstrip() for line in lines)
     return res
Example #12
0
 def __init__(self, width, char):
     self.width = max(width, 1)
     self.char = u(char)