| 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 |
import org.h2.api.ErrorCode; |
| 10 |
import org.h2.command.CommandInterface; |
| 11 |
import org.h2.engine.SessionLocal; |
| 12 |
import org.h2.expression.Expression; |
| 13 |
import org.h2.message.DbException; |
| 14 |
import org.h2.schema.Domain; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.table.Table; |
| 17 |
import org.h2.util.HasSQL; |
| 18 |
import org.h2.util.Utils; |
| 19 |
import org.h2.value.DataType; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
import org.h2.value.Value; |
| 22 |
|
| 23 |
/** |
| 24 |
* This class represents the statement |
| 25 |
* CREATE DOMAIN |
| 26 |
*/ |
| 27 |
public class CreateDomain extends SchemaOwnerCommand { |
| 28 |
|
| 29 |
private String typeName; |
| 30 |
private boolean ifNotExists; |
| 31 |
|
| 32 |
private TypeInfo dataType; |
| 33 |
|
| 34 |
private Domain parentDomain; |
| 35 |
|
| 36 |
private Expression defaultExpression; |
| 37 |
|
| 38 |
private Expression onUpdateExpression; |
| 39 |
|
| 40 |
private String comment; |
| 41 |
|
| 42 |
private ArrayList<AlterDomainAddConstraint> constraintCommands; |
| 43 |
|
| 44 |
public CreateDomain(SessionLocal session, Schema schema) { |
| 45 |
super(session, schema); |
| 46 |
} |
| 47 |
|
| 48 |
public void setTypeName(String name) { |
| 49 |
this.typeName = name; |
| 50 |
} |
| 51 |
|
| 52 |
public void setIfNotExists(boolean ifNotExists) { |
| 53 |
this.ifNotExists = ifNotExists; |
| 54 |
} |
| 55 |
|
| 56 |
public void setDataType(TypeInfo dataType) { |
| 57 |
this.dataType = dataType; |
| 58 |
} |
| 59 |
|
| 60 |
public void setParentDomain(Domain parentDomain) { |
| 61 |
this.parentDomain = parentDomain; |
| 62 |
} |
| 63 |
|
| 64 |
public void setDefaultExpression(Expression defaultExpression) { |
| 65 |
this.defaultExpression = defaultExpression; |
| 66 |
} |
| 67 |
|
| 68 |
public void setOnUpdateExpression(Expression onUpdateExpression) { |
| 69 |
this.onUpdateExpression = onUpdateExpression; |
| 70 |
} |
| 71 |
|
| 72 |
public void setComment(String comment) { |
| 73 |
this.comment = comment; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
long update(Schema schema) { |
| 78 |
if (schema.findDomain(typeName) != null) { |
| 79 |
if (ifNotExists) { |
| 80 |
return 0; |
| 81 |
} |
| 82 |
throw DbException.get(ErrorCode.DOMAIN_ALREADY_EXISTS_1, typeName); |
| 83 |
} |
| 84 |
if (typeName.indexOf(' ') < 0) { |
| 85 |
DataType builtIn = DataType.getTypeByName(typeName, getDatabase().getMode()); |
| 86 |
if (builtIn != null) { |
| 87 |
if (getDatabase().equalsIdentifiers(typeName, Value.getTypeName(builtIn.type))) { |
| 88 |
throw DbException.get(ErrorCode.DOMAIN_ALREADY_EXISTS_1, typeName); |
| 89 |
} |
| 90 |
Table table = getDatabase().getFirstUserTable(); |
| 91 |
if (table != null) { |
| 92 |
StringBuilder builder = new StringBuilder(typeName).append(" ("); |
| 93 |
table.getSQL(builder, HasSQL.TRACE_SQL_FLAGS).append(')'); |
| 94 |
throw DbException.get(ErrorCode.DOMAIN_ALREADY_EXISTS_1, builder.toString()); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
int id = getObjectId(); |
| 99 |
Domain domain = new Domain(schema, id, typeName); |
| 100 |
domain.setDataType(dataType != null ? dataType : parentDomain.getDataType()); |
| 101 |
domain.setDomain(parentDomain); |
| 102 |
domain.setDefaultExpression(session, defaultExpression); |
| 103 |
domain.setOnUpdateExpression(session, onUpdateExpression); |
| 104 |
domain.setComment(comment); |
| 105 |
schema.getDatabase().addSchemaObject(session, domain); |
| 106 |
if (constraintCommands != null) { |
| 107 |
for (AlterDomainAddConstraint command : constraintCommands) { |
| 108 |
command.update(); |
| 109 |
} |
| 110 |
} |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public int getType() { |
| 116 |
return CommandInterface.CREATE_DOMAIN; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Add a constraint command. |
| 121 |
* |
| 122 |
* @param command the command to add |
| 123 |
*/ |
| 124 |
public void addConstraintCommand(AlterDomainAddConstraint command) { |
| 125 |
if (constraintCommands == null) { |
| 126 |
constraintCommands = Utils.newSmallArrayList(); |
| 127 |
} |
| 128 |
constraintCommands.add(command); |
| 129 |
} |
| 130 |
|
| 131 |
} |