Search in sources :

Example 1 with SetComment

use of org.h2.command.ddl.SetComment in project h2database by h2database.

the class SetComment method update.

@Override
public int update() {
    session.commit(true);
    Database db = session.getDatabase();
    session.getUser().checkAdmin();
    DbObject object = null;
    int errorCode = ErrorCode.GENERAL_ERROR_1;
    if (schemaName == null) {
        schemaName = session.getCurrentSchemaName();
    }
    switch(objectType) {
        case DbObject.CONSTANT:
            object = db.getSchema(schemaName).getConstant(objectName);
            break;
        case DbObject.CONSTRAINT:
            object = db.getSchema(schemaName).getConstraint(objectName);
            break;
        case DbObject.FUNCTION_ALIAS:
            object = db.getSchema(schemaName).findFunction(objectName);
            errorCode = ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1;
            break;
        case DbObject.INDEX:
            object = db.getSchema(schemaName).getIndex(objectName);
            break;
        case DbObject.ROLE:
            schemaName = null;
            object = db.findRole(objectName);
            errorCode = ErrorCode.ROLE_NOT_FOUND_1;
            break;
        case DbObject.SCHEMA:
            schemaName = null;
            object = db.findSchema(objectName);
            errorCode = ErrorCode.SCHEMA_NOT_FOUND_1;
            break;
        case DbObject.SEQUENCE:
            object = db.getSchema(schemaName).getSequence(objectName);
            break;
        case DbObject.TABLE_OR_VIEW:
            object = db.getSchema(schemaName).getTableOrView(session, objectName);
            break;
        case DbObject.TRIGGER:
            object = db.getSchema(schemaName).findTrigger(objectName);
            errorCode = ErrorCode.TRIGGER_NOT_FOUND_1;
            break;
        case DbObject.USER:
            schemaName = null;
            object = db.getUser(objectName);
            break;
        case DbObject.USER_DATATYPE:
            schemaName = null;
            object = db.findUserDataType(objectName);
            errorCode = ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1;
            break;
        default:
    }
    if (object == null) {
        throw DbException.get(errorCode, objectName);
    }
    String text = expr.optimize(session).getValue(session).getString();
    if (column) {
        Table table = (Table) object;
        table.getColumn(columnName).setComment(text);
    } else {
        object.setComment(text);
    }
    if (column || objectType == DbObject.TABLE_OR_VIEW || objectType == DbObject.USER || objectType == DbObject.INDEX || objectType == DbObject.CONSTRAINT) {
        db.updateMeta(session, object);
    } else {
        Comment comment = db.findComment(object);
        if (comment == null) {
            if (text == null) {
            // reset a non-existing comment - nothing to do
            } else {
                int id = getObjectId();
                comment = new Comment(db, id, object);
                comment.setCommentText(text);
                db.addDatabaseObject(session, comment);
            }
        } else {
            if (text == null) {
                db.removeDatabaseObject(session, comment);
            } else {
                comment.setCommentText(text);
                db.updateMeta(session, comment);
            }
        }
    }
    return 0;
}
Also used : Comment(org.h2.engine.Comment) Table(org.h2.table.Table) DbObject(org.h2.engine.DbObject) Database(org.h2.engine.Database)

Example 2 with SetComment

use of org.h2.command.ddl.SetComment in project h2database by h2database.

the class Parser method parseComment.

private Prepared parseComment() {
    int type = 0;
    read("ON");
    boolean column = false;
    if (readIf("TABLE") || readIf("VIEW")) {
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("COLUMN")) {
        column = true;
        type = DbObject.TABLE_OR_VIEW;
    } else if (readIf("CONSTANT")) {
        type = DbObject.CONSTANT;
    } else if (readIf("CONSTRAINT")) {
        type = DbObject.CONSTRAINT;
    } else if (readIf("ALIAS")) {
        type = DbObject.FUNCTION_ALIAS;
    } else if (readIf("INDEX")) {
        type = DbObject.INDEX;
    } else if (readIf("ROLE")) {
        type = DbObject.ROLE;
    } else if (readIf("SCHEMA")) {
        type = DbObject.SCHEMA;
    } else if (readIf("SEQUENCE")) {
        type = DbObject.SEQUENCE;
    } else if (readIf("TRIGGER")) {
        type = DbObject.TRIGGER;
    } else if (readIf("USER")) {
        type = DbObject.USER;
    } else if (readIf("DOMAIN")) {
        type = DbObject.USER_DATATYPE;
    } else {
        throw getSyntaxError();
    }
    SetComment command = new SetComment(session);
    String objectName;
    if (column) {
        // can't use readIdentifierWithSchema() because
        // it would not read schema.table.column correctly
        // if the db name is equal to the schema name
        ArrayList<String> list = New.arrayList();
        do {
            list.add(readUniqueIdentifier());
        } while (readIf("."));
        schemaName = session.getCurrentSchemaName();
        if (list.size() == 4) {
            if (!equalsToken(database.getShortName(), list.remove(0))) {
                throw DbException.getSyntaxError(sqlCommand, parseIndex, "database name");
            }
        }
        if (list.size() == 3) {
            schemaName = list.remove(0);
        }
        if (list.size() != 2) {
            throw DbException.getSyntaxError(sqlCommand, parseIndex, "table.column");
        }
        objectName = list.get(0);
        command.setColumn(true);
        command.setColumnName(list.get(1));
    } else {
        objectName = readIdentifierWithSchema();
    }
    command.setSchemaName(schemaName);
    command.setObjectName(objectName);
    command.setObjectType(type);
    read("IS");
    command.setCommentExpression(readExpression());
    return command;
}
Also used : SetComment(org.h2.command.ddl.SetComment) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint)

Aggregations

AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)1 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)1 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)1 SetComment (org.h2.command.ddl.SetComment)1 Comment (org.h2.engine.Comment)1 Database (org.h2.engine.Database)1 DbObject (org.h2.engine.DbObject)1 Table (org.h2.table.Table)1 ValueString (org.h2.value.ValueString)1