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