KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSSTATEMENTSRowFactory
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.reference.Property;
25
26 import org.apache.derby.iapi.types.DataTypeDescriptor;
27 import org.apache.derby.iapi.types.TypeId;
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29
30 import org.apache.derby.iapi.store.raw.RawStoreFactory;
31
32 import org.apache.derby.iapi.types.TypeId;
33 import org.apache.derby.iapi.sql.dictionary.SystemColumn;
34 import org.apache.derby.catalog.TypeDescriptor;
35
36 import org.apache.derby.iapi.types.DataValueFactory;
37 import org.apache.derby.iapi.types.RowLocation;
38
39 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory;
40 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
41 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
42 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
43 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor;
44 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
45
46 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
47 import org.apache.derby.iapi.sql.execute.ExecRow;
48 import org.apache.derby.iapi.sql.execute.ExecPreparedStatement;
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.services.monitor.Monitor;
54 import org.apache.derby.catalog.UUID;
55 import org.apache.derby.iapi.services.uuid.UUIDFactory;
56 import org.apache.derby.iapi.types.*;
57
58 import org.apache.derby.iapi.services.sanity.SanityManager;
59
60 import java.sql.Timestamp JavaDoc;
61 import java.util.Properties JavaDoc;
62
63 /**
64  * Factory for creating a SYSSTATEMENTS row.
65  *
66  *
67  * @version 0.1
68  * @author Jamie
69  */

70
71 public class SYSSTATEMENTSRowFactory extends CatalogRowFactory
72 {
73     static final String JavaDoc TABLENAME_STRING = "SYSSTATEMENTS";
74
75     /* Column #s for sysinfo (1 based) */
76     public static final int SYSSTATEMENTS_STMTID = 1;
77     public static final int SYSSTATEMENTS_STMTNAME = 2;
78     public static final int SYSSTATEMENTS_SCHEMAID = 3;
79     public static final int SYSSTATEMENTS_TYPE = 4;
80     public static final int SYSSTATEMENTS_VALID = 5;
81     public static final int SYSSTATEMENTS_TEXT = 6;
82     public static final int SYSSTATEMENTS_LASTCOMPILED = 7;
83     public static final int SYSSTATEMENTS_COMPILATION_SCHEMAID = 8;
84     public static final int SYSSTATEMENTS_USINGTEXT = 9;
85     public static final int SYSSTATEMENTS_CONSTANTSTATE = 10;
86     public static final int SYSSTATEMENTS_INITIALLY_COMPILABLE = 11;
87
88     public static final int SYSSTATEMENTS_COLUMN_COUNT = SYSSTATEMENTS_INITIALLY_COMPILABLE;
89
90     public static final int SYSSTATEMENTS_HIDDEN_COLUMN_COUNT = 2;
91
92     protected static final int SYSSTATEMENTS_INDEX1_ID = 0;
93     protected static final int SYSSTATEMENTS_INDEX2_ID = 1;
94
95
96     private static final int[][] indexColumnPositions =
97     {
98         {SYSSTATEMENTS_STMTID},
99         {SYSSTATEMENTS_STMTNAME, SYSSTATEMENTS_SCHEMAID}
100     };
101
102     private static final boolean[] uniqueness = null;
103
104     private static final String JavaDoc[] uuids =
105     {
106          "80000000-00d1-15f7-ab70-000a0a0b1500" // catalog UUID
107
,"80000000-00d1-15fc-60b9-000a0a0b1500" // heap UUID
108
,"80000000-00d1-15fc-eda1-000a0a0b1500" // SYSSTATEMENTS_INDEX1
109
,"80000000-00d1-15fe-bdf8-000a0a0b1500" // SYSSTATEMENTS_INDEX2
110
};
111
112     /////////////////////////////////////////////////////////////////////////////
113
//
114
// CONSTRUCTORS
115
//
116
/////////////////////////////////////////////////////////////////////////////
117

118     public SYSSTATEMENTSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf,
119                                  boolean convertIdToLower)
120     {
121         super(uuidf,ef,dvf,convertIdToLower);
122         initInfo(SYSSTATEMENTS_COLUMN_COUNT, TABLENAME_STRING,
123                  indexColumnPositions, uniqueness, uuids);
124     }
125
126     /////////////////////////////////////////////////////////////////////////////
127
//
128
// METHODS
129
//
130
/////////////////////////////////////////////////////////////////////////////
131

