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.engine;
007:
008: import java.sql.SQLException;
009:
010: import org.h2.message.Message;
011: import org.h2.message.Trace;
012: import org.h2.table.Table;
013: import org.h2.util.StringUtils;
014:
015: /**
016: * Represents a database object comment.
017: */
018: public class Comment extends DbObjectBase {
019:
020: private final int objectType;
021: private final String objectName;
022: private String commentText;
023:
024: public Comment(Database database, int id, DbObject obj) {
025: super (database, id, getKey(obj), Trace.DATABASE);
026: this .objectType = obj.getType();
027: this .objectName = obj.getSQL();
028: }
029:
030: public String getCreateSQLForCopy(Table table, String quotedName) {
031: throw Message.getInternalError();
032: }
033:
034: private static String getTypeName(int type) {
035: switch (type) {
036: case DbObject.CONSTANT:
037: return "CONSTANT";
038: case DbObject.CONSTRAINT:
039: return "CONSTRAINT";
040: case DbObject.FUNCTION_ALIAS:
041: return "ALIAS";
042: case DbObject.INDEX:
043: return "INDEX";
044: case DbObject.ROLE:
045: return "ROLE";
046: case DbObject.SCHEMA:
047: return "SCHEMA";
048: case DbObject.SEQUENCE:
049: return "SEQUENCE";
050: case DbObject.TABLE_OR_VIEW:
051: return "TABLE";
052: case DbObject.TRIGGER:
053: return "TRIGGER";
054: case DbObject.USER:
055: return "USER";
056: case DbObject.USER_DATATYPE:
057: return "DOMAIN";
058: default:
059: // not supported by parser, but required when trying to find a comment
060: return "type" + type;
061: }
062: }
063:
064: public String getDropSQL() {
065: return null;
066: }
067:
068: public String getCreateSQL() {
069: StringBuffer buff = new StringBuffer();
070: buff.append("COMMENT ON ");
071: buff.append(getTypeName(objectType));
072: buff.append(' ');
073: buff.append(objectName);
074: buff.append(" IS ");
075: if (commentText == null) {
076: buff.append("NULL");
077: } else {
078: buff.append(StringUtils.quoteStringSQL(commentText));
079: }
080: return buff.toString();
081: }
082:
083: public int getType() {
084: return DbObject.COMMENT;
085: }
086:
087: public void removeChildrenAndResources(Session session)
088: throws SQLException {
089: database.removeMeta(session, getId());
090: }
091:
092: public void checkRename() throws SQLException {
093: throw Message.getInternalError();
094: }
095:
096: public static String getKey(DbObject obj) {
097: return getTypeName(obj.getType()) + " " + obj.getSQL();
098: }
099:
100: public void setCommentText(String comment) {
101: this.commentText = comment;
102: }
103:
104: }
|