KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > ConstraintInfo


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.ConstraintInfo
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.sql.dictionary.ConsInfo;
25 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
26 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
27 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
28 import org.apache.derby.catalog.UUID;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.services.io.StoredFormatIds;
33 import org.apache.derby.iapi.services.io.FormatIdUtil;
34 import org.apache.derby.iapi.services.io.ArrayUtil;
35 import org.apache.derby.iapi.services.io.Formatable;
36
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38
39 import java.io.ObjectOutput JavaDoc;
40 import java.io.ObjectInput JavaDoc;
41 import java.io.IOException JavaDoc;
42 /**
43  * This is a simple class used to store the run time information
44  * about a constraint.
45  *
46  * @author jamie
47  */

48 public class ConstraintInfo implements ConsInfo
49 {
50     /********************************************************
51     **
52     ** This class implements Formatable. That means that it
53     ** can write itself to and from a formatted stream. If
54     ** you add more fields to this class, make sure that you
55     ** also write/read them with the writeExternal()/readExternal()
56     ** methods.
57     **
58     ** If, inbetween releases, you add more fields to this class,
59     ** then you should bump the version number emitted by the getTypeFormatId()
60     ** method. OR, since this is something that is used
61     ** in stored prepared statements, it is ok to change it
62     ** if you make sure that stored prepared statements are
63     ** invalidated across releases.
64     **
65     ********************************************************/

66
67     /*
68     ** See the constructor for the meaning of these fields
69     */

70     private String JavaDoc tableName;
71     private SchemaDescriptor tableSd;
72     private UUID tableSchemaId;
73     private String JavaDoc[] columnNames;
74     private int raDeleteRule;
75     private int raUpdateRule;
76
77
78     /**
79      * Niladic constructor for Formattable
80      */

81     public ConstraintInfo() {}
82
83     /**
84      * Consructor
85      *
86      */

87     public ConstraintInfo(
88                             String JavaDoc tableName,
89                             SchemaDescriptor tableSd,
90                             String JavaDoc[] columnNames,
91                             int raDeleteRule,
92                             int raUpdateRule
93                         )
94     {
95         this.tableName = tableName;
96         this.tableSd = tableSd;
97         this.columnNames = columnNames;
98         this.raDeleteRule = raDeleteRule;
99         this.raUpdateRule = raUpdateRule;
100     }
101
102     //////////////////////////////////////////////
103
//
104
// FORMATABLE
105
//
106
//////////////////////////////////////////////
107
/**
108      * Write this object out
109      *
110      * @param out write bytes here
111      *
112      * @exception IOException thrown on error
113      */

114     public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
115     {
116         out.writeObject(tableName);
117         if (tableSd == null)
118         {
119             out.writeBoolean(false);
120         }
121         else
122         {
123             out.writeBoolean(true);
124             out.writeObject(tableSd.getUUID());
125         }
126
127         if (columnNames == null)
128         {
129             out.writeBoolean(false);
130         }
131         else
132         {
133             out.writeBoolean(true);
134             ArrayUtil.writeArrayLength(out, columnNames);
135             ArrayUtil.writeArrayItems(out, columnNames);
136         }
137
138         //write referential actions for delete and update
139
out.writeInt(raDeleteRule);
140         out.writeInt(raUpdateRule);
141     }
142
143     /**
144      * Read this object from a stream of stored objects.
145      *
146      * @param in read this.
147      *
148      * @exception IOException thrown on error
149      * @exception ClassNotFoundException thrown on error
150      */

151     public void readExternal(ObjectInput JavaDoc in)
152         throws IOException JavaDoc, ClassNotFoundException JavaDoc
153     {
154         tableName = (String JavaDoc)in.readObject();
155         if (in.readBoolean())
156         {
157             tableSchemaId = (UUID)in.readObject();
158         }
159
160         if (in.readBoolean())
161         {
162             columnNames = new String JavaDoc[ArrayUtil.readArrayLength(in)];
163             ArrayUtil.readArrayItems(in, columnNames);
164         }
165
166         //read referential actions for delete and update
167
raDeleteRule = in.readInt();
168         raUpdateRule = in.readInt();
169     }
170     
171     /**
172      * Get the formatID which corresponds to this class.
173      *
174      * @return the formatID of this class
175      */

176     public int getTypeFormatId() { return StoredFormatIds.CONSTRAINT_INFO_V01_ID; }
177
178     //////////////////////////////////////////////////////////////
179
//
180
// Misc
181
//
182
//////////////////////////////////////////////////////////////
183
public String JavaDoc toString()
184     {
185         if (SanityManager.DEBUG)
186         {
187             StringBuffer JavaDoc str = new StringBuffer JavaDoc();
188             str.append("Referencing ");
189             str.append(tableName);
190             if (columnNames != null)
191             {
192                 str.append("(");
193                 for (int i = 0; i < columnNames.length; i++)
194                 {
195                     if (i > 0)
196                         str.append(",");
197
198                     str.append(columnNames[i]);
199                 }
200                 str.append(")");
201             }
202         
203             return str.toString();
204         }
205         else
206         {
207             return "";
208         }
209     }
210
211     public SchemaDescriptor getReferencedTableSchemaDescriptor(DataDictionary dd)
212         throws StandardException
213     {
214         if (tableSd != null)
215         {
216             return tableSd;
217         }
218         else
219         {
220             return dd.getSchemaDescriptor(tableSchemaId, null);
221         }
222     }
223             
224     public TableDescriptor getReferencedTableDescriptor(DataDictionary dd)
225         throws StandardException
226     {
227         if (tableName == null)
228         {
229             return null;
230         }
231     
232         return dd.getTableDescriptor(tableName,
233                 getReferencedTableSchemaDescriptor(dd));
234     }
235
236     /**
237       * This ConsInfo describes columns in a referenced table. What are
238       * their names?
239       *
240       * @return array of referenced column names
241       */

242     public String JavaDoc[] getReferencedColumnNames()
243     { return columnNames; }
244
245     /**
246       * Get the name of the table that these column live in.
247       *
248       * @return referenced table name
249       */

250     public String JavaDoc getReferencedTableName()
251     { return tableName; }
252
253     /**
254       * Get the referential Action for an Update.
255       *
256       * @return referential Action for update
257       */

258     public int getReferentialActionUpdateRule()
259     { return raUpdateRule; }
260
261     
262     /**
263       * Get the referential Action for a Delete.
264       *
265       * @return referential Action Delete rule
266       */

267     public int getReferentialActionDeleteRule()
268     { return raDeleteRule; }
269
270
271
272 }
273
274
275
276
277
278
279
280
281
282
Popular Tags