Example #1
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expect_optional("OR", "REPLACE")
        parser.expect("VIEW")

        viewName = parser.parse_identifier()

        columnsExist = parser.expect_optional("(")
        columnNames = list()

        if (columnsExist):
            while not parser.expect_optional(")"):
                columnNames.append(
                    ParserUtils.get_object_name(parser.parse_identifier()))
                parser.expect_optional(",")

        parser.expect("AS")

        query = parser.get_rest()

        view = PgView(ParserUtils.get_object_name(viewName))
        view.columnNames = columnNames
        view.query = query

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

        if schema is None:
            raise Exception("CannotFindSchema" % (schemaName, statement))

        schema.addView(view)
Example #2
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expect_optional("OR", "REPLACE")
        parser.expect("VIEW")

        viewName = parser.parse_identifier()

        columnsExist = parser.expect_optional("(")
        columnNames = list()

        if (columnsExist):
            while not parser.expect_optional(")"):
                columnNames.append(ParserUtils.get_object_name(parser.parse_identifier()))
                parser.expect_optional(",")

        parser.expect("AS")

        query = parser.get_rest()

        view = PgView(ParserUtils.get_object_name(viewName))
        view.columnNames = columnNames
        view.query = query

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

        if schema is None:
            raise Exception("CannotFindSchema" % (schemaName, statement))

        schema.addView(view)
Example #3
0
    def parseFunction(parser, database):
        functionName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(functionName)
        schemaName = ParserUtils.get_schema_name(functionName, database)
        schema = database.getSchema(schemaName)

        parser.expect("(")

        tmpFunction = PgFunction()
        tmpFunction.name = objectName

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

            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"
            else:
                mode = None

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

            position2 = parser.position

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

            argument = Argument()
            argument.dataType = dataType
            argument.mode = mode
            argument.name = argumentName
            tmpFunction.addArgument(argument)

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

        function = schema.functions.get(tmpFunction.getSignature())

        parser.expect("IS")
        function.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #4
0
    def parseFunction(parser, database):
        functionName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(functionName)
        schemaName = ParserUtils.get_schema_name(functionName, database)
        schema = database.getSchema(schemaName)

        parser.expect("(")

        tmpFunction = PgFunction()
        tmpFunction.name = objectName

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

            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"
            else:
                mode = None

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

            position2 = parser.position

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

            argument = Argument()
            argument.dataType = dataType
            argument.mode = mode
            argument.name = argumentName
            tmpFunction.addArgument(argument)

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

        function = schema.functions.get(tmpFunction.getSignature())

        parser.expect("IS")
        function.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #5
0
    def parseTrigger(parser, database):
        triggerName = ParserUtils.get_object_name(parser.parse_identifier())

        parser.expect("ON")

        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(triggerName, database)

        trigger = database.getSchema(schemaName).tables[objectName].triggers[triggerName]

        parser.expect("IS")
        trigger.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #6
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE", "SEQUENCE")

        sequenceName = parser.parse_identifier()
        sequence = PgSequence(ParserUtils.get_object_name(sequenceName))
        schemaName = ParserUtils.get_schema_name(sequenceName, database)
        schema = database.getSchema(schemaName)

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

        schema.addSequence(sequence)

        while not parser.expect_optional(";"):
            if parser.expect_optional("INCREMENT"):
                parser.expect_optional("BY")
                sequence.increment = parser.parse_string()
            elif parser.expect_optional("MINVALUE"):
                sequence.minValue = parser.parse_string()
            elif parser.expect_optional("MAXVALUE"):
                sequence.maxValue = parser.parse_string()
            elif parser.expect_optional("START"):
                parser.expect_optional("WITH")
                sequence.startWith = parser.parse_string()
            elif parser.expect_optional("CACHE"):
                sequence.cache = parser.parse_string()
            elif parser.expect_optional("CYCLE"):
                sequence.cycle = True
            elif parser.expect_optional("OWNED", "BY"):
                if parser.expect_optional("NONE"):
                    sequence.ownedBy = None
                else:
                    sequence.ownedBy = ParserUtils.get_object_name(
                        parser.parse_identifier())

            elif parser.expect_optional("NO"):
                if parser.expect_optional("MINVALUE"):
                    sequence.minValue = None
                elif parser.expect_optional("MAXVALUE"):
                    sequence.maxValue = None
                elif parser.expect_optional("CYCLE"):
                    sequence.cycle = False
                else:
                    parser.throw_unsupported_command()

            else:
                parser.throw_unsupported_command()
