/**
* Copyright (C) 2006 Robin Bygrave
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.util.codegen;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Map;
import com.avaje.ebean.util.DataTypes;
/**
* Creates a Map of Java Class types mapping to JDBC SQL Types.
* <p>
* Note that logical types are not db/jdbc types. So that means java.util.Date,
* java.util.Calendar and java.math.BigInteger.
* </p>
* <p>
* If may be useful to refer to <a
* href="http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/mapping.html">
* j2se/1.5.0/docs/guide/jdbc/getstart/mapping.html</a>
* </p>
*/
public class JdbcTypeMapFactory {
public static final int MATH_BIGINT = DataTypes.MATH_BIGINTEGER;
public static final int UTIL_DATE = DataTypes.UTIL_DATE;
public static final int UTIL_CALENDAR = DataTypes.UTIL_CALENDAR;
/**
* Adds logical types to the map including java.util.Date,
* java.util.Calendar and java.math.BigInteger.
*/
public static void addTypesLogical(Map map) {
// additional types mapped
map.put(BigInteger.class, MATH_BIGINT);
map.put(java.util.Date.class, UTIL_DATE);
map.put(java.util.Calendar.class, UTIL_CALENDAR);
}
/**
* Creates a Map of Java Class types mapping to JDBC SQL Types.
* <p>
* Note that Java types that are not in this map include....<br>
* <em>java.util.Date, java.util.Calendar and java.math.BigInteger</em>.
* </p>
*/
public static void addTypes(Map map) {
map.put(Boolean.class, Types.BOOLEAN);
map.put(boolean.class, Types.BOOLEAN);
map.put(char.class, Types.CHAR);
map.put(String.class, Types.VARCHAR);
map.put(byte.class, Types.TINYINT);
map.put(Byte.class, Types.TINYINT);
map.put(Short.class, Types.SMALLINT);
map.put(short.class, Types.SMALLINT);
map.put(Integer.class, Types.INTEGER);
map.put(int.class, Types.INTEGER);
map.put(Long.class, Types.BIGINT);
map.put(long.class, Types.BIGINT);
map.put(Float.class, Types.REAL);
map.put(float.class, Types.REAL);
map.put(Double.class, Types.DOUBLE);
map.put(double.class, Types.DOUBLE);
map.put(BigDecimal.class, Types.DECIMAL);
// Date based types
map.put(Date.class, Types.DATE);
map.put(Timestamp.class, Types.TIMESTAMP);
map.put(Time.class, Types.TIME);
map.put(byte[].class, Types.VARBINARY);
}
/**
* Add logical types keyed by integer and value of class.
*/
public static void addReverseLogical(Map map) {
// additional types mapped
map.put(MATH_BIGINT, BigInteger.class);
map.put(UTIL_DATE, java.util.Date.class);
map.put(UTIL_CALENDAR, java.util.Calendar.class);
}
/**
* Creates a map of java.sql.Types to Java Class types.
* <p>
* The key is of type Integer and the value is class.
* </p>
* <p>
* Note multiple SQL Types can map to a single Java type. For example
* NUMERIC and DECIMAL both map to BigDecimal.
* </p>
*/
public static void addReverse(Map map) {
map.put(java.sql.Types.BOOLEAN, Boolean.class);
map.put(java.sql.Types.CHAR, char.class);
map.put(java.sql.Types.VARCHAR, String.class);
map.put(java.sql.Types.LONGVARCHAR, String.class);
map.put(java.sql.Types.CLOB, String.class);
map.put(java.sql.Types.LONGVARBINARY, byte[].class);
map.put(java.sql.Types.VARBINARY, byte[].class);
map.put(java.sql.Types.BINARY, byte[].class);
map.put(java.sql.Types.BLOB, byte[].class);
map.put(java.sql.Types.TINYINT, Byte.class);
map.put(java.sql.Types.SMALLINT, Short.class);
map.put(java.sql.Types.INTEGER, Integer.class);
map.put(java.sql.Types.BIGINT, Long.class);
map.put(java.sql.Types.REAL, Float.class);
map.put(java.sql.Types.DOUBLE, Double.class);
map.put(java.sql.Types.FLOAT, Double.class);
map.put(java.sql.Types.NUMERIC, BigDecimal.class);
map.put(java.sql.Types.DECIMAL, BigDecimal.class);
map.put(java.sql.Types.DATE, Date.class);
map.put(java.sql.Types.TIMESTAMP, Timestamp.class);
map.put(java.sql.Types.TIME, Time.class);
}
}
|