Debian Codegraph @

src/main/org/h2/constraint/ConstraintUnique.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.ArrayList;
9 import java.util.HashSet;
10 import org.h2.engine.SessionLocal;
11 import org.h2.engine.NullsDistinct;
12 import org.h2.index.Index;
13 import org.h2.result.Row;
14 import org.h2.schema.Schema;
15 import org.h2.table.Column;
16 import org.h2.table.IndexColumn;
17 import org.h2.table.Table;
18 import org.h2.util.StringUtils;
19
20 /**
21 * A unique constraint. This object always backed by a unique index.
22 */
23 public class ConstraintUnique extends Constraint {
24
25 private Index index;
26 private boolean indexOwner;
27 private IndexColumn[] columns;
28 private final boolean primaryKey;
29 private NullsDistinct nullsDistinct;
30
31 public ConstraintUnique(Schema schema, int id, String name, Table table, boolean primaryKey,
32 NullsDistinct nullsDistinct) {
33 super(schema, id, name, table);
34 this.primaryKey = primaryKey;
35 this.nullsDistinct = nullsDistinct;
36 }
37
38 @Override
39 public Type getConstraintType() {
40 return primaryKey ? Constraint.Type.PRIMARY_KEY : Constraint.Type.UNIQUE;
41 }
42
43 @Override
44 public String getCreateSQLForCopy(Table forTable, String quotedName) {
45 return getCreateSQLForCopy(forTable, quotedName, true);
46 }
47
48 private String getCreateSQLForCopy(Table forTable, String quotedName, boolean internalIndex) {
49 StringBuilder builder = new StringBuilder("ALTER TABLE ");
50 forTable.getSQL(builder, DEFAULT_SQL_FLAGS).append(" ADD CONSTRAINT ");
51 if (forTable.isHidden()) {
52 builder.append("IF NOT EXISTS ");
53 }
54 builder.append(quotedName);
55 if (comment != null) {
56 builder.append(" COMMENT ");
57 StringUtils.quoteStringSQL(builder, comment);
58 }
59 builder.append(' ').append(getConstraintType().getSqlName()).append('(');
60 IndexColumn.writeColumns(builder, columns, DEFAULT_SQL_FLAGS).append(')');
61 if (internalIndex && indexOwner && forTable == this.table) {
62 builder.append(" INDEX ");
63 index.getSQL(builder, DEFAULT_SQL_FLAGS);
64 }
65 return builder.toString();
66 }
67
68 @Override
69 public String getCreateSQLWithoutIndexes() {
70 return getCreateSQLForCopy(table, getSQL(DEFAULT_SQL_FLAGS), false);
71 }
72
73 @Override
74 public String getCreateSQL() {
75 return getCreateSQLForCopy(table, getSQL(DEFAULT_SQL_FLAGS));
76 }
77
78 public void setColumns(IndexColumn[] columns) {
79 this.columns = columns;
80 }
81
82 public IndexColumn[] getColumns() {
83 return columns;
84 }
85
86 /**
87 * Set the index to use for this unique constraint.
88 *
89 * @param index the index
90 * @param isOwner true if the index is generated by the system and belongs
91 * to this constraint
92 */
93 public void setIndex(Index index, boolean isOwner) {
94 this.index = index;
95 this.indexOwner = isOwner;
96 }
97
98 @Override
99 public void removeChildrenAndResources(SessionLocal session) {
100 ArrayList<Constraint> constraints = table.getConstraints();
101 if (constraints != null) {
102 constraints = new ArrayList<>(table.getConstraints());
103 for (Constraint c : constraints) {
104 if (c.getReferencedConstraint() == this) {
105 database.removeSchemaObject(session, c);
106 }
107 }
108 }
109 table.removeConstraint(this);
110 if (indexOwner) {
111 table.removeIndexOrTransferOwnership(session, index);
112 }
113 database.removeMeta(session, getId());
114 index = null;
115 columns = null;
116 table = null;
117 invalidate();
118 }
119
120 @Override
121 public void checkRow(SessionLocal session, Table t, Row oldRow, Row newRow) {
122 // unique index check is enough
123 }
124
125 @Override
126 public boolean usesIndex(Index idx) {
127 return idx == index;
128 }
129
130 @Override
131 public void setIndexOwner(Index index) {
132 indexOwner = true;
133 }
134
135 @Override
136 public HashSet<Column> getReferencedColumns(Table table) {
137 HashSet<Column> result = new HashSet<>();
138 for (IndexColumn c : columns) {
139 result.add(c.column);
140 }
141 return result;
142 }
143
144 @Override
145 public boolean isBefore() {
146 return true;
147 }
148
149 @Override
150 public void checkExistingData(SessionLocal session) {
151 // no need to check: when creating the unique index any problems are
152 // found
153 }
154
155 @Override
156 public Index getIndex() {
157 return index;
158 }
159
160 @Override
161 public void rebuild() {
162 // nothing to do
163 }
164
165 /**
166 * @return are nulls distinct
167 */
168 public NullsDistinct getNullsDistinct() {
169 return nullsDistinct;
170 }
171
172 }