Example #7
0
    def parseConstraint(parser, database):
        constraintName = ParserUtils.get_object_name(parser.parse_identifier())

        parser.expect("ON")

        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(constraintName, database)

        constraint = database.getSchema(schemaName).getTable(objectName).constraints[constraintName]

        parser.expect("IS")
        constraint.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #8
0
    def parseConstraint(parser, database):
        constraintName = ParserUtils.get_object_name(parser.parse_identifier())

        parser.expect("ON")

        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(constraintName, database)

        constraint = database.getSchema(schemaName).getTable(
            objectName).constraints[constraintName]

        parser.expect("IS")
        constraint.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #9
0
    def parseTrigger(parser, database):
        triggerName = ParserUtils.get_object_name(parser.parse_identifier())

        parser.expect("ON")

        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(triggerName, database)

        trigger = database.getSchema(
            schemaName).tables[objectName].triggers[triggerName]

        parser.expect("IS")
        trigger.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #10
0
    def parseColumn(parser, database):
        columnName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(columnName)
        tableName = ParserUtils.get_second_object_name(columnName)
        schemaName = ParserUtils.get_third_object_name(columnName)
        schema = database.getSchema(schemaName)

        table = schema.getTable(tableName)

        if table is None:
            view = schema.views[tableName]
            parser.expect("IS")

            comment = CommentParser.getComment(parser)

            if comment is None:
                view.removeColumnComment(objectName)
            else:
                view.addColumnComment(objectName, comment)
            parser.expect(";")
        else:
            column = table.columns.get(objectName)

            if column is None:
                raise Error("Cannot find column '%s' in table '%s'" %
                            (columnName, table.name))

            parser.expect("IS")
            column.comment = CommentParser.getComment(parser)
            parser.expect(";")
Example #11
0
    def parseSchema(parser, database):
        schemaName = ParserUtils.get_object_name(parser.parse_identifier())
        schema = database.getSchema(schemaName)

        parser.expect("IS")
        schema.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #12
0
    def parseSchema(parser, database):
        schemaName = ParserUtils.get_object_name(parser.parse_identifier())
        schema = database.getSchema(schemaName)

        parser.expect("IS")
        schema.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #13
0
    def parseColumn(parser, database):
        columnName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(columnName)
        tableName = ParserUtils.get_second_object_name(columnName)
        schemaName = ParserUtils.get_third_object_name(columnName)
        schema = database.getSchema(schemaName)

        table = schema.getTable(tableName)

        if table is None:
            view = schema.views[tableName]
            parser.expect("IS")

            comment = CommentParser.getComment(parser)

            if comment is None:
                view.removeColumnComment(objectName)
            else:
                view.addColumnComment(objectName, comment)
            parser.expect(";")
        else:
            column = table.columns.get(objectName)

            if column is None:
                raise Error("Cannot find column '%s' in table '%s'" % (columnName, table.name))

            parser.expect("IS")
            column.comment = CommentParser.getComment(parser)
            parser.expect(";")
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE", "SEQUENCE")

        sequenceName = parser.parse_identifier();
        sequence = PgSequence(ParserUtils.get_object_name(sequenceName))
        schemaName = ParserUtils.get_schema_name(sequenceName, database)
        schema = database.getSchema(schemaName)

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

        schema.addSequence(sequence)

        while not parser.expect_optional(";"):
            if parser.expect_optional("INCREMENT"):
                parser.expect_optional("BY")
                sequence.increment = parser.parse_string()
            elif parser.expect_optional("MINVALUE"):
                sequence.minValue = parser.parse_string()
            elif parser.expect_optional("MAXVALUE"):
                sequence.maxValue = parser.parse_string()
            elif parser.expect_optional("START"):
                parser.expect_optional("WITH")
                sequence.startWith = parser.parse_string()
            elif parser.expect_optional("CACHE"):
                sequence.cache = parser.parse_string()
            elif parser.expect_optional("CYCLE"):
                sequence.cycle = True
            elif parser.expect_optional("OWNED", "BY"):
                if parser.expect_optional("NONE"):
                    sequence.ownedBy = None
                else:
                    sequence.ownedBy = ParserUtils.get_object_name(parser.parse_identifier())

            elif parser.expect_optional("NO"):
                if parser.expect_optional("MINVALUE"):
                    sequence.minValue = None
                elif parser.expect_optional("MAXVALUE"):
                    sequence.maxValue = None
                elif parser.expect_optional("CYCLE"):
                    sequence.cycle = False
                else:
                    parser.throw_unsupported_command()

            else:
                parser.throw_unsupported_command()
