Ejemplo n.º 1
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("ALTER", "VIEW")

        viewName = parser.parse_identifier()
        schemaName = ParserUtils.get_schema_name(viewName, database)
        schema = database.getSchema(schemaName)

        if schema is None:
            raise Exception(
                "Cannot find schema '%s' for statement '%s'. Missing CREATE SCHEMA statement?"
                % (schemaName, statement))

        objectName = ParserUtils.get_object_name(viewName)
        view = schema.views[objectName]

        if view is None:
            raise Exception(
                "Cannot find view '%s' for statement '%s'. Missing CREATE VIEW statement?"
                % (viewName, statement))

        while not parser.expect_optional(";"):
            if parser.expect_optional("ALTER"):
                parser.expect_optional("COLUMN")

                columnName = ParserUtils.get_object_name(
                    parser.parse_identifier())

                if parser.expect_optional("SET", "DEFAULT"):
                    expression = parser.get_expression()
                    view.addColumnDefaultValue(columnName, expression)
                elif parser.expect_optional("DROP", "DEFAULT"):
                    view.removeColumnDefaultValue(columnName)
                else:
                    parser.throw_unsupported_command()

            elif parser.expect_optional("OWNER", "TO"):
                # we do not parse this one so we just consume the identifier
                # if (outputIgnoredStatements) {
                #     database.addIgnoredStatement("ALTER TABLE " + viewName
                #             + " OWNER TO " + parser.parseIdentifier() + ';');
                # } else {
                parser.parse_identifier()
                # }
            else:
                parser.throw_unsupported_command()
Ejemplo n.º 2
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("ALTER", "VIEW")

        viewName = parser.parse_identifier()
        schemaName = ParserUtils.get_schema_name(viewName, database)
        schema = database.getSchema(schemaName)

        if schema is None:
            raise Exception("Cannot find schema '%s' for statement '%s'. Missing CREATE SCHEMA statement?" % (schemaName, statement))

        objectName = ParserUtils.get_object_name(viewName)
        view = schema.views[objectName]

        if view is None:
            raise Exception("Cannot find view '%s' for statement '%s'. Missing CREATE VIEW statement?" % (viewName, statement))

        while not parser.expect_optional(";"):
            if parser.expect_optional("ALTER"):
                parser.expect_optional("COLUMN")

                columnName = ParserUtils.get_object_name(parser.parse_identifier())

                if parser.expect_optional("SET", "DEFAULT"):
                    expression = parser.get_expression()
                    view.addColumnDefaultValue(columnName, expression)
                elif parser.expect_optional("DROP", "DEFAULT"):
                    view.removeColumnDefaultValue(columnName)
                else:
                    parser.throw_unsupported_command()

            elif parser.expect_optional("OWNER", "TO"):
                # we do not parse this one so we just consume the identifier
                # if (outputIgnoredStatements) {
                #     database.addIgnoredStatement("ALTER TABLE " + viewName
                #             + " OWNER TO " + parser.parseIdentifier() + ';');
                # } else {
                parser.parse_identifier()
                # }
            else:
                parser.throw_unsupported_command()
Ejemplo n.º 3
0
    def parse(database, statement, ignoreSlonyTriggers):
        parser = Parser(statement)
        parser.expect("CREATE", "TRIGGER")

        triggerName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(triggerName)

        trigger = PgTrigger()
        trigger.name = objectName

        if parser.expect_optional("BEFORE"):
            trigger.event = PgTrigger.EVENT_BEFORE
        elif parser.expect_optional("AFTER"):
            trigger.event = PgTrigger.EVENT_AFTER
        elif parser.expect_optional("INSTEAD OF"):
            trigger.event = PgTrigger.EVENT_INSTEAD_OF

        first = True

        while True:
            if not first and not parser.expect_optional("OR"):
                break
            elif parser.expect_optional("INSERT"):
                trigger.onInsert = True
            elif parser.expect_optional("UPDATE"):
                trigger.onUpdate = True

                if parser.expect_optional("OF"):
                    while True:
                        trigger.updateColumns.append(parser.parse_identifier())
                        if not parser.expect_optional(","):
                            break

            elif parser.expect_optional("DELETE"):
                trigger.onDelete = True
            elif parser.expect_optional("TRUNCATE"):
                trigger.onTruncate = True
            elif (first):
                break
            else:
                parser.throw_unsupported_command()

            first = False

        parser.expect("ON")

        trigger.tableName = ParserUtils.get_object_name(parser.parse_identifier())

        if parser.expect_optional("FOR"):
            parser.expect_optional("EACH")

            if parser.expect_optional("ROW"):
                trigger.forEachRow = True
            elif parser.expect_optional("STATEMENT"):
                trigger.forEachRow = False
            else:
                parser.throw_unsupported_command()

        if parser.expect_optional("WHEN"):
            parser.expect("(")
            trigger.when = parser.get_expression()
            parser.expect(")")

        parser.expect("EXECUTE", "PROCEDURE")
        trigger.function = parser.get_rest()

        ignoreSlonyTrigger = ignoreSlonyTriggers and ("_slony_logtrigger" == trigger.name or "_slony_denyaccess" == trigger.name)

        if (not ignoreSlonyTrigger):
            schema = database.getSchema(ParserUtils.get_schema_name(trigger.tableName, database))
            container = schema.tables.get(trigger.tableName)
            if not container:
                container = schema.views.get(trigger.tableName)

            if container:
                container.triggers[trigger.name] = trigger
            else:
                raise Exception()
