KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > DITableInfo


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.util.Locale JavaDoc;
35
36 import org.hsqldb.resources.BundleHandler;
37 import org.hsqldb.store.ValuePool;
38
39 /**
40  * Provides extended information about HSQLDB tables and their
41  * columns/indices. <p>
42  *
43  * @author boucherb@users
44  * @version 1.8.0
45  * @since 1.7.2
46  */

47 final class DITableInfo {
48
49     // related to DatabaseMetaData
50
int bestRowTemporary = 0;
51     int bestRowTransaction = 1;
52     int bestRowSession = 2;
53     int bestRowUnknown = 0;
54     int bestRowNotPseudo = 1;
55     static final short tableIndexOther = 3;
56
57     /** Used in buffer size and character octet length determinations. */
58     private static final int HALF_MAX_INT = Integer.MAX_VALUE >>> 1;
59
60     /** BundleHandler id for column remarks resource bundle. */
61     private int hnd_column_remarks = -1;
62
63     /** BundleHandler id for table remarks resource bundle. */
64     private int hnd_table_remarks = -1;
65
66     /** The Table object upon which this object is reporting. */
67     private Table table;
68
69     /** Provides intrinsic type infformation support. */
70     private static final DITypeInfo ti = new DITypeInfo();
71
72     /**
73      * Creates a new DITableInfo object with the default Locale and reporting
74      * on no table. It is absolutely essential the a valid Table object is
75      * assigned to this object, using the setTable method, before any Table,
76      * Column or Index oriented value retrieval methods are called; this class
77      * contains no assertions or exception handling related to a null or
78      * invalid table member attribute.
79      */

80     DITableInfo() {
81
82 /** @todo fredt - remove from here: should be set in a database-wide context */
83         setLocale(Locale.getDefault());
84     }
85
86     /**
87      * Sets the Locale for table and column remarks. <p>
88      *
89      * @param l The Locale object
90      */

91     void setLocale(Locale JavaDoc l) {
92
93         Locale JavaDoc oldLocale;
94
95         synchronized (BundleHandler.class) {
96             oldLocale = BundleHandler.getLocale();
97
98             BundleHandler.setLocale(l);
99
100             hnd_column_remarks =
101                 BundleHandler.getBundleHandle("column-remarks", null);
102             hnd_table_remarks = BundleHandler.getBundleHandle("table-remarks",
103                     null);
104
105             BundleHandler.setLocale(oldLocale);
106         }
107     }
108
109     /**
110      * Retrieves whether the best row identifier column is
111      * a pseudo column, like an Oracle ROWID. <p>
112      *
113      * Currently, this always returns an Integer whose value is
114      * DatabaseMetaData.bestRowNotPseudo, as HSQLDB does not support
115      * pseudo columns such as ROWID. <p>
116      *
117      * @return whether the best row identifier column is
118      * a pseudo column
119      */

120     Integer JavaDoc getBRIPseudo() {
121         return ValuePool.getInt(bestRowNotPseudo);
122     }
123
124     /**
125      * Retrieves the scope of the best row identifier. <p>
126      *
127      * This implements the rules described in
128      * DatabaseInformationMain.SYSTEM_BESTROWIDENTIFIER. <p>
129      *
130      * @return the scope of the best row identifier
131      */

132     Integer JavaDoc getBRIScope() {
133         return (table.isWritable()) ? ValuePool.getInt(bestRowTemporary)
134                                     : ValuePool.getInt(bestRowSession);
135     }
136
137     /**
138      * Retrieves, if definitely known, the transfer size for values of the
139      * specified column, in bytes. <p>
140      *
141      * @param i zero-based column index
142      * @return the transfer size for values of the
143      * specified column, in bytes
144      */

145     Integer JavaDoc getColBufLen(int i) {
146
147         int size;
148         int type;
149         Column column;
150
151         column = table.getColumn(i);
152         type = column.getDIType();
153
154         switch (type) {
155
156             case Types.CHAR :
157             case Types.CLOB :
158             case Types.LONGVARCHAR :
159             case Types.VARCHAR : {
160                 size = column.getSize();
161
162                 if (size == 0) {}
163                 else if (size > HALF_MAX_INT) {
164                     size = 0;
165                 } else {
166                     size = 2 * size;
167                 }
168
169                 break;
170             }
171             case Types.BINARY :
172             case Types.BLOB :
173             case Types.LONGVARBINARY :
174             case Types.VARBINARY : {
175                 size = column.getSize();
176
177                 break;
178             }
179             case Types.BIGINT :
180             case Types.DOUBLE :
181             case Types.FLOAT :
182             case Types.DATE :
183             case Types.REAL :
184             case Types.TIME : {
185                 size = 8;
186
187                 break;
188             }
189             case Types.TIMESTAMP : {
190                 size = 12;
191
192                 break;
193             }
194             case Types.INTEGER :
195             case Types.SMALLINT :
196             case Types.TINYINT : {
197                 size = 4;
198
199                 break;
200             }
201             case Types.BOOLEAN : {
202                 size = 1;
203
204                 break;
205             }
206             default : {
207                 size = 0;
208
209                 break;
210             }
211         }
212
213         return (size > 0) ? ValuePool.getInt(size)
214                           : null;
215     }
216
217     /**
218      * Retrieves the declared size, in bytes, for character-valued
219      * columns. <p>
220      *
221      * If the size cannot be represented in the range [0,Integer.MAX_VALUE],
222      * this returns null. <p>
223      *
224      * @param i zero-based column index
225      * @return the size, in bytes, for character-valued columns
226      */

227     Integer JavaDoc getColCharOctLen(int i) {
228
229         int size;
230         int type;
231         Column column;
232
233         column = table.getColumn(i);
234         type = column.getDIType();
235
236         switch (type) {
237
238             case Types.CHAR :
239             case Types.CLOB :
240             case Types.LONGVARCHAR :
241             case Types.VARCHAR : {
242                 size = column.getSize();
243
244                 if (size == 0) {}
245                 else if (size > HALF_MAX_INT) {
246                     size = 0;
247                 } else {
248                     size = 2 * size;
249                 }
250
251                 break;
252             }
253             default : {
254                 size = 0;
255
256                 break;
257             }
258         }
259
260         return (size == 0) ? null
261                            : ValuePool.getInt(size);
262     }
263
264     /**
265      * Retrieves the SQL data type code for the specified column. <p>
266      *
267      * @param i zero-based column index
268      * @return the SQL data type code for the specified column
269      */

270     Integer JavaDoc getColDataType(int i) {
271         return ValuePool.getInt(table.getColumn(i).getDIType());
272     }
273
274     /**
275      * Retrieves the SQL data type name for the specified column. <p>
276      *
277      * @param i zero-based column index
278      * @return the SQL data type name for the specified column
279      */

280     String JavaDoc getColDataTypeName(int i) {
281
282         Column column = table.getColumn(i);
283
284         ti.setTypeCode(column.getDIType());
285         ti.setTypeSub(column.getDITypeSub());
286
287         return ti.getTypeName();
288     }
289
290     /**
291      * Retrieves the HSQLDB data subtype code for the specified column. <p>
292      *
293      * @param i zero-based column index
294      * @return the HSQLDB data subtype code for the specified column
295      */

296     Integer JavaDoc getColDataTypeSub(int i) {
297         return ValuePool.getInt(table.getColumn(i).getDITypeSub());
298     }
299
300     /**
301      * Retrieves the declared default value expression for the column. <p>
302      *
303      * @param i zero-based column index
304      * @return the declared default value expression for the column
305      */

306     String JavaDoc getColDefault(int i) {
307         return table.getColumn(i).getDefaultDDL();
308     }
309
310     /**
311      * Retrieves whether the specified column is the identity column for
312      * the table. <p>
313      *
314      * @param i zero-based column index
315      * @return whether the specified column is the identity column for
316      * the table.
317      */

318     Boolean JavaDoc getColIsIdentity(int i) {
319         return ValuePool.getBoolean(table.getColumn(i).isIdentity());
320     }
321
322     /**
323      * Retrieves whether the specified column is nullable. <p>
324      *
325      * If the column is nullable, "YES" is retrieved, else "NO". <p>
326      *
327      * @param i zero-based column index
328      * @return the nullability of the specified column
329      */

330     String JavaDoc getColIsNullable(int i) {
331
332         Column column = table.getColumn(i);
333
334         return (column.isNullable() &&!column.isIdentity()) ? "YES"
335                                                             : "NO";
336     }
337
338     /**
339      * Retrieves the simple name of the specified column. <p>
340      *
341      * @param i zero-based column index
342      * @return the simple name of the specified column.
343      */

344     String JavaDoc getColName(int i) {
345         return table.getColumn(i).columnName.name;
346     }
347
348     /**
349      * Retrieves the specified column's nullablility. <p>
350      *
351      * @param i zero-based column index
352      * @return the specified column's nullablilit
353      */

354     Integer JavaDoc getColNullability(int i) {
355
356         Column column = table.getColumn(i);
357
358         return (column.isNullable() &&!column.isIdentity())
359                ? ValuePool.getInt(DITypeInfo.columnNullable)
360                : ValuePool.getInt(DITypeInfo.columnNoNulls);
361     }
362
363     /**
364      * Retrieves the number base that should be used to interpret the
365      * specified column's numeric precision, as reported by getColSize(int).
366      *
367      * @param i zero-based column index
368      * @return the number base that should be used to
369      * interpret the column's numeric precision,
370      * as reported by getColSize(int).
371      */

372     Integer JavaDoc getColPrecRadix(int i) {
373
374         ti.setTypeCode(table.getColumn(i).getDIType());
375
376         return ti.getNumPrecRadix();
377     }
378
379     /**
380      * Retrieves the remarks, if any, recorded against the specified
381      * column. <p>
382      *
383      * @param i zero-based column index
384      * @return the remarks recorded against the specified column.
385      */

386     String JavaDoc getColRemarks(int i) {
387
388         String JavaDoc key;
389
390         if (table.getTableType() != Table.SYSTEM_TABLE) {
391             return null;
392         }
393
394         key = getName() + "_" + getColName(i);
395
396         return BundleHandler.getString(hnd_column_remarks, key);
397     }
398
399     /**
400      * Retrieves the declared (but not currently enforced) or implicit fixed
401      * number of digits to the right of the decimal point for exact numeric
402      * types.
403      *
404      * If the column's type precludes scale declaration, null is returned.
405      *
406      * @param i zero-based column index
407      * @return the fixed number of digits to the right of the decimal point
408      * for exact numeric types.
409      */

410     Integer JavaDoc getColScale(int i) {
411
412         Column column;
413         int type;
414
415         column = table.getColumn(i);
416         type = column.getDIType();
417
418         return Types.acceptsScaleCreateParam(type)
419                ? ValuePool.getInt(column.getScale())
420                : null;
421     }
422
423     /**
424      * Retrieves null (not implemented). <p>
425      *
426      * @param i zero-based column index
427      * @return null (not implemented)
428      */

429     String JavaDoc getColScopeCat(int i) {
430         return null;
431     }
432
433     /**
434      * Retrieves null (not implemented). <p>
435      *
436      * @param i zero-based column index
437      * @return null (not implemented)
438      */

439     String JavaDoc getColScopeSchem(int i) {
440         return null;
441     }
442
443     /**
444      * Retrieves null (not implemented). <p>
445      *
446      * @param i zero-based column index
447      * @return null (not implemented)
448      */

449     String JavaDoc getColScopeTable(int i) {
450         return null;
451     }
452
453     /**
454      * Retrieves either the declared or maximum length/precision for
455      * the specified column, if its type allows a precision/length
456      * declaration, else null. <p>
457      *
458      * @param i zero-based column index
459      * @return the declared or maximum length/precision for
460      * the specified column
461      */

462     Integer JavaDoc getColSize(int i) {
463
464         Column column;
465         int type;
466         int size;
467
468         column = table.getColumn(i);
469         type = column.getDIType();
470
471         if (!Types.acceptsPrecisionCreateParam(type)) {
472             return null;
473         }
474
475         size = column.getSize();
476
477         if (size > 0) {
478             return ValuePool.getInt(size);
479         } else {
480             ti.setTypeCode(type);
481
482             return ti.getPrecision();
483         }
484     }
485
486     /**
487      * Retrieves the SQL CLI data type code for the specified column. <p>
488      *
489      * @param i zero-based column index
490      * @return the SQL CLI data type code for the specified column
491      */

492     Integer JavaDoc getColSqlDataType(int i) {
493
494         ti.setTypeCode(table.getColumn(i).getDIType());
495
496         return ti.getSqlDataType();
497     }
498
499     /**
500      * Retrieves the SQL CLI datetime subtype for the specified column. <p>
501      *
502      * @param i zero-based column index
503      * @return the SQL CLI datetime subtype for the specified column
504      */

505     Integer JavaDoc getColSqlDateTimeSub(int i) {
506
507         ti.setTypeCode(table.getColumn(i).getDIType());
508
509         return ti.getSqlDateTimeSub();
510     }
511
512     /**
513      * Retrieves the full data source descriptor for [TEMP] TEXT tables. <p>
514      *
515      * @return the full data source descriptor
516      */

517     String JavaDoc getDataSource() {
518         return table.getDataSource();
519     }
520
521     /**
522      * Retrieves the HSQLDB-specific type of the table. <p>
523      *
524      * @return the HSQLDB-specific type of the table
525      */

526     String JavaDoc getHsqlType() {
527
528         switch (table.getTableType()) {
529
530             case Table.MEMORY_TABLE :
531             case Table.TEMP_TABLE :
532             case Table.SYSTEM_TABLE :
533                 return "MEMORY";
534
535             case Table.CACHED_TABLE :
536                 return "CACHED";
537
538             case Table.TEMP_TEXT_TABLE :
539             case Table.TEXT_TABLE :
540                 return "TEXT";
541
542             case Table.VIEW :
543             default :
544                 return null;
545         }
546     }
547
548     /**
549      * Retrieves null (not implemented). <p>
550      *
551      * @param i zero-based index specifier
552      * @return null (not implemented)
553      */

554     Integer JavaDoc getIndexCardinality(int i) {
555         return null;
556     }
557
558     /**
559      * Retrieves the sort-direction for the specified column in the
560      * specified index. <p>
561      *
562      * @param i zero-based index specifier
563      * @param columnPosition zero-based ordinal position of column in index
564      * @return the sort-direction for the specified column in the
565      * specified index
566      */

567     String JavaDoc getIndexColDirection(int i, int columnPosition) {
568
569         // so far, hsqldb only supports completely ascending indexes
570
return "A";
571     }
572
573     /**
574      * Retrieves an array map from the zero-based ordinal positions of the
575      * columns in the specfied Index to the zero-based ordinal positions of
576      * the same columns in the table. <p>
577      *
578      * @param i zero-based index specifier
579      * @return an array map from the zero-based ordinal positions of
580      * the columns in the specfied Index to the zero-based
581      * ordinal positions of the same columns in the table
582      */

583     int[] getIndexColumns(int i) {
584         return table.getIndex(i).getColumns();
585     }
586
587     /**
588      * Retrieves the simple name of the specified Index. <p>
589      *
590      * @param i zero-based index specifier
591      * @return the simple name of the specified Index
592      */

593     String JavaDoc getIndexName(int i) {
594         return table.getIndex(i).getName().name;
595     }
596
597     /**
598      * Retrieves null (not implemented). <p>
599      *
600      * @param i zero-based index specifier
601      * @return null (not implemented)
602      */

603     Integer JavaDoc getIndexRowCardinality(int i) {
604         return null;
605     }
606
607     /**
608      * Retrieves the DatabaseMetaData type code of the specified Index. <p>
609      *
610      * @param i zero-based index specifier
611      * @return the DatabaseMetaData type code of the specified Index
612      */

613     Integer JavaDoc getIndexType(int i) {
614         return ValuePool.getInt(tableIndexOther);
615     }
616
617     /**
618      * Retrieves the number of visible columns in the specified Index. That
619      * is, this retrieves one less than the physical number of columns if the
620      * table maintains an internal primary index on an internal identity
621      * column, as is the case when the table has no declared primary key or
622      * identity column. <p>
623      *
624      * @param i zero-based index specifier
625      * @return the number of visible columns in the specified Index
626      */

627     int getIndexVisibleColumns(int i) {
628         return table.getIndex(i).getVisibleColumns();
629     }
630
631     /**
632      * Retrieves the simple name of the table. <p>
633      *
634      * @return the simple name of the table
635      */

636     String JavaDoc getName() {
637         return table.getName().name;
638     }
639
640     /**
641      * Retrieves the value of the next automatically assigned identity. <p>
642      *
643      * Be aware that this is not necessarily the value that will be assigned
644      * to the identity column during the next insert or update. This value is
645      * used if NULL is either implicitly or explicity assigned. <p>
646      *
647      * @return the value of the next automatically assigned identity
648      */

649     Long JavaDoc getNextIdentity() {
650
651         Index pi;
652
653         if (table.identityColumn < 0) {
654             return null;
655         }
656
657         return ValuePool.getLong(table.identitySequence.peek());
658     }
659
660     /**
661      * Retrieves the remarks (if any) recorded against the Table. <p>
662      *
663      * @return the remarks recorded against the Table
664      */

665     String JavaDoc getRemark() {
666
667         return (table.getTableType() == Table.SYSTEM_TABLE)
668                ? BundleHandler.getString(hnd_table_remarks, getName())
669                : null;
670     }
671
672     /**
673      * Retrieves the standard JDBC type of the table. <p>
674      *
675      * "TABLE" for user-defined tables, "VIEW" for user-defined views,
676      * and so on.
677      *
678      * @return the standard JDBC type of the table
679      */

680     String JavaDoc getStandardType() {
681
682         switch (table.getTableType()) {
683
684             case Table.VIEW :
685                 return "VIEW";
686
687             case Table.TEMP_TABLE :
688             case Table.TEMP_TEXT_TABLE :
689                 return "GLOBAL TEMPORARY";
690
691             case Table.SYSTEM_TABLE :
692                 return "SYSTEM TABLE";
693
694             default :
695                 return "TABLE";
696         }
697     }
698
699     /**
700      * Retrieves the Table object on which this object is currently
701      * reporting. <p>
702      *
703      * @return the Table object on which this object
704      * is currently reporting
705      */

706     Table getTable() {
707         return this.table;
708     }
709
710     /**
711      * Retrieves, for [TEMP] TEXT tables, whether the table's data source
712      * descriptor requests descending read semantics. That is, when this
713      * value is true, it indicate that the text file is to be read from
714      * the bottom up. <p>
715      *
716      * @return whether the table's data source
717      * descriptor requests descending
718      * read semantics
719      */

720     Boolean JavaDoc isDataSourceDescending() {
721         return ValuePool.getBoolean(table.isDescDataSource());
722     }
723
724     /**
725      * Retrieves whether the specified Index is non-unique. <p>
726      *
727      * @param i zero-based index specifier
728      * @return whether the specified Index is non-unique
729      */

730     Boolean JavaDoc isIndexNonUnique(int i) {
731         return ValuePool.getBoolean(!table.getIndex(i).isUnique());
732     }
733
734     /**
735      * Retrieves whether the table is in data read-only mode. This value does
736      * not reflect the various read-only modes of the database or the
737      * read-only mode of the connection. <p>
738      *
739      * @return whether the table is in data read-only mode
740      */

741     Boolean JavaDoc isReadOnly() {
742         return ValuePool.getBoolean(table.isDataReadOnly());
743     }
744
745     /**
746      * Assigns the Table object on which this object is to report. <p>
747      *
748      * @param table the Table object on which this object is to report
749      */

750     void setTable(Table table) {
751         this.table = table;
752     }
753 }
754
Popular Tags