EMMA Coverage Report (generated Sun Mar 01 22:06:14 CET 2015)
[all classes][org.h2.command.ddl]

COVERAGE SUMMARY FOR SOURCE FILE [SetComment.java]

nameclass, %method, %block, %line, %
SetComment.java100% (1/1)100% (9/9)93%  (252/271)94%  (72.7/77)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SetComment100% (1/1)100% (9/9)93%  (252/271)94%  (72.7/77)
update (): int 100% (1/1)92%  (222/241)93%  (57.7/62)
SetComment (Session): void 100% (1/1)100% (4/4)100% (2/2)
getType (): int 100% (1/1)100% (2/2)100% (1/1)
setColumn (boolean): void 100% (1/1)100% (4/4)100% (2/2)
setColumnName (String): void 100% (1/1)100% (4/4)100% (2/2)
setCommentExpression (Expression): void 100% (1/1)100% (4/4)100% (2/2)
setObjectName (String): void 100% (1/1)100% (4/4)100% (2/2)
setObjectType (int): void 100% (1/1)100% (4/4)100% (2/2)
setSchemaName (String): void 100% (1/1)100% (4/4)100% (2/2)

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 */
6package org.h2.command.ddl;
7 
8import org.h2.api.ErrorCode;
9import org.h2.command.CommandInterface;
10import org.h2.engine.Comment;
11import org.h2.engine.Database;
12import org.h2.engine.DbObject;
13import org.h2.engine.Session;
14import org.h2.expression.Expression;
15import org.h2.message.DbException;
16import org.h2.table.Table;
17 
18/**
19 * This class represents the statement
20 * COMMENT
21 */
22public class SetComment extends DefineCommand {
23 
24    private String schemaName;
25    private String objectName;
26    private boolean column;
27    private String columnName;
28    private int objectType;
29    private Expression expr;
30 
31    public SetComment(Session session) {
32        super(session);
33    }
34 
35    @Override
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 ||
101                objectType == DbObject.USER ||
102                objectType == DbObject.INDEX ||
103                objectType == DbObject.CONSTRAINT) {
104            db.updateMeta(session, object);
105        } else {
106            Comment comment = db.findComment(object);
107            if (comment == null) {
108                if (text == null) {
109                    // reset a non-existing comment - nothing to do
110                } else {
111                    int id = getObjectId();
112                    comment = new Comment(db, id, object);
113                    comment.setCommentText(text);
114                    db.addDatabaseObject(session, comment);
115                }
116            } else {
117                if (text == null) {
118                    db.removeDatabaseObject(session, comment);
119                } else {
120                    comment.setCommentText(text);
121                    db.updateMeta(session, comment);
122                }
123            }
124        }
125        return 0;
126    }
127 
128    public void setCommentExpression(Expression expr) {
129        this.expr = expr;
130    }
131 
132    public void setObjectName(String objectName) {
133        this.objectName = objectName;
134    }
135 
136    public void setObjectType(int objectType) {
137        this.objectType = objectType;
138    }
139 
140    public void setColumnName(String columnName) {
141        this.columnName = columnName;
142    }
143 
144    public void setSchemaName(String schemaName) {
145        this.schemaName = schemaName;
146    }
147 
148    public void setColumn(boolean column) {
149        this.column = column;
150    }
151 
152    @Override
153    public int getType() {
154        return CommandInterface.COMMENT;
155    }
156 
157}

[all classes][org.h2.command.ddl]
EMMA 2.0.5312 (C) Vladimir Roubtsov