KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > catalog > SYSFOREIGNKEYSRowFactory


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSFOREIGNKEYSRowFactory
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.catalog;
23
24 import org.apache.derby.iapi.types.TypeId;
25 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
26 import org.apache.derby.catalog.TypeDescriptor;
27
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29
30 import org.apache.derby.iapi.types.DataValueFactory;
31 import org.apache.derby.iapi.types.RowLocation;
32
33 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
34 import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor;
35 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
36 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
37 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
38 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext;
39 import org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor;
40 import org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor;
41 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor;
42 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
43 import org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor;
44 import org.apache.derby.iapi.sql.StatementType;
45
46 import org.apache.derby.iapi.sql.execute.ExecutionContext;
47 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
48 import org.apache.derby.iapi.sql.execute.ExecRow;
49 import org.apache.derby.iapi.sql.execute.ExecutionFactory;
50
51 import org.apache.derby.iapi.error.StandardException;
52
53 import org.apache.derby.iapi.types.RowLocation;
54
55 import org.apache.derby.iapi.services.sanity.SanityManager;
56
57 import org.apache.derby.iapi.services.uuid.UUIDFactory;
58 import org.apache.derby.catalog.UUID;
59 import org.apache.derby.catalog.IndexDescriptor;
60
61 /**
62  * Factory for creating a SYSFOREIGNKEYS row.
63  *
64  * @author jerry
65  */

66
67 public class SYSFOREIGNKEYSRowFactory extends CatalogRowFactory
68 {
69     private static final String JavaDoc TABLENAME_STRING = "SYSFOREIGNKEYS";
70
71     protected static final int SYSFOREIGNKEYS_COLUMN_COUNT = 5;
72     protected static final int SYSFOREIGNKEYS_CONSTRAINTID = 1;
73     protected static final int SYSFOREIGNKEYS_CONGLOMERATEID = 2;
74     protected static final int SYSFOREIGNKEYS_KEYCONSTRAINTID = 3;
75     protected static final int SYSFOREIGNKEYS_DELETERULE = 4;
76     protected static final int SYSFOREIGNKEYS_UPDATERULE = 5;
77
78     // Column widths
79
protected static final int SYSFOREIGNKEYS_CONSTRAINTID_WIDTH = 36;
80
81     protected static final int SYSFOREIGNKEYS_INDEX1_ID = 0;
82     protected static final int SYSFOREIGNKEYS_INDEX2_ID = 1;
83
84     private static final int[][] indexColumnPositions =
85     {
86         {SYSFOREIGNKEYS_CONSTRAINTID},
87         {SYSFOREIGNKEYS_KEYCONSTRAINTID}
88     };
89
90     private static final boolean[] uniqueness = {
91                                                        true,
92                                                        false
93                                                      };
94
95     private static final String JavaDoc[] uuids =
96     {
97          "8000005b-00d0-fd77-3ed8-000a0a0b1900" // catalog UUID
98
,"80000060-00d0-fd77-3ed8-000a0a0b1900" // heap UUID
99
,"8000005d-00d0-fd77-3ed8-000a0a0b1900" // SYSFOREIGNKEYS_INDEX1
100
,"8000005f-00d0-fd77-3ed8-000a0a0b1900" // SYSFOREIGNKEYS_INDEX2
101
};
102
103     /////////////////////////////////////////////////////////////////////////////
104
//
105
// CONSTRUCTORS
106
//
107
/////////////////////////////////////////////////////////////////////////////
108

109     public SYSFOREIGNKEYSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
110                                  boolean convertIdToLower)
111     {
112         super(uuidf,ef,dvf,convertIdToLower);
113         initInfo(SYSFOREIGNKEYS_COLUMN_COUNT, TABLENAME_STRING,
114                  indexColumnPositions, uniqueness, uuids );
115     }
116
117     /////////////////////////////////////////////////////////////////////////////
118
//
119
// METHODS
120
//
121
/////////////////////////////////////////////////////////////////////////////
122

123   /**
124      * Make a SYSFOREIGNKEYS row
125      *
126      * @return Row suitable for inserting into SYSFOREIGNKEYS.
127      *
128      * @exception StandardException thrown on failure
129      */

