001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.ConstraintInfo
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.impl.sql.execute;
023:
024: import org.apache.derby.iapi.sql.dictionary.ConsInfo;
025: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
026: import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
027: import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
028: import org.apache.derby.catalog.UUID;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.services.io.StoredFormatIds;
033: import org.apache.derby.iapi.services.io.FormatIdUtil;
034: import org.apache.derby.iapi.services.io.ArrayUtil;
035: import org.apache.derby.iapi.services.io.Formatable;
036:
037: import org.apache.derby.iapi.services.sanity.SanityManager;
038:
039: import java.io.ObjectOutput;
040: import java.io.ObjectInput;
041: import java.io.IOException;
042:
043: /**
044: * This is a simple class used to store the run time information
045: * about a constraint.
046: *
047: * @author jamie
048: */
049: public class ConstraintInfo implements ConsInfo {
050: /********************************************************
051: **
052: ** This class implements Formatable. That means that it
053: ** can write itself to and from a formatted stream. If
054: ** you add more fields to this class, make sure that you
055: ** also write/read them with the writeExternal()/readExternal()
056: ** methods.
057: **
058: ** If, inbetween releases, you add more fields to this class,
059: ** then you should bump the version number emitted by the getTypeFormatId()
060: ** method. OR, since this is something that is used
061: ** in stored prepared statements, it is ok to change it
062: ** if you make sure that stored prepared statements are
063: ** invalidated across releases.
064: **
065: ********************************************************/
066:
067: /*
068: ** See the constructor for the meaning of these fields
069: */
070: private String tableName;
071: private SchemaDescriptor tableSd;
072: private UUID tableSchemaId;
073: private String[] columnNames;
074: private int raDeleteRule;
075: private int raUpdateRule;
076:
077: /**
078: * Niladic constructor for Formattable
079: */
080: public ConstraintInfo() {
081: }
082:
083: /**
084: * Consructor
085: *
086: */
087: public ConstraintInfo(String tableName, SchemaDescriptor tableSd,
088: String[] columnNames, int raDeleteRule, int raUpdateRule) {
089: this .tableName = tableName;
090: this .tableSd = tableSd;
091: this .columnNames = columnNames;
092: this .raDeleteRule = raDeleteRule;
093: this .raUpdateRule = raUpdateRule;
094: }
095:
096: //////////////////////////////////////////////
097: //
098: // FORMATABLE
099: //
100: //////////////////////////////////////////////
101: /**
102: * Write this object out
103: *
104: * @param out write bytes here
105: *
106: * @exception IOException thrown on error
107: */
108: public void writeExternal(ObjectOutput out) throws IOException {
109: out.writeObject(tableName);
110: if (tableSd == null) {
111: out.writeBoolean(false);
112: } else {
113: out.writeBoolean(true);
114: out.writeObject(tableSd.getUUID());
115: }
116:
117: if (columnNames == null) {
118: out.writeBoolean(false);
119: } else {
120: out.writeBoolean(true);
121: ArrayUtil.writeArrayLength(out, columnNames);
122: ArrayUtil.writeArrayItems(out, columnNames);
123: }
124:
125: //write referential actions for delete and update
126: out.writeInt(raDeleteRule);
127: out.writeInt(raUpdateRule);
128: }
129:
130: /**
131: * Read this object from a stream of stored objects.
132: *
133: * @param in read this.
134: *
135: * @exception IOException thrown on error
136: * @exception ClassNotFoundException thrown on error
137: */
138: public void readExternal(ObjectInput in) throws IOException,
139: ClassNotFoundException {
140: tableName = (String) in.readObject();
141: if (in.readBoolean()) {
142: tableSchemaId = (UUID) in.readObject();
143: }
144:
145: if (in.readBoolean()) {
146: columnNames = new String[ArrayUtil.readArrayLength(in)];
147: ArrayUtil.readArrayItems(in, columnNames);
148: }
149:
150: //read referential actions for delete and update
151: raDeleteRule = in.readInt();
152: raUpdateRule = in.readInt();
153: }
154:
155: /**
156: * Get the formatID which corresponds to this class.
157: *
158: * @return the formatID of this class
159: */
160: public int getTypeFormatId() {
161: return StoredFormatIds.CONSTRAINT_INFO_V01_ID;
162: }
163:
164: //////////////////////////////////////////////////////////////
165: //
166: // Misc
167: //
168: //////////////////////////////////////////////////////////////
169: public String toString() {
170: if (SanityManager.DEBUG) {
171: StringBuffer str = new StringBuffer();
172: str.append("Referencing ");
173: str.append(tableName);
174: if (columnNames != null) {
175: str.append("(");
176: for (int i = 0; i < columnNames.length; i++) {
177: if (i > 0)
178: str.append(",");
179:
180: str.append(columnNames[i]);
181: }
182: str.append(")");
183: }
184:
185: return str.toString();
186: } else {
187: return "";
188: }
189: }
190:
191: public SchemaDescriptor getReferencedTableSchemaDescriptor(
192: DataDictionary dd) throws StandardException {
193: if (tableSd != null) {
194: return tableSd;
195: } else {
196: return dd.getSchemaDescriptor(tableSchemaId, null);
197: }
198: }
199:
200: public TableDescriptor getReferencedTableDescriptor(
201: DataDictionary dd) throws StandardException {
202: if (tableName == null) {
203: return null;
204: }
205:
206: return dd.getTableDescriptor(tableName,
207: getReferencedTableSchemaDescriptor(dd));
208: }
209:
210: /**
211: * This ConsInfo describes columns in a referenced table. What are
212: * their names?
213: *
214: * @return array of referenced column names
215: */
216: public String[] getReferencedColumnNames() {
217: return columnNames;
218: }
219:
220: /**
221: * Get the name of the table that these column live in.
222: *
223: * @return referenced table name
224: */
225: public String getReferencedTableName() {
226: return tableName;
227: }
228:
229: /**
230: * Get the referential Action for an Update.
231: *
232: * @return referential Action for update
233: */
234: public int getReferentialActionUpdateRule() {
235: return raUpdateRule;
236: }
237:
238: /**
239: * Get the referential Action for a Delete.
240: *
241: * @return referential Action Delete rule
242: */
243: public int getReferentialActionDeleteRule() {
244: return raDeleteRule;
245: }
246:
247: }
|