132     /**
133      * Make a SYSSTATEMENTS row.
134      * <p>
135      * <B>WARNING</B>: When empty row is true, this method takes
136      * a snapshot of the SPSD and creates a row. It is imperative
137      * that that row remain consistent with the descriptor (the
138      * valid and StorablePreparedStatement fields must be in sync).
139      * If this row is to be written out and valid is true, then
140      * this call and the insert should be synchronized on the
141      * SPSD. This method has <B>NO</B> synchronization.
142      *
143      * @param compileMe passed into SPSDescriptorImpl.getPreparedStatement().
144      * if true, we (re)compile the stmt
145      * @param spsDescriptor In-memory tuple to be converted to a disk row.
146      *
147      * @return Row suitable for inserting into SYSSTATEMENTS.
148      *
149      * @exception StandardException thrown on failure
150      */

151
152     public ExecRow makeSYSSTATEMENTSrow(
153         boolean compileMe,
154         SPSDescriptor spsDescriptor
155     ) throws StandardException
156     {
157         DataTypeDescriptor dtd;
158         ExecRow row;
159         DataValueDescriptor col;
160         String JavaDoc name = null;
161         UUID uuid = null;
162         String JavaDoc uuidStr = null;
163         String JavaDoc suuidStr = null; // schema
164
String JavaDoc compUuidStr = null; // compilation schema
165
String JavaDoc text = null;
166         String JavaDoc usingText = null;
167         ExecPreparedStatement preparedStatement = null;
168         String JavaDoc typeStr = null;
169         boolean valid = true;
170         Timestamp JavaDoc time = null;
171         boolean initiallyCompilable = true;
172
173         if (spsDescriptor != null)
174         {
175             name = spsDescriptor.getName();
176             uuid = spsDescriptor.getUUID();
177             if ( uuid == null )
178             {
179                 uuid = getUUIDFactory().createUUID();
180                 spsDescriptor.setUUID(uuid);
181             }
182             suuidStr = spsDescriptor.getSchemaDescriptor().getUUID().toString();
183             uuidStr = uuid.toString();
184             text = spsDescriptor.getText();
185             valid = spsDescriptor.isValid();
186             time = spsDescriptor.getCompileTime();
187             typeStr = spsDescriptor.getTypeAsString();
188             initiallyCompilable = spsDescriptor.initiallyCompilable();
189             preparedStatement = spsDescriptor.getPreparedStatement(compileMe);
190             compUuidStr = spsDescriptor.getCompSchemaId().toString();
191             usingText = spsDescriptor.getUsingText();
192         }
193
194         /* Build the row to insert */
195         row = getExecutionFactory().getValueRow(SYSSTATEMENTS_COLUMN_COUNT);
196
197         /* 1st column is STMTID */
198         row.setColumn(1, dvf.getCharDataValue(uuidStr));
199
200         /* 2nd column is STMTNAME */
201         row.setColumn(2, dvf.getVarcharDataValue(name));
202
203         /* 3rd column is SCHEMAID */
204         row.setColumn(3, dvf.getCharDataValue(suuidStr));
205
206         /* 4th column is TYPE */
207         row.setColumn(4, dvf.getCharDataValue(typeStr));
208
209         /* 5th column is VALID */
210         row.setColumn(5, dvf.getDataValue(valid));
211
212         /* 6th column is TEXT */
213         row.setColumn(6, dvf.getLongvarcharDataValue(text));
214
215         /* 7th column is LASTCOMPILED */
216         row.setColumn(7, new SQLTimestamp(time));
217
218         /* 8th column is COMPILATIONSCHEMAID */
219         row.setColumn(8, dvf.getCharDataValue(compUuidStr));
220
221         /* 9th column is USINGTEXT */
222         row.setColumn(9, dvf.getLongvarcharDataValue(usingText));
223
224         /*
225         ** 10th column is CONSTANTSTATE
226         **
227         ** CONSTANTSTATE is really a formatable StorablePreparedStatement.
228         */

229         row.setColumn(10, dvf.getDataValue(preparedStatement));
230
231         /* 11th column is INITIALLY_COMPILABLE */
232         row.setColumn(11, dvf.getDataValue(initiallyCompilable));
233
234         return row;
235     }
236
237     ///////////////////////////////////////////////////////////////////////////
238
//
239
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
240
//
241
///////////////////////////////////////////////////////////////////////////
242

243     /**
244      * Make an Tuple Descriptor out of a SYSSTATEMENTS row
245      *
246      * @param row a SYSSTATEMENTS row
247      * @param parentTupleDescriptor unused
248      * @param dd dataDictionary
249      *
250      * @return a descriptor equivalent to a SYSSTATEMENTS row
251      *
252      * @exception StandardException thrown on failure
253      */

