001: //==============================================================================
002: //===
003: //=== SqlMapper (adapted class from druid project http://druid.sf.net)
004: //===
005: //=== Copyright (C) by Andrea Carboni.
006: //=== This file may be distributed under the terms of the GPL license.
007: //==============================================================================
008:
009: package org.fao.gast.lib.druid;
010:
011: import java.sql.Types;
012:
013: //==============================================================================
014:
015: public class SqlMapper {
016: private static SqlType[] types = {
017: new SqlType(Types.ARRAY, "ARRAY", SqlType.UNKNOWN),
018: new SqlType(Types.BIGINT, "BIGINT", SqlType.CONST),
019: new SqlType(Types.BINARY, "BINARY", SqlType.UNKNOWN),
020: new SqlType(Types.BIT, "BIT", SqlType.BOTH),
021: new SqlType(Types.BLOB, "BLOB", SqlType.CONST),
022: new SqlType(Types.CHAR, "CHAR", SqlType.BOTH),
023: new SqlType(Types.CLOB, "CLOB", SqlType.CONST),
024: new SqlType(Types.DATE, "DATE", SqlType.CONST),
025: new SqlType(Types.DECIMAL, "DECIMAL", SqlType.BOTH),
026: new SqlType(Types.DISTINCT, "DISTINCT", SqlType.UNKNOWN),
027: new SqlType(Types.DOUBLE, "DOUBLE", SqlType.CONST),
028: new SqlType(Types.FLOAT, "FLOAT", SqlType.BOTH),
029: new SqlType(Types.INTEGER, "INTEGER", SqlType.CONST),
030: new SqlType(Types.JAVA_OBJECT, "JAVA_OBJECT",
031: SqlType.UNKNOWN),
032: new SqlType(Types.LONGVARBINARY, "LONGVARBINARY",
033: SqlType.CONST),
034: new SqlType(Types.LONGVARCHAR, "LONGVARCHAR", SqlType.CONST),
035: new SqlType(Types.NULL, "NULL", SqlType.UNKNOWN),
036: new SqlType(Types.NUMERIC, "NUMERIC", SqlType.BOTH),
037: new SqlType(Types.OTHER, "OTHER", SqlType.UNKNOWN),
038: new SqlType(Types.REAL, "REAL", SqlType.CONST),
039: new SqlType(Types.REF, "REF", SqlType.UNKNOWN),
040: new SqlType(Types.SMALLINT, "SMALLINT", SqlType.CONST),
041: new SqlType(Types.STRUCT, "STRUCT", SqlType.UNKNOWN),
042: new SqlType(Types.TIME, "TIME", SqlType.BOTH),
043: new SqlType(Types.TIMESTAMP, "TIMESTAMP", SqlType.BOTH),
044: new SqlType(Types.TINYINT, "TINYINT", SqlType.CONST),
045: new SqlType(Types.VARBINARY, "VARBINARY", SqlType.VAR),
046: new SqlType(Types.VARCHAR, "VARCHAR", SqlType.VAR) };
047:
048: //---------------------------------------------------------------------------
049:
050: public static SqlType mapId(Object id) {
051: if (id != null)
052: for (int i = 0; i < types.length; i++)
053: if (id.toString().equals("" + types[i].iId))
054: return types[i];
055:
056: return new SqlType(-999, "(unknown)", SqlType.UNKNOWN);
057: }
058:
059: //---------------------------------------------------------------------------
060:
061: public static SqlType mapName(String name) {
062: if (name != null)
063: for (int i = 0; i < types.length; i++)
064: if (name.equals(types[i].sName))
065: return types[i];
066:
067: return new SqlType(-999, "(unknown)", SqlType.UNKNOWN);
068: }
069:
070: //---------------------------------------------------------------------------
071:
072: /** This method is called to obtain the datatype of a table column
073: * id is the numeric java id as defined in java.sql.Types
074: * dbType is the string name of the type as reported by the dbms
075: * decimals is an integer specifying how many decimal digits the type has
076: */
077:
078: public static SqlType map(Object id, String dbType, Object decimals) {
079: boolean hasDecimals = false;
080:
081: if (decimals != null && !decimals.toString().equals("0"))
082: hasDecimals = true;
083:
084: SqlType sqlType = mapId(id);
085:
086: //--- some DBMSs (like Oracle) treat a decimal(nn) like an integer
087: //--- in this case we must remap the type
088:
089: if (sqlType.iId == Types.DECIMAL
090: || sqlType.iId == Types.NUMERIC)
091: if (!hasDecimals)
092: return mapId("" + Types.INTEGER);
093:
094: //--- some DBMSs (like Oracle) treat the blob type as 'other'
095: //--- in this case another remap is needed
096:
097: if (sqlType.iId == Types.OTHER
098: && dbType.toLowerCase().equals("blob"))
099: return mapId("" + Types.BLOB);
100:
101: if (sqlType.iId == Types.OTHER
102: && dbType.toLowerCase().equals("clob"))
103: return mapId("" + Types.CLOB);
104:
105: return sqlType;
106: }
107: }
108:
109: //==============================================================================
|