Beispiel #1
0
    def parse(self, sql, encoding):
        stream = lexer.tokenize(sql, encoding)
        statements = _split_statements(stream)

        default_stack = engine.FilterStack()
        default_stack.enable_grouping()
        create_table_statement_filter_stack = engine.FilterStack(
            stmtprocess=[filters.MysqlCreateStatementFilter()],
            grouping_funcs=[grouping.group_brackets]
        )
        create_table_statement_filter_stack.enable_grouping()
        for statement in statements:
            if _is_create_table_statement(statement):
                yield create_table_statement_filter_stack.run(statement)
            else:
                yield default_stack.run(statement)
Beispiel #2
0
 def parse(self, sql, encoding):
     stream = lexer.tokenize(sql, encoding)
     statements = _split_statements(stream)
     stack = engine.FilterStack()
     stack.enable_grouping()
     for statement in statements:
         yield stack.run(statement)
Beispiel #3
0
def split(sql):
    """Split *sql* into single statements.

    Returns a list of strings.
    """
    stack = engine.FilterStack()
    stack.split_statements = True
    return [unicode(stmt) for stmt in stack.run(sql)]
Beispiel #4
0
def parsestream(stream):
    """Parses sql statements from file-like object.

    Returns a generator of Statement instances.
    """
    stack = engine.FilterStack()
    stack.full_analyze()
    return stack.run(stream)
Beispiel #5
0
def split(sql, encoding=None):
    """Split *sql* into single statements.

    :param sql: A string containing one or more SQL statements.
    :param encoding: The encoding of the statement (optional).
    :returns: A list of strings.
    """
    stack = engine.FilterStack()
    return [text_type(stmt).strip() for stmt in stack.run(sql, encoding)]
Beispiel #6
0
def parsestream(stream, encoding=None):
    """Parses sql statements from file-like object.

    :param stream: A file-like object.
    :param encoding: The encoding of the stream contents (optional).
    :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
    """
    stack = engine.FilterStack()
    stack.full_analyze()
    return stack.run(stream, encoding)
Beispiel #7
0
def parse(sql):
    """Parse sql and return a list of statements.

    *sql* is a single string containting one or more SQL statements.

    Returns a tuple of :class:`~sqlparse.sql.Statement` instances.
    """
    stack = engine.FilterStack()
    stack.full_analyze()
    return tuple(stack.run(sql))
Beispiel #8
0
def sqlparse_format(sql, encoding=None, **options):
    from sqlparse import engine, formatter, filters, lexer
    options = formatter.validate_options(options)
    stack = engine.FilterStack()
    stack = formatter.build_filter_stack(stack, options)
    stack.postprocess.append(filters.SerializerUnicode())

    stack.preprocess.append(ZColorFilter())
    parsed = stack.run(sql, encoding)
    result = ''.join(parsed)
    return result
Beispiel #9
0
def format(sql, **options):
    """Format *sql* according to *options*.

    Available options are documented in :ref:`formatting`.

    Returns the formatted SQL statement as string.
    """
    stack = engine.FilterStack()
    options = formatter.validate_options(options)
    stack = formatter.build_filter_stack(stack, options)
    stack.postprocess.append(filters.SerializerUnicode())
    return ''.join(stack.run(sql))
Beispiel #10
0
def parsestream(stream, encoding=None, **options):
    """Parses sql statements from file-like object.

    :param stream: A file-like object.
    :param encoding: The encoding of the stream contents (optional).
    :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
    """
    stack = engine.FilterStack()
    stack.enable_grouping()
    options = formatter.validate_options(options)
    stack = formatter.build_filter_stack(stack, options)
    return stack.run(stream, encoding)
Beispiel #11
0
def parsestream(stream, encoding=None, **options):
    """Parses sql statements from file-like object.

    :param stream: A file-like object.
    :param encoding: The encoding of the stream contents.

    :param options: The options to parse with. (optional).
    Available options are documented in :ref:`parsing`.

    :returns: A generator of :class:`~sqlparse.sql.Statement` instances.
    """
    stack = engine.FilterStack()
    stack.enable_grouping()
    return stack.run(stream, encoding, **options)
Beispiel #12
0
def format(sql, encoding=None, **options):
    """Format *sql* according to *options*.

    Available options are documented in :ref:`formatting`.

    In addition to the formatting options this function accepts the
    keyword "encoding" which determines the encoding of the statement.

    :returns: The formatted SQL statement as string.
    """
    stack = engine.FilterStack()
    options = formatter.validate_options(options)
    stack = formatter.build_filter_stack(stack, options)
    stack.postprocess.append(filters.SerializerUnicode())
    return u''.join(stack.run(sql, encoding))
Beispiel #13
0
    def format_sql(statement, prefix_length=0):
        width, height = getTerminalSize()
        stack = engine.FilterStack()
        stack.enable_grouping()
        stack.stmtprocess.append(filters.StripWhitespaceFilter())
        stack.stmtprocess.append(MyReindentFilter(width - 30))
        stack.postprocess.append(filters.SerializerUnicode())
        statement = ''.join(stack.run(statement))

        lines = statement.split('\n')
        new_lines = [lines[0]]
        for line in lines[1:]:
            new_lines.append(' ' * prefix_length + line)
        statement = '\n'.join(new_lines)
        return statement
    def __init__(self, a_sql_text=""):
        self.sql_statements = []
        self.x_statements = []
        self.sql_text = a_sql_text
        self.func_group_orig = x_grouping.grouping.group
        x_grouping.grouping.group = x_grouping.group
        self.engine_parser_sql = engine.FilterStack()
        self.engine_parser_sql.enable_grouping()

        self.sql_statements = tuple(self.engine_parser_sql.run(self.sql_text))

        # Asigna statements_clas sin Tokens inutiles (newline, space) y clasificados por tipo
        for stmnt in self.sql_statements:
            a_new_x_stmnt = xStatementSql(stmnt)

            if a_new_x_stmnt is not None:
                self.x_statements.append(a_new_x_stmnt)
Beispiel #15
0
def format(sql, **options):
    """Format *sql* according to *options*.

    Available options are documented in :ref:`formatting`.

    In addition to the formatting options this function accepts the
    keyword "encoding" which determines the encoding of the statement.

    :returns: The formatted SQL statement as string.
    """
    options = formatter.validate_options(options)
    encoding = options.pop('encoding', None)
    stream = lexer.tokenize(sql, encoding)
    stream = _format_pre_process(stream, options)
    stack = engine.FilterStack()
    stack = formatter.build_filter_stack(stack, options)
    stack.postprocess.append(filters.SerializerUnicode())
    statements = split2(stream)
    return ''.join(stack.run(statement) for statement in statements)
def parse(query):
    stack = engine.FilterStack()
    stack.preprocess.append(ValueFilter())
    stack.postprocess.append(filters.SerializerUnicode())
    return ''.join(stack.run(query))