File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\constraint\ConstraintUnique.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.constraint;
8
9 import java.util.HashSet;
10 import org.h2.command.Parser;
11 import org.h2.engine.Session;
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.New;
19 import org.h2.util.StatementBuilder;
20 import org.h2.util.StringUtils;
21
22 /**
23 * A unique constraint. This object always backed by a unique index.
24 */
25 public class ConstraintUnique extends Constraint {
26
27 private Index index;
28 private boolean indexOwner;
29 private IndexColumn[] columns;
30 private final boolean primaryKey;
31
32 public ConstraintUnique(Schema schema, int id, String name, Table table, boolean primaryKey) {
33 super(schema, id, name, table);
34 this.primaryKey = primaryKey;
35 }
36
37 public String getConstraintType() {
38 return primaryKey ? Constraint.PRIMARY_KEY : Constraint.UNIQUE;
39 }
40
41 public String getCreateSQLForCopy(Table forTable, String quotedName) {
42 return getCreateSQLForCopy(forTable, quotedName, true);
43 }
44
45 private String getCreateSQLForCopy(Table forTable, String quotedName, boolean internalIndex) {
46 StatementBuilder buff = new StatementBuilder("ALTER TABLE ");
47 buff.append(forTable.getSQL()).append(" ADD CONSTRAINT ");
48 if (forTable.isHidden()) {
49 buff.append("IF NOT EXISTS ");
50 }
51 buff.append(quotedName);
52 if (comment != null) {
53 buff.append(" COMMENT ").append(StringUtils.quoteStringSQL(comment));
54 }
55 buff.append(' ').append(getTypeName()).append('(');
56 for (IndexColumn c : columns) {
57 buff.appendExceptFirst(", ");
58 buff.append(Parser.quoteIdentifier(c.column.getName()));
59 }
60 buff.append(')');
61 if (internalIndex && indexOwner && forTable == this.table) {
62 buff.append(" INDEX ").append(index.getSQL());
63 }
64 return buff.toString();
65 }
66
67 private String getTypeName() {
68 if (primaryKey) {
69 return "PRIMARY KEY";
70 }
71 return "UNIQUE";
72 }
73
74 public String getCreateSQLWithoutIndexes() {
75 return getCreateSQLForCopy(table, getSQL(), false);
76 }
77
78 public String getCreateSQL() {
79 return getCreateSQLForCopy(table, getSQL());
80 }
81
82 public void setColumns(IndexColumn[] columns) {
83 this.columns = columns;
84 }
85
86 public IndexColumn[] getColumns() {
87 return columns;
88 }
89
90 /**
91 * Set the index to use for this unique constraint.
92 *
93 * @param index the index
94 * @param isOwner true if the index is generated by the system and belongs
95 * to this constraint
96 */
97 public void setIndex(Index index, boolean isOwner) {
98 this.index = index;
99 this.indexOwner = isOwner;
100 }
101
102 public void removeChildrenAndResources(Session session) {
103 table.removeConstraint(this);
104 if (indexOwner) {
105 table.removeIndexOrTransferOwnership(session, index);
106 }
107 database.removeMeta(session, getId());
108 index = null;
109 columns = null;
110 table = null;
111 invalidate();
112 }
113
114 public void checkRow(Session session, Table t, Row oldRow, Row newRow) {
115 // unique index check is enough
116 }
117
118 public boolean usesIndex(Index idx) {
119 return idx == index;
120 }
121
122 public void setIndexOwner(Index index) {
123 indexOwner = true;
124 }
125
126 public HashSet<Column> getReferencedColumns(Table table) {
127 HashSet<Column> result = New.hashSet();
128 for (IndexColumn c : columns) {
129 result.add(c.column);
130 }
131 return result;
132 }
133
134 public boolean isBefore() {
135 return true;
136 }
137
138 public void checkExistingData(Session session) {
139 // no need to check: when creating the unique index any problems are found
140 }
141
142 public Index getUniqueIndex() {
143 return index;
144 }
145
146 public void rebuild() {
147 // nothing to do
148 }
149
150 }
151