def splitf(sql, encoding=None): """ Split & format *sql* into single statements. """ # stack = EFilterStack() # return [(pos, text_type(stmt).strip()) for pos, stmt in stack.run(sql, encoding)] stack = EFilterStack() options = formatter.validate_options({'strip_comments': True, 'strip_whitespace': True}) formatter.build_filter_stack(stack, options) stack.postprocess.append(filters.SerializerUnicode()) # optimize for skipping empty and comment lines return [(pos, stmt, str(origin_stmt)) for pos, stmt, origin_stmt in stack.run(sql, encoding)]
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
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))
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)
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))
def pprint_query(query, term_colors=True, outer_indent=4): options = { 'reindent': True, 'keyword_case': 'upper', } if not isinstance(query, basestring): query = compile_query(query) if not term_colors: lines = sqlparse.format(query, **options).split('\n') print '\n'.join(' ' * outer_indent + line for line in lines) return stack = engine.FilterStack() options = formatter.validate_options(options) stack = formatter.build_filter_stack(stack, options) stack.postprocess.append(_ColorFilter()) result = ''.join(map(str, stack.run(query))) print '\n'.join(' ' * outer_indent + line for line in result.split('\n'))
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)