01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
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.Message;
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: // TODO constant: if exists - probably better use 'or replace'
35: this .ifNotExists = ifNotExists;
36: }
37:
38: public int update() throws SQLException {
39: session.commit(true);
40: session.getUser().checkAdmin();
41: Database db = session.getDatabase();
42: if (getSchema().findConstant(constantName) != null) {
43: if (ifNotExists) {
44: return 0;
45: }
46: throw Message.getSQLException(
47: ErrorCode.CONSTANT_ALREADY_EXISTS_1, constantName);
48: }
49: int id = getObjectId(false, true);
50: Constant constant = new Constant(getSchema(), id, constantName);
51: expression = expression.optimize(session);
52: Value value = expression.getValue(session);
53: constant.setValue(value);
54: db.addSchemaObject(session, constant);
55: return 0;
56: }
57:
58: public void setConstantName(String constantName) {
59: this .constantName = constantName;
60: }
61:
62: public void setExpression(Expression expr) {
63: this.expression = expr;
64: }
65:
66: }
|