File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateConstant.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.Database;
12 import org.h2.engine.Session;
13 import org.h2.expression.Expression;
14 import org.h2.message.DbException;
15 import org.h2.schema.Constant;
16 import org.h2.schema.Schema;
17 import org.h2.value.Value;
18
19 /**
20 * This class represents the statement
21 * CREATE CONSTANT
22 */
23 public class CreateConstant extends SchemaCommand {
24
25 private String constantName;
26 private Expression expression;
27 private boolean ifNotExists;
28
29 public CreateConstant(Session session, Schema schema) {
30 super(session, schema);
31 }
32
33 public void setIfNotExists(boolean ifNotExists) {
34 this.ifNotExists = ifNotExists;
35 }
36
37 public int update() {
38 session.commit(true);
39 session.getUser().checkAdmin();
40 Database db = session.getDatabase();
41 if (getSchema().findConstant(constantName) != null) {
42 if (ifNotExists) {
43 return 0;
44 }
45 throw DbException.get(ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
46 }
47 int id = getObjectId();
48 Constant constant = new Constant(getSchema(), id, constantName);
49 expression = expression.optimize(session);
50 Value value = expression.getValue(session);
51 constant.setValue(value);
52 db.addSchemaObject(session, constant);
53 return 0;
54 }
55
56 public void setConstantName(String constantName) {
57 this.constantName = constantName;
58 }
59
60 public void setExpression(Expression expr) {
61 this.expression = expr;
62 }
63
64 public int getType() {
65 return CommandInterface.CREATE_CONSTANT;
66 }
67
68 }
69