254     public TupleDescriptor buildDescriptor(
255         ExecRow row,
256         TupleDescriptor parentTupleDescriptor,
257         DataDictionary dd )
258                     throws StandardException
259     {
260         DataValueDescriptor col;
261         SPSDescriptor descriptor;
262         String JavaDoc name;
263         String JavaDoc text;
264         String JavaDoc usingText;
265         UUID uuid;
266         UUID compUuid;
267         String JavaDoc uuidStr;
268         UUID suuid; // schema
269
String JavaDoc suuidStr; // schema
270
String JavaDoc typeStr;
271         char type;
272         boolean valid;
273         Timestamp JavaDoc time = null;
274         ExecPreparedStatement preparedStatement = null;
275         boolean initiallyCompilable;
276         DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
277
278         if (SanityManager.DEBUG)
279         {
280             SanityManager.ASSERT(row.nColumns() == SYSSTATEMENTS_COLUMN_COUNT,
281                                  "Wrong number of columns for a SYSSTATEMENTS row");
282         }
283
284         // 1st column is STMTID (UUID - char(36))
285
col = row.getColumn(1);
286         uuidStr = col.getString();
287         uuid = getUUIDFactory().recreateUUID(uuidStr);
288
289         // 2nd column is STMTNAME (varchar(128))
290
col = row.getColumn(2);
291         name = col.getString();
292
293         // 3rd column is SCHEMAID (UUID - char(36))
294
col = row.getColumn(3);
295         suuidStr = col.getString();
296         suuid = getUUIDFactory().recreateUUID(suuidStr);
297
298         // 4th column is TYPE (char(1))
299
col = row.getColumn(4);
300         type = col.getString().charAt(0);
301
302         if (SanityManager.DEBUG)
303         {
304             if (!SPSDescriptor.validType(type))
305             {
306                 SanityManager.THROWASSERT("Bad type value ("+type+") for statement "+name);
307             }
308         }
309
310         // In soft upgrade mode the plan may not be understand by this engine
311
// so force a recompile.
312
if (((DataDictionaryImpl) dd).readOnlyUpgrade) {
313             valid = false;
314         } else {
315             // 5th column is VALID (boolean)
316
col = row.getColumn(5);
317             valid = col.getBoolean();
318         }
319
320         // 6th column is TEXT (LONG VARCHAR)
321
col = row.getColumn(6);
322         text = col.getString();
323
324         /* 7th column is LASTCOMPILED (TIMESTAMP) */
325         col = row.getColumn(7);
326         time = col.getTimestamp(new java.util.GregorianCalendar JavaDoc());
327
328         // 8th column is COMPILATIONSCHEMAID (UUID - char(36))
329
col = row.getColumn(8);
330         uuidStr = col.getString();
331         compUuid = getUUIDFactory().recreateUUID(uuidStr);
332
333         // 9th column is TEXT (LONG VARCHAR)
334
col = row.getColumn(9);
335         usingText = col.getString();
336
337         // 10th column is CONSTANTSTATE (COM...ExecPreparedStatement)
338

339         // Only load the compiled plan if the statement is valid
340
if (valid) {
341             col = row.getColumn(10);
342             preparedStatement = (ExecPreparedStatement) col.getObject();
343         }
344
345         // 11th column is INITIALLY_COMPILABLE (boolean)
346
col = row.getColumn(11);
347         if ( col.isNull() ) { initiallyCompilable = true; }
348         else { initiallyCompilable = col.getBoolean(); }
349
350         descriptor = new SPSDescriptor(dd, name,
351                                     uuid,
352                                     suuid,
353                                     compUuid,
354                                     type,
355                                     valid,
356                                     text,
357                                     usingText,
358                                     time,
359                                     preparedStatement,
360                                     initiallyCompilable
361                                     );
362
363         return descriptor;
364     }
365
366     public ExecRow makeEmptyRow()
367         throws StandardException
368     {
369         return makeSYSSTATEMENTSrow(false,
370                                     (SPSDescriptor) null);
371     }
372
373     /**
374      * Builds a list of columns suitable for creating this Catalog.
375      * The last column, the serialized statement, is not added
376      * to the column list. This is done deliberately to make it
377      * a 'hidden' column -- one that is not visible to customers,
378      * but is visible to the system.
379      *
380      *
381      * @return array of SystemColumn suitable for making this catalog.
382      */

