001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.ddl;
007:
008: import java.sql.SQLException;
009: import org.h2.constant.ErrorCode;
010: import org.h2.engine.Comment;
011: import org.h2.engine.Database;
012: import org.h2.engine.DbObject;
013: import org.h2.engine.Session;
014: import org.h2.expression.Expression;
015: import org.h2.message.Message;
016: import org.h2.table.Table;
017:
018: /**
019: * This class represents the statement
020: * COMMENT
021: */
022: public class SetComment extends DefineCommand {
023:
024: private String schemaName;
025: private String objectName;
026: private boolean column;
027: private String columnName;
028: private int objectType;
029: private Expression expr;
030:
031: public SetComment(Session session) {
032: super (session);
033: }
034:
035: public int update() throws SQLException {
036: session.commit(true);
037: Database db = session.getDatabase();
038: session.getUser().checkAdmin();
039: DbObject object = null;
040: int errorCode = ErrorCode.GENERAL_ERROR_1;
041: if (schemaName == null) {
042: schemaName = session.getCurrentSchemaName();
043: }
044: switch (objectType) {
045: case DbObject.CONSTANT:
046: object = db.getSchema(schemaName).getConstant(session,
047: objectName);
048: break;
049: case DbObject.CONSTRAINT:
050: object = db.getSchema(schemaName).getConstraint(objectName);
051: break;
052: case DbObject.FUNCTION_ALIAS:
053: schemaName = null;
054: object = db.findFunctionAlias(objectName);
055: errorCode = ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1;
056: break;
057: case DbObject.INDEX:
058: object = db.getSchema(schemaName).getIndex(objectName);
059: break;
060: case DbObject.ROLE:
061: schemaName = null;
062: object = db.findRole(objectName);
063: errorCode = ErrorCode.ROLE_NOT_FOUND_1;
064: break;
065: case DbObject.SCHEMA:
066: schemaName = null;
067: object = db.findSchema(objectName);
068: errorCode = ErrorCode.SCHEMA_NOT_FOUND_1;
069: break;
070: case DbObject.SEQUENCE:
071: object = db.getSchema(schemaName).getSequence(objectName);
072: break;
073: case DbObject.TABLE_OR_VIEW:
074: object = db.getSchema(schemaName).getTableOrView(session,
075: objectName);
076: break;
077: case DbObject.TRIGGER:
078: object = db.getSchema(schemaName).findTrigger(objectName);
079: errorCode = ErrorCode.TRIGGER_NOT_FOUND_1;
080: break;
081: case DbObject.USER:
082: schemaName = null;
083: object = db.getUser(objectName);
084: break;
085: case DbObject.USER_DATATYPE:
086: schemaName = null;
087: object = db.findUserDataType(objectName);
088: errorCode = ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1;
089: break;
090: default:
091: }
092: if (object == null) {
093: throw Message.getSQLException(errorCode, objectName);
094: }
095: String text = expr.getValue(session).getString();
096: if (column) {
097: Table table = (Table) object;
098: table.getColumn(columnName).setComment(text);
099: } else {
100: object.setComment(text);
101: }
102: if (column || objectType == DbObject.TABLE_OR_VIEW
103: || objectType == DbObject.USER
104: || objectType == DbObject.INDEX
105: || objectType == DbObject.CONSTRAINT) {
106: db.update(session, object);
107: } else {
108: Comment comment = db.findComment(object);
109: if (comment == null) {
110: if (text == null) {
111: // reset a non-existing comment - nothing to do
112: } else {
113: int id = getObjectId(false, false);
114: comment = new Comment(db, id, object);
115: comment.setCommentText(text);
116: db.addDatabaseObject(session, comment);
117: }
118: } else {
119: if (text == null) {
120: db.removeDatabaseObject(session, comment);
121: } else {
122: comment.setCommentText(text);
123: db.update(session, comment);
124: }
125: }
126: }
127: return 0;
128: }
129:
130: public void setCommentExpression(Expression expr) {
131: this .expr = expr;
132: }
133:
134: public void setObjectName(String objectName) {
135: this .objectName = objectName;
136: }
137:
138: public void setObjectType(int objectType) {
139: this .objectType = objectType;
140: }
141:
142: public void setColumnName(String columnName) {
143: this .columnName = columnName;
144: }
145:
146: public void setSchemaName(String schemaName) {
147: this .schemaName = schemaName;
148: }
149:
150: public void setColumn(boolean column) {
151: this.column = column;
152: }
153:
154: }
|