| 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.Constraint; |
| 11 |
import org.h2.constraint.Constraint.Type; |
| 12 |
import org.h2.constraint.ConstraintDomain; |
| 13 |
import org.h2.engine.SessionLocal; |
| 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 |
| 20 |
* ALTER DOMAIN RENAME CONSTRAINT |
| 21 |
*/ |
| 22 |
public class AlterDomainRenameConstraint extends AlterDomain { |
| 23 |
|
| 24 |
private String constraintName; |
| 25 |
private String newConstraintName; |
| 26 |
|
| 27 |
public AlterDomainRenameConstraint(SessionLocal session, Schema schema) { |
| 28 |
super(session, schema); |
| 29 |
} |
| 30 |
|
| 31 |
public void setConstraintName(String string) { |
| 32 |
constraintName = string; |
| 33 |
} |
| 34 |
|
| 35 |
public void setNewConstraintName(String newName) { |
| 36 |
this.newConstraintName = newName; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
long update(Schema schema, Domain domain) { |
| 41 |
Constraint constraint = getSchema().findConstraint(session, constraintName); |
| 42 |
if (constraint == null || constraint.getConstraintType() != Type.DOMAIN |
| 43 |
|| ((ConstraintDomain) constraint).getDomain() != domain) { |
| 44 |
throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName); |
| 45 |
} |
| 46 |
if (getSchema().findConstraint(session, newConstraintName) != null |
| 47 |
|| newConstraintName.equals(constraintName)) { |
| 48 |
throw DbException.get(ErrorCode.CONSTRAINT_ALREADY_EXISTS_1, newConstraintName); |
| 49 |
} |
| 50 |
getDatabase().renameSchemaObject(session, constraint, newConstraintName); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public int getType() { |
| 56 |
return CommandInterface.ALTER_DOMAIN_RENAME_CONSTRAINT; |
| 57 |
} |
| 58 |
|
| 59 |
} |