feed61eb0904969b8c1509d7ae4e9e8a199df768
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / sql / TypesHelper.java
1 /* Created on Jan 9, 2004 */
2 package com.quantum.util.sql;
3
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Modifier;
6 import java.sql.Types;
7
8 /**
9  * This class provides some utilities for working with Types, as well as providing some
10  * support around SQL types that exist in certain versions of the JDK.
11  * 
12  * @author BC Holmes
13  * @see java.lang.Types
14  */
15 public class TypesHelper {
16         
17         public static final int BIT = Types.BIT;
18         public static final int TINYINT = Types.TINYINT;
19         public static final int SMALLINT = Types.SMALLINT;
20         public static final int INTEGER = Types.INTEGER;
21         public static final int BIGINT = Types.BIGINT;
22         public static final int FLOAT = Types.FLOAT;
23         public static final int REAL = Types.REAL;
24         public static final int DOUBLE = Types.DOUBLE;
25         public static final int NUMERIC = Types.NUMERIC;
26         public static final int DECIMAL = Types.DECIMAL;
27         public static final int CHAR = Types.CHAR;
28         public static final int VARCHAR = Types.VARCHAR;
29         public static final int LONGVARCHAR = Types.LONGVARCHAR;
30         public static final int DATE = Types.DATE;
31         public static final int TIME = Types.TIME;
32         public static final int TIMESTAMP = Types.TIMESTAMP;
33         public static final int BINARY = Types.BINARY;
34         public static final int VARBINARY = Types.VARBINARY;
35         public static final int LONGVARBINARY = Types.LONGVARBINARY;
36         public static final int NULL = Types.NULL;
37         public static final int OTHER = Types.OTHER;
38         public static final int JAVA_OBJECT = Types.JAVA_OBJECT;
39         public static final int DISTINCT = Types.DISTINCT;
40         public static final int STRUCT = Types.STRUCT;
41         public static final int ARRAY = Types.ARRAY;
42         public static final int BLOB = Types.BLOB;
43         public static final int CLOB = Types.CLOB;
44         public static final int REF = Types.REF;
45         /**
46          * <p>The constant in the Java programming language, somtimes referred to 
47          * as a type code, that identifies the generic SQL type <code>DATALINK</code>.
48          * 
49          * <p>Note: For some reason, some versions of DB2 and/or the DB2 driver use an invalid 
50          * type code for DATALINK.  The correct value should be 70, but DB2/NT 7.01.00 uses
51          * -400.
52          */
53         public static final int DATALINK;
54         public static final int BOOLEAN;
55         
56         static {
57                 // These fields only exist in the JDK 1.4 version of the Types class.
58                 BOOLEAN = getType("BOOLEAN", 16);
59                 DATALINK = getType("DATALINK", 70);
60         }
61         
62         private static int getType(String typeName, int defaultValue) {
63                 try {
64                         Field field = Types.class.getField(typeName);
65                         defaultValue = field.getInt(null);
66                 } catch (NoSuchFieldException e) {
67                 } catch (IllegalAccessException e) {
68                 }
69                 return defaultValue;
70         }
71
72         public static String getTypeName(int type) {
73                 String name = null;
74                 try {
75                         Field[] fields = TypesHelper.class.getFields();
76                         for (int i = 0, length = fields == null ? 0 : fields.length; 
77                                         name == null && i < length; i++) {
78                                 if (Modifier.isStatic(fields[i].getModifiers()) 
79                                                 && Modifier.isPublic(fields[i].getModifiers()) 
80                                                 && fields[i].getType() == Integer.TYPE 
81                                                 && type == fields[i].getInt(null)) {
82                                         name = fields[i].getName();
83                                 }
84                         }
85                 } catch (IllegalAccessException e) {
86                 }
87                 return name;
88         }
89
90 }