Debian Codegraph @

src/main/org/h2/constraint/Constraint.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.constraint;
7
8 import java.util.HashSet;
9 import org.h2.engine.DbObject;
10 import org.h2.engine.SessionLocal;
11 import org.h2.expression.Expression;
12 import org.h2.expression.ExpressionVisitor;
13 import org.h2.index.Index;
14 import org.h2.message.Trace;
15 import org.h2.result.Row;
16 import org.h2.schema.Schema;
17 import org.h2.schema.SchemaObject;
18 import org.h2.table.Column;
19 import org.h2.table.Table;
20
21 /**
22 * The base class for constraint checking.
23 */
24 public abstract class Constraint extends SchemaObject implements Comparable<Constraint> {
25
26 public enum Type {
27 /**
28 * The constraint type for check constraints.
29 */
30 CHECK,
31 /**
32 * The constraint type for primary key constraints.
33 */
34 PRIMARY_KEY,
35 /**
36 * The constraint type for unique constraints.
37 */
38 UNIQUE,
39 /**
40 * The constraint type for referential constraints.
41 */
42 REFERENTIAL,
43 /**
44 * The constraint type for domain constraints.
45 */
46 DOMAIN;
47
48 /**
49 * Get standard SQL type name.
50 *
51 * @return standard SQL type name
52 */
53 public String getSqlName() {
54 if (this == Constraint.Type.PRIMARY_KEY) {
55 return "PRIMARY KEY";
56 }
57 if (this == Constraint.Type.REFERENTIAL) {
58 return "FOREIGN KEY";
59 }
60 return name();
61 }
62
63 }
64
65 /**
66 * The table for which this constraint is defined.
67 */
68 protected Table table;
69
70 Constraint(Schema schema, int id, String name, Table table) {
71 super(schema, id, name, Trace.CONSTRAINT);
72 this.table = table;
73 if (table != null) {
74 this.setTemporary(table.isTemporary());
75 }
76 }
77
78 /**
79 * The constraint type name
80 *
81 * @return the name
82 */
83 public abstract Type getConstraintType();
84
85 /**
86 * Check if this row fulfils the constraint.
87 * This method throws an exception if not.
88 *
89 * @param session the session
90 * @param t the table
91 * @param oldRow the old row
92 * @param newRow the new row
93 */
94 public abstract void checkRow(SessionLocal session, Table t, Row oldRow, Row newRow);
95
96 /**
97 * Check if this constraint needs the specified index.
98 *
99 * @param index the index
100 * @return true if the index is used
101 */
102 public abstract boolean usesIndex(Index index);
103
104 /**
105 * This index is now the owner of the specified index.
106 *
107 * @param index the index
108 */
109 public abstract void setIndexOwner(Index index);
110
111 /**
112 * Get all referenced columns.
113 *
114 * @param table the table
115 * @return the set of referenced columns
116 */
117 public abstract HashSet<Column> getReferencedColumns(Table table);
118
119 /**
120 * Returns the CHECK expression or null.
121 *
122 * @return the CHECK expression or null.
123 */
124 public Expression getExpression() {
125 return null;
126 }
127
128 /**
129 * Get the SQL statement to create this constraint.
130 *
131 * @return the SQL statement
132 */
133 public abstract String getCreateSQLWithoutIndexes();
134
135 /**
136 * Check if this constraint needs to be checked before updating the data.
137 *
138 * @return true if it must be checked before updating
139 */
140 public abstract boolean isBefore();
141
142 /**
143 * Check the existing data. This method is called if the constraint is added
144 * after data has been inserted into the table.
145 *
146 * @param session the session
147 */
148 public abstract void checkExistingData(SessionLocal session);
149
150 /**
151 * This method is called after a related table has changed
152 * (the table was renamed, or columns have been renamed).
153 */
154 public abstract void rebuild();
155
156 /**
157 * Get the index of this constraint in the source table, or null if no index
158 * is used.
159 *
160 * @return the index
161 */
162 public Index getIndex() {
163 return null;
164 }
165
166 /**
167 * Returns the referenced unique constraint, or null.
168 *
169 * @return the referenced unique constraint, or null
170 */
171 public ConstraintUnique getReferencedConstraint() {
172 return null;
173 }
174
175 @Override
176 public int getType() {
177 return DbObject.CONSTRAINT;
178 }
179
180 public Table getTable() {
181 return table;
182 }
183
184 public Table getRefTable() {
185 return table;
186 }
187
188 @Override
189 public int compareTo(Constraint other) {
190 if (this == other) {
191 return 0;
192 }
193 return Integer.compare(getConstraintType().ordinal(), other.getConstraintType().ordinal());
194 }
195
196 @Override
197 public boolean isHidden() {
198 return table != null && table.isHidden();
199 }
200
201 /**
202 * Visit all elements in the constraint.
203 *
204 * @param visitor the visitor
205 * @return true if every visited expression returned true, or if there are
206 * no expressions
207 */
208 public boolean isEverything(@SuppressWarnings("unused") ExpressionVisitor visitor) {
209 return true;
210 }
211
212 }