| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.engine; |
| 7 |
|
| 8 |
import org.h2.message.DbException; |
| 9 |
import org.h2.message.Trace; |
| 10 |
import org.h2.util.StringUtils; |
| 11 |
|
| 12 |
/** |
| 13 |
* Represents a database object comment. |
| 14 |
*/ |
| 15 |
public final class Comment extends DbObject { |
| 16 |
|
| 17 |
private final int objectType; |
| 18 |
private final String quotedObjectName; |
| 19 |
private String commentText; |
| 20 |
|
| 21 |
public Comment(Database database, int id, DbObject obj) { |
| 22 |
super(database, id, getKey(obj), Trace.DATABASE); |
| 23 |
this.objectType = obj.getType(); |
| 24 |
this.quotedObjectName = obj.getSQL(DEFAULT_SQL_FLAGS); |
| 25 |
} |
| 26 |
|
| 27 |
private static String getTypeName(int type) { |
| 28 |
switch (type) { |
| 29 |
case DbObject.CONSTANT: |
| 30 |
return "CONSTANT"; |
| 31 |
case DbObject.CONSTRAINT: |
| 32 |
return "CONSTRAINT"; |
| 33 |
case DbObject.FUNCTION_ALIAS: |
| 34 |
return "ALIAS"; |
| 35 |
case DbObject.INDEX: |
| 36 |
return "INDEX"; |
| 37 |
case DbObject.ROLE: |
| 38 |
return "ROLE"; |
| 39 |
case DbObject.SCHEMA: |
| 40 |
return "SCHEMA"; |
| 41 |
case DbObject.SEQUENCE: |
| 42 |
return "SEQUENCE"; |
| 43 |
case DbObject.TABLE_OR_VIEW: |
| 44 |
return "TABLE"; |
| 45 |
case DbObject.TRIGGER: |
| 46 |
return "TRIGGER"; |
| 47 |
case DbObject.USER: |
| 48 |
return "USER"; |
| 49 |
case DbObject.DOMAIN: |
| 50 |
return "DOMAIN"; |
| 51 |
default: |
| 52 |
// not supported by parser, but required when trying to find a |
| 53 |
// comment |
| 54 |
return "type" + type; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public String getCreateSQL() { |
| 60 |
StringBuilder buff = new StringBuilder("COMMENT ON "); |
| 61 |
buff.append(getTypeName(objectType)).append(' '). |
| 62 |
append(quotedObjectName).append(" IS "); |
| 63 |
if (commentText == null) { |
| 64 |
buff.append("NULL"); |
| 65 |
} else { |
| 66 |
StringUtils.quoteStringSQL(buff, commentText); |
| 67 |
} |
| 68 |
return buff.toString(); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public int getType() { |
| 73 |
return DbObject.COMMENT; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void removeChildrenAndResources(SessionLocal session) { |
| 78 |
database.removeMeta(session, getId()); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void checkRename() { |
| 83 |
throw DbException.getInternalError(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Get the comment key name for the given database object. This key name is |
| 88 |
* used internally to associate the comment to the object. |
| 89 |
* |
| 90 |
* @param obj the object |
| 91 |
* @return the key name |
| 92 |
*/ |
| 93 |
static String getKey(DbObject obj) { |
| 94 |
StringBuilder builder = new StringBuilder(getTypeName(obj.getType())).append(' '); |
| 95 |
obj.getSQL(builder, DEFAULT_SQL_FLAGS); |
| 96 |
return builder.toString(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Set the comment text. |
| 101 |
* |
| 102 |
* @param comment the text |
| 103 |
*/ |
| 104 |
public void setCommentText(String comment) { |
| 105 |
this.commentText = comment; |
| 106 |
} |
| 107 |
|
| 108 |
} |