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 17:58:41
    6    */
    7   package org.asyrinx.joey.gen.hibernate;
    8
    9   import java.sql.Types;
    10  import java.util.HashMap;
    11  import java.util.Map;
    12
    13  import net.sf.hibernate.Hibernate;
    14  import net.sf.hibernate.type.NullableType;
    15
    16  import org.apache.commons.lang.StringUtils;
    17  import org.asyrinx.joey.gen.jdbc.JdbcUtils;
    18  import org.asyrinx.joey.gen.model.rdb.Column;
    19
    20  /***
    21   * @author takeshi
    22   */
    23  public class HibernateUtils {
    24
    25      /***
    26       *
    27       */
    28      public HibernateUtils() {
    29          super();
    30      }
    31
    32      public static NullableType getHibernateType(String sqlTypeName, int columnSize, int decimalDigits) {
    33          final int sqlType = JdbcUtils.toJdbcType(sqlTypeName);
    34          return (sqlType == JdbcUtils.UNKNOWN_TYPE) ? null : getHibernateType(sqlType, columnSize, decimalDigits);
    35      }
    36
    37      public static NullableType getHibernateType(int sqlType, int columnSize, int decimalDigits) {
    38          //logger.debug("sqlType=" + sqlType);
    39          //logger.debug("columnSize=" + columnSize);
    40          //logger.debug("decimalDigits=" + decimalDigits);
    41          NullableType rv = Hibernate.SERIALIZABLE;
    42          if (sqlType == Types.CHAR || sqlType == Types.VARCHAR) {
    43              rv = Hibernate.STRING;
    44          } else if (sqlType == Types.LONGVARCHAR) {
    45              rv = Hibernate.TEXT;
    46          } else if (sqlType == Types.BLOB) {
    47              rv = Hibernate.BLOB;
    48          } else if (sqlType == Types.CLOB) {
    49              rv = Hibernate.CLOB;
    50          } else if (sqlType == Types.FLOAT || sqlType == Types.REAL) {
    51              rv = Hibernate.FLOAT;
    52          } else if (sqlType == Types.INTEGER) {
    53              rv = Hibernate.INTEGER;
    54          } else if (sqlType == Types.DOUBLE) {
    55              rv = Hibernate.DOUBLE;
    56          } else if (sqlType == Types.DATE) {
    57              rv = Hibernate.DATE;
    58          } else if (sqlType == Types.TIMESTAMP) {
    59              rv = Hibernate.TIMESTAMP;
    60          } else if (sqlType == Types.TIME) {
    61              rv = Hibernate.TIME;
    62          } else if (sqlType == Types.BOOLEAN) {
    63              rv = Hibernate.BOOLEAN;
    64          } else if (sqlType == Types.SMALLINT) {
    65              rv = Hibernate.SHORT;
    66          } else if (sqlType == Types.BIT) {
    67              rv = Hibernate.BOOLEAN;
    68          } else if (sqlType == Types.BIGINT) {
    69              rv = Hibernate.LONG;
    70          } else if (sqlType == Types.NUMERIC || sqlType == Types.DECIMAL) {
    71              if (decimalDigits == 0) {
    72                  if (columnSize == 1) {
    73                      rv = Hibernate.BYTE;
    74                  } else if (columnSize < 5) {
    75                      rv = Hibernate.SHORT;
    76                  } else if (columnSize < 10) {
    77                      rv = Hibernate.INTEGER;
    78                  } else {
    79                      rv = Hibernate.LONG;
    80                  }
    81              } else {
    82                  if (columnSize < 9) {
    83                      rv = Hibernate.FLOAT;
    84                  } else {
    85                      rv = Hibernate.DOUBLE;
    86                  }
    87              }
    88          }
    89          return rv;
    90      }
    91
    92      public static NullableType getHibernateType(Column column) {
    93          return getHibernateType(column.getJdbcType(), column.getSizeAsInt(), column.getDeciamlSizeAsInt());
    94      }
    95
    96      public static String toHibernateCascade(String cascade) {
    97          if (StringUtils.isEmpty(cascade))
    98              return "none";
    99          if ("none".equals(cascade))
    100             return "none";
    101         if ("all".equals(cascade))
    102             return "all";
    103         if ("update".equals(cascade))
    104             return "save-update";
    105         if ("delete".equals(cascade))
    106             return "delete";
    107         return "none";
    108     }
    109
    110     public static String getHibernateTypeName(Column column) {
    111         final NullableType type = getHibernateType(column);
    112         return getHibernateTypeName(type);
    113     }
    114
    115     public static String getHibernateTypeName(NullableType type) {
    116         return (String) hibernateType2Name.get(type);
    117     }
    118
    119     private static final Map hibernateType2Name = newHibernateType2Name();
    120
    121     /***
    122      * @return
    123      */
    124     private static Map newHibernateType2Name() {
    125         final Map result = new HashMap();
    126         result.put(Hibernate.LONG, "LONG");
    127         result.put(Hibernate.SHORT, "SHORT");
    128         result.put(Hibernate.INTEGER, "INTEGER");
    129         result.put(Hibernate.BYTE, "BYTE");
    130         result.put(Hibernate.FLOAT, "FLOAT");
    131         result.put(Hibernate.DOUBLE, "DOUBLE");
    132         result.put(Hibernate.CHARACTER, "CHARACTER");
    133         result.put(Hibernate.STRING, "STRING");
    134         result.put(Hibernate.TIME, "TIME");
    135         result.put(Hibernate.DATE, "DATE");
    136         result.put(Hibernate.TIMESTAMP, "TIMESTAMP");
    137         result.put(Hibernate.BOOLEAN, "BOOLEAN");
    138         result.put(Hibernate.TRUE_FALSE, "TRUE_FALSE");
    139         result.put(Hibernate.YES_NO, "YES_NO");
    140         result.put(Hibernate.BIG_DECIMAL, "BIG_DECIMAL");
    141         result.put(Hibernate.BINARY, "BINARY");
    142         result.put(Hibernate.TEXT, "TEXT");
    143         result.put(Hibernate.BLOB, "BLOB");
    144         result.put(Hibernate.CLOB, "CLOB");
    145         result.put(Hibernate.CALENDAR, "CALENDAR");
    146         result.put(Hibernate.CALENDAR_DATE, "CALENDAR_DATE");
    147         result.put(Hibernate.LOCALE, "LOCALE");
    148         result.put(Hibernate.CURRENCY, "CURRENCY");
    149         result.put(Hibernate.TIMEZONE, "TIMEZONE");
    150         result.put(Hibernate.CLASS, "CLASS");
    151         result.put(Hibernate.SERIALIZABLE, "SERIALIZABLE");
    152         //result.put(Hibernate.OBJECT, "OBJECT");
    153         return result;
    154     }
    155
    156 }