Debian Codegraph @

src/main/org/h2/schema/Domain.java

raw
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.schema;
7
8 import java.util.ArrayList;
9 import org.h2.constraint.Constraint;
10 import org.h2.constraint.ConstraintDomain;
11 import org.h2.engine.DbObject;
12 import org.h2.engine.SessionLocal;
13 import org.h2.expression.Expression;
14 import org.h2.expression.ValueExpression;
15 import org.h2.message.Trace;
16 import org.h2.table.ColumnTemplate;
17 import org.h2.util.Utils;
18 import org.h2.value.TypeInfo;
19 import org.h2.value.Value;
20
21 /**
22 * Represents a domain.
23 */
24 public final class Domain extends SchemaObject implements ColumnTemplate {
25
26 private TypeInfo type;
27
28 /**
29 * Parent domain.
30 */
31 private Domain domain;
32
33 private Expression defaultExpression;
34
35 private Expression onUpdateExpression;
36
37 private ArrayList<ConstraintDomain> constraints;
38
39 public Domain(Schema schema, int id, String name) {
40 super(schema, id, name, Trace.SCHEMA);
41 }
42
43 @Override
44 public String getDropSQL() {
45 StringBuilder builder = new StringBuilder("DROP DOMAIN IF EXISTS ");
46 return getSQL(builder, DEFAULT_SQL_FLAGS).toString();
47 }
48
49 @Override
50 public String getCreateSQL() {
51 StringBuilder builder = getSQL(new StringBuilder("CREATE DOMAIN "), DEFAULT_SQL_FLAGS).append(" AS ");
52 if (domain != null) {
53 domain.getSQL(builder, DEFAULT_SQL_FLAGS);
54 } else {
55 type.getSQL(builder, DEFAULT_SQL_FLAGS);
56 }
57 if (defaultExpression != null) {
58 defaultExpression.getUnenclosedSQL(builder.append(" DEFAULT "), DEFAULT_SQL_FLAGS);
59 }
60 if (onUpdateExpression != null) {
61 onUpdateExpression.getUnenclosedSQL(builder.append(" ON UPDATE "), DEFAULT_SQL_FLAGS);
62 }
63 return builder.toString();
64 }
65
66 public void setDataType(TypeInfo type) {
67 this.type = type;
68 }
69
70 public TypeInfo getDataType() {
71 return type;
72 }
73
74 @Override
75 public void setDomain(Domain domain) {
76 this.domain = domain;
77 }
78
79 @Override
80 public Domain getDomain() {
81 return domain;
82 }
83
84 @Override
85 public void setDefaultExpression(SessionLocal session, Expression defaultExpression) {
86 // also to test that no column names are used
87 if (defaultExpression != null) {
88 defaultExpression = defaultExpression.optimize(session);
89 if (defaultExpression.isConstant()) {
90 defaultExpression = ValueExpression.get(defaultExpression.getValue(session));
91 }
92 }
93 this.defaultExpression = defaultExpression;
94 }
95
96 @Override
97 public Expression getDefaultExpression() {
98 return defaultExpression;
99 }
100
101 @Override
102 public Expression getEffectiveDefaultExpression() {
103 return defaultExpression != null ? defaultExpression
104 : domain != null ? domain.getEffectiveDefaultExpression() : null;
105 }
106
107 @Override
108 public String getDefaultSQL() {
109 return defaultExpression == null ? null
110 : defaultExpression.getUnenclosedSQL(new StringBuilder(), DEFAULT_SQL_FLAGS).toString();
111 }
112
113 @Override
114 public void setOnUpdateExpression(SessionLocal session, Expression onUpdateExpression) {
115 // also to test that no column names are used
116 if (onUpdateExpression != null) {
117 onUpdateExpression = onUpdateExpression.optimize(session);
118 if (onUpdateExpression.isConstant()) {
119 onUpdateExpression = ValueExpression.get(onUpdateExpression.getValue(session));
120 }
121 }
122 this.onUpdateExpression = onUpdateExpression;
123 }
124
125 @Override
126 public Expression getOnUpdateExpression() {
127 return onUpdateExpression;
128 }
129
130 @Override
131 public Expression getEffectiveOnUpdateExpression() {
132 return onUpdateExpression != null ? onUpdateExpression
133 : domain != null ? domain.getEffectiveOnUpdateExpression() : null;
134 }
135
136 @Override
137 public String getOnUpdateSQL() {
138 return onUpdateExpression == null ? null
139 : onUpdateExpression.getUnenclosedSQL(new StringBuilder(), DEFAULT_SQL_FLAGS).toString();
140 }
141
142 @Override
143 public void prepareExpressions(SessionLocal session) {
144 if (defaultExpression != null) {
145 defaultExpression = defaultExpression.optimize(session);
146 }
147 if (onUpdateExpression != null) {
148 onUpdateExpression = onUpdateExpression.optimize(session);
149 }
150 if (domain != null) {
151 domain.prepareExpressions(session);
152 }
153 }
154
155 /**
156 * Add a constraint to the domain.
157 *
158 * @param constraint the constraint to add
159 */
160 public void addConstraint(ConstraintDomain constraint) {
161 if (constraints == null) {
162 constraints = Utils.newSmallArrayList();
163 }
164 if (!constraints.contains(constraint)) {
165 constraints.add(constraint);
166 }
167 }
168
169 public ArrayList<ConstraintDomain> getConstraints() {
170 return constraints;
171 }
172
173 /**
174 * Remove the given constraint from the list.
175 *
176 * @param constraint the constraint to remove
177 */
178 public void removeConstraint(Constraint constraint) {
179 if (constraints != null) {
180 constraints.remove(constraint);
181 }
182 }
183
184 @Override
185 public int getType() {
186 return DbObject.DOMAIN;
187 }
188
189 @Override
190 public void removeChildrenAndResources(SessionLocal session) {
191 if (constraints != null && !constraints.isEmpty()) {
192 for (ConstraintDomain constraint : constraints.toArray(new ConstraintDomain[0])) {
193 database.removeSchemaObject(session, constraint);
194 }
195 constraints = null;
196 }
197 database.removeMeta(session, getId());
198 }
199
200 /**
201 * Check the specified value.
202 *
203 * @param session the session
204 * @param value the value
205 */
206 public void checkConstraints(SessionLocal session, Value value) {
207 if (constraints != null) {
208 for (ConstraintDomain constraint : constraints) {
209 constraint.check(session, value);
210 }
211 }
212 if (domain != null) {
213 domain.checkConstraints(session, value);
214 }
215 }
216
217 }