KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > SetComment


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

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