1 | /* |
2 | |
3 | Derby - Class org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor |
4 | |
5 | Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.iapi.sql.dictionary; |
22 | import org.apache.derby.catalog.ReferencedColumns; |
23 | import org.apache.derby.catalog.UUID; |
24 | import org.apache.derby.iapi.services.sanity.SanityManager; |
25 | import org.apache.derby.iapi.sql.StatementType; |
26 | |
27 | /** |
28 | * This class represents a check constraint descriptor. |
29 | * |
30 | * @author jamie |
31 | */ |
32 | public class CheckConstraintDescriptor extends ConstraintDescriptor |
33 | { |
34 | ReferencedColumns referencedColumns; |
35 | String constraintText; |
36 | |
37 | CheckConstraintDescriptor( |
38 | DataDictionary dataDictionary, |
39 | TableDescriptor table, |
40 | String constraintName, |
41 | boolean deferrable, |
42 | boolean initiallyDeferred, |
43 | UUID constraintId, |
44 | String constraintText, |
45 | ReferencedColumns referencedColumns, |
46 | SchemaDescriptor schemaDesc, |
47 | boolean isEnabled |
48 | ) |
49 | { |
50 | super(dataDictionary, table, constraintName, deferrable, |
51 | initiallyDeferred, (int []) null, |
52 | constraintId, schemaDesc, isEnabled); |
53 | this.constraintText = constraintText; |
54 | this.referencedColumns = referencedColumns; |
55 | } |
56 | |
57 | /** |
58 | * Does this constraint have a backing index? |
59 | * |
60 | * @return boolean Whether or not there is a backing index for this constraint. |
61 | */ |
62 | public boolean hasBackingIndex() |
63 | { |
64 | return false; |
65 | } |
66 | |
67 | /** |
68 | * Gets an identifier telling what type of descriptor it is |
69 | * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK). |
70 | * |
71 | * @return An identifier telling what type of descriptor it is |
72 | * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK). |
73 | */ |
74 | public int getConstraintType() |
75 | { |
76 | return DataDictionary.CHECK_CONSTRAINT; |
77 | } |
78 | |
79 | /** |
80 | * Get the text of the constraint. (Only non-null/meaningful for check |
81 | * constraints.) |
82 | * @return The constraint text. |
83 | */ |
84 | public String getConstraintText() |
85 | { |
86 | return constraintText; |
87 | } |
88 | |
89 | /** |
90 | * Get the UUID of the backing index, if one exists. |
91 | * |
92 | * @return The UUID of the backing index, if one exists, else null. |
93 | */ |
94 | public UUID getConglomerateId() |
95 | { |
96 | return null; |
97 | } |
98 | |
99 | /** |
100 | * Get the ReferencedColumns. |
101 | * |
102 | * @return The ReferencedColumns. |
103 | */ |
104 | public ReferencedColumns getReferencedColumnsDescriptor() |
105 | { |
106 | return referencedColumns; |
107 | } |
108 | |
109 | /** |
110 | * Set the ReferencedColumns; used in drop column |
111 | * |
112 | * @param rcd The new ReferencedColumns. |
113 | */ |
114 | public void setReferencedColumnsDescriptor(ReferencedColumns rcd) |
115 | { |
116 | referencedColumns = rcd; |
117 | } |
118 | |
119 | /** |
120 | * Get the referenced columns as an int[] of column ids. |
121 | * |
122 | * @return The array of referenced column ids. |
123 | */ |
124 | public int[] getReferencedColumns() |
125 | { |
126 | return referencedColumns.getReferencedColumnPositions(); |
127 | } |
128 | |
129 | /** |
130 | * Does this constraint need to fire on this type of |
131 | * DML? For a check constraint, all inserts, and |
132 | * appropriate updates |
133 | * |
134 | * @param stmtType the type of DML |
135 | * (StatementType.INSERT|StatementType.UPDATE|StatementType.DELETE) |
136 | * @param modifiedCols the columns modified, or null for all |
137 | * |
138 | * @return true/false |
139 | */ |
140 | public boolean needsToFire(int stmtType, int[] modifiedCols) |
141 | { |
142 | /* |
143 | ** If we are disabled, we never fire |
144 | */ |
145 | if (!isEnabled) |
146 | { |
147 | return false; |
148 | } |
149 | |
150 | if (stmtType == StatementType.INSERT) |
151 | { |
152 | return true; |
153 | } |
154 | |
155 | if (stmtType == StatementType.DELETE) |
156 | { |
157 | return false; |
158 | } |
159 | |
160 | // if update, only relevant if columns intersect |
161 | return doColumnsIntersect(modifiedCols, getReferencedColumns()); |
162 | } |
163 | |
164 | /** |
165 | * Convert the CheckConstraintDescriptor to a String. |
166 | * |
167 | * @return A String representation of this CheckConstraintDescriptor |
168 | */ |
169 | |
170 | public String toString() |
171 | { |
172 | if (SanityManager.DEBUG) |
173 | { |
174 | return "constraintText: " + constraintText + "\n" + |
175 | "referencedColumns: " + referencedColumns + "\n" + |
176 | super.toString(); |
177 | } |
178 | else |
179 | { |
180 | return ""; |
181 | } |
182 | } |
183 | |
184 | |
185 | } |