130     public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent)
131                     throws StandardException
132     {
133         DataValueDescriptor col;
134         ExecIndexRow row;
135         String JavaDoc constraintId = null;
136         String JavaDoc keyConstraintId = null;
137         String JavaDoc conglomId = null;
138         String JavaDoc raDeleteRule="N";
139         String JavaDoc raUpdateRule="N";
140
141         if (td != null)
142         {
143             ForeignKeyConstraintDescriptor cd = (ForeignKeyConstraintDescriptor)td;
144             constraintId = cd.getUUID().toString();
145             
146             ReferencedKeyConstraintDescriptor refCd = cd.getReferencedConstraint();
147             if (SanityManager.DEBUG)
148             {
149                 SanityManager.ASSERT(refCd != null, "this fk returned a null referenced key");
150             }
151             keyConstraintId = refCd.getUUID().toString();
152             conglomId = cd.getIndexUUIDString();
153
154             raDeleteRule = getRefActionAsString(cd.getRaDeleteRule());
155             raUpdateRule = getRefActionAsString(cd.getRaUpdateRule());
156         }
157             
158             
159         /* Build the row */
160         row = getExecutionFactory().getIndexableRow(SYSFOREIGNKEYS_COLUMN_COUNT);
161
162         /* 1st column is CONSTRAINTID (UUID - char(36)) */
163         row.setColumn(SYSFOREIGNKEYS_CONSTRAINTID, dvf.getCharDataValue(constraintId));
164
165         /* 2nd column is CONGLOMERATEID (UUID - char(36)) */
166         row.setColumn(SYSFOREIGNKEYS_CONGLOMERATEID, dvf.getCharDataValue(conglomId));
167
168         /* 3rd column is KEYCONSTRAINTID (UUID - char(36)) */
169         row.setColumn(SYSFOREIGNKEYS_KEYCONSTRAINTID, dvf.getCharDataValue(keyConstraintId));
170
171         // currently, DELETERULE and UPDATERULE are always "R" for restrict
172
/* 4th column is DELETERULE char(1) */
173         row.setColumn(SYSFOREIGNKEYS_DELETERULE, dvf.getCharDataValue(raDeleteRule));
174
175         /* 5th column is UPDATERULE char(1) */
176         row.setColumn(SYSFOREIGNKEYS_UPDATERULE, dvf.getCharDataValue(raUpdateRule));
177
178         return row;
179     }
180
181     ///////////////////////////////////////////////////////////////////////////
182
//
183
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
184
//
185
///////////////////////////////////////////////////////////////////////////
186

187     /**
188      * Make a ViewDescriptor out of a SYSFOREIGNKEYS row
189      *
190      * @param row a SYSFOREIGNKEYS row
191      * @param parentTupleDescriptor Null for this kind of descriptor.
192      * @param dd dataDictionary
193      *
194      * @exception StandardException thrown on failure
195      */

196     public TupleDescriptor buildDescriptor(
197         ExecRow row,
198         TupleDescriptor parentTupleDescriptor,
199         DataDictionary dd )
200                     throws StandardException
201     {
202
203         if (SanityManager.DEBUG)
204         {
205             SanityManager.ASSERT(
206                 row.nColumns() == SYSFOREIGNKEYS_COLUMN_COUNT,
207                 "Wrong number of columns for a SYSKEYS row");
208         }
209
210         DataValueDescriptor col;
211         DataDescriptorGenerator ddg;
212         UUID constraintUUID;
213         UUID conglomerateUUID;
214         UUID keyConstraintUUID;
215         String JavaDoc constraintUUIDString;
216         String JavaDoc conglomerateUUIDString;
217         String JavaDoc raRuleString;
218         int raDeleteRule;
219         int raUpdateRule;
220
221         ddg = dd.getDataDescriptorGenerator();
222
223         /* 1st column is CONSTRAINTID (UUID - char(36)) */
224         col = row.getColumn(SYSFOREIGNKEYS_CONSTRAINTID);
225         constraintUUIDString = col.getString();
226         constraintUUID = getUUIDFactory().recreateUUID(constraintUUIDString);
227
228         /* 2nd column is CONGLOMERATEID (UUID - char(36)) */
229         col = row.getColumn(SYSFOREIGNKEYS_CONGLOMERATEID);
230         conglomerateUUIDString = col.getString();
231         conglomerateUUID = getUUIDFactory().recreateUUID(conglomerateUUIDString);
232
233         /* 3rd column is KEYCONSTRAINTID (UUID - char(36)) */
234         col = row.getColumn(SYSFOREIGNKEYS_KEYCONSTRAINTID);
235         constraintUUIDString = col.getString();
236         keyConstraintUUID = getUUIDFactory().recreateUUID(constraintUUIDString);
237
238
239         /* 4th column is DELETERULE char(1) */
240         col= row.getColumn(SYSFOREIGNKEYS_DELETERULE);
241         raRuleString = col.getString();
242         raDeleteRule = getRefActionAsInt(raRuleString);
243         
244         /* 5th column is UPDATERULE char(1) */
245         col = row.getColumn(SYSFOREIGNKEYS_UPDATERULE);
246         raRuleString = col.getString();
247         raUpdateRule = getRefActionAsInt(raRuleString);
248
249
250         /* now build and return the descriptor */
251         return new SubKeyConstraintDescriptor(
252                                         constraintUUID,
253                                         conglomerateUUID,
254                                         keyConstraintUUID,
255                                         raDeleteRule,
256                                         raUpdateRule);
257     }
258
259     /**
260      * Builds a list of columns suitable for creating this Catalog.
261      *
262      *
263      * @return array of SystemColumn suitable for making this catalog.
264      */

