Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def on_buffer_changed(self, buf):
sql = self.get_text()
# Build custom filter stack. sqlparse.split() removes whitespace ATM.
stack = sqlparse.engine.FilterStack()
stack.split_statements = True
statements = [str(stmt) for stmt in stack.run(sql, None)]
stmt_data = []
start, end = buf.get_bounds()
buf.remove_source_marks(start, end, 'stmt_start')
buf.remove_source_marks(start, end, 'stmt_end')
cur_pos = buf.get_iter_at_mark(buf.get_insert()).get_offset()
# Mark statements (aka the statement splitter)
offset = 0
for statement in statements:
# Calculate offsets of stripped statement
offset_start = offset + (len(statement) - len(statement.lstrip()))
offset_end = offset_start + len(statement.strip())
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)]
# and add all its tokens to the main stack recursively
try:
filtr = IncludeStatement(self.dirpath,
self.maxRecursive - 1,
self.raiseexceptions)
# Max recursion limit reached
except ValueError as err:
# Raise the exception to the interpreter
if self.raiseexceptions:
raise
# Put the exception as a comment on the SQL code
yield Comment, '-- ValueError: %s\n' % err
stack = FilterStack()
stack.preprocess.append(filtr)
for tv in stack.run(raw_sql):
yield tv
# Set normal mode
self.detected = False
# Don't include any token while in detected mode
continue
# Normal token
yield token_type, value
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.enable_grouping()
return stack.run(stream, encoding)
def reformat_sql(sql):
stack = sqlparse.engine.FilterStack()
stack.preprocess.append(BoldKeywordFilter()) # add our custom filter
stack.postprocess.append(sqlparse.filters.SerializerUnicode()) # tokens -> strings
return swap_fields(''.join(stack.run(sql)))
# and add all its tokens to the main stack recursively
try:
filtr = IncludeStatement(self.dirpath,
self.maxRecursive - 1,
self.raiseexceptions)
# Max recursion limit reached
except ValueError, err:
# Raise the exception to the interpreter
if self.raiseexceptions:
raise
# Put the exception as a comment on the SQL code
yield Comment, u'-- ValueError: %s\n' % err
stack = FilterStack()
stack.preprocess.append(filtr)
for tv in stack.run(raw_sql):
yield tv
# Set normal mode
self.detected = False
# Don't include any token while in detected mode
continue
# Normal token
yield token_type, value
def parse(query):
stack = engine.FilterStack()
stack.preprocess.append(ValueFilter())
stack.postprocess.append(filters.SerializerUnicode())
return "".join(stack.run(query))
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))
path = join(self.dirpath, value[1:-1])
# Include file if path was found
if path:
try:
f = open(path)
raw_sql = f.read()
f.close()
except IOError as err:
yield Comment, u'-- IOError: %s\n' % err
else:
# Create new FilterStack to parse readed file
# and add all its tokens to the main stack recursively
# [ToDo] Add maximum recursive iteration value
stack = FilterStack()
stack.preprocess.append(IncludeStatement(self.dirpath))
for tv in stack.run(raw_sql):
yield tv
# Set normal mode
self.detected = False
# Don't include any token while in detected mode
continue
# Normal token
yield token_type, value
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.enable_grouping()
return stack.run(stream, encoding)