1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.Session; 11 import org.h2.expression.Expression; 12 import org.h2.message.Message; 13 import org.h2.schema.Constant; 14 import org.h2.schema.Schema; 15 import org.h2.value.Value; 16 17 public class CreateConstant extends SchemaCommand { 18 19 private String constantName; 20 private Expression expression; 21 private boolean ifNotExists; 22 23 public CreateConstant(Session session, Schema schema) { 24 super(session, schema); 25 } 26 27 public void setIfNotExists(boolean ifNotExists) { 28 this.ifNotExists = ifNotExists; 30 } 31 32 public int update() throws SQLException { 33 session.commit(); 34 session.getUser().checkAdmin(); 35 Database db = session.getDatabase(); 36 if(getSchema().findConstant(constantName)!=null) { 37 if (ifNotExists) { 38 return 0; 39 } 40 throw Message.getSQLException(Message.CONSTANT_ALREADY_EXISTS_1, 41 constantName); 42 } 43 int id = getObjectId(false, true); 44 Constant constant = new Constant(getSchema(), id, constantName); 45 expression = expression.optimize(session); 46 Value value = expression.getValue(session); 47 constant.setValue(value); 48 db.addSchemaObject(session, constant); 49 return 0; 50 } 51 52 public void setConstantName(String constantName) { 53 this.constantName = constantName; 54 } 55 56 public void setExpression(Expression expr) { 57 this.expression = expr; 58 } 59 60 } 61 | Popular Tags |