265     public SystemColumn[] buildColumnList()
266     {
267         int index = 0;
268         SystemColumn[] columnList = new SystemColumn[SYSFOREIGNKEYS_COLUMN_COUNT];
269
270         // describe columns
271

272         columnList[index++] = new SystemColumnImpl(
273                             convertIdCase( "CONSTRAINTID"), // name
274
SYSFOREIGNKEYS_CONSTRAINTID, // column number
275
0, // precision
276
0, // scale
277
false, // nullability
278
"CHAR", // dataType
279
true, // built-in type
280
36 // maxLength
281
);
282         columnList[index++] = new SystemColumnImpl(
283                             convertIdCase( "CONGLOMERATEID"), // name
284
SYSFOREIGNKEYS_CONGLOMERATEID, // column number
285
0, // precision
286
0, // scale
287
false, // nullability
288
"CHAR", // dataType
289
true, // built-in type
290
36 // maxLength
291
);
292         columnList[index++] = new SystemColumnImpl(
293                             convertIdCase( "KEYCONSTRAINTID"), // name
294
SYSFOREIGNKEYS_KEYCONSTRAINTID, // column number
295
0, // precision
296
0, // scale
297
false, // nullability
298
"CHAR", // dataType
299
true, // built-in type
300
36 // maxLength
301
);
302
303         columnList[index++] = new SystemColumnImpl(
304                             convertIdCase( "DELETERULE"), // name
305
SYSFOREIGNKEYS_DELETERULE, // column number
306
0, // precision
307
0, // scale
308
false, // nullability
309
"CHAR", // dataType
310
true, // built-in type
311
1 // maxLength
312
);
313
314         columnList[index++] = new SystemColumnImpl(
315                             convertIdCase( "UPDATERULE"), // name
316
SYSFOREIGNKEYS_UPDATERULE, // column number
317
0, // precision
318
0, // scale
319
false, // nullability
320
"CHAR", // dataType
321
true, // built-in type
322
1 // maxLength
323
);
324         return columnList;
325     }
326
327
328     int getRefActionAsInt(String JavaDoc raRuleString)
329     {
330         int raRule ;
331         switch (raRuleString.charAt(0)){
332         case 'C':
333             raRule = StatementType.RA_CASCADE;
334             break;
335         case 'S':
336             raRule = StatementType.RA_RESTRICT;
337             break;
338         case 'R':
339             raRule = StatementType.RA_NOACTION;
340             break;
341         case 'U':
342             raRule = StatementType.RA_SETNULL;
343             break;
344         case 'D':
345             raRule = StatementType.RA_SETDEFAULT;
346             break;
347         default:
348             raRule =StatementType.RA_NOACTION; ;
349             if (SanityManager.DEBUG)
350             {
351                 SanityManager.THROWASSERT("Invalid value '"
352                                           +raRuleString+ "' for a referetial Action");
353             }
354         }
355         return raRule ;
356     }
357
358
359     String JavaDoc getRefActionAsString(int raRule)
360     {
361         String JavaDoc raRuleString ;
362         switch (raRule){
363         case StatementType.RA_CASCADE:
364             raRuleString = "C";
365             break;
366         case StatementType.RA_RESTRICT:
367             raRuleString = "S";
368                 break;
369         case StatementType.RA_NOACTION:
370             raRuleString = "R";
371             break;
372         case StatementType.RA_SETNULL:
373             raRuleString = "U";
374             break;
375         case StatementType.RA_SETDEFAULT:
376             raRuleString = "D";
377             raRule = StatementType.RA_SETDEFAULT;
378             break;
379         default:
380             raRuleString ="N" ; // NO ACTION (default value)
381
if (SanityManager.DEBUG)
382             {
383                 SanityManager.THROWASSERT("Invalid value '"
384                             +raRule+ "' for a referetial Action");
385             }
386
387         }
388         return raRuleString ;
389     }
390
391
392 }
393
Popular Tags