1 /* Created on Jan 9, 2004 */
2 package com.quantum.util.sql;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.Modifier;
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.
13 * @see java.lang.Types
15 public class TypesHelper {
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;
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>.
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
53 public static final int DATALINK;
54 public static final int BOOLEAN;
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);
62 private static int getType(String typeName, int defaultValue) {
64 Field field = Types.class.getField(typeName);
65 defaultValue = field.getInt(null);
66 } catch (NoSuchFieldException e) {
67 } catch (IllegalAccessException e) {
72 public static String getTypeName(int type) {
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();
85 } catch (IllegalAccessException e) {
91 * True if the type is Real (numeric and with decimal part) according to java.sql.Types
95 public static boolean isReal(int type) {
96 return (type == DECIMAL || type == DOUBLE || type == FLOAT ||
97 type == NUMERIC || type == REAL );
101 * True if the type is Numeric according to java.sql.Types
105 public static boolean isNumeric(int type) {
106 return (type == DECIMAL || type == DOUBLE || type ==FLOAT ||
107 type == NUMERIC || type == REAL || type == BIGINT ||
108 type == TINYINT || type == SMALLINT || type == INTEGER );
112 * True if the type is textual according to java.sql.Types
116 public static boolean isText(int type) {
117 return (type == CLOB || type == VARBINARY || type ==VARCHAR
118 || type == CHAR || type == LONGVARCHAR );