001: /*
002:
003: Derby - Class org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.sql.dictionary;
023:
024: import org.apache.derby.catalog.ReferencedColumns;
025: import org.apache.derby.catalog.UUID;
026: import org.apache.derby.iapi.services.sanity.SanityManager;
027: import org.apache.derby.iapi.sql.StatementType;
028:
029: /**
030: * This class represents a check constraint descriptor.
031: *
032: * @author jamie
033: */
034: public class CheckConstraintDescriptor extends ConstraintDescriptor {
035: ReferencedColumns referencedColumns;
036: String constraintText;
037:
038: CheckConstraintDescriptor(DataDictionary dataDictionary,
039: TableDescriptor table, String constraintName,
040: boolean deferrable, boolean initiallyDeferred,
041: UUID constraintId, String constraintText,
042: ReferencedColumns referencedColumns,
043: SchemaDescriptor schemaDesc, boolean isEnabled) {
044: super (dataDictionary, table, constraintName, deferrable,
045: initiallyDeferred, (int[]) null, constraintId,
046: schemaDesc, isEnabled);
047: this .constraintText = constraintText;
048: this .referencedColumns = referencedColumns;
049: }
050:
051: /**
052: * Does this constraint have a backing index?
053: *
054: * @return boolean Whether or not there is a backing index for this constraint.
055: */
056: public boolean hasBackingIndex() {
057: return false;
058: }
059:
060: /**
061: * Gets an identifier telling what type of descriptor it is
062: * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK).
063: *
064: * @return An identifier telling what type of descriptor it is
065: * (UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK).
066: */
067: public int getConstraintType() {
068: return DataDictionary.CHECK_CONSTRAINT;
069: }
070:
071: /**
072: * Get the text of the constraint. (Only non-null/meaningful for check
073: * constraints.)
074: * @return The constraint text.
075: */
076: public String getConstraintText() {
077: return constraintText;
078: }
079:
080: /**
081: * Get the UUID of the backing index, if one exists.
082: *
083: * @return The UUID of the backing index, if one exists, else null.
084: */
085: public UUID getConglomerateId() {
086: return null;
087: }
088:
089: /**
090: * Get the ReferencedColumns.
091: *
092: * @return The ReferencedColumns.
093: */
094: public ReferencedColumns getReferencedColumnsDescriptor() {
095: return referencedColumns;
096: }
097:
098: /**
099: * Set the ReferencedColumns; used in drop column
100: *
101: * @param rcd The new ReferencedColumns.
102: */
103: public void setReferencedColumnsDescriptor(ReferencedColumns rcd) {
104: referencedColumns = rcd;
105: }
106:
107: /**
108: * Get the referenced columns as an int[] of column ids.
109: *
110: * @return The array of referenced column ids.
111: */
112: public int[] getReferencedColumns() {
113: return referencedColumns.getReferencedColumnPositions();
114: }
115:
116: /**
117: * Does this constraint need to fire on this type of
118: * DML? For a check constraint, all inserts, and
119: * appropriate updates
120: *
121: * @param stmtType the type of DML
122: * (StatementType.INSERT|StatementType.UPDATE|StatementType.DELETE)
123: * @param modifiedCols the columns modified, or null for all
124: *
125: * @return true/false
126: */
127: public boolean needsToFire(int stmtType, int[] modifiedCols) {
128: /*
129: ** If we are disabled, we never fire
130: */
131: if (!isEnabled) {
132: return false;
133: }
134:
135: if (stmtType == StatementType.INSERT) {
136: return true;
137: }
138:
139: if (stmtType == StatementType.DELETE) {
140: return false;
141: }
142:
143: // if update, only relevant if columns intersect
144: return doColumnsIntersect(modifiedCols, getReferencedColumns());
145: }
146:
147: /**
148: * Convert the CheckConstraintDescriptor to a String.
149: *
150: * @return A String representation of this CheckConstraintDescriptor
151: */
152:
153: public String toString() {
154: if (SanityManager.DEBUG) {
155: return "constraintText: " + constraintText + "\n"
156: + "referencedColumns: " + referencedColumns + "\n"
157: + super .toString();
158: } else {
159: return "";
160: }
161: }
162:
163: }
|