db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/util JdbcTypesHelper.java
Date Fri, 19 Dec 2003 16:41:22 GMT
arminw      2003/12/19 08:41:22

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_BRANCH_1_0
                        JdbcTypesHelper.java
  Log:
  avoid use of long if-else statements to lookup JdbcType by name
  (synchronize with changes made in trunk)
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.2.2.1   +72 -130   db-ojb/src/java/org/apache/ojb/broker/util/JdbcTypesHelper.java
  
  Index: JdbcTypesHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/JdbcTypesHelper.java,v
  retrieving revision 1.2
  retrieving revision 1.2.2.1
  diff -u -r1.2 -r1.2.2.1
  --- JdbcTypesHelper.java	9 Dec 2003 19:24:02 -0000	1.2
  +++ JdbcTypesHelper.java	19 Dec 2003 16:41:22 -0000	1.2.2.1
  @@ -1,5 +1,6 @@
   package org.apache.ojb.broker.util;
   
  +import java.lang.reflect.Field;
   import java.math.BigDecimal;
   import java.net.URL;
   import java.sql.Blob;
  @@ -13,14 +14,13 @@
   import java.sql.Time;
   import java.sql.Timestamp;
   import java.sql.Types;
  -import java.util.Map;
   import java.util.HashMap;
  -import java.lang.reflect.Field;
  +import java.util.Map;
   
  +import org.apache.commons.lang.BooleanUtils;
   import org.apache.ojb.broker.OJBRuntimeException;
   import org.apache.ojb.broker.metadata.JdbcType;
   import org.apache.ojb.broker.util.sequence.SequenceManagerException;
  -import org.apache.commons.lang.BooleanUtils;
   
   /**
    * Helper class centralise all SQL Types operations and make
  @@ -31,40 +31,71 @@
    */
   public final class JdbcTypesHelper
   {
  -    private static Map jdbcObjectTypes = new HashMap();
  +    private static Map jdbcObjectTypesFromType = new HashMap();
  +    private static Map jdbcObjectTypesFromName = new HashMap();
   
       /*
  -    Hold out all JdbcType in a static map
  +    Hold out all JdbcType in a static maps
       */
       static
       {
  -        jdbcObjectTypes.put(new Integer(Types.ARRAY), new JdbcTypesHelper.T_Array());
  -        jdbcObjectTypes.put(new Integer(Types.BIGINT), new JdbcTypesHelper.T_BigInt());
  -        jdbcObjectTypes.put(new Integer(Types.BINARY), new JdbcTypesHelper.T_Binary());
  -        jdbcObjectTypes.put(new Integer(Types.BIT), new JdbcTypesHelper.T_Bit());
  -        jdbcObjectTypes.put(new Integer(Types.BLOB), new JdbcTypesHelper.T_Blob());
  -        jdbcObjectTypes.put(new Integer(Types.CHAR), new JdbcTypesHelper.T_Char());
  -        jdbcObjectTypes.put(new Integer(Types.CLOB), new JdbcTypesHelper.T_Clob());
  -        jdbcObjectTypes.put(new Integer(Types.DATE), new JdbcTypesHelper.T_Date());
  -        jdbcObjectTypes.put(new Integer(Types.DECIMAL), new JdbcTypesHelper.T_Decimal());
  -        jdbcObjectTypes.put(new Integer(Types.DOUBLE), new JdbcTypesHelper.T_Double());
  -        jdbcObjectTypes.put(new Integer(Types.FLOAT), new JdbcTypesHelper.T_Float());
  -        jdbcObjectTypes.put(new Integer(Types.INTEGER), new JdbcTypesHelper.T_Integer());
  -        jdbcObjectTypes.put(new Integer(Types.LONGVARBINARY), new JdbcTypesHelper.T_LongVarBinary());
  -        jdbcObjectTypes.put(new Integer(Types.LONGVARCHAR), new JdbcTypesHelper.T_LongVarChar());
  -        jdbcObjectTypes.put(new Integer(Types.NUMERIC), new JdbcTypesHelper.T_Numeric());
  -        jdbcObjectTypes.put(new Integer(Types.REAL), new JdbcTypesHelper.T_Real());
  -        jdbcObjectTypes.put(new Integer(Types.REF), new JdbcTypesHelper.T_Ref());
  -        jdbcObjectTypes.put(new Integer(Types.SMALLINT), new JdbcTypesHelper.T_SmallInt());
  -        jdbcObjectTypes.put(new Integer(Types.STRUCT), new JdbcTypesHelper.T_Struct());
  -        jdbcObjectTypes.put(new Integer(Types.TIME), new JdbcTypesHelper.T_Time());
  -        jdbcObjectTypes.put(new Integer(Types.TIMESTAMP), new JdbcTypesHelper.T_Timestamp());
  -        jdbcObjectTypes.put(new Integer(Types.TINYINT), new JdbcTypesHelper.T_TinyInt());
  -        jdbcObjectTypes.put(new Integer(Types.VARBINARY), new JdbcTypesHelper.T_VarBinary());
  -        jdbcObjectTypes.put(new Integer(Types.VARCHAR), new JdbcTypesHelper.T_Varchar());
  +        jdbcObjectTypesFromType.put(new Integer(Types.ARRAY), new JdbcTypesHelper.T_Array());
  +        jdbcObjectTypesFromType.put(new Integer(Types.BIGINT), new JdbcTypesHelper.T_BigInt());
  +        jdbcObjectTypesFromType.put(new Integer(Types.BINARY), new JdbcTypesHelper.T_Binary());
  +        jdbcObjectTypesFromType.put(new Integer(Types.BIT), new JdbcTypesHelper.T_Bit());
  +        jdbcObjectTypesFromType.put(new Integer(Types.BLOB), new JdbcTypesHelper.T_Blob());
  +        jdbcObjectTypesFromType.put(new Integer(Types.CHAR), new JdbcTypesHelper.T_Char());
  +        jdbcObjectTypesFromType.put(new Integer(Types.CLOB), new JdbcTypesHelper.T_Clob());
  +        jdbcObjectTypesFromType.put(new Integer(Types.DATE), new JdbcTypesHelper.T_Date());
  +        jdbcObjectTypesFromType.put(new Integer(Types.DECIMAL), new JdbcTypesHelper.T_Decimal());
  +        jdbcObjectTypesFromType.put(new Integer(Types.DOUBLE), new JdbcTypesHelper.T_Double());
  +        jdbcObjectTypesFromType.put(new Integer(Types.FLOAT), new JdbcTypesHelper.T_Float());
  +        jdbcObjectTypesFromType.put(new Integer(Types.INTEGER), new JdbcTypesHelper.T_Integer());
  +        jdbcObjectTypesFromType.put(new Integer(Types.LONGVARBINARY), new JdbcTypesHelper.T_LongVarBinary());
  +        jdbcObjectTypesFromType.put(new Integer(Types.LONGVARCHAR), new JdbcTypesHelper.T_LongVarChar());
  +        jdbcObjectTypesFromType.put(new Integer(Types.NUMERIC), new JdbcTypesHelper.T_Numeric());
  +        jdbcObjectTypesFromType.put(new Integer(Types.REAL), new JdbcTypesHelper.T_Real());
  +        jdbcObjectTypesFromType.put(new Integer(Types.REF), new JdbcTypesHelper.T_Ref());
  +        jdbcObjectTypesFromType.put(new Integer(Types.SMALLINT), new JdbcTypesHelper.T_SmallInt());
  +        jdbcObjectTypesFromType.put(new Integer(Types.STRUCT), new JdbcTypesHelper.T_Struct());
  +        jdbcObjectTypesFromType.put(new Integer(Types.TIME), new JdbcTypesHelper.T_Time());
  +        jdbcObjectTypesFromType.put(new Integer(Types.TIMESTAMP), new JdbcTypesHelper.T_Timestamp());
  +        jdbcObjectTypesFromType.put(new Integer(Types.TINYINT), new JdbcTypesHelper.T_TinyInt());
  +        jdbcObjectTypesFromType.put(new Integer(Types.VARBINARY), new JdbcTypesHelper.T_VarBinary());
  +        jdbcObjectTypesFromType.put(new Integer(Types.VARCHAR), new JdbcTypesHelper.T_Varchar());
  +//#ifdef JDBC30
  +        jdbcObjectTypesFromType.put(new Integer(Types.BOOLEAN), new JdbcTypesHelper.T_Boolean());
  +        jdbcObjectTypesFromType.put(new Integer(Types.DATALINK), new JdbcTypesHelper.T_Datalink());
  +//#endif
  +
  +
  +        jdbcObjectTypesFromName.put("array", jdbcObjectTypesFromType.get(new Integer(Types.ARRAY)));
  +        jdbcObjectTypesFromName.put("bigint", jdbcObjectTypesFromType.get(new Integer(Types.BIGINT)));
  +        jdbcObjectTypesFromName.put("binary", jdbcObjectTypesFromType.get(new Integer(Types.BINARY)));
  +        jdbcObjectTypesFromName.put("bit", jdbcObjectTypesFromType.get(new Integer(Types.BIT)));
  +        jdbcObjectTypesFromName.put("blob", jdbcObjectTypesFromType.get(new Integer(Types.BLOB)));
  +        jdbcObjectTypesFromName.put("char", jdbcObjectTypesFromType.get(new Integer(Types.CHAR)));
  +        jdbcObjectTypesFromName.put("clob", jdbcObjectTypesFromType.get(new Integer(Types.CLOB)));
  +        jdbcObjectTypesFromName.put("date", jdbcObjectTypesFromType.get(new Integer(Types.DATE)));
  +        jdbcObjectTypesFromName.put("decimal", jdbcObjectTypesFromType.get(new Integer(Types.DECIMAL)));
  +        jdbcObjectTypesFromName.put("double", jdbcObjectTypesFromType.get(new Integer(Types.DOUBLE)));
  +        jdbcObjectTypesFromName.put("float", jdbcObjectTypesFromType.get(new Integer(Types.FLOAT)));
  +        jdbcObjectTypesFromName.put("integer", jdbcObjectTypesFromType.get(new Integer(Types.INTEGER)));
  +        jdbcObjectTypesFromName.put("longvarbinary", jdbcObjectTypesFromType.get(new Integer(Types.LONGVARBINARY)));
  +        jdbcObjectTypesFromName.put("longvarchar", jdbcObjectTypesFromType.get(new Integer(Types.LONGVARCHAR)));
  +        jdbcObjectTypesFromName.put("numeric", jdbcObjectTypesFromType.get(new Integer(Types.NUMERIC)));
  +        jdbcObjectTypesFromName.put("real", jdbcObjectTypesFromType.get(new Integer(Types.REAL)));
  +        jdbcObjectTypesFromName.put("ref", jdbcObjectTypesFromType.get(new Integer(Types.REF)));
  +        jdbcObjectTypesFromName.put("smallint", jdbcObjectTypesFromType.get(new Integer(Types.SMALLINT)));
  +        jdbcObjectTypesFromName.put("struct", jdbcObjectTypesFromType.get(new Integer(Types.STRUCT)));
  +        jdbcObjectTypesFromName.put("time", jdbcObjectTypesFromType.get(new Integer(Types.TIME)));
  +        jdbcObjectTypesFromName.put("timestamp", jdbcObjectTypesFromType.get(new Integer(Types.TIMESTAMP)));
  +        jdbcObjectTypesFromName.put("tinyint", jdbcObjectTypesFromType.get(new Integer(Types.TINYINT)));
  +        jdbcObjectTypesFromName.put("varbinary", jdbcObjectTypesFromType.get(new Integer(Types.VARBINARY)));
  +        jdbcObjectTypesFromName.put("varchar", jdbcObjectTypesFromType.get(new Integer(Types.VARCHAR)));
   //#ifdef JDBC30
  -        jdbcObjectTypes.put(new Integer(Types.BOOLEAN), new JdbcTypesHelper.T_Boolean());
  -        jdbcObjectTypes.put(new Integer(Types.DATALINK), new JdbcTypesHelper.T_Datalink());
  +        jdbcObjectTypesFromName.put("boolean", jdbcObjectTypesFromType.get(new Integer(Types.BOOLEAN)));
  +        jdbcObjectTypesFromName.put("datalink", jdbcObjectTypesFromType.get(new Integer(Types.DATALINK)));
   //#endif
       }
   
  @@ -73,7 +104,7 @@
        */
       public static JdbcType getJdbcTypeByTypesIndex(Integer type)
       {
  -        return (JdbcType) jdbcObjectTypes.get(type);
  +        return (JdbcType) jdbcObjectTypesFromType.get(type);
       }
   
       /**
  @@ -82,100 +113,13 @@
        */
       public static JdbcType getJdbcTypeByName(String typeName)
       {
  -        JdbcType result;
  -        String type = typeName.toLowerCase();
  -
  -        if (type.equals("char"))
  -            //result = new T_Char();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.CHAR));
  -        else if (type.equals("varchar"))
  -            //result = new T_Varchar();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.VARCHAR));
  -        else if (type.equals("longvarchar"))
  -            //result = new T_LongVarChar();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.LONGVARCHAR));
  -        else if (type.equals("numeric"))
  -            //result = new T_Numeric();
  -        result = getJdbcTypeByTypesIndex(new Integer(Types.NUMERIC));
  -        else if (type.equals("decimal"))
  -            //result = new T_Decimal();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.DECIMAL));
  -        else if (type.equals("bit"))
  -            //result = new T_Bit();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.BIT));
  -//#ifdef JDBC30
  -        else if (type.equals("boolean"))
  -            //result = new T_Boolean();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.BOOLEAN));
  -//#endif
  -        else if (type.equals("tinyint"))
  -            //result = new T_TinyInt();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.TINYINT));
  -        else if (type.equals("smallint"))
  -            //result = new T_SmallInt();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.SMALLINT));
  -        else if (type.equals("integer"))
  -            //result = new T_Integer();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.INTEGER));
  -        else if (type.equals("bigint"))
  -            //result = new T_BigInt();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.BIGINT));
  -        else if (type.equals("real"))
  -            //result = new T_Real();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.REAL));
  -        else if (type.equals("float"))
  -            //result = new T_Float();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.FLOAT));
  -        else if (type.equals("double"))
  -            //result = new T_Double();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.DOUBLE));
  -        else if (type.equals("binary"))
  -            //result = new T_Binary();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.BINARY));
  -        else if (type.equals("varbinary"))
  -            //result = new T_VarBinary();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.VARBINARY));
  -        else if (type.equals("longvarbinary"))
  -            //result = new T_LongVarBinary();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.LONGVARBINARY));
  -        else if (type.equals("date"))
  -            //result = new T_Date();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.DATE));
  -        else if (type.equals("time"))
  -            //result = new T_Time();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.TIME));
  -        else if (type.equals("timestamp"))
  -            //result = new T_Timestamp();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.TIMESTAMP));
  -        else if (type.equals("clob"))
  -            //result = new T_Clob();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.CLOB));
  -        else if (type.equals("blob"))
  -            //result = new T_Blob();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.BLOB));
  -//        else if (type.equals("distinct"))
  -//            NOT SUPPORTED
  -//        else if (type.equals("javaobject"))
  -//            NOT SUPPORTED
  -        else if (type.equals("array"))
  -            //result = new T_Array();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.ARRAY));
  -        else if (type.equals("struct"))
  -            //result = new T_Struct();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.STRUCT));
  -        else if (type.equals("ref"))
  -            //result = new T_Ref();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.REF));
  -//#ifdef JDBC30
  -        else if (type.equals("datalink"))
  -            //result = new T_Datalink();
  -            result = getJdbcTypeByTypesIndex(new Integer(Types.DATALINK));
  -//#endif
  -        else
  -            throw new OJBRuntimeException(
  -                    "The type "
  -                    + typeName
  -                    + " can not be handled by OJB. Please specify only types as defined
by java.sql.Types.");
  +        JdbcType result = null;
  +        result = (JdbcType) jdbcObjectTypesFromName.get(typeName.toLowerCase());
  +        if(result == null)
  +        {
  +            throw new OJBRuntimeException("The type " + typeName + " can not be handled
by OJB." +
  +                    " Please specify only types as defined by java.sql.Types.");
  +        }
           return result;
       }
   
  @@ -230,9 +174,7 @@
   //#endif
           else
               throw new OJBRuntimeException(
  -                    "The type "
  -                    + fieldType
  -                    + " can not be handled by OJB automatically."
  +                    "The type " + fieldType + " can not be handled by OJB automatically."
                       + " Please specify a type as defined by java.sql.Types in your field-descriptor");
           return result;
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message