Example #15
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()
Example #16
0
    def parseTable(parser, database):
        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(tableName, database)

        table = database.getSchema(schemaName).getTable(objectName)

        parser.expect("IS")
        table.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #17
0
    def parseSequence(parser, database):
        sequenceName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(sequenceName)
        schemaName = ParserUtils.get_schema_name(sequenceName, database)

        sequence = database.getSchema(schemaName).sequences[objectName]

        parser.expect("IS")
        sequence.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #18
0
    def parseInherits(parser, table):
        parser.expect("(")

        while not parser.expect_optional(")"):
            table.addInherits(ParserUtils.get_object_name(parser.parse_identifier()))

            if parser.expect_optional(")"):
                break
            else:
                parser.expect(",")
Example #19
0
    def parseView(parser, database):
        viewName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(viewName)
        schemaName = ParserUtils.get_schema_name(viewName, database)

        view = database.getSchema(schemaName).views[objectName]

        parser.expect("IS")
        view.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #20
0
    def parseTable(parser, database):
        tableName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(tableName)
        schemaName = ParserUtils.get_schema_name(tableName, database)

        table = database.getSchema(schemaName).getTable(objectName)

        parser.expect("IS")
        table.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #21
0
    def parseView(parser, database):
        viewName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(viewName)
        schemaName = ParserUtils.get_schema_name(viewName, database)

        view = database.getSchema(schemaName).views[objectName]

        parser.expect("IS")
        view.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #22
0
    def parseSequence(parser, database):
        sequenceName = parser.parse_identifier();
        objectName = ParserUtils.get_object_name(sequenceName)
        schemaName = ParserUtils.get_schema_name(sequenceName, database)

        sequence = database.getSchema(schemaName).sequences[objectName]

        parser.expect("IS")
        sequence.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #23
0
    def parseAddConstraint(parser, table, schema):
        constraintName = ParserUtils.get_object_name(parser.parse_identifier())
        constraint = PgConstraint(constraintName)
        constraint.tableName = table.name
        table.addConstraint(constraint)

        if parser.expect_optional("PRIMARY", "KEY"):
            schema.addPrimaryKey(constraint)
            constraint.definition = "PRIMARY KEY " + parser.get_expression()
        else:
            constraint.definition = parser.get_expression()
