Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def roundtrip(sql):
orig_ast = parse_sql(sql)
_remove_stmt_len_and_location(orig_ast)
serialized = RawStream()(Node(orig_ast))
try:
serialized_ast = parse_sql(serialized)
except: # noqa
raise RuntimeError("Could not reparse %r" % serialized)
_remove_stmt_len_and_location(serialized_ast)
assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)
indented = IndentedStream()(Node(orig_ast))
try:
indented_ast = parse_sql(indented)
except: # noqa
raise RuntimeError("Could not reparse %r" % indented)
_remove_stmt_len_and_location(indented_ast)
assert orig_ast == indented_ast, "%r != %r" % (sql, indented)
# Run ``pytest -s tests/`` to see the following output
print()
print(indented)
def roundtrip(sql):
orig_ast = parse_sql(sql)
_remove_stmt_len_and_location(orig_ast)
serialized = RawStream()(Node(orig_ast))
try:
serialized_ast = parse_sql(serialized)
except: # noqa
raise RuntimeError("Could not reparse %r" % serialized)
_remove_stmt_len_and_location(serialized_ast)
assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)
indented = IndentedStream()(Node(orig_ast))
try:
indented_ast = parse_sql(indented)
except: # noqa
raise RuntimeError("Could not reparse %r" % indented)
_remove_stmt_len_and_location(indented_ast)
def test_stream_call_with_single_node():
# See https://github.com/lelit/pglast/pull/10
parsed = parse_sql('select a from x; select b from y')
node = Node(parsed[0])
result = RawStream()(node)
assert result == 'SELECT a FROM x'
node = Node(parsed[1])
result = RawStream()(node)
assert result == 'SELECT b FROM y'
def roundtrip(sql):
orig_ast = parse_sql(sql)
_remove_stmt_len_and_location(orig_ast)
serialized = RawStream()(Node(orig_ast))
try:
serialized_ast = parse_sql(serialized)
except: # noqa
raise RuntimeError("Could not reparse %r" % serialized)
_remove_stmt_len_and_location(serialized_ast)
assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)
indented = IndentedStream()(Node(orig_ast))
try:
indented_ast = parse_sql(indented)
except: # noqa
raise RuntimeError("Could not reparse %r" % indented)
_remove_stmt_len_and_location(indented_ast)
assert orig_ast == indented_ast, "%r != %r" % (sql, indented)
# Run ``pytest -s tests/`` to see the following output
print()
print(indented)
compared with the original statement: if they don't match, a warning is emitted and the
original statement is returned. This is a transient protection against possible bugs in the
serialization machinery that may disappear before 1.0.
"""
# Intentional lazy imports, so the modules are loaded on demand
import warnings
from .printer import IndentedStream
from . import printers # noqa
orig_pt = parse_sql(statement)
prettified = IndentedStream(**options)(Node(orig_pt))
if safety_belt:
try:
pretty_pt = parse_sql(prettified)
except Error as e: # pragma: no cover
print(prettified)
warnings.warn("Detected a bug in pglast serialization, please report: %s\n%s"
% (e, prettified), RuntimeWarning)
return statement
_remove_stmt_len_and_location(orig_pt)
_remove_stmt_len_and_location(pretty_pt)
if pretty_pt != orig_pt: # pragma: no cover
print(prettified)
warnings.warn("Detected a non-cosmetic difference between original and"
" prettified statements, please report", RuntimeWarning)
return statement
return prettified
constructor
:returns: a string with the equivalent prettified statement(s)
When `safety_belt` is ``True``, the resulting statement is parsed again and its *AST*
compared with the original statement: if they don't match, a warning is emitted and the
original statement is returned. This is a transient protection against possible bugs in the
serialization machinery that may disappear before 1.0.
"""
# Intentional lazy imports, so the modules are loaded on demand
import warnings
from .printer import IndentedStream
from . import printers # noqa
orig_pt = parse_sql(statement)
prettified = IndentedStream(**options)(Node(orig_pt))
if safety_belt:
try:
pretty_pt = parse_sql(prettified)
except Error as e: # pragma: no cover
print(prettified)
warnings.warn("Detected a bug in pglast serialization, please report: %s\n%s"
% (e, prettified), RuntimeWarning)
return statement
_remove_stmt_len_and_location(orig_pt)
_remove_stmt_len_and_location(pretty_pt)
if pretty_pt != orig_pt: # pragma: no cover
print(prettified)
warnings.warn("Detected a non-cosmetic difference between original and"