383     public SystemColumn[] buildColumnList()
384     {
385         /*
386         ** Create one less than the number of columns, we
387         ** skip the last one deliberately.
388         */

389         SystemColumn[] columnList = new SystemColumn[SYSSTATEMENTS_COLUMN_COUNT - SYSSTATEMENTS_HIDDEN_COLUMN_COUNT];
390
391
392         // describe columns
393
columnList[0] = new SystemColumnImpl(
394                             convertIdCase( "STMTID"), // name
395
SYSSTATEMENTS_STMTID, // column number
396
0, // precision
397
0, // scale
398
false, // nullability
399
"CHAR", // dataType
400
true, // built-in type
401
36 // maxLength
402
);
403
404         columnList[1] =
405                     new SystemColumnImpl( // SQL IDENTIFIER
406
convertIdCase( "STMTNAME"), // column name
407
SYSSTATEMENTS_STMTNAME, // column number
408
false // nullability
409
);
410
411         columnList[2] = new SystemColumnImpl(
412                             convertIdCase( "SCHEMAID"), // name
413
SYSSTATEMENTS_SCHEMAID, // column number
414
0, // precision
415
0, // scale
416
false, // nullability
417
"CHAR", // dataType
418
true, // built-in type
419
36 // maxLength
420
);
421
422         columnList[3] =
423                     new SystemColumnImpl(
424                             convertIdCase( "TYPE"), // name
425
SYSSTATEMENTS_TYPE, // column number
426
0, // precision
427
0, // scale
428
false, // nullability
429
"CHAR", // dataType
430
true, // built-in type
431
1 // maxLength
432
);
433
434         columnList[4] =
435                     new SystemColumnImpl(
436                             convertIdCase( "VALID"), // name
437
SYSSTATEMENTS_VALID,// column number
438
0, // precision
439
0, // scale
440
false, // nullability
441
"BOOLEAN", // dataType
442
true, // built-in type
443
1 // maxLength
444
);
445
446         columnList[5] =
447                     new SystemColumnImpl(
448                             convertIdCase( "TEXT"), // name
449
SYSSTATEMENTS_TEXT, // column number
450
0, // precision
451
0, // scale
452
false, // nullability
453
"LONG VARCHAR", // dataType
454
true, // built-in type
455
TypeId.LONGVARCHAR_MAXWIDTH // maxLength
456
);
457
458         columnList[6] =
459                     new SystemColumnImpl(
460                             convertIdCase( "LASTCOMPILED"), // name
461
SYSSTATEMENTS_LASTCOMPILED, // column number
462
0, // precision
463
0, // scale
464
true, // nullability
465
"TIMESTAMP", // dataType
466
true, // built-in type
467
TypeId.TIMESTAMP_MAXWIDTH // maxLength
468
);
469
470         columnList[7] = new SystemColumnImpl(
471                             convertIdCase( "COMPILATIONSCHEMAID"), // name
472
SYSSTATEMENTS_COMPILATION_SCHEMAID, // column number
473
0, // precision
474
0, // scale
475
false, // nullability
476
"CHAR", // dataType
477
true, // built-in type
478
36 // maxLength
479
);
480
481         columnList[8] =
482                     new SystemColumnImpl(
483                             convertIdCase( "USINGTEXT"), // name
484
SYSSTATEMENTS_USINGTEXT,// column number
485
0, // precision
486
0, // scale
487
true, // nullability
488
"LONG VARCHAR", // dataType
489
true, // built-in type
490
TypeId.LONGVARCHAR_MAXWIDTH // maxLength
491
);
492         /*
493         ** This column is deliberately left out. It
494         ** is effectively 'hidden' from users. The code
495         ** to create it is left here to demonstrate what
496         ** it really looks like.
497         */

498         //columnList[9] =
499
// new SystemColumnImpl(
500
// convertIdCase( "CONSTANTSTATE"), // name
501
// SYSSTATEMENTS_CONSTANTSTATE,// column number
502
// 0, // precision
503
// 0, // scale
504
// false, // nullability
505
// ExecPreparedStatement.CLASS_NAME, //datatype
506
// false, // built-in type
507
// DataTypeDescriptor.MAXIMUM_WIDTH_UNKNOWN // maxLength
508
// );
509

510         /*
511         ** This column is also deliberately left out. It
512         ** is effectively 'hidden' from users. The code
513         ** to create it is left here to demonstrate what
514         ** it really looks like.
515         */

516         //columnList[10] =
517
// new SystemColumnImpl(
518
// convertIdCase( "INITIALLY_COMPILABLE"), // name
519
// SYSSTATEMENTS_INITIALLY_COMPILABLE,// column number
520
// 0, // precision
521
// 0, // scale
522
// true, // nullability
523
// "BOOLEAN", // dataType
524
// true, // built-in type
525
// 1 // maxLength
526
// );
527

528
529         return columnList;
530     }
531
532     /**
533      * Get the Properties associated with creating the heap.
534      *
535      * @return The Properties associated with creating the heap.
536      */

537     public Properties JavaDoc getCreateHeapProperties()
538     {
539         Properties JavaDoc properties = new Properties JavaDoc();
540
541         // keep page size at 2K since most stmts are that size
542
// anyway
543
properties.put(Property.PAGE_SIZE_PARAMETER,"2048");
544
545         // default properties for system tables:
546
properties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER,"0");
547         properties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER,"1");
548         return properties;
549     }
550
551 }
552
Popular Tags