KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > Types


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 org.hsqldb.lib.IntKeyHashMap;
35 import org.hsqldb.lib.IntValueHashMap;
36 import org.hsqldb.lib.HashMap;
37
38 /**
39  * Defines the constants that are used to identify SQL types for HSQLDB JDBC
40  * inteface type reporting. The actual type constant values are equivalent
41  * to those defined in the latest java.sql.Types, where available,
42  * or those defined by ansi/iso SQL 200n otherwise. A type sub-identifer
43  * has been added to differentiate HSQLDB-specific type specializations.
44  *
45  * @author boucherb@users
46  * @version 1.7.2
47  * @since 1.7.2
48  */

49 public class Types {
50
51     /**
52      * Names of types.
53      * Used for external, JDBC reporting
54      * Used for library and user function arguments
55      */

56     public static final String JavaDoc DecimalClassName = "java.math.BigDecimal";
57     public static final String JavaDoc DateClassName = "java.sql.Date";
58     public static final String JavaDoc TimeClassName = "java.sql.Time";
59     public static final String JavaDoc TimestampClassName = "java.sql.Timestamp";
60
61     /**
62      * The constant in the Java programming language, sometimes referred to
63      * as a type code, that identifies the generic SQL type
64      * <code>ARRAY</code>.
65      *
66      * @since JDK 1.2
67      */

68     public static final int ARRAY = 2003;
69
70     /**
71      * <P>The constant in the Java programming language, sometimes referred
72      * to as a type code, that identifies the generic SQL type
73      * <code>BIGINT</code>.
74      */

75     public static final int BIGINT = -5;
76
77     /**
78      * <P>The constant in the Java programming language, sometimes referred
79      * to as a type code, that identifies the generic SQL type
80      * <code>BINARY</code>.
81      */

82     public static final int BINARY = -2;
83
84     /**
85      * The constant in the Java programming language, sometimes referred to
86      * as a type code, that identifies the generic SQL type
87      * <code>BLOB</code>.
88      *
89      * @since JDK 1.2
90      */

91     public static final int BLOB = 2004;
92
93     /**
94      * The constant in the Java programming language, somtimes referred to
95      * as a type code, that identifies the generic SQL type
96      * <code>BOOLEAN</code>.
97      *
98      * @since JDK 1.4
99      */

100     public static final int BOOLEAN = 16;
101
102     /**
103      * <P>The constant in the Java programming language, sometimes referred
104      * to as a type code, that identifies the generic SQL type
105      * <code>CHAR</code>.
106      */

107     public static final int CHAR = 1;
108
109     /**
110      * The constant in the Java programming language, sometimes referred to
111      * as a type code, that identifies the generic SQL type
112      * <code>CLOB</code>
113      *
114      * @since JDK 1.2
115      */

116     public static final int CLOB = 2005;
117
118     /**
119      * The constant in the Java programming language, somtimes referred to
120      * as a type code, that identifies the generic SQL type <code>DATALINK</code>.
121      *
122      * @since JDK 1.4
123      */

124     public static final int DATALINK = 70;
125
126     /**
127      * <P>The constant in the Java programming language, sometimes referred
128      * to as a type code, that identifies the generic SQL type
129      * <code>DATE</code>.
130      */

131     public static final int DATE = 91;
132
133     /**
134      * <P>The constant in the Java programming language, sometimes referred
135      * to as a type code, that identifies the generic SQL type
136      * <code>DECIMAL</code>.
137      */

138     public static final int DECIMAL = 3;
139
140     /**
141      * The constant in the Java programming language, sometimes referred to
142      * as a type code, that identifies the generic SQL type
143      * <code>DISTINCT</code>.
144      *
145      * @since JDK 1.2
146      */

147     public static final int DISTINCT = 2001;
148
149     /**
150      * <P>The constant in the Java programming language, sometimes referred
151      * to as a type code, that identifies the generic SQL type
152      * <code>DOUBLE</code>.
153      */

154     public static final int DOUBLE = 8;
155
156     /**
157      * <P>The constant in the Java programming language, sometimes referred
158      * to as a type code, that identifies the generic SQL type
159      * <code>FLOAT</code>.
160      */

161     public static final int FLOAT = 6;
162
163     /**
164      * <P>The constant in the Java programming language, sometimes referred
165      * to as a type code, that identifies the generic SQL type
166      * <code>INTEGER</code>.
167      */

168     public static final int INTEGER = 4;
169
170     /**
171      * The constant in the Java programming language, sometimes referred to
172      * as a type code, that identifies the generic SQL type
173      * <code>JAVA_OBJECT</code>.
174      *
175      * @since JDK 1.2
176      */

177     public static final int JAVA_OBJECT = 2000;
178
179     /**
180      * <P>The constant in the Java programming language, sometimes referred
181      * to as a type code, that identifies the generic SQL type
182      * <code>LONGVARBINARY</code>.
183      */

184     public static final int LONGVARBINARY = -4;
185
186     /**
187      * <P>The constant in the Java programming language, sometimes referred
188      * to as a type code, that identifies the generic SQL type
189      * <code>LONGVARCHAR</code>.
190      */

191     public static final int LONGVARCHAR = -1;
192
193     /**
194      * <P>The constant in the Java programming language, sometimes referred
195      * to as a type code, that identifies the generic SQL type
196      * <code>NULL</code>.
197      */

198     public static final int NULL = 0;
199
200     /**
201      * <P>The constant in the Java programming language, sometimes referred
202      * to as a type code, that identifies the generic SQL type
203      * <code>NUMERIC</code>.
204      */

205     public static final int NUMERIC = 2;
206
207     /**
208      * The constant in the Java programming language that indicates
209      * that the SQL type is database-specific and
210      * gets mapped to a Java object that can be accessed via
211      * the methods <code>getObject</code> and <code>setObject</code>.
212      */

213     public static final int OTHER = 1111;
214
215     /**
216      * <P>The constant in the Java programming language, sometimes referred
217      * to as a type code, that identifies the generic SQL type
218      * <code>REAL</code>.
219      */

220     public static final int REAL = 7;
221
222     /**
223      * The constant in the Java programming language, sometimes referred to
224      * as a type code, that identifies the generic SQL type
225      * <code>REF</code>.
226      *
227      * @since JDK 1.2
228      */

229     public static final int REF = 2006;
230
231     /**
232      * <P>The constant in the Java programming language, sometimes referred
233      * to as a type code, that identifies the generic SQL type
234      * <code>SMALLINT</code>.
235      */

236     public static final int SMALLINT = 5;
237
238     /**
239      * The constant in the Java programming language, sometimes referred to
240      * as a type code, that identifies the generic SQL type
241      * <code>STRUCT</code>.
242      *
243      * @since JDK 1.2
244      */

245     public static final int STRUCT = 2002;
246
247     /**
248      * <P>The constant in the Java programming language, sometimes referred
249      * to as a type code, that identifies the generic SQL type
250      * <code>TIME</code>.
251      */

252     public static final int TIME = 92;
253
254     /**
255      * <P>The constant in the Java programming language, sometimes referred
256      * to as a type code, that identifies the generic SQL type
257      * <code>TIMESTAMP</code>.
258      */

259     public static final int TIMESTAMP = 93;
260
261     /**
262      * <P>The constant in the Java programming language, sometimes referred
263      * to as a type code, that identifies the generic SQL type
264      * <code>TINYINT</code>.
265      */

266     public static final int TINYINT = -6;
267
268     /**
269      * <P>The constant in the Java programming language, sometimes referred
270      * to as a type code, that identifies the generic SQL type
271      * <code>VARBINARY</code>.
272      */

273     public static final int VARBINARY = -3;
274
275     /**
276      * <P>The constant in the Java programming language, sometimes referred
277      * to as a type code, that identifies the generic SQL type
278      * <code>VARCHAR</code>.
279      */

280     public static final int VARCHAR = 12;
281
282     /**
283      * <P>The constant in the Java programming language, sometimes referred
284      * to as a type code, that identifies the recent SQL 200n SQL type
285      * <code>XML</code>.
286      *
287      * @since SQL 200n
288      */

289     public static final int XML = 137;
290
291     /**
292      * The default HSQLODB type sub-identifier. This indicates that an
293      * HSQLDB type with this sub-type, if supported, is the very closest
294      * thing HSQLDB offerers to the JDBC/SQL200n type
295      */

296     public static final int TYPE_SUB_DEFAULT = 1;
297
298     /**
299      * The IGNORECASE type sub-identifier. This indicates that an HSQLDB type
300      * with this sub-type, if supported, is the closest thing HSQLDB offerers
301      * to the JDBC/SQL200n type, except that case is ignored in comparisons
302      */

303     public static final int TYPE_SUB_IGNORECASE = TYPE_SUB_DEFAULT << 2;
304
305     /**
306      * Every (type,type-sub) combination known in the HSQLDB context.
307      * Not every combination need be supported as a table or procedure
308      * column type -- such determinations are handled in DITypeInfo.
309      */

310     static final int[][] ALL_TYPES = {
311         {
312             ARRAY, TYPE_SUB_DEFAULT
313         }, {
314             BIGINT, TYPE_SUB_DEFAULT
315         }, {
316             BINARY, TYPE_SUB_DEFAULT
317         }, {
318             BLOB, TYPE_SUB_DEFAULT
319         }, {
320             BOOLEAN, TYPE_SUB_DEFAULT
321         }, {
322             CHAR, TYPE_SUB_DEFAULT
323         }, {
324             CLOB, TYPE_SUB_DEFAULT
325         }, {
326             DATALINK, TYPE_SUB_DEFAULT
327         }, {
328             DATE, TYPE_SUB_DEFAULT
329         }, {
330             DECIMAL, TYPE_SUB_DEFAULT
331         }, {
332             DISTINCT, TYPE_SUB_DEFAULT
333         }, {
334             DOUBLE, TYPE_SUB_DEFAULT
335         }, {
336             FLOAT, TYPE_SUB_DEFAULT
337         }, {
338             INTEGER, TYPE_SUB_DEFAULT
339         }, {
340             JAVA_OBJECT, TYPE_SUB_DEFAULT
341         }, {
342             LONGVARBINARY, TYPE_SUB_DEFAULT
343         }, {
344             LONGVARCHAR, TYPE_SUB_DEFAULT
345         }, {
346             NULL, TYPE_SUB_DEFAULT
347         }, {
348             NUMERIC, TYPE_SUB_DEFAULT
349         }, {
350             OTHER, TYPE_SUB_DEFAULT
351         }, {
352             REAL, TYPE_SUB_DEFAULT
353         }, {
354             REF, TYPE_SUB_DEFAULT
355         }, {
356             SMALLINT, TYPE_SUB_DEFAULT
357         }, {
358             STRUCT, TYPE_SUB_DEFAULT
359         }, {
360             TIME, TYPE_SUB_DEFAULT
361         }, {
362             TIMESTAMP, TYPE_SUB_DEFAULT
363         }, {
364             TINYINT, TYPE_SUB_DEFAULT
365         }, {
366             VARBINARY, TYPE_SUB_DEFAULT
367         }, {
368             VARCHAR, TYPE_SUB_DEFAULT
369         }, {
370             VARCHAR, TYPE_SUB_IGNORECASE
371         }, {
372             XML, TYPE_SUB_DEFAULT
373         }
374     };
375     /*
376      SQL specifies predefined data types named by the following <key word>s:
377      CHARACTER, CHARACTER VARYING, CHARACTER LARGE OBJECT, BINARY LARGE OBJECT,
378      NUMERIC, DECIMAL, SMALLINT, INTEGER, BIGINT, FLOAT, REAL, DOUBLE PRECISION,
379      BOOLEAN, DATE, TIME, TIMESTAMP, and INTERVAL.
380      SQL 200n adds DATALINK in Part 9: Management of External Data (SQL/MED)
381      and adds XML in Part 14: XML-Related Specifications (SQL/XML)
382      */

383
384     // CLI type list from Table 37
385
static final int SQL_CHARACTER = 1;
386     static final int SQL_CHAR = 1;
387     static final int SQL_NUMERIC = 2;
388     static final int SQL_DECIMAL = 3;
389     static final int SQL_DEC = 3;
390     static final int SQL_INTEGER = 4;
391     static final int SQL_INT = 4;
392     static final int SQL_SMALLINT = 5;
393     static final int SQL_FLOAT = 6;
394     static final int SQL_REAL = 7;
395     static final int SQL_DOUBLE = 8;
396     static final int SQL_CHARACTER_VARYING = 12;
397     static final int SQL_CHAR_VARYING = 12;
398     static final int SQL_VARCHAR = 12;
399     static final int SQL_BOOLEAN = 16;
400     static final int SQL_USER_DEFINED_TYPE = 17;
401     static final int SQL_ROW = 19;
402     static final int SQL_REF = 20;
403     static final int SQL_BIGINT = 25;
404     static final int SQL_BINARY_LARGE_OBJECT = 30;
405     static final int SQL_BLOB = 30;
406     static final int SQL_CHARACTER_LARGE_OBJECT = 40;
407     static final int SQL_CLOB = 40;
408     static final int SQL_ARRAY = 50; // not predefined
409
static final int SQL_MULTISET = 55; //
410
static final int SQL_DATE = 91;
411     static final int SQL_TIME = 92;
412     static final int SQL_TIMESTAMP = 93; //
413
static final int SQL_TIME_WITH_TIME_ZONE = 94;
414     static final int SQL_TIMESTAMP_WITH_TIME_ZONE = 95; //
415
static final int SQL_INTERVAL_YEAR = 101; //
416
static final int SQL_INTERVAL_MONTH = 102;
417     static final int SQL_INTERVAL_DAY = 103;
418     static final int SQL_INTERVAL_HOUR = 104;
419     static final int SQL_INTERVAL_MINUTE = 105;
420     static final int SQL_INTERVAL_SECOND = 106;
421     static final int SQL_INTERVAL_YEAR_TO_MONTH = 107;
422     static final int SQL_INTERVAL_DAY_TO_HOUR = 108;
423     static final int SQL_INTERVAL_DAY_TO_MINUTE = 109;
424     static final int SQL_INTERVAL_DAY_TO_SECOND = 110;
425     static final int SQL_INTERVAL_HOUR_TO_MINUTE = 111;
426     static final int SQL_INTERVAL_HOUR_TO_SECOND = 112;
427     static final int SQL_INTERVAL_MINUTE_TO_SECOND = 113;
428
429     // These values are not in table 37 of the SQL CLI 200n FCD, but some
430
// are found in tables 6-9 and some are found in Annex A1:
431
// c Header File SQLCLI.H and/or addendums in other documents,
432
// such as:
433
// SQL 200n Part 9: Management of External Data (SQL/MED) : DATALINK
434
// SQL 200n Part 14: XML-Related Specifications (SQL/XML) : XML
435
static final int SQL_BIT_VARYING = 15; // is in SQL99 but removed from 200n
436
static final int SQL_DATALINK = 70;
437     static final int SQL_UDT = 17;
438     static final int SQL_UDT_LOCATOR = 18;
439     static final int SQL_BLOB_LOCATOR = 31;
440     static final int SQL_CLOB_LOCATOR = 41;
441     static final int SQL_ARRAY_LOCATOR = 51;
442     static final int SQL_MULTISET_LOCATOR = 56;
443     static final int SQL_ALL_TYPES = 0;
444     static final int SQL_DATETIME = 9; // collective name
445
static final int SQL_INTERVAL = 10; // collective name
446
static final int SQL_XML = 137;
447
448     // SQL_UDT subcodes
449
static final int SQL_DISTINCT = 1;
450     static final int SQL_SCTRUCTURED = 2;
451
452     // non-standard type not in JDBC or SQL CLI
453
public static final int VARCHAR_IGNORECASE = 100;
454
455 // lookup for types
456
// boucherb@users - access changed for metadata 1.7.2
457
static IntValueHashMap typeAliases;
458     static IntKeyHashMap typeNames;
459     static HashMap javaTypeNames;
460
461 // boucherb@users - We can't handle method invocations in
462
// Function.java or user-defined methods whose parameters
463
// number class is
464
// narrower than the corresponding internal
465
// wrapper
466
private static org.hsqldb.lib.HashSet illegalParameterClasses;
467
468     static {
469         typeAliases = new IntValueHashMap(50);
470
471         typeAliases.put("INTEGER", Types.INTEGER);
472         typeAliases.put("INT", Types.INTEGER);
473         typeAliases.put("int", Types.INTEGER);
474         typeAliases.put("java.lang.Integer", Types.INTEGER);
475         typeAliases.put("IDENTITY", Types.INTEGER);
476         typeAliases.put("DOUBLE", Types.DOUBLE);
477         typeAliases.put("double", Types.DOUBLE);
478         typeAliases.put("java.lang.Double", Types.DOUBLE);
479         typeAliases.put("FLOAT", Types.FLOAT);
480         typeAliases.put("REAL", Types.REAL);
481         typeAliases.put("VARCHAR", Types.VARCHAR);
482         typeAliases.put("java.lang.String", Types.VARCHAR);
483         typeAliases.put("CHAR", Types.CHAR);
484         typeAliases.put("CHARACTER", Types.CHAR);
485         typeAliases.put("LONGVARCHAR", Types.LONGVARCHAR);
486         typeAliases.put("VARCHAR_IGNORECASE", VARCHAR_IGNORECASE);
487         typeAliases.put("DATE", Types.DATE);
488         typeAliases.put(DateClassName, Types.DATE);
489         typeAliases.put("TIME", Types.TIME);
490         typeAliases.put(TimeClassName, Types.TIME);
491         typeAliases.put("TIMESTAMP", Types.TIMESTAMP);
492         typeAliases.put(TimestampClassName, Types.TIMESTAMP);
493         typeAliases.put("DATETIME", Types.TIMESTAMP);
494         typeAliases.put("DECIMAL", Types.DECIMAL);
495         typeAliases.put(DecimalClassName, Types.DECIMAL);
496         typeAliases.put("NUMERIC", Types.NUMERIC);
497         typeAliases.put("BIT", Types.BOOLEAN);
498         typeAliases.put("BOOLEAN", Types.BOOLEAN);
499         typeAliases.put("boolean", Types.BOOLEAN);
500         typeAliases.put("java.lang.Boolean", Types.BOOLEAN);
501         typeAliases.put("TINYINT", Types.TINYINT);
502         typeAliases.put("byte", Types.TINYINT);
503         typeAliases.put("java.lang.Byte", Types.TINYINT);
504         typeAliases.put("SMALLINT", Types.SMALLINT);
505         typeAliases.put("short", Types.SMALLINT);
506         typeAliases.put("java.lang.Short", Types.SMALLINT);
507         typeAliases.put("BIGINT", Types.BIGINT);
508         typeAliases.put("long", Types.BIGINT);
509         typeAliases.put("java.lang.Long", Types.BIGINT);
510         typeAliases.put("BINARY", Types.BINARY);
511         typeAliases.put("[B", Types.BINARY);
512         typeAliases.put("VARBINARY", Types.VARBINARY);
513         typeAliases.put("LONGVARBINARY", Types.LONGVARBINARY);
514         typeAliases.put("OTHER", Types.OTHER);
515         typeAliases.put("OBJECT", Types.OTHER);
516         typeAliases.put("java.lang.Object", Types.OTHER);
517         typeAliases.put("NULL", Types.NULL);
518         typeAliases.put("void", Types.NULL);
519         typeAliases.put("java.lang.Void", Types.NULL);
520
521         //
522
typeNames = new IntKeyHashMap();
523
524         typeNames.put(Types.NULL, "NULL");
525         typeNames.put(Types.INTEGER, "INTEGER");
526         typeNames.put(Types.DOUBLE, "DOUBLE");
527         typeNames.put(VARCHAR_IGNORECASE, "VARCHAR_IGNORECASE");
528         typeNames.put(Types.VARCHAR, "VARCHAR");
529         typeNames.put(Types.CHAR, "CHAR");
530         typeNames.put(Types.LONGVARCHAR, "LONGVARCHAR");
531         typeNames.put(Types.DATE, "DATE");
532         typeNames.put(Types.TIME, "TIME");
533         typeNames.put(Types.DECIMAL, "DECIMAL");
534         typeNames.put(Types.BOOLEAN, "BOOLEAN");
535         typeNames.put(Types.TINYINT, "TINYINT");
536         typeNames.put(Types.SMALLINT, "SMALLINT");
537         typeNames.put(Types.BIGINT, "BIGINT");
538         typeNames.put(Types.REAL, "REAL");
539         typeNames.put(Types.FLOAT, "FLOAT");
540         typeNames.put(Types.NUMERIC, "NUMERIC");
541         typeNames.put(Types.TIMESTAMP, "TIMESTAMP");
542         typeNames.put(Types.BINARY, "BINARY");
543         typeNames.put(Types.VARBINARY, "VARBINARY");
544         typeNames.put(Types.LONGVARBINARY, "LONGVARBINARY");
545         typeNames.put(Types.OTHER, "OBJECT");
546
547         //
548
illegalParameterClasses = new org.hsqldb.lib.HashSet();
549
550         illegalParameterClasses.add(Byte.TYPE);
551         illegalParameterClasses.add(Short.TYPE);
552         illegalParameterClasses.add(Float.TYPE);
553         illegalParameterClasses.add(Byte JavaDoc.class);
554         illegalParameterClasses.add(Short JavaDoc.class);
555         illegalParameterClasses.add(Float JavaDoc.class);
556
557         //
558
javaTypeNames = new HashMap();
559
560         javaTypeNames.put(DateClassName, "java.sql.Date");
561         javaTypeNames.put(TimeClassName, "java.sql.Time");
562         javaTypeNames.put(TimestampClassName, "java.sql.Timestamp");
563         javaTypeNames.put(DecimalClassName, "java.math.BigDecimal");
564         javaTypeNames.put("byte", "java.lang.Integer");
565         javaTypeNames.put("java.lang.Byte", "java.lang.Integer");
566         javaTypeNames.put("short", "java.lang.Integer");
567         javaTypeNames.put("java.lang.Short", "java.lang.Integer");
568         javaTypeNames.put("int", "java.lang.Integer");
569         javaTypeNames.put("java.lang.Integer", "java.lang.Integer");
570         javaTypeNames.put("long", "java.lang.Long");
571         javaTypeNames.put("java.lang.Long", "java.lang.Long");
572         javaTypeNames.put("double", "java.lang.Double");
573         javaTypeNames.put("java.lang.Double", "java.lang.Double");
574         javaTypeNames.put("boolean", "java.lang.Boolean");
575         javaTypeNames.put("java.lang.Boolean", "java.lang.Boolean");
576         javaTypeNames.put("java.lang.String", "java.lang.String");
577         javaTypeNames.put("void", "java.lang.Void");
578         javaTypeNames.put("[B", "[B");
579     }
580
581     /**
582      * Translates a type name returned from a method into the name of type
583      * returned in a ResultSet
584      */

585     static String JavaDoc getFunctionReturnClassName(String JavaDoc methodReturnType) {
586
587         String JavaDoc name = (String JavaDoc) javaTypeNames.get(methodReturnType);
588
589         return name == null ? methodReturnType
590                             : name;
591     }
592
593     /**
594      * `
595      *
596      * @param type string
597      * @return java.sql.Types int value
598      * @throws HsqlException
599      */

600     static int getTypeNr(String JavaDoc type) throws HsqlException {
601
602         int i = typeAliases.get(type, Integer.MIN_VALUE);
603
604         Trace.check(i != Integer.MIN_VALUE, Trace.WRONG_DATA_TYPE, type);
605
606         return i;
607     }
608
609     /**
610      * Returns SQL type string for a java.sql.Types int value
611      */

612     public static String JavaDoc getTypeString(int type) {
613         return (String JavaDoc) typeNames.get(type);
614     }
615
616     /**
617      * Returns SQL type string for a java.sql.Types int value
618      */

619     public static String JavaDoc getTypeString(int type, int precision, int scale) {
620
621         String JavaDoc s = (String JavaDoc) typeNames.get(type);
622
623         if (precision != 0 && acceptsPrecisionCreateParam(type)) {
624             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
625
626             sb.append(Token.T_OPENBRACKET);
627             sb.append(precision);
628
629             if (scale != 0 && acceptsScaleCreateParam(type)) {
630                 sb.append(Token.T_COMMA);
631                 sb.append(scale);
632             }
633
634             sb.append(Token.T_CLOSEBRACKET);
635
636             return sb.toString();
637         }
638
639         return s;
640     }
641
642     /**
643      * Retieves the type number corresponding to the class
644      * of an IN, IN OUT or OUT parameter. <p>
645      *
646      * This method extends getTypeNr to return OTHER for
647      * primitive arrays, classes that directly implement
648      * java.io.Serializable and non-primitive arrays whose
649      * base component implements java.io.Serializable,
650      * allowing, for instance, arguments and return types of
651      * primitive arrays, Serializable objects and arrays,
652      * of Serializable objects. Direct primitive types
653      * other than those mapping directly to the internal
654      * wrapper form are not yet handled. That is, HSQLDB
655      * cannot yet properly deal with CALLs involving methods
656      * with primitive byte, short, float or their
657      * corresponding wrappers, due to the way internal
658      * conversion works and lack of detection and narrowing
659      * code in Function to allow this. In other words,
660      * passing in or retrieving any of the mentioned types
661      * always causes conversion to a wider internal wrapper
662      * which is genrally incompatible under reflective
663      * invocation, resulting in an IllegalArgumentException.
664      *
665      * @param c a Class instance
666      * @return java.sql.Types int value
667      * @throws HsqlException
668      */

669     static int getParameterTypeNr(Class JavaDoc c) throws HsqlException {
670
671         String JavaDoc name;
672         int type;
673
674         if (c == null) {
675             Trace.doAssert(false, "c is null");
676         }
677
678         if (Void.TYPE.equals(c)) {
679             return Types.NULL;
680         }
681
682         if (illegalParameterClasses.contains(c)) {
683             throw Trace.error(Trace.WRONG_DATA_TYPE,
684                               Trace.UNSUPPORTED_PARAM_CLASS, c.getName());
685         }
686
687         name = c.getName();
688         type = typeAliases.get(name, Integer.MIN_VALUE);
689
690         if (type == Integer.MIN_VALUE) {
691
692             // ensure all nested types are serializable
693
// byte[] is already covered as BINARY in typeAliases
694
if (c.isArray()) {
695                 while (c.isArray()) {
696                     c = c.getComponentType();
697                 }
698
699                 if (c.isPrimitive()
700                         || java.io.Serializable JavaDoc.class.isAssignableFrom(c)) {
701                     type = OTHER;
702                 }
703             } else if (java.io.Serializable JavaDoc.class.isAssignableFrom(c)) {
704                 type = OTHER;
705             }
706         }
707
708         Trace.check(type != Integer.MIN_VALUE, Trace.WRONG_DATA_TYPE, name);
709
710         return type;
711     }
712
713 /*
714     static boolean areSimilar(int t1, int t2) {
715
716         if (t1 == t2) {
717             return true;
718         }
719
720         if (isNumberType(t1)) {
721             return isNumberType(t2);
722         }
723
724         if (isCharacterType(t1)) {
725             return isCharacterType(t2);
726         }
727
728         if (isBinaryType(t1)) {
729             return isBinaryType(t2);
730         }
731
732         return false;
733     }
734
735     static boolean haveSameInternalRepresentation(int t1, int t2) {
736
737         if (t1 == t2) {
738             return true;
739         }
740
741         if (isCharacterType(t1)) {
742             return isCharacterType(t2);
743         }
744
745         if (isBinaryType(t1)) {
746             return isBinaryType(t2);
747         }
748
749         switch (t1) {
750
751             case TINYINT :
752             case SMALLINT :
753             case INTEGER : {
754                 switch (t2) {
755
756                     case TINYINT :
757                     case SMALLINT :
758                     case INTEGER : {
759                         return true;
760                     }
761                     default : {
762                         return false;
763                     }
764                 }
765             }
766             case FLOAT :
767             case REAL :
768             case DOUBLE : {
769                 switch (t2) {
770
771                     case FLOAT :
772                     case REAL :
773                     case DOUBLE : {
774                         return true;
775                     }
776                     default : {
777                         return false;
778                     }
779                 }
780             }
781             case DECIMAL :
782             case NUMERIC : {
783                 switch (t2) {
784
785                     case DECIMAL :
786                     case NUMERIC : {
787                         return true;
788                     }
789                     default : {
790                         return false;
791                     }
792                 }
793             }
794             default : {
795                 return false;
796             }
797         }
798     }
799
800     static boolean isExactNumberType(int type) {
801
802         switch (type) {
803
804             case BIGINT :
805             case DECIMAL :
806             case INTEGER :
807             case NUMERIC :
808             case SMALLINT :
809             case TINYINT :
810                 return true;
811
812             default :
813                 return false;
814         }
815     }
816
817     static boolean isStrictlyIntegralNumberType(int type) {
818
819         switch (type) {
820
821             case BIGINT :
822             case INTEGER :
823             case SMALLINT :
824             case TINYINT :
825                 return true;
826
827             default :
828                 return false;
829         }
830     }
831
832     static boolean isApproximateNumberType(int type) {
833
834         switch (type) {
835
836             case DOUBLE :
837             case FLOAT :
838             case REAL :
839                 return true;
840
841             default :
842                 return false;
843         }
844     }
845
846     public static boolean isBinaryType(int type) {
847
848         switch (type) {
849
850             case BINARY :
851
852             case BLOB :
853             case LONGVARBINARY :
854             case VARBINARY :
855                 return true;
856
857             default :
858                 return false;
859         }
860     }
861
862 */

863     static boolean isDatetimeType(int type) {
864
865         switch (type) {
866
867             case DATE :
868             case TIME :
869             case TIMESTAMP :
870                 return true;
871
872             default :
873                 return false;
874         }
875     }
876
877     /**
878      * Types that accept precition params in column definition or casts.
879      * We ignore the parameter in many cases but accept it for compatibility
880      * with other engines. CHAR, VARCHAR and VARCHAR_IGNORECASE params
881      * are used when the sql.enforce_strict_types is true.
882      *
883      */

884     public static boolean acceptsPrecisionCreateParam(int type) {
885
886         switch (type) {
887
888             case BINARY :
889             case BLOB :
890             case CHAR :
891             case CLOB :
892
893             // case LONGVARBINARY :
894
// case LONGVARCHAR :
895
case VARBINARY :
896             case VARCHAR :
897             case VARCHAR_IGNORECASE :
898             case DECIMAL :
899             case NUMERIC :
900             case FLOAT :
901             case TIMESTAMP :
902             case TIME :
903                 return true;
904
905             default :
906                 return false;
907         }
908     }
909
910     public static int numericPrecisionCreateParamRadix(int type) {
911
912         switch (type) {
913
914             case Types.DECIMAL :
915             case Types.NUMERIC :
916                 return 10;
917
918             case FLOAT :
919                 return 2;
920
921             default :
922
923                 // to mean NOT APPLICABLE (i.e. NULL)
924
return 0;
925         }
926     }
927
928     public static boolean acceptsScaleCreateParam(int type) {
929
930         switch (type) {
931
932             case Types.DECIMAL :
933             case Types.NUMERIC :
934                 return true;
935
936             default :
937                 return false;
938         }
939     }
940
941     public static boolean isNumberType(int type) {
942
943         switch (type) {
944
945             case BIGINT :
946             case DECIMAL :
947             case DOUBLE :
948             case FLOAT :
949             case INTEGER :
950             case NUMERIC :
951             case REAL :
952             case SMALLINT :
953             case TINYINT :
954                 return true;
955
956             default :
957                 return false;
958         }
959     }
960
961     public static boolean isCharacterType(int type) {
962
963         switch (type) {
964
965             case CHAR :
966             case CLOB :
967             case LONGVARCHAR :
968             case VARCHAR :
969             case VARCHAR_IGNORECASE :
970                 return true;
971
972             default :
973                 return false;
974         }
975     }
976
977     public static String JavaDoc getTypeName(int type) {
978
979         switch (type) {
980
981             case Types.ARRAY :
982                 return "ARRAY";
983
984             case Types.BIGINT :
985                 return "BIGINT";
986
987             case Types.BINARY :
988                 return "BINARY";
989
990             case Types.BLOB :
991                 return "BLOB";
992
993             case Types.BOOLEAN :
994                 return "BOOLEAN";
995
996             case Types.CHAR :
997                 return "CHAR";
998
999             case Types.CLOB :
1000                return "CLOB";
1001
1002            case Types.DATALINK :
1003                return "DATALINK";
1004
1005            case Types.DATE :
1006                return "DATE";
1007
1008            case Types.DECIMAL :
1009                return "DECIMAL";
1010
1011            case Types.DISTINCT :
1012                return "DISTINCT";
1013
1014            case Types.DOUBLE :
1015                return "DOUBLE";
1016
1017            case Types.FLOAT :
1018                return "FLOAT";
1019
1020            case Types.INTEGER :
1021                return "INTEGER";
1022
1023            case Types.JAVA_OBJECT :
1024                return "JAVA_OBJECT";
1025
1026            case Types.LONGVARBINARY :
1027                return "LONGVARBINARY";
1028
1029            case Types.LONGVARCHAR :
1030                return "LONGVARCHAR";
1031
1032            case Types.NULL :
1033                return "NULL";
1034
1035            case Types.NUMERIC :
1036                return "NUMERIC";
1037
1038            case Types.OTHER :
1039                return "OTHER";
1040
1041            case Types.REAL :
1042                return "REAL";
1043
1044            case Types.REF :
1045                return "REF";
1046
1047            case Types.SMALLINT :
1048                return "SMALLINT";
1049
1050            case Types.STRUCT :
1051                return "STUCT";
1052
1053            case Types.TIME :
1054                return "TIME";
1055
1056            case Types.TIMESTAMP :
1057                return "TIMESTAMP";
1058
1059            case Types.TINYINT :
1060                return "TINYINT";
1061
1062            case Types.VARBINARY :
1063                return "VARBINARY";
1064
1065            case Types.VARCHAR :
1066                return "VARCHAR";
1067
1068            case Types.VARCHAR_IGNORECASE :
1069                return "VARCHAR_IGNORECASE";
1070
1071            case Types.XML :
1072                return "XML";
1073
1074            default :
1075                return null;
1076        }
1077    }
1078
1079    /**
1080     * A reasonable/customizable number to avoid the shortcomings/defects
1081     * associated with doing a dynamic scan of results to determine
1082     * the value. In practice, it turns out that single query yielding
1083     * widely varying values for display size of CHAR and VARCHAR columns
1084     * on repeated execution results in patently poor usability, as some fairly
1085     * high-profile, otherwise "enterprise-quality" RAD tools depend on
1086     * on the first value returned to lay out forms and limit the size of
1087     * single line edit controls, set corresponding local datastore storage
1088     * sizes, etc. In practice, It also turns out that many tools (due to
1089     * the original lack of PreparedStatement.getMetaData() in JDK 1.1) emulate
1090     * a SQL_DESCRIBE by executing a query hopefully guaranteed to return no
1091     * or very few rows for example: select ... from ... where 1=0.
1092     * Using the dynamic scan of 1.7.2 RC5 and previous, therefore, the
1093     * minimum display size value (1) was often being generated during
1094     * a tool's describe phase. Upon subsequent "real" retrievals, some
1095     * tools complain that CHAR and VARCHAR result values exceeded the
1096     * originally reported display size and refused to fetch further values.
1097     */

1098    public static final int MAX_CHAR_OR_VARCHAR_DISPLAY_SIZE =
1099        MAX_CHAR_OR_VARCHAR_DISPLAY_SIZE();
1100
1101    // So that the variable can be both public static final and
1102
// customizable through system properties if required.
1103
//
1104
// 32766 (0x7ffe) seems to be a magic number over which several
1105
// rather high-profile RAD tools start to have problems
1106
// regarding layout and allocation stress. It is gently
1107
// recommended that LONGVARCHAR be used for larger values in RAD
1108
// tool layout & presentation use cases until such time as we provide
1109
// true BLOB support (at which point, LONGVARCHAR will most likely become
1110
// an alias for CLOB).
1111
//
1112
// Most GUI tools seem to handle LONGVARCHAR gracefully by:
1113
//
1114
// 1.) refusing to directly display such columns in graphical query results
1115
// 2.) providing other means to retrieve and display such values
1116
private static int MAX_CHAR_OR_VARCHAR_DISPLAY_SIZE() {
1117
1118        try {
1119            return Integer.getInteger(
1120                "hsqldb.max_char_or_varchar_display_size", 32766).intValue();
1121        } catch (SecurityException JavaDoc e) {
1122            return 32766;
1123        }
1124    }
1125
1126    public static int getMaxDisplaySize(int type) {
1127
1128        switch (type) {
1129
1130            case Types.BINARY :
1131            case Types.LONGVARBINARY :
1132            case Types.LONGVARCHAR :
1133            case Types.OTHER :
1134            case Types.VARBINARY :
1135            case Types.XML :
1136                return Integer.MAX_VALUE; // max string length
1137

1138            case Types.CHAR :
1139            case Types.VARCHAR :
1140                return MAX_CHAR_OR_VARCHAR_DISPLAY_SIZE;
1141
1142            case Types.BIGINT : // PowerBuilder barfs, wants 19
1143

1144                // ...not our problem, tho,
1145
// according to JDBC
1146
return 20; // precision + "-".length();
1147

1148            case Types.BOOLEAN :
1149                return 5; // Math.max("true".length(),"false".length);
1150

1151            case Types.DATALINK :
1152                return 20004; // same as precision
1153

1154            case Types.DECIMAL :
1155            case Types.NUMERIC :
1156                return 646456995; // precision + "-.".length()
1157

1158            case Types.DATE :
1159                return 10; // same as precision
1160

1161            case Types.INTEGER :
1162                return 11; // precision + "-".length();
1163

1164            case Types.FLOAT :
1165            case Types.REAL :
1166            case Types.DOUBLE :
1167                return 23; // String.valueOf(-Double.MAX_VALUE).length();
1168

1169            case Types.TIME :
1170                return 8; // same as precision
1171

1172            case Types.SMALLINT :
1173                return 6; // precision + "-".length();
1174

1175            case Types.TIMESTAMP :
1176                return 29; // same as precision
1177

1178            case Types.TINYINT :
1179                return 4; // precision + "-".length();
1180

1181            default :
1182                return 0; // unknown
1183
}
1184    }
1185
1186    public static boolean isSearchable(int type) {
1187
1188        switch (type) {
1189
1190            case Types.ARRAY :
1191            case Types.BLOB :
1192            case Types.CLOB :
1193            case Types.JAVA_OBJECT :
1194            case Types.STRUCT :
1195            case Types.OTHER :
1196                return false;
1197
1198            default :
1199                return true;
1200        }
1201    }
1202
1203    public static Boolean JavaDoc isCaseSensitive(int type) {
1204
1205        switch (type) {
1206
1207            case Types.ARRAY :
1208            case Types.BLOB :
1209            case Types.CLOB :
1210            case Types.DISTINCT :
1211            case Types.JAVA_OBJECT :
1212            case Types.NULL :
1213            case Types.REF :
1214            case Types.STRUCT :
1215                return null;
1216
1217            case Types.CHAR :
1218            case Types.DATALINK :
1219            case Types.LONGVARCHAR :
1220            case Types.OTHER :
1221            case Types.XML :
1222                return Boolean.TRUE;
1223
1224            case Types.VARCHAR_IGNORECASE :
1225            default :
1226                return Boolean.FALSE;
1227        }
1228    }
1229
1230    public static Boolean JavaDoc isUnsignedAttribute(int type) {
1231
1232        switch (type) {
1233
1234            case Types.BIGINT :
1235            case Types.DECIMAL :
1236            case Types.DOUBLE :
1237            case Types.FLOAT :
1238            case Types.INTEGER :
1239            case Types.NUMERIC :
1240            case Types.REAL :
1241            case Types.SMALLINT :
1242            case Types.TINYINT :
1243                return Boolean.FALSE;
1244
1245            default :
1246                return null;
1247        }
1248    }
1249
1250    public static int getPrecision(int type) {
1251
1252        switch (type) {
1253
1254            case Types.BINARY :
1255            case Types.CHAR :
1256            case Types.LONGVARBINARY :
1257            case Types.LONGVARCHAR :
1258            case Types.OTHER :
1259            case Types.VARBINARY :
1260            case Types.VARCHAR :
1261            case Types.XML :
1262                return Integer.MAX_VALUE;
1263
1264            case Types.BIGINT :
1265                return 19;
1266
1267            case Types.BOOLEAN :
1268                return 1;
1269
1270            case Types.DATALINK :
1271
1272                // from SQL CLI spec. TODO: Interpretation?
1273
return 20004;
1274
1275            case Types.DECIMAL :
1276            case Types.NUMERIC :
1277
1278// Integer.MAX_VALUE bit 2's complement number:
1279
// (Integer.MAX_VALUE-1) / ((ln(10)/ln(2)) bits per decimal digit)
1280
// See: java.math.BigInteger
1281
// - the other alternative is that we could report the numprecradix as 2 and
1282
// report Integer.MAX_VALUE here
1283
return 646456993;
1284
1285            case Types.DATE :
1286            case Types.INTEGER :
1287                return 10;
1288
1289            case Types.FLOAT :
1290            case Types.REAL :
1291            case Types.DOUBLE :
1292                return 17;
1293
1294            case Types.TIME :
1295                return 8;
1296
1297            case Types.SMALLINT :
1298                return 5;
1299
1300            case Types.TIMESTAMP :
1301                return 29;
1302
1303            case Types.TINYINT :
1304                return 3;
1305
1306            default :
1307                return 0;
1308        }
1309    }
1310
1311    public static String JavaDoc getColStClsName(int type) {
1312
1313        switch (type) {
1314
1315            case Types.BIGINT :
1316                return "java.lang.Long";
1317
1318            case Types.BINARY :
1319            case Types.LONGVARBINARY :
1320            case Types.VARBINARY :
1321
1322                // but wrapped by org.hsqldb.Binary
1323
return "[B";
1324
1325            case Types.OTHER :
1326
1327                // but wrapped by org.hsqldb.JavaObject
1328
return "java.lang.Object";
1329
1330            case Types.BOOLEAN :
1331                return "java.lang.Boolean";
1332
1333            case Types.CHAR :
1334            case Types.LONGVARCHAR :
1335            case Types.VARCHAR :
1336            case Types.XML : //?
1337
return "java.lang.String";
1338
1339            case Types.DATALINK :
1340                return "java.net.URL";
1341
1342            case Types.DATE :
1343                return DateClassName;
1344
1345            case Types.DECIMAL :
1346            case Types.NUMERIC :
1347                return DecimalClassName;
1348
1349            case Types.DOUBLE :
1350            case Types.FLOAT :
1351            case Types.REAL :
1352                return "java.lang.Double";
1353
1354            case Types.INTEGER :
1355            case Types.SMALLINT :
1356            case Types.TINYINT :
1357                return "java.lang.Integer";
1358
1359            case Types.TIME :
1360                return TimeClassName;
1361
1362            case Types.TIMESTAMP :
1363                return TimestampClassName;
1364
1365            default :
1366                return null;
1367        }
1368    }
1369}
1370
Popular Tags