| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.command.ddl; |
| 7 |
|
| 8 |
import org.h2.api.ErrorCode; |
| 9 |
import org.h2.command.CommandInterface; |
| 10 |
import org.h2.constraint.ConstraintDomain; |
| 11 |
import org.h2.engine.Database; |
| 12 |
import org.h2.engine.SessionLocal; |
| 13 |
import org.h2.expression.Expression; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Domain; |
| 16 |
import org.h2.schema.Schema; |
| 17 |
|
| 18 |
/** |
| 19 |
* This class represents the statement ALTER DOMAIN ADD CONSTRAINT |
| 20 |
*/ |
| 21 |
public class AlterDomainAddConstraint extends AlterDomain { |
| 22 |
|
| 23 |
private String constraintName; |
| 24 |
private Expression checkExpression; |
| 25 |
private String comment; |
| 26 |
private boolean checkExisting; |
| 27 |
private final boolean ifNotExists; |
| 28 |
|
| 29 |
public AlterDomainAddConstraint(SessionLocal session, Schema schema, boolean ifNotExists) { |
| 30 |
super(session, schema); |
| 31 |
this.ifNotExists = ifNotExists; |
| 32 |
} |
| 33 |
|
| 34 |
private String generateConstraintName(Domain domain) { |
| 35 |
if (constraintName == null) { |
| 36 |
constraintName = getSchema().getUniqueDomainConstraintName(session, domain); |
| 37 |
} |
| 38 |
return constraintName; |
| 39 |
} |
| 40 |
|
| 41 |
@Override |
| 42 |
long update(Schema schema, Domain domain) { |
| 43 |
try { |
| 44 |
return tryUpdate(schema, domain); |
| 45 |
} finally { |
| 46 |
getSchema().freeUniqueName(constraintName); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Try to execute the statement. |
| 52 |
* |
| 53 |
* @param schema the schema |
| 54 |
* @param domain the domain |
| 55 |
* @return the update count |
| 56 |
*/ |
| 57 |
private int tryUpdate(Schema schema, Domain domain) { |
| 58 |
if (constraintName != null && schema.findConstraint(session, constraintName) != null) { |
| 59 |
if (ifNotExists) { |
| 60 |
return 0; |
| 61 |
} |
| 62 |
throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, constraintName); |
| 63 |
} |
| 64 |
Database db = getDatabase(); |
| 65 |
db.lockMeta(session); |
| 66 |
|
| 67 |
int id = getObjectId(); |
| 68 |
String name = generateConstraintName(domain); |
| 69 |
ConstraintDomain constraint = new ConstraintDomain(schema, id, name, domain); |
| 70 |
constraint.setExpression(session, checkExpression); |
| 71 |
if (checkExisting) { |
| 72 |
constraint.checkExistingData(session); |
| 73 |
} |
| 74 |
constraint.setComment(comment); |
| 75 |
db.addSchemaObject(session, constraint); |
| 76 |
domain.addConstraint(constraint); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
public void setConstraintName(String constraintName) { |
| 81 |
this.constraintName = constraintName; |
| 82 |
} |
| 83 |
|
| 84 |
public String getConstraintName() { |
| 85 |
return constraintName; |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public int getType() { |
| 90 |
return CommandInterface.ALTER_DOMAIN_ADD_CONSTRAINT; |
| 91 |
} |
| 92 |
|
| 93 |
public void setCheckExpression(Expression expression) { |
| 94 |
this.checkExpression = expression; |
| 95 |
} |
| 96 |
|
| 97 |
public void setComment(String comment) { |
| 98 |
this.comment = comment; |
| 99 |
} |
| 100 |
|
| 101 |
public void setCheckExisting(boolean b) { |
| 102 |
this.checkExisting = b; |
| 103 |
} |
| 104 |
|
| 105 |
} |