preparing new release
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / util / sql / TypesHelper.java
index e8b87f6..b43c227 100644 (file)
@@ -2,10 +2,15 @@
 package com.quantum.util.sql;
 
 import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
 import java.sql.Types;
 
 /**
- * @author holmesbc
+ * This class provides some utilities for working with Types, as well as providing some
+ * support around SQL types that exist in certain versions of the JDK.
+ * 
+ * @author BC Holmes
+ * @see java.lang.Types
  */
 public class TypesHelper {
        
@@ -37,6 +42,14 @@ public class TypesHelper {
        public static final int BLOB = Types.BLOB;
        public static final int CLOB = Types.CLOB;
        public static final int REF = Types.REF;
+       /**
+        * <p>The constant in the Java programming language, somtimes referred to 
+        * as a type code, that identifies the generic SQL type <code>DATALINK</code>.
+        * 
+        * <p>Note: For some reason, some versions of DB2 and/or the DB2 driver use an invalid 
+        * type code for DATALINK.  The correct value should be 70, but DB2/NT 7.01.00 uses
+        * -400.
+        */
        public static final int DATALINK;
        public static final int BOOLEAN;
        
@@ -56,4 +69,53 @@ public class TypesHelper {
                return defaultValue;
        }
 
+       public static String getTypeName(int type) {
+               String name = null;
+               try {
+                       Field[] fields = TypesHelper.class.getFields();
+                       for (int i = 0, length = fields == null ? 0 : fields.length; 
+                                       name == null && i < length; i++) {
+                               if (Modifier.isStatic(fields[i].getModifiers()) 
+                                               && Modifier.isPublic(fields[i].getModifiers()) 
+                                               && fields[i].getType() == Integer.TYPE 
+                                               && type == fields[i].getInt(null)) {
+                                       name = fields[i].getName();
+                               }
+                       }
+               } catch (IllegalAccessException e) {
+               }
+               return name;
+       }
+
+       /**
+        * True if the type is Real (numeric and with decimal part) according to java.sql.Types
+        * @param type
+        * @return
+        */
+       public static boolean isReal(int type) {
+               return (type == DECIMAL || type == DOUBLE || type == FLOAT || 
+                       type == NUMERIC || type == REAL );
+       }
+
+       /**
+        * True if the type is Numeric according to java.sql.Types
+        * @param type
+        * @return
+        */
+       public static boolean isNumeric(int type) {
+               return (type == DECIMAL || type == DOUBLE || type ==FLOAT || 
+                       type == NUMERIC || type == REAL || type == BIGINT ||
+                       type == TINYINT || type == SMALLINT || type == INTEGER );
+       }
+
+       /**
+        * True if the type is textual according to java.sql.Types
+        * @param type
+        * @return
+        */
+       public static boolean isText(int type) {
+               return ( type == CHAR || type == VARCHAR || type == LONGVARCHAR
+                               || type == BINARY || type == VARBINARY || type == LONGVARBINARY 
+                                || type == CLOB || type == BLOB);
+       }
 }