Example #24
0
    def parseAlterColumn(parser, table):
        parser.expect_optional("COLUMN")

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

        if parser.expect_optional("SET"):
            if parser.expect_optional("STATISTICS"):
                column = table.columns[columnName]

                if column is None:
                    raise Exception(
                        "Cannot find column '%s' in table '%s' for statement '%s'."
                    ) % (columnName, table.name, parser.statement)

                column.statistics = parser.parse_integer()
            elif parser.expect_optional("DEFAULT"):
                defaultValue = parser.get_expression()

                if columnName in table.columns:
                    column = table.columns[columnName]

                    if column is None:
                        raise Exception(
                            "Cannot find column '%s' in table '%s' for statement '%s'."
                        ) % (columnName, table.name, parser.statement)

                    column.defaultValue = defaultValue
                else:
                    raise Exception(
                        "Cannot find column '%s' in table '%s' for statement '%s'."
                    ) % (columnName, table.name, parser.statement)

            elif parser.expect_optional("STORAGE"):
                column = table.columns[columnName]

                if (column is None):
                    raise Exception(
                        "Cannot find column '%s' in table '%s' for statement '%s'."
                    ) % (columnName, table.name, parser.statement)

                if parser.expect_optional("PLAIN"):
                    column.storage = "PLAIN"
                elif parser.expect_optional("EXTERNAL"):
                    column.storage = "EXTERNAL"
                elif parser.expect_optional("EXTENDED"):
                    column.storage = "EXTENDED"
                elif parser.expect_optional("MAIN"):
                    column.storage = "MAIN"
                else:
                    parser.throw_unsupported_command()
            else:
                parser.throw_unsupported_command()
        else:
            parser.throw_unsupported_command()
Example #25
0
    def parseAddConstraint(parser, table, schema):
        constraintName = ParserUtils.get_object_name(parser.parse_identifier())
        constraint = PgConstraint(constraintName)
        constraint.tableName = table.name
        table.addConstraint(constraint)

        if parser.expect_optional("PRIMARY", "KEY"):
            schema.addPrimaryKey(constraint)
            constraint.definition = "PRIMARY KEY " + parser.get_expression()
        else:
            constraint.definition = parser.get_expression()
Example #26
0
    def parseInherits(parser, table):
        parser.expect("(")

        while not parser.expect_optional(")"):
            table.addInherits(
                ParserUtils.get_object_name(parser.parse_identifier()))

            if parser.expect_optional(")"):
                break
            else:
                parser.expect(",")
Example #27
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()
Example #28
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")

        unique = parser.expect_optional("UNIQUE")

        parser.expect("INDEX")
        parser.expect_optional("CONCURRENTLY")

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

        parser.expect("ON")

        tableName = parser.parse_identifier()
        definition = parser.get_rest()
        schemaName =ParserUtils.get_schema_name(tableName, database)
        schema = database.getSchema(schemaName)

        if (schema is None):
            print 'ERROR: CreateIndexParser[Line 21]'
            # throw new RuntimeException(MessageFormat.format(
            #         Resources.getString("CannotFindSchema"), schemaName,
            #         statement));

        objectName = ParserUtils.get_object_name(tableName)
        table = schema.getTable(objectName)

        if (table is None):
            print 'ERROR: CreateIndexParser[Line 32]'
            # throw new RuntimeException(MessageFormat.format(
            #         Resources.getString("CannotFindTable"), tableName,
            #         statement));

        index = PgIndex(indexName)
        table.addIndex(index)
        schema.addIndex(index)
        index.definition = definition.strip()
        index.tableName = table.name
        index.unique = unique
Example #29
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")

        unique = parser.expect_optional("UNIQUE")

        parser.expect("INDEX")
        parser.expect_optional("CONCURRENTLY")

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

        parser.expect("ON")

        tableName = parser.parse_identifier()
        definition = parser.get_rest()
        schemaName = ParserUtils.get_schema_name(tableName, database)
        schema = database.getSchema(schemaName)

        if (schema is None):
            print 'ERROR: CreateIndexParser[Line 21]'
            # throw new RuntimeException(MessageFormat.format(
            #         Resources.getString("CannotFindSchema"), schemaName,
            #         statement));

        objectName = ParserUtils.get_object_name(tableName)
        table = schema.getTable(objectName)

        if (table is None):
            print 'ERROR: CreateIndexParser[Line 32]'
            # throw new RuntimeException(MessageFormat.format(
            #         Resources.getString("CannotFindTable"), tableName,
            #         statement));

        index = PgIndex(indexName)
        table.addIndex(index)
        schema.addIndex(index)
        index.definition = definition.strip()
        index.tableName = table.name
        index.unique = unique
Example #30
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect('CREATE', 'TABLE')

        # Optional IF NOT EXISTS, irrelevant for our purposes
        parser.expect_optional("IF", "NOT", "EXISTS")

        tableName = ParserUtils.get_object_name(parser.parse_identifier())
        table = PgTable(tableName)
        # Figure it out why do we need this
        schemaName = ParserUtils.get_schema_name(tableName, database)
        schema = database.getSchema(schemaName)

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

        schema.addTable(table)

        parser.expect("(")

        while not parser.expect_optional(")"):
            if parser.expect_optional("CONSTRAINT"):
                CreateTableParser.parseConstraint(parser, table)
            else:
                CreateTableParser.parseColumn(parser, table)

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

        while not parser.expect_optional(";"):
            if parser.expect_optional("INHERITS"):
                CreateTableParser.parseInherits(parser, table)
            elif parser.expect_optional("WITHOUT"):
                table.oids = "OIDS=false"
            elif parser.expect_optional("WITH"):
                if (parser.expect_optional("OIDS")
                        or parser.expect_optional("OIDS=true")):
                    table.oids = "OIDS=true"
                elif parser.expect_optional("OIDS=false"):
                    table.oids = "OIDS=false"
                else:
                    print 'table.setWith(parser.getExpression())'
            elif parser.expect_optional("TABLESPACE"):
                print 'table.setTablespace(parser.parseString()'
            else:
                parser.throw_unsupported_command()
Example #31
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE", "SCHEMA")

        if parser.expect_optional("AUTHORIZATION"):
            schema = PgSchema(
                ParserUtils.get_object_name(parser.parse_identifier()))
            database.addSchema(schema)
            schema.authorization = schema.name

        else:
            schemaName = ParserUtils.get_object_name(parser.parse_identifier())
            schema = PgSchema(schemaName)
            database.schemas[schemaName] = schema

            if parser.expect_optional("AUTHORIZATION"):
                schema.authorization = ParserUtils.get_object_name(
                    parser.parse_identifier())

        definition = parser.get_rest()

        if definition:
            schema.definition = definition
Example #32
0
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect('CREATE', 'TABLE')

        # Optional IF NOT EXISTS, irrelevant for our purposes
        parser.expect_optional("IF", "NOT", "EXISTS")

        tableName = ParserUtils.get_object_name(parser.parse_identifier())
        table = PgTable(tableName)
        # Figure it out why do we need this
        schemaName = ParserUtils.get_schema_name(tableName, database)
        schema = database.getSchema(schemaName)

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

        schema.addTable(table)

        parser.expect("(")

        while not parser.expect_optional(")"):
            if parser.expect_optional("CONSTRAINT"):
                CreateTableParser.parseConstraint(parser, table)
            else:
                CreateTableParser.parseColumn(parser, table)

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

        while not parser.expect_optional(";"):
            if parser.expect_optional("INHERITS"):
                CreateTableParser.parseInherits(parser, table)
            elif parser.expect_optional("WITHOUT"):
                table.oids = "OIDS=false"
            elif parser.expect_optional("WITH"):
                if (parser.expect_optional("OIDS") or parser.expect_optional("OIDS=true")):
                    table.oids = "OIDS=true"
                elif parser.expect_optional("OIDS=false"):
                    table.oids = "OIDS=false"
                else:
                    print 'table.setWith(parser.getExpression())'
            elif parser.expect_optional("TABLESPACE"):
                print 'table.setTablespace(parser.parseString()'
            else:
                parser.throw_unsupported_command()
Example #33
0
    def parseAlterColumn(parser, table):
        parser.expect_optional("COLUMN")

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

        if parser.expect_optional("SET"):
            if parser.expect_optional("STATISTICS"):
                column = table.columns[columnName]

                if column is None:
                    raise Exception("Cannot find column '%s' in table '%s' for statement '%s'.") % (columnName, table.name, parser.statement)

                column.statistics = parser.parse_integer()
            elif parser.expect_optional("DEFAULT"):
                defaultValue = parser.get_expression()

                if columnName in table.columns:
                    column = table.columns[columnName]

                    if column is None:
                        raise Exception("Cannot find column '%s' in table '%s' for statement '%s'.") % (columnName, table.name, parser.statement)

                    column.defaultValue = defaultValue
                else:
                    raise Exception("Cannot find column '%s' in table '%s' for statement '%s'.") % (columnName, table.name, parser.statement)

            elif parser.expect_optional("STORAGE"):
                column = table.columns[columnName]

                if (column is None):
                    raise Exception("Cannot find column '%s' in table '%s' for statement '%s'.") % (columnName, table.name, parser.statement)

                if parser.expect_optional("PLAIN"):
                    column.storage = "PLAIN"
                elif parser.expect_optional("EXTERNAL"):
                    column.storage = "EXTERNAL"
                elif parser.expect_optional("EXTENDED"):
                    column.storage = "EXTENDED"
                elif parser.expect_optional("MAIN"):
                    column.storage = "MAIN"
                else:
                    parser.throw_unsupported_command()
            else:
                parser.throw_unsupported_command()
        else:
            parser.throw_unsupported_command()
Example #34
0
    def parseIndex(parser, database):
        indexName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(indexName)
        schemaName = ParserUtils.get_schema_name(indexName, database)
        schema = database.getSchema(schemaName)

        index = schema.indexes.get(objectName)

        if index is None:
            primaryKey = schema.primaryKeys[objectName]
            parser.expect("IS")
            primaryKey.comment = CommentParser.getComment(parser)
            parser.expect(";")
        else:
            parser.expect("IS")
            index.comment = CommentParser.getComment(parser)
            parser.expect(";")
Example #35
0
    def parseIndex(parser, database):
        indexName = parser.parse_identifier()
        objectName = ParserUtils.get_object_name(indexName)
        schemaName = ParserUtils.get_schema_name(indexName, database)
        schema = database.getSchema(schemaName)

        index = schema.indexes.get(objectName)

        if index is None:
            primaryKey = schema.primaryKeys[objectName]
            parser.expect("IS")
            primaryKey.comment = CommentParser.getComment(parser)
            parser.expect(";")
        else:
            parser.expect("IS")
            index.comment = CommentParser.getComment(parser)
            parser.expect(";")
Example #36
0
    def parseView(parser, view, viewName, database):
        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"):
                    parser.parse_identifier()
            else:
                parser.throw_unsupported_command()
Example #37
0
    def parseView(parser, view, viewName, database):
        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"):
                parser.parse_identifier()
            else:
                parser.throw_unsupported_command()
Example #38
0
    def parse(database, statement):
        parser = Parser(statement)

        parser.expect("ALTER", "TABLE")
        parser.expect_optional("ONLY")

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

        if schema is None:
            raise Exception("CannotFindSchema")

        objectName = ParserUtils.get_object_name(tableName)
        table = schema.tables.get(objectName)

        if table is None:
            logging.debug('Table %s was not fount in schema %s' % (tableName, schema))
            view = schema.views.get(objectName)

            if view is not None:
                AlterTableParser.parseView(parser, view, tableName, database)
                return

            sequence = schema.sequences.get(objectName)

            if sequence is not None:
                AlterTableParser.parseSequence(parser, sequence, tableName, database);
                return

            raise Exception("Cannot find object '%s' for statement '%s'." % (tableName, statement))

        while not parser.expect_optional(";"):
            if parser.expect_optional("ALTER"):
                AlterTableParser.parseAlterColumn(parser, table)
            elif (parser.expect_optional("CLUSTER", "ON")):
                table.clusterIndexName = ParserUtils.get_object_name(parser.parse_identifier())
            elif (parser.expect_optional("OWNER", "TO")):
                # we do not parse this one so we just consume the identifier
                # if (outputIgnoredStatements):
                #     print 'database.addIgnoredStatement("ALTER TABLE " + tableName + " OWNER TO " + parser.parseIdentifier() + ';')'
                # else:
                    parser.parse_identifier()
            elif (parser.expect_optional("ADD")):
                if (parser.expect_optional("FOREIGN", "KEY")):
                    print 'parseAddForeignKey(parser, table);'
                elif (parser.expect_optional("CONSTRAINT")):
                    AlterTableParser.parseAddConstraint(parser, table, schema)
                else:
                    parser.throw_unsupported_command()
            elif (parser.expect_optional("ENABLE")):
                print 'parseEnable(parser, outputIgnoredStatements, tableName, database);'
            elif (parser.expect_optional("DISABLE")):
                print 'parseDisable(parser, outputIgnoredStatements, tableName, database);'
            else:
                parser.throw_unsupported_command()

            if (parser.expect_optional(";")):
                break
            else:
                parser.expect(",")
Example #39
0
    def parse(database, statement):
        parser = Parser(statement)

        parser.expect("ALTER", "TABLE")
        parser.expect_optional("ONLY")

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

        if schema is None:
            raise Exception("CannotFindSchema")

        objectName = ParserUtils.get_object_name(tableName)
        table = schema.tables.get(objectName)

        if table is None:
            logging.debug('Table %s was not fount in schema %s' %
                          (tableName, schema))
            view = schema.views.get(objectName)

            if view is not None:
                AlterTableParser.parseView(parser, view, tableName, database)
                return

            sequence = schema.sequences.get(objectName)

            if sequence is not None:
                AlterTableParser.parseSequence(parser, sequence, tableName,
                                               database)
                return

            raise Exception("Cannot find object '%s' for statement '%s'." %
                            (tableName, statement))

        while not parser.expect_optional(";"):
            if parser.expect_optional("ALTER"):
                AlterTableParser.parseAlterColumn(parser, table)
            elif (parser.expect_optional("CLUSTER", "ON")):
                table.clusterIndexName = ParserUtils.get_object_name(
                    parser.parse_identifier())
            elif (parser.expect_optional("OWNER", "TO")):
                # we do not parse this one so we just consume the identifier
                # if (outputIgnoredStatements):
                #     print 'database.addIgnoredStatement("ALTER TABLE " + tableName + " OWNER TO " + parser.parseIdentifier() + ';')'
                # else:
                parser.parse_identifier()
            elif (parser.expect_optional("ADD")):
                if (parser.expect_optional("FOREIGN", "KEY")):
                    print 'parseAddForeignKey(parser, table);'
                elif (parser.expect_optional("CONSTRAINT")):
                    AlterTableParser.parseAddConstraint(parser, table, schema)
                else:
                    parser.throw_unsupported_command()
            elif (parser.expect_optional("ENABLE")):
                print 'parseEnable(parser, outputIgnoredStatements, tableName, database);'
            elif (parser.expect_optional("DISABLE")):
                print 'parseDisable(parser, outputIgnoredStatements, tableName, database);'
            else:
                parser.throw_unsupported_command()

            if (parser.expect_optional(";")):
                break
            else:
                parser.expect(",")
Example #40
0
 def parseColumn(parser, table):
     column = PgColumn(
         ParserUtils.get_object_name(parser.parse_identifier()))
     table.addColumn(column)
     column.parseDefinition(parser.get_expression())
Example #41
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)
    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)
Example #43
0
 def parseColumn(parser, table):
     column = PgColumn(ParserUtils.get_object_name(parser.parse_identifier()))
     table.addColumn(column)
     column.parseDefinition(parser.get_expression())
Example #44
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()
Example #45
0
 def parseConstraint(parser, table):
     constraint = PgConstraint(ParserUtils.get_object_name(parser.parse_identifier()));
     table.addConstraint(constraint)
     constraint.definition = parser.get_expression()
     constraint.tableName = table.name
Example #46
0
 def parseConstraint(parser, table):
     constraint = PgConstraint(
         ParserUtils.get_object_name(parser.parse_identifier()))
     table.addConstraint(constraint)
     constraint.definition = parser.get_expression()
     constraint.tableName = table.name