Ejemplo n.º 4
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expect_optional("OR", "REPLACE")
        parser.expect("FUNCTION")

        functionName = parser.parse_identifier();
        schemaName = ParserUtils.get_schema_name(functionName, database)
        schema = database.getSchema(schemaName)

        if schema is None:
            raise Exception("Cannot find schema '%s' for statement '%s'. Missing CREATE SCHEMA statement?" % (schemaName, statement))

        function = PgFunction()
        function.name = ParserUtils.get_object_name(functionName)

        parser.expect("(")

        while not parser.expect_optional(")"):
            mode = None

            if parser.expect_optional("IN"):
                mode = "IN"
            elif parser.expect_optional("OUT"):
                mode = "OUT"
            elif parser.expect_optional("INOUT"):
                mode = "INOUT"
            elif parser.expect_optional("VARIADIC"):
                mode = "VARIADIC"

            position = parser.position
            argumentName = None
            dataType = parser.parse_data_type()

            position2 = parser.position

            if (not parser.expect_optional(")") and not parser.expect_optional(",")
                    and not parser.expect_optional("=")
                    and not parser.expect_optional("DEFAULT")):
                parser.position = position
                argumentName = ParserUtils.get_object_name(parser.parse_identifier())
                dataType = parser.parse_data_type()
            else:
                parser.position = position2

            defaultExpression = ''

            if (parser.expect_optional("=")
                    or parser.expect_optional("DEFAULT")):
                defaultExpression = parser.get_expression()
            else:
                defaultExpression = None

            argument = Argument()
            argument.dataType = dataType
            argument.defaultExpression = defaultExpression
            argument.mode = mode
            argument.name = argumentName
            function.addArgument(argument)

            if (parser.expect_optional(")")):
                break
            else:
                parser.expect(",")

        function.body = parser.get_rest()

        schema.addFunction(function)
Ejemplo n.º 5
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expect_optional("OR", "REPLACE")
        parser.expect("FUNCTION")

        functionName = parser.parse_identifier()
        schemaName = ParserUtils.get_schema_name(functionName, database)
        schema = database.getSchema(schemaName)

        if schema is None:
            raise Exception(
                "Cannot find schema '%s' for statement '%s'. Missing CREATE SCHEMA statement?"
                % (schemaName, statement))

        function = PgFunction()
        function.name = ParserUtils.get_object_name(functionName)

        parser.expect("(")

        while not parser.expect_optional(")"):
            mode = None

            if parser.expect_optional("IN"):
                mode = "IN"
            elif parser.expect_optional("OUT"):
                mode = "OUT"
            elif parser.expect_optional("INOUT"):
                mode = "INOUT"
            elif parser.expect_optional("VARIADIC"):
                mode = "VARIADIC"

            position = parser.position
            argumentName = None
            dataType = parser.parse_data_type()

            position2 = parser.position

            if (not parser.expect_optional(")")
                    and not parser.expect_optional(",")
                    and not parser.expect_optional("=")
                    and not parser.expect_optional("DEFAULT")):
                parser.position = position
                argumentName = ParserUtils.get_object_name(
                    parser.parse_identifier())
                dataType = parser.parse_data_type()
            else:
                parser.position = position2

            defaultExpression = ''

            if (parser.expect_optional("=")
                    or parser.expect_optional("DEFAULT")):
                defaultExpression = parser.get_expression()
            else:
                defaultExpression = None

            argument = Argument()
            argument.dataType = dataType
            argument.defaultExpression = defaultExpression
            argument.mode = mode
            argument.name = argumentName
            function.addArgument(argument)

            if (parser.expect_optional(")")):
                break
            else:
                parser.expect(",")

        function.body = parser.get_rest()

        schema.addFunction(function)