Beispiel #1
0
    def statements(self):
        """
        Returns:
            generator: Return value yielding fully parsed statements from the
            feed.
        """
        logger = logging.getLogger()
        while self.finished_statements:
            stmt, comments = self.finished_statements.pop(0)
            if self.validate:
                with warnings.catch_warnings():
                    warnings.filterwarnings("error")
                    # The prettify stmt includes a sanity check raising
                    # warnings
                    try:
                        prettify(stmt, expression_level=1)
                    except Exception as e:
                        # We should raise Error, but pglast don't know about REPLICA
                        # And we are not able to fix and rebuild pglast package
                        logger.debug("Error while parsing statement")
                        logger.debug(stmt)
                        logger.debug(e)
                        continue

            parse_stmt = Node(parse_sql(stmt)[0]).stmt
            if comments:
                parse_stmt.parse_tree['comments'] = comments
            parse_stmt.parse_tree['original_string'] = stmt
            if parse_stmt.node_tag == 'CreateFunctionStmt':
                # Find the language used
                for option in parse_stmt.options:
                    if option.defname == 'language':
                        parse_stmt.parse_tree[
                            'language'] = option.arg.string_value
            yield parse_stmt
Beispiel #2
0
def workhorse(args):
    input = args.infile or sys.stdin
    with input:
        statement = input.read()

    if args.parse_tree or args.plpgsql:
        tree = parse_plpgsql(statement) if args.plpgsql else parse_sql(statement)
        if args.no_location:
            _remove_stmt_len_and_location(tree)
        output = args.outfile or sys.stdout
        with output:
            json.dump(tree, output, sort_keys=True, indent=2)
            output.write('\n')
    else:
        try:
            prettified = prettify(
                statement,
                compact_lists_margin=args.compact_lists_margin,
                split_string_literals_threshold=args.split_string_literals,
                special_functions=args.special_functions,
                comma_at_eoln=args.comma_at_eoln,
                semicolon_after_last_statement=args.semicolon_after_last_statement)
        except Error as e:
            print()
            raise SystemExit(e)

        output = args.outfile or sys.stdout
        with output:
            output.write(prettified)
            output.write('\n')
Beispiel #3
0
def pretty_sql(
    sql,
    compact_lists_margin=0,
    split_string_literals_threshold=0,
    special_functions=True,
    comma_at_eoln=True,
):
    """
    Prettify and validate the syntax of an SQL query, using pglast

    Parameters
    ----------
    sql : str
        SQL to prettyify and validate
    compact_lists_margin : int, default 0
        Use compact form for lists shorter than this
    split_string_literals_threshold : int, default 0
        Split strings (in the sql) longer than this threshold
    special_functions : bool, default True
        Translate some special functions to their more commonly used forms
    comma_at_eoln

    Raises
    ------

    pglast.parser.ParseError
        Raises a parse error if the query syntax was bad.

    See Also
    --------
    pglast.prettify: Function wrapped, use this for additional prettification options

    Returns
    -------
    str
        The prettified string.

    """

    return prettify(
        sql,
        compact_lists_margin=compact_lists_margin,
        split_string_literals_threshold=split_string_literals_threshold,
        special_functions=special_functions,
        comma_at_eoln=comma_at_eoln,
    )
Beispiel #4
0
    def cursor(connection):
        class Lifecycle:
            class DummyCursor:
                def execute(self, query, args):
                    connection.sql_history.append((query, args))

            def __enter__(self):
                return self.DummyCursor()

            def __exit__(self, *args):
                pass

        return Lifecycle()


JSONSchemaToPostgres(
    schema,
    postgres_schema='schm',
    abbreviations={
        # source column name: target column name
        'AbbreviateThisReallyLongColumn': 'AbbTRLC'
    },
).create_tables(DummyConnection())

from pglast import prettify
for (query, args) in DummyConnection.get_statements():
    if query.startswith('create'):
        sql_pretty = prettify(query)
        sys.stdout.write(f'{sql_pretty}\n')