def _view_stmt(self, node: dict) -> dict: handle = printer.IndentedStream() handle.comma_at_eoln = True query = pgl_node.Node(node['query']) dml.select_stmt(query, handle) handle.seek(0) view = {} if 'schemaname' in node['view']['RangeVar']: view['schema'] = node['view']['RangeVar']['schemaname'] view['name'] = node['view']['RangeVar']['relname'] view['columns'] = {} view['options'] = {} view['query'] = handle.read() if node.get('aliases'): LOGGER.critical('Unsupported _view_stmt attribute: %r', node.get('aliases')) raise RuntimeError if node.get('withCheckOption', 0) > 0: view['options']['check_option'] = \ constants.ViewCheckOption(node['withCheckOption']).name if node.get('options'): options = { o['name']: o['arg'] for o in self.reformat(node['options']) } if options.get('security_barrier'): view['options']['security_barrier'] = \ options['security_barrier'] if not view['columns']: del view['columns'] if not view['options']: del view['options'] # @TODO Need to handle recursive parsing, but seems broken in pg_query return view
def test_indented_stream_with_sql(): raw_stmt_printer = printer.NODE_PRINTERS.pop('RawStmt', None) try: @printer.node_printer('RawStmt') def raw_stmt(node, output): output.write('Yeah') output = printer.IndentedStream() result = output('SELECT 1; SELECT 2') assert result == 'Yeah;\n\nYeah' output = printer.IndentedStream(separate_statements=False) result = output('SELECT 1; SELECT 2') assert result == 'Yeah;\nYeah' finally: if raw_stmt_printer is not None: printer.NODE_PRINTERS['RawStmt'] = raw_stmt_printer else: printer.NODE_PRINTERS.pop('RawStmt', None)
def test_separate_statements(): """Separate statements by ``separate_statements`` (int) newlines.""" raw_stmt_printer = printer.NODE_PRINTERS.pop('RawStmt', None) try: @printer.node_printer('RawStmt') def raw_stmt(node, output): output.write('Yeah') output = printer.IndentedStream(separate_statements=2) result = output('SELECT 1; SELECT 2') assert result == 'Yeah;\n\n\nYeah' finally: if raw_stmt_printer is not None: printer.NODE_PRINTERS['RawStmt'] = raw_stmt_printer else: printer.NODE_PRINTERS.pop('RawStmt', None)
def test_indented_stream_basics(): ptree = [{'TestRoot': {'bar': {'TestChild': {'a': [ {'TestNiece': {'x': 0, 'y': 0}}, {'TestNiece': {'x': 1, 'z': [ {'TestNiece': {'x': 2, 'y': 2}}, {'TestNiece': {'x': 3, 'z': [ {'TestNiece': {'x': 4, 'y': 4}}, {'TestNiece': {'x': 5, 'y': 5}}, ]}} ]}}, ]}}}}] root = Node(ptree) output = printer.RawStream() with pytest.raises(NotImplementedError) as exc: output(root) assert "'TestRoot'" in str(exc) try: @printer.node_printer('TestRoot') def test_root(node, output): output.write('bar = ') output.print_node(node.bar) @printer.node_printer('TestChild') def test_child(node, output): output.write('a(') with output.push_indent(): output.print_list(node.a, '*') output.write(')') @printer.node_printer('TestNiece') def test_niece(node, output): output.write('{') output.write('x') output.print_node(node.x) output.swrites(',') if node.y: output.write('y') output.print_node(node.y) else: if output.test_child_z_is_expression: with output.push_indent(2, relative=False): output.print_list(node.z, '+') else: list_sep = output.test_child_z_list_sep output.print_list(node.z, list_sep, standalone_items=len(list_sep) == 2) output.write('}') output = printer.IndentedStream() output.test_child_z_is_expression = True output.test_child_z_list_sep = '/' result = output(root) assert result == """\ bar = a({x0, y0} * {x1,{x2, y2} + {x3,{x4, y4} + {x5, y5}}})""" output = printer.IndentedStream() output.test_child_z_is_expression = False output.test_child_z_list_sep = '/' result = output(root) assert result == """\ bar = a({x0, y0} * {x1,{x2, y2} / {x3,{x4, y4} / {x5, y5}}})""" output = printer.IndentedStream() output.test_child_z_is_expression = False output.test_child_z_list_sep = '//' result = output(root) assert result == """\ bar = a({x0, y0} * {x1, {x2, y2} // {x3, {x4, y4} // {x5, y5}}})""" finally: printer.NODE_PRINTERS.pop('TestRoot', None) printer.NODE_PRINTERS.pop('TestChild', None) printer.NODE_PRINTERS.pop('TestNiece', None)