View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/11/24 18:54:22
    6    */
    7   package org.asyrinx.joey.gen.jdbc;
    8
    9   import java.sql.Types;
    10  import java.util.HashMap;
    11
    12  import org.asyrinx.brownie.core.collection.IntegerKeyMap;
    13  import org.asyrinx.brownie.core.collection.MapUtils;
    14
    15  /***
    16   * @author takeshi
    17   */
    18  public class JdbcUtils {
    19
    20      public static final int UNKNOWN_TYPE = 99999;
    21
    22      /***
    23       *
    24       */
    25      public JdbcUtils() {
    26          super();
    27      }
    28
    29      static final IntegerKeyMap type2name;
    30
    31      static final IntValueMap name2type;
    32      static {
    33          type2name = MapUtils.toIntegerKeyMap(new HashMap());
    34          name2type = new IntValueMap(UNKNOWN_TYPE);
    35          //
    36          add(Types.BOOLEAN, "BOOLEAN");
    37          add(Types.BIT, "BIT");
    38          add(Types.TINYINT, "TINYINT");
    39          add(Types.SMALLINT, "SMALLINT");
    40          add(Types.INTEGER, "INTEGER");
    41          add(Types.BIGINT, "BIGINT");
    42          add(Types.FLOAT, "FLOAT");
    43          add(Types.REAL, "REAL");
    44          add(Types.DOUBLE, "DOUBLE");
    45          add(Types.NUMERIC, "NUMERIC");
    46          add(Types.DECIMAL, "DECIMAL");
    47          add(Types.CHAR, "CHAR");
    48          add(Types.VARCHAR, "VARCHAR");
    49          add(Types.LONGVARCHAR, "LONGVARCHAR");
    50          add(Types.DATE, "DATE");
    51          add(Types.TIME, "TIME");
    52          add(Types.TIMESTAMP, "TIMESTAMP");
    53          add(Types.BINARY, "BINARY");
    54          add(Types.VARBINARY, "VARBINARY");
    55          add(Types.LONGVARBINARY, "LONGVARBINARY");
    56          add(Types.NULL, "NULL");
    57          add(Types.OTHER, "OTHER");
    58          add(Types.JAVA_OBJECT, "JAVA_OBJECT");
    59          add(Types.DISTINCT, "DISTINCT");
    60          add(Types.STRUCT, "STRUCT");
    61          add(Types.ARRAY, "ARRAY");
    62          add(Types.BLOB, "BLOB");
    63          add(Types.CLOB, "CLOB");
    64          add(Types.REF, "REF");
    65          add(Types.DATALINK, "DATALINK");
    66          add(Types.BOOLEAN, "BOOLEAN");
    67      }
    68
    69      private static final void add(int jdbcType, String jdbcTypeName) {
    70          type2name.put(jdbcType, jdbcTypeName);
    71          name2type.put(jdbcTypeName, jdbcType);
    72      }
    73
    74      public static String toJdbcTypeName(int jdbcType) {
    75          return (String) type2name.get(jdbcType);
    76      }
    77
    78      public static int toJdbcType(String jdbcTypeName) {
    79          return name2type.getAsInt(jdbcTypeName);
    80      }
    81
    82  }
    83
    84  class IntValueMap extends HashMap {
    85      public IntValueMap(int unknown) {
    86          super();
    87          this.unknown = unknown;
    88      }
    89
    90      private final int unknown;
    91
    92      public void put(Object key, int value) {
    93          super.put(key, new Integer(value));
    94      }
    95
    96      public int getAsInt(Object key) {
    97          final Object result = super.get(key);
    98          if (result instanceof Number) {
    99              return ((Number) result).intValue();
    100         } else {
    101             return unknown;
    102         }
    103     }
    104
    105 }