File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\SetComment.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
11 import org.h2.engine.Comment;
12 import org.h2.engine.Database;
13 import org.h2.engine.DbObject;
14 import org.h2.engine.Session;
15 import org.h2.expression.Expression;
16 import org.h2.message.DbException;
17 import org.h2.table.Table;
18
19 /**
20 * This class represents the statement
21 * COMMENT
22 */
23 public class SetComment extends DefineCommand {
24
25 private String schemaName;
26 private String objectName;
27 private boolean column;
28 private String columnName;
29 private int objectType;
30 private Expression expr;
31
32 public SetComment(Session session) {
33 super(session);
34 }
35
36 public int update() {
37 session.commit(true);
38 Database db = session.getDatabase();
39 session.getUser().checkAdmin();
40 DbObject object = null;
41 int errorCode = ErrorCode.GENERAL_ERROR_1;
42 if (schemaName == null) {
43 schemaName = session.getCurrentSchemaName();
44 }
45 switch (objectType) {
46 case DbObject.CONSTANT:
47 object = db.getSchema(schemaName).getConstant(objectName);
48 break;
49 case DbObject.CONSTRAINT:
50 object = db.getSchema(schemaName).getConstraint(objectName);
51 break;
52 case DbObject.FUNCTION_ALIAS:
53 object = db.getSchema(schemaName).findFunction(objectName);
54 errorCode = ErrorCode.FUNCTION_ALIAS_NOT_FOUND_1;
55 break;
56 case DbObject.INDEX:
57 object = db.getSchema(schemaName).getIndex(objectName);
58 break;
59 case DbObject.ROLE:
60 schemaName = null;
61 object = db.findRole(objectName);
62 errorCode = ErrorCode.ROLE_NOT_FOUND_1;
63 break;
64 case DbObject.SCHEMA:
65 schemaName = null;
66 object = db.findSchema(objectName);
67 errorCode = ErrorCode.SCHEMA_NOT_FOUND_1;
68 break;
69 case DbObject.SEQUENCE:
70 object = db.getSchema(schemaName).getSequence(objectName);
71 break;
72 case DbObject.TABLE_OR_VIEW:
73 object = db.getSchema(schemaName).getTableOrView(session, objectName);
74 break;
75 case DbObject.TRIGGER:
76 object = db.getSchema(schemaName).findTrigger(objectName);
77 errorCode = ErrorCode.TRIGGER_NOT_FOUND_1;
78 break;
79 case DbObject.USER:
80 schemaName = null;
81 object = db.getUser(objectName);
82 break;
83 case DbObject.USER_DATATYPE:
84 schemaName = null;
85 object = db.findUserDataType(objectName);
86 errorCode = ErrorCode.USER_DATA_TYPE_ALREADY_EXISTS_1;
87 break;
88 default:
89 }
90 if (object == null) {
91 throw DbException.get(errorCode, objectName);
92 }
93 String text = expr.optimize(session).getValue(session).getString();
94 if (column) {
95 Table table = (Table) object;
96 table.getColumn(columnName).setComment(text);
97 } else {
98 object.setComment(text);
99 }
100 if (column || objectType == DbObject.TABLE_OR_VIEW || objectType == DbObject.USER
101 || objectType == DbObject.INDEX || objectType == DbObject.CONSTRAINT) {
102 db.update(session, object);
103 } else {
104 Comment comment = db.findComment(object);
105 if (comment == null) {
106 if (text == null) {
107 // reset a non-existing comment - nothing to do
108 } else {
109 int id = getObjectId();
110 comment = new Comment(db, id, object);
111 comment.setCommentText(text);
112 db.addDatabaseObject(session, comment);
113 }
114 } else {
115 if (text == null) {
116 db.removeDatabaseObject(session, comment);
117 } else {
118 comment.setCommentText(text);
119 db.update(session, comment);
120 }
121 }
122 }
123 return 0;
124 }
125
126 public void setCommentExpression(Expression expr) {
127 this.expr = expr;
128 }
129
130 public void setObjectName(String objectName) {
131 this.objectName = objectName;
132 }
133
134 public void setObjectType(int objectType) {
135 this.objectType = objectType;
136 }
137
138 public void setColumnName(String columnName) {
139 this.columnName = columnName;
140 }
141
142 public void setSchemaName(String schemaName) {
143 this.schemaName = schemaName;
144 }
145
146 public void setColumn(boolean column) {
147 this.column = column;
148 }
149
150 public int getType() {
151 return CommandInterface.COMMENT;
152 }
153
154 }
155