def parse(database, statement):
        parser = Parser(statement)

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

        tableName = parser.parseIdentifier()
        schemaName = ParserUtils.getSchemaName(tableName, database)
        schema = database.getSchema(schemaName)

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

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

        if table is None:
            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.expectOptional(";"):
            if parser.expectOptional("ALTER"):
                AlterTableParser.parseAlterColumn(parser, table)
            elif (parser.expectOptional("CLUSTER", "ON")):
                table.clusterIndexName = ParserUtils.getObjectName(parser.parseIdentifier())
            elif (parser.expectOptional("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.parseIdentifier()
            elif (parser.expectOptional("ADD")):
                if (parser.expectOptional("FOREIGN", "KEY")):
                    print 'parseAddForeignKey(parser, table);'
                elif (parser.expectOptional("CONSTRAINT")):
                    AlterTableParser.parseAddConstraint(parser, table, schema)
                else:
                    parser.throwUnsupportedCommand()
            elif (parser.expectOptional("ENABLE")):
                print 'parseEnable(parser, outputIgnoredStatements, tableName, database);'
            elif (parser.expectOptional("DISABLE")):
                print 'parseDisable(parser, outputIgnoredStatements, tableName, database);'
            else:
                parser.throwUnsupportedCommand()

            if (parser.expectOptional(";")):
                break
            else:
                parser.expect(",")
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expectOptional("OR", "REPLACE")
        parser.expect("VIEW")

        viewName = parser.parseIdentifier()

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

        if (columnsExist):
            while not parser.expectOptional(")"):
                columnNames.append(ParserUtils.getObjectName(parser.parseIdentifier()))
                parser.expectOptional(",")

        parser.expect("AS")

        query = parser.getRest()

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

        schemaName = ParserUtils.getSchemaName(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.parseIdentifier()
        objectName = ParserUtils.getObjectName(functionName)
        schemaName = ParserUtils.getSchemaName(functionName, database)
        schema = database.getSchema(schemaName)

        parser.expect("(")

        tmpFunction = PgFunction()
        tmpFunction.name = objectName

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

            if parser.expectOptional("IN"):
                mode = "IN"
            elif parser.expectOptional("OUT"):
                mode = "OUT"
            elif parser.expectOptional("INOUT"):
                mode = "INOUT"
            elif parser.expectOptional("VARIADIC"):
                mode = "VARIADIC"
            else:
                mode = None

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

            position2 = parser.position

            if not parser.expectOptional(")") and not parser.expectOptional(","):
                parser.position = position
                argumentName = ParserUtils.getObjectName(parser.parseIdentifier())
                dataType = parser.parseDataType()
            else:
                parser.position = position2

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

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

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

        parser.expect("IS")
        function.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #4
0
    def parseTrigger(parser, database):
        triggerName = ParserUtils.getObjectName(parser.parseIdentifier())

        parser.expect("ON")

        tableName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(tableName)
        schemaName = ParserUtils.getSchemaName(triggerName, database)

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

        parser.expect("IS")
        trigger.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #5
0
    def parseConstraint(parser, database):
        constraintName = ParserUtils.getObjectName(parser.parseIdentifier())

        parser.expect("ON")

        tableName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(tableName)
        schemaName = ParserUtils.getSchemaName(constraintName, database)

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

        parser.expect("IS")
        constraint.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #6
0
    def parseColumn(parser, database):
        columnName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(columnName)
        tableName = ParserUtils.getSecondObjectName(columnName)
        schemaName = ParserUtils.getThirdObjectName(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("ALTER", "SEQUENCE")

        sequenceName = parser.parseIdentifier()
        schemaName = ParserUtils.getSchemaName(sequenceName, database)
        schema = database.getSchema(schemaName)

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

        objectName = ParserUtils.getObjectName(sequenceName);
        sequence = schema.sequences[objectName]

        if sequence is None:
            raise Exception("Cannot find sequence '%s' for statement '%s'. Missing CREATE SEQUENCE?" % (sequenceName, statement))

        while not parser.expectOptional(";"):

            if (parser.expectOptional("OWNED", "BY")):
                if parser.expectOptional("NONE"):
                    sequence.ownedBy = None
                else:
                    sequence.ownedBy = parser.getExpression()
            else:
                parser.throwUnsupportedCommand()
Example #8
0
    def parseSchema(parser, database):
        schemaName = ParserUtils.getObjectName(parser.parseIdentifier())
        schema = database.getSchema(schemaName)

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

        sequenceName = parser.parseIdentifier();
        sequence = PgSequence(ParserUtils.getObjectName(sequenceName))
        schemaName = ParserUtils.getSchemaName(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.expectOptional(";"):
            if parser.expectOptional("INCREMENT"):
                parser.expectOptional("BY")
                sequence.increment = parser.parseString()
            elif parser.expectOptional("MINVALUE"):
                sequence.minValue = parser.parseString()
            elif parser.expectOptional("MAXVALUE"):
                sequence.maxValue = parser.parseString()
            elif parser.expectOptional("START"):
                parser.expectOptional("WITH")
                sequence.startWith = parser.parseString()
            elif parser.expectOptional("CACHE"):
                sequence.cache = parser.parseString()
            elif parser.expectOptional("CYCLE"):
                sequence.cycle = True
            elif parser.expectOptional("OWNED", "BY"):
                if parser.expectOptional("NONE"):
                    sequence.ownedBy = None
                else:
                    sequence.ownedBy = ParserUtils.getObjectName(parser.parseIdentifier())

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

            else:
                parser.throwUnsupportedCommand()
Example #10
0
    def parseSequence(parser, database):
        sequenceName = parser.parseIdentifier();
        objectName = ParserUtils.getObjectName(sequenceName)
        schemaName = ParserUtils.getSchemaName(sequenceName, database)

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

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

        while not parser.expectOptional(")"):
            table.addInherits(ParserUtils.getObjectName(parser.parseIdentifier()))

            if parser.expectOptional(")"):
                break
            else:
                parser.expect(",")
Example #12
0
    def parseView(parser, database):
        viewName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(viewName)
        schemaName = ParserUtils.getSchemaName(viewName, database)

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

        parser.expect("IS")
        view.comment = CommentParser.getComment(parser)
        parser.expect(";")
Example #13
0
    def parseTable(parser, database):
        tableName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(tableName)
        schemaName = ParserUtils.getSchemaName(tableName, database)

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

        parser.expect("IS")
        table.comment = CommentParser.getComment(parser)
        parser.expect(";")
    def parseAddConstraint(parser, table, schema):
        constraintName = ParserUtils.getObjectName(parser.parseIdentifier())
        constraint = PgConstraint(constraintName)
        constraint.tableName = table.name
        table.addConstraint(constraint)

        if parser.expectOptional("PRIMARY", "KEY"):
            schema.addPrimaryKey(constraint)
            constraint.definition = "PRIMARY KEY " + parser.getExpression()
        else:
            constraint.setDefinition = parser.getExpression()
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("ALTER", "VIEW")

        viewName = parser.parseIdentifier()
        schemaName = ParserUtils.getSchemaName(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.getObjectName(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.expectOptional(";"):
            if parser.expectOptional("ALTER"):
                parser.expectOptional("COLUMN")

                columnName = ParserUtils.getObjectName(parser.parseIdentifier())

                if parser.expectOptional("SET", "DEFAULT"):
                    expression = parser.getExpression()
                    view.addColumnDefaultValue(columnName, expression)
                elif parser.expectOptional("DROP", "DEFAULT"):
                    view.removeColumnDefaultValue(columnName)
                else:
                    parser.throwUnsupportedCommand()

            elif parser.expectOptional("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.parseIdentifier()
                # }
            else:
                parser.throwUnsupportedCommand()
	def parse(database, statement):
		parser = Parser(statement)
		parser.expect("CREATE", "SCHEMA")

		if parser.expectOptional("AUTHORIZATION"):
			schema = PgSchema(ParserUtils.getObjectName(parser.parseIdentifier()))
			database.addSchema(schema)
			schema.authorization = schema.name

		else:
			schemaName = ParserUtils.getObjectName(parser.parseIdentifier())
			schema = PgSchema(schemaName)
			database.schemas[schemaName] = schema

			if parser.expectOptional("AUTHORIZATION"):
				schema.authorization = ParserUtils.getObjectName(parser.parseIdentifier())

		definition = parser.getRest()

		if definition:
			schema.definition = definition
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")

        unique = parser.expectOptional("UNIQUE")

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

        indexName = ParserUtils.getObjectName(parser.parseIdentifier())

        parser.expect("ON")

        tableName = parser.parseIdentifier()
        definition = parser.getRest()
        schemaName =ParserUtils.getSchemaName(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.getObjectName(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
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect('CREATE', 'TABLE')

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

        tableName = ParserUtils.getObjectName(parser.parseIdentifier())
        table = PgTable(tableName)
        # Figure it out why do we need this
        schemaName = ParserUtils.getSchemaName(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.expectOptional(")"):
            if parser.expectOptional("CONSTRAINT"):
                CreateTableParser.parseConstraint(parser, table)
            else:
                CreateTableParser.parseColumn(parser, table)

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


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

        columnName = ParserUtils.getObjectName(parser.parseIdentifier());

        if parser.expectOptional("SET"):
            if parser.expectOptional("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.parseInteger()
            elif parser.expectOptional("DEFAULT"):
                defaultValue = parser.getExpression()

                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.expectOptional("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.expectOptional("PLAIN"):
                    column.storage = "PLAIN"
                elif parser.expectOptional("EXTERNAL"):
                    column.storage = "EXTERNAL"
                elif parser.expectOptional("EXTENDED"):
                    column.storage = "EXTENDED"
                elif parser.expectOptional("MAIN"):
                    column.storage = "MAIN"
                else:
                    parser.throwUnsupportedCommand()
            else:
                parser.throwUnsupportedCommand()
        else:
            parser.throwUnsupportedCommand()
Example #20
0
    def parseIndex(parser, database):
        indexName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(indexName)
        schemaName = ParserUtils.getSchemaName(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(";")
    def parseView(parser, view, viewName, database):
        while not parser.expectOptional(";"):
            if parser.expectOptional("ALTER"):
                parser.expectOptional("COLUMN")

                columnName = ParserUtils.getObjectName(parser.parseIdentifier())

                if parser.expectOptional("SET", "DEFAULT"):
                    expression = parser.getExpression()
                    view.addColumnDefaultValue(columnName, expression)
                elif parser.expectOptional("DROP", "DEFAULT"):
                    view.removeColumnDefaultValue(columnName)
                else:
                    parser.throwUnsupportedCommand()

            elif parser.expectOptional("OWNER", "TO"):
                    parser.parseIdentifier()
            else:
                parser.throwUnsupportedCommand()
 def parseColumn(parser, table):
     column = PgColumn(ParserUtils.getObjectName(parser.parseIdentifier()))
     table.addColumn(column)
     column.parseDefinition(parser.getExpression())
    def parse(database, statement):
        parser = Parser(statement)
        parser.expect("CREATE")
        parser.expectOptional("OR", "REPLACE")
        parser.expect("FUNCTION")

        functionName = parser.parseIdentifier();
        schemaName = ParserUtils.getSchemaName(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.getObjectName(functionName)

        parser.expect("(")

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

            if parser.expectOptional("IN"):
                mode = "IN"
            elif parser.expectOptional("OUT"):
                mode = "OUT"
            elif parser.expectOptional("INOUT"):
                mode = "INOUT"
            elif parser.expectOptional("VARIADIC"):
                mode = "VARIADIC"
            else:
                mode = None

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

            position2 = parser.position

            if (not parser.expectOptional(")") and not parser.expectOptional(",")
                    and not parser.expectOptional("=")
                    and not parser.expectOptional("DEFAULT")):
                parser.position = position
                argumentName = ParserUtils.getObjectName(parser.parseIdentifier())
                dataType = parser.parseDataType()
            else:
                parser.position = position2

            defaultExpression = ''

            if (parser.expectOptional("=")
                    or parser.expectOptional("DEFAULT")):
                defaultExpression = parser.getExpression()
            else:
                defaultExpression = None

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

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

        function.body = parser.getRest()

        schema.addFunction(function)
    def parse(database, statement, ignoreSlonyTriggers):
        parser = Parser(statement)
        parser.expect("CREATE", "TRIGGER")

        triggerName = parser.parseIdentifier()
        objectName = ParserUtils.getObjectName(triggerName)

        trigger = PgTrigger()
        trigger.name = objectName

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

        first = True

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

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

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

            first = False

        parser.expect("ON")

        trigger.tableName = ParserUtils.getObjectName(parser.parseIdentifier())

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

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

        if parser.expectOptional("WHEN"):
            parser.expect("(")
            trigger.when = parser.getExpression()
            parser.expect(")")

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

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

        if (not ignoreSlonyTrigger):
            schema = database.getSchema(ParserUtils.getSchemaName(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()
 def parseConstraint(parser, table):
     constraint = PgConstraint(ParserUtils.getObjectName(parser.parseIdentifier()));
     table.addConstraint(constraint)
     constraint.definition = parser.getExpression()
     constraint.tableName = table.name