You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2003/08/13 20:08:53 UTC
cvs commit: db-ojb/src/java/org/apache/ojb/broker/util SqlTypeHelper.java
arminw 2003/08/13 11:08:53
Added: src/java/org/apache/ojb/broker/util SqlTypeHelper.java
Log:
add class to centralize all sql/java type
mapping
Revision Changes Path
1.1 db-ojb/src/java/org/apache/ojb/broker/util/SqlTypeHelper.java
Index: SqlTypeHelper.java
===================================================================
package org.apache.ojb.broker.util;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache ObjectRelationalBridge" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import org.apache.ojb.broker.util.sequence.SequenceManagerException;
import org.apache.ojb.broker.OJBRuntimeException;
import java.sql.Ref;
import java.sql.Types;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
import java.sql.Blob;
import java.sql.Clob;
import java.math.BigDecimal;
import java.net.URL;
/**
* Helper class centralise all SQL Types operations to
* make it easier to deal with different J2SDK/JDBC versions
* and fix mapping bugs.
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: SqlTypeHelper.java,v 1.1 2003/08/13 18:08:53 arminw Exp $
*/
public class SqlTypeHelper
{
private static final int MIN_INT = Integer.MIN_VALUE;
public static int getSqlTypeByName(String typeName)
{
int result;
String type = typeName.toLowerCase();
if (type.equals("bit"))
result = Types.BIT;
else if (type.equals("tinyint"))
result = Types.TINYINT;
else if (type.equals("smallint"))
result = Types.SMALLINT;
else if (type.equals("integer"))
result = Types.INTEGER;
else if (type.equals("bigint"))
result = Types.BIGINT;
else if (type.equals("float"))
result = Types.FLOAT;
else if (type.equals("real"))
result = Types.REAL;
else if (type.equals("double"))
result = Types.DOUBLE;
else if (type.equals("numeric"))
result = Types.NUMERIC;
else if (type.equals("decimal"))
result = Types.DECIMAL;
else if (type.equals("char"))
result = Types.CHAR;
else if (type.equals("varchar"))
result = Types.VARCHAR;
else if (type.equals("longvarchar"))
result = Types.LONGVARCHAR;
else if (type.equals("date"))
result = Types.DATE;
else if (type.equals("time"))
result = Types.TIME;
else if (type.equals("timestamp"))
result = Types.TIMESTAMP;
else if (type.equals("binary"))
result = Types.BINARY;
else if (type.equals("varbinary"))
result = Types.VARBINARY;
else if (type.equals("longvarbinary"))
result = Types.LONGVARBINARY;
else if (type.equals("clob"))
result = Types.CLOB;
else if (type.equals("blob"))
result = Types.BLOB;
else if (type.equals("struct"))
result = Types.STRUCT;
else if (type.equals("ref"))
result = Types.REF;
else if (type.equals("array"))
result = Types.ARRAY;
//#ifdef JDBC30
else if (type.equals("boolean"))
result = Types.BOOLEAN;
else if (type.equals("datalink"))
result = 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.");
// System.out.println("### field: " + typeName + ", columnType: " + result);
return result;
}
public static int getSqlTypeByReflection(String fieldType)
{
int result;
if (fieldType.equals(Character.class.getName()) || fieldType.equals("char"))
result = Types.CHAR;
else if (fieldType.equals(Short.class.getName()) || fieldType.equals("short"))
result = Types.SMALLINT;
else if (fieldType.equals(Integer.class.getName()) || fieldType.equals("int"))
result = Types.INTEGER;
else if (fieldType.equals(Long.class.getName()) || fieldType.equals("long"))
result = Types.BIGINT;
else if (fieldType.equals(Byte.class.getName()) || fieldType.equals("byte"))
result = Types.CHAR;
else if (fieldType.equals(Float.class.getName()) || fieldType.equals("float"))
result = Types.FLOAT;
else if (fieldType.equals(Double.class.getName()) || fieldType.equals("double"))
result = Types.DOUBLE;
else if (fieldType.equals(String.class.getName()))
result = Types.VARCHAR;
else if (fieldType.equals(java.util.Date.class.getName()))
result = Types.DATE;
else if (fieldType.equals(java.sql.Time.class.getName()))
result = Types.TIME;
else if (fieldType.equals(Timestamp.class.getName()))
result = Types.TIMESTAMP;
else if (fieldType.equals(java.sql.Date.class.getName()))
result = Types.DATE;
else if (fieldType.equals(BigDecimal.class.getName()))
result = Types.DECIMAL;
else if (fieldType.equals(Ref.class.getName()))
result = Types.REF;
else if (fieldType.equals(Struct.class.getName()))
result = Types.STRUCT;
//#ifdef JDBC30
else if (fieldType.equals(Boolean.class.getName()) || fieldType.equals("boolean"))
result = Types.BOOLEAN;
else if (fieldType.equals(URL.class.getName()))
result = Types.DATALINK;
//#endif
else
throw new OJBRuntimeException(
"The type "
+ fieldType
+ " can not be handled by OJB automatically."
+ "Please specify a type as defined by java.sql.Types in your field-descriptor");
// System.out.println("field: " + fieldType + " result: " + getJdbcTypeAsString(result));
return result;
}
/**
* Convert the given {@link java.lang.Long} value to
* a java object representation of the given jdbc type.
*/
public static Object sequenceKeyConversion(int jdbcType, Long identifier) throws SequenceManagerException
{
Object result = null;
switch (jdbcType)
{
case Types.ARRAY:
{
Object[] arr = {identifier.toString()};
result = arr;
break;
}
case Types.BIGINT:
{
result = identifier;
break;
}
case Types.BINARY:
{
result = identifier.toString().getBytes();
break;
}
case Types.CHAR:
{
result = identifier.toString();
break;
}
case Types.DATE:
{
result = new Date(identifier.longValue());
break;
}
case Types.DECIMAL:
{
result = new BigDecimal(identifier.longValue());
break;
}
case Types.DOUBLE:
{
result = new Double(identifier.doubleValue());
break;
}
case Types.FLOAT:
{
result = new Double(identifier.doubleValue());
break;
}
case Types.INTEGER:
{
result = new Integer(identifier.intValue());
break;
}
case Types.LONGVARBINARY:
{
result = identifier.toString().getBytes();
break;
}
case Types.LONGVARCHAR:
{
result = identifier.toString();
break;
}
case Types.NUMERIC:
{
result = new BigDecimal(identifier.longValue());
break;
}
case Types.REAL:
{
result = new Float(identifier.floatValue());
break;
}
case Types.SMALLINT:
{
result = new Short(identifier.shortValue());
break;
}
case Types.TIME:
{
result = new Time(identifier.longValue());
break;
}
case Types.TIMESTAMP:
{
result = new Timestamp(identifier.longValue());
break;
}
case Types.TINYINT:
{
result = new Byte(identifier.byteValue());
break;
}
case Types.VARBINARY:
{
result = identifier.toString().getBytes();
break;
}
case Types.VARCHAR:
{
result = identifier.toString();
break;
}
default :
{
throw new SequenceManagerException(
"Not supported sequence key type '"
+ getJdbcTypeAsString(jdbcType)
+ "' (int: "
+ jdbcType
+ ")");
}
}
return result;
}
/**
* Returns an java object read from the specified ResultSet column.
*/
public static Object getObjectFromColumn(ResultSet rs, int jdbcType, String columnName)
throws SQLException
{
return getObjectFromColumn(rs, jdbcType, columnName, MIN_INT);
}
/**
* Returns an java object read from the specified ResultSet column.
*/
public static Object getObjectFromColumn(ResultSet rs, int jdbcType, int columnId) throws SQLException
{
return getObjectFromColumn(rs, jdbcType, null, columnId);
}
/**
* Returns an java object read from the specified ResultSet column.
* If argument columnName is null, the columnId argument was used
* to find the column.
*/
private static Object getObjectFromColumn(ResultSet rs, int jdbcType, String columnName, int columnId)
throws SQLException
{
Object result = null;
switch (jdbcType)
{
case Types.BIT:
{
boolean boolVal = columnId == MIN_INT ? rs.getBoolean(columnName) : rs.getBoolean(columnId);
result = (rs.wasNull() ? null : new Boolean(boolVal));
break;
}
case Types.TINYINT:
{
byte byteVal = columnId == MIN_INT ? rs.getByte(columnName) : rs.getByte(columnId);
result = (rs.wasNull() ? null : new Byte(byteVal));
break;
}
case Types.SMALLINT:
{
short shortVal = columnId == MIN_INT ? rs.getShort(columnName) : rs.getShort(columnId);
result = (rs.wasNull() ? null : new Short(shortVal));
break;
}
case Types.INTEGER:
{
int intVal = columnId == MIN_INT ? rs.getInt(columnName) : rs.getInt(columnId);
result = (rs.wasNull() ? null : new Integer(intVal));
break;
}
case Types.BIGINT:
{
long longVal = columnId == MIN_INT ? rs.getLong(columnName) : rs.getLong(columnId);
result = (rs.wasNull() ? null : new Long(longVal));
break;
}
case Types.DOUBLE:
case Types.FLOAT:
{
double doubleVal = columnId == MIN_INT ? rs.getDouble(columnName) : rs.getDouble(columnId);
result = (rs.wasNull() ? null : new Double(doubleVal));
break;
}
case Types.REAL:
{
float floatVal = columnId == MIN_INT ? rs.getFloat(columnName) : rs.getFloat(columnId);
result = (rs.wasNull() ? null : new Float(floatVal));
break;
}
case Types.NUMERIC:
{
result = columnId == MIN_INT ? rs.getBigDecimal(columnName) : rs.getBigDecimal(columnId);
break;
}
case Types.DECIMAL:
{
result = columnId == MIN_INT ? rs.getBigDecimal(columnName) : rs.getBigDecimal(columnId);
break;
}
case Types.CHAR:
{
result = columnId == MIN_INT ? rs.getString(columnName) : rs.getString(columnId);
break;
}
case Types.VARCHAR:
{
result = columnId == MIN_INT ? rs.getString(columnName) : rs.getString(columnId);
break;
}
case Types.LONGVARCHAR:
{
result = columnId == MIN_INT ? rs.getString(columnName) : rs.getString(columnId);
break;
}
case Types.DATE:
{
result = columnId == MIN_INT ? rs.getDate(columnName) : rs.getDate(columnId);
break;
}
case Types.TIME:
{
result = columnId == MIN_INT ? rs.getTime(columnName) : rs.getTime(columnId);
break;
}
case Types.TIMESTAMP:
{
result = columnId == MIN_INT ? rs.getTimestamp(columnName) : rs.getTimestamp(columnId);
break;
}
case Types.BINARY:
{
result = columnId == MIN_INT ? rs.getBytes(columnName) : rs.getBytes(columnId);
break;
}
case Types.VARBINARY:
{
result = columnId == MIN_INT ? rs.getBytes(columnName) : rs.getBytes(columnId);
break;
}
case Types.LONGVARBINARY:
{
result = columnId == MIN_INT ? rs.getBytes(columnName) : rs.getBytes(columnId);
break;
}
case Types.CLOB:
{
Clob aClob = columnId == MIN_INT ? rs.getClob(columnName) : rs.getClob(columnId);
result = (rs.wasNull() ? null : aClob.getSubString(1L, (int) aClob.length()));
break;
}
case Types.BLOB:
{
Blob aBlob = columnId == MIN_INT ? rs.getBlob(columnName) : rs.getBlob(columnId);
result = (rs.wasNull() ? null : aBlob.getBytes(1L, (int) aBlob.length()));
break;
}
case Types.STRUCT:
{
result = columnId == MIN_INT ? rs.getObject(columnName) : rs.getObject(columnId);
break;
}
case Types.ARRAY:
{
result = columnId == MIN_INT ? rs.getArray(columnName) : rs.getArray(columnId);
break;
}
case Types.REF:
{
result = columnId == MIN_INT ? rs.getRef(columnName) : rs.getRef(columnId);
break;
}
//#ifdef JDBC30
case Types.BOOLEAN:
{
boolean boolVal = columnId == MIN_INT ? rs.getBoolean(columnName) : rs.getBoolean(columnId);
result = (rs.wasNull() ? null : new Boolean(boolVal));
break;
}
case Types.DATALINK:
{
result = columnId == MIN_INT ? rs.getURL(columnName) : rs.getURL(columnId);
break;
}
//#endif
default :
{
throw new OJBRuntimeException(
"The type "
+ getJdbcTypeAsString(jdbcType)
+ " (id: " + jdbcType + ")"
+ " for attribute "
+ columnName
+ " can not be handled by OJB. Please specify only types as defined by java.sql.Types.");
}
}
return result;
}
/**
* TODO: ??we don't need this method, because FieldDescriptor known
* the sql type??.
*
* Return SQL type for the given value - Types.OTHER if not found.
*/
public static int getSqlTypeByValue(Object value)
{
if (value instanceof Integer)
{
return Types.INTEGER;
}
else if (value instanceof BigDecimal)
{
return Types.NUMERIC;
}
else if (value instanceof String)
{
return Types.VARCHAR;
}
else if (value instanceof Byte)
{
return Types.TINYINT;
}
else if (value instanceof Short)
{
return Types.SMALLINT;
}
else if (value instanceof Long)
{
return Types.BIGINT;
}
else if (value instanceof Float)
{
return Types.REAL;
}
else if (value instanceof Double)
{
return Types.DOUBLE;
}
else if (value instanceof byte[])
{
return Types.VARBINARY;
}
else if (value instanceof java.sql.Date)
{
return Types.DATE;
}
else if (value instanceof java.sql.Time)
{
return Types.TIME;
}
else if (value instanceof Timestamp)
{
return Types.TIMESTAMP;
}
else if (value instanceof Boolean)
{
return Types.BIT;
}
else if (value instanceof Struct)
{
return Types.STRUCT;
}
//#ifdef JDBC30
else if (value instanceof URL)
{
return Types.DATALINK;
}
//#endif
else
{
return Types.OTHER;
}
}
public static Object getJdbcTypeAsString(int jdbcType)
{
switch (jdbcType)
{
case Types.BIT:
{
return "Types.BIT";
}
case Types.TINYINT:
{
return "Types.TINYINT";
}
case Types.SMALLINT:
{
return "Types.SMALLINT";
}
case Types.INTEGER:
{
return "Types.INTEGER";
}
case Types.BIGINT:
{
return "Types.BIGINT";
}
case Types.DOUBLE:
{
return "Types.DOUBLE";
}
case Types.FLOAT:
{
return "Types.FLOAT";
}
case Types.REAL:
{
return "Types.REAL";
}
case Types.NUMERIC:
{
return "Types.NUMERIC";
}
case Types.DECIMAL:
{
return "Types.DECIMAL";
}
case Types.CHAR:
{
return "Types.CHAR";
}
case Types.VARCHAR:
{
return "Types.VARCHAR";
}
case Types.LONGVARCHAR:
{
return "Types.LONGVARCHAR";
}
case Types.DATE:
{
return "Types.DATE";
}
case Types.TIME:
{
return "Types.TIME";
}
case Types.TIMESTAMP:
{
return "Types.TIMESTAMP";
}
case Types.BINARY:
{
return "Types.BINARY";
}
case Types.VARBINARY:
{
return "Types.VARBINARY";
}
case Types.LONGVARBINARY:
{
return "Types.LONGVARBINARY";
}
case Types.CLOB:
{
return "Types.CLOB";
}
case Types.BLOB:
{
return "Types.BLOB";
}
case Types.STRUCT:
{
return "Types.STRUCT";
}
case Types.ARRAY:
{
return "Types.ARRAY";
}
case Types.DISTINCT:
{
return "Types.DISTINCT";
}
case Types.REF:
{
return "Types.REF";
}
case Types.JAVA_OBJECT:
{
return "Types.JAVA_OBJECT";
}
case Types.NULL:
{
return "Types.NULL";
}
//#ifdef JDBC30
case Types.BOOLEAN:
{
return "Types.BOOLEAN";
}
case Types.DATALINK:
{
return "Types.DATALINK";
}
//#endif
default :
{
return "UNKOWN Type id " + jdbcType;
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org