| 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 java.util.ArrayList; |
| 9 |
|
| 10 |
import org.h2.api.ErrorCode; |
| 11 |
import org.h2.command.CommandInterface; |
| 12 |
import org.h2.constraint.ConstraintActionType; |
| 13 |
import org.h2.constraint.ConstraintDomain; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.expression.Expression; |
| 16 |
import org.h2.message.DbException; |
| 17 |
import org.h2.schema.Domain; |
| 18 |
import org.h2.schema.Schema; |
| 19 |
import org.h2.table.Column; |
| 20 |
import org.h2.table.ColumnTemplate; |
| 21 |
import org.h2.table.Table; |
| 22 |
|
| 23 |
/** |
| 24 |
* This class represents the statement DROP DOMAIN |
| 25 |
*/ |
| 26 |
public class DropDomain extends AlterDomain { |
| 27 |
|
| 28 |
private ConstraintActionType dropAction; |
| 29 |
|
| 30 |
public DropDomain(SessionLocal session, Schema schema) { |
| 31 |
super(session, schema); |
| 32 |
dropAction = getDatabase().getSettings().dropRestrict ? ConstraintActionType.RESTRICT |
| 33 |
: ConstraintActionType.CASCADE; |
| 34 |
} |
| 35 |
|
| 36 |
public void setDropAction(ConstraintActionType dropAction) { |
| 37 |
this.dropAction = dropAction; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
long update(Schema schema, Domain domain) { |
| 42 |
forAllDependencies(session, domain, this::copyColumn, this::copyDomain, true); |
| 43 |
getDatabase().removeSchemaObject(session, domain); |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
private boolean copyColumn(Domain domain, Column targetColumn) { |
| 48 |
Table targetTable = targetColumn.getTable(); |
| 49 |
if (dropAction == ConstraintActionType.RESTRICT) { |
| 50 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, domainName, targetTable.getCreateSQL()); |
| 51 |
} |
| 52 |
String columnName = targetColumn.getName(); |
| 53 |
ArrayList<ConstraintDomain> constraints = domain.getConstraints(); |
| 54 |
if (constraints != null && !constraints.isEmpty()) { |
| 55 |
for (ConstraintDomain constraint : constraints) { |
| 56 |
Expression checkCondition = constraint.getCheckConstraint(session, columnName); |
| 57 |
AlterTableAddConstraint check = new AlterTableAddConstraint(session, targetTable.getSchema(), |
| 58 |
CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK, false); |
| 59 |
check.setTableName(targetTable.getName()); |
| 60 |
check.setCheckExpression(checkCondition); |
| 61 |
check.update(); |
| 62 |
} |
| 63 |
} |
| 64 |
copyExpressions(session, domain, targetColumn); |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
private boolean copyDomain(Domain domain, Domain targetDomain) { |
| 69 |
if (dropAction == ConstraintActionType.RESTRICT) { |
| 70 |
throw DbException.get(ErrorCode.CANNOT_DROP_2, domainName, targetDomain.getTraceSQL()); |
| 71 |
} |
| 72 |
ArrayList<ConstraintDomain> constraints = domain.getConstraints(); |
| 73 |
if (constraints != null && !constraints.isEmpty()) { |
| 74 |
for (ConstraintDomain constraint : constraints) { |
| 75 |
Expression checkCondition = constraint.getCheckConstraint(session, null); |
| 76 |
AlterDomainAddConstraint check = new AlterDomainAddConstraint(session, targetDomain.getSchema(), // |
| 77 |
false); |
| 78 |
check.setDomainName(targetDomain.getName()); |
| 79 |
check.setCheckExpression(checkCondition); |
| 80 |
check.update(); |
| 81 |
} |
| 82 |
} |
| 83 |
copyExpressions(session, domain, targetDomain); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
private static boolean copyExpressions(SessionLocal session, Domain domain, ColumnTemplate targetColumn) { |
| 88 |
targetColumn.setDomain(domain.getDomain()); |
| 89 |
Expression e = domain.getDefaultExpression(); |
| 90 |
boolean modified = false; |
| 91 |
if (e != null && targetColumn.getDefaultExpression() == null) { |
| 92 |
targetColumn.setDefaultExpression(session, e); |
| 93 |
modified = true; |
| 94 |
} |
| 95 |
e = domain.getOnUpdateExpression(); |
| 96 |
if (e != null && targetColumn.getOnUpdateExpression() == null) { |
| 97 |
targetColumn.setOnUpdateExpression(session, e); |
| 98 |
modified = true; |
| 99 |
} |
| 100 |
return modified; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public int getType() { |
| 105 |
return CommandInterface.DROP_DOMAIN; |
| 106 |
} |
| 107 |
|
| 108 |
} |