| 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.command.CommandInterface; |
| 9 |
import org.h2.engine.SessionLocal; |
| 10 |
import org.h2.expression.Expression; |
| 11 |
import org.h2.message.DbException; |
| 12 |
import org.h2.schema.Domain; |
| 13 |
import org.h2.schema.Schema; |
| 14 |
import org.h2.table.Column; |
| 15 |
import org.h2.table.ColumnTemplate; |
| 16 |
|
| 17 |
/** |
| 18 |
* This class represents the statements |
| 19 |
* ALTER DOMAIN SET DEFAULT |
| 20 |
* ALTER DOMAIN DROP DEFAULT |
| 21 |
* ALTER DOMAIN SET ON UPDATE |
| 22 |
* ALTER DOMAIN DROP ON UPDATE |
| 23 |
*/ |
| 24 |
public class AlterDomainExpressions extends AlterDomain { |
| 25 |
|
| 26 |
private final int type; |
| 27 |
|
| 28 |
private Expression expression; |
| 29 |
|
| 30 |
public AlterDomainExpressions(SessionLocal session, Schema schema, int type) { |
| 31 |
super(session, schema); |
| 32 |
this.type = type; |
| 33 |
} |
| 34 |
|
| 35 |
public void setExpression(Expression expression) { |
| 36 |
this.expression = expression; |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
long update(Schema schema, Domain domain) { |
| 41 |
switch (type) { |
| 42 |
case CommandInterface.ALTER_DOMAIN_DEFAULT: |
| 43 |
domain.setDefaultExpression(session, expression); |
| 44 |
break; |
| 45 |
case CommandInterface.ALTER_DOMAIN_ON_UPDATE: |
| 46 |
domain.setOnUpdateExpression(session, expression); |
| 47 |
break; |
| 48 |
default: |
| 49 |
throw DbException.getInternalError("type=" + type); |
| 50 |
} |
| 51 |
if (expression != null) { |
| 52 |
forAllDependencies(session, domain, this::copyColumn, this::copyDomain, true); |
| 53 |
} |
| 54 |
getDatabase().updateMeta(session, domain); |
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
private boolean copyColumn(Domain domain, Column targetColumn) { |
| 59 |
return copyExpressions(session, domain, targetColumn); |
| 60 |
} |
| 61 |
|
| 62 |
private boolean copyDomain(Domain domain, Domain targetDomain) { |
| 63 |
return copyExpressions(session, domain, targetDomain); |
| 64 |
} |
| 65 |
|
| 66 |
private boolean copyExpressions(SessionLocal session, Domain domain, ColumnTemplate targetColumn) { |
| 67 |
switch (type) { |
| 68 |
case CommandInterface.ALTER_DOMAIN_DEFAULT: { |
| 69 |
Expression e = domain.getDefaultExpression(); |
| 70 |
if (e != null && targetColumn.getDefaultExpression() == null) { |
| 71 |
targetColumn.setDefaultExpression(session, e); |
| 72 |
return true; |
| 73 |
} |
| 74 |
break; |
| 75 |
} |
| 76 |
case CommandInterface.ALTER_DOMAIN_ON_UPDATE: { |
| 77 |
Expression e = domain.getOnUpdateExpression(); |
| 78 |
if (e != null && targetColumn.getOnUpdateExpression() == null) { |
| 79 |
targetColumn.setOnUpdateExpression(session, e); |
| 80 |
return true; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public int getType() { |
| 89 |
return type; |
| 90 |
} |
| 91 |
|
| 92 |
} |