001: package com.salmonllc.sql;
002:
003: /////////////////////////
004: //$Archive: /SOFIA/SourceCode/com/salmonllc/sql/DataType.java $
005: //$Author: Srufle $
006: //$Revision: 19 $
007: //$Modtime: 11/04/04 10:57a $
008: /////////////////////////
009:
010: import java.util.*;
011:
012: /**
013: * This class converts sql datatypes into datastore datatypes
014: */
015:
016: public class DataType {
017: private static final Hashtable _types = new Hashtable();
018:
019: static {
020: addType("char", DataStore.DATATYPE_STRING);
021: addType("date", DataStore.DATATYPE_DATETIME);
022: addType(DBConnection.DB2_CONNECTION, "date",
023: DataStore.DATATYPE_DATE);
024: addType(DBConnection.DB2MVS_CONNECTION, "date",
025: DataStore.DATATYPE_DATE);
026: addType(DBConnection.DB2VSE_CONNECTION, "date",
027: DataStore.DATATYPE_DATE);
028: addType(DBConnection.MYSQL_CONNECTION, "date",
029: DataStore.DATATYPE_DATE);
030: addType("datetime", DataStore.DATATYPE_DATETIME);
031: addType("timestmp", DataStore.DATATYPE_DATETIME);
032: addType("timestamp", DataStore.DATATYPE_DATETIME);
033: addType("time", DataStore.DATATYPE_DATETIME);
034: addType(DBConnection.MYSQL_CONNECTION, "time",
035: DataStore.DATATYPE_TIME);
036: addType("decimal", DataStore.DATATYPE_DOUBLE);
037: addType("float", DataStore.DATATYPE_DOUBLE);
038: addType("double", DataStore.DATATYPE_DOUBLE);
039: addType("int", DataStore.DATATYPE_INT);
040: addType("integer", DataStore.DATATYPE_INT);
041: addType("smallint", DataStore.DATATYPE_SHORT);
042: addType("bit", DataStore.DATATYPE_SHORT);
043: addType("bigint", DataStore.DATATYPE_LONG);
044: addType("text", DataStore.DATATYPE_STRING);
045: addType("varchar", DataStore.DATATYPE_STRING);
046: addType("longvar", DataStore.DATATYPE_STRING);
047: addType("image", DataStore.DATATYPE_BYTEARRAY);
048: addType("blob", DataStore.DATATYPE_BYTEARRAY);
049: addType("numeric", DataStore.DATATYPE_INT);
050: addType(DBConnection.POSTGRES_CONNECTION, "numeric",
051: DataStore.DATATYPE_DOUBLE);
052: addType("nvarchar", DataStore.DATATYPE_STRING);
053: addType("ntext", DataStore.DATATYPE_STRING);
054: addType("number", DataStore.DATATYPE_DOUBLE);
055: addType("timestamptz", DataStore.DATATYPE_DATETIME);
056: addType("long", DataStore.DATATYPE_LONG);
057: addType(DBConnection.ORACLE_CONNECTION, "long",
058: DataStore.DATATYPE_STRING);
059: }
060:
061: private static void addType(String dbDataType, int dsDataType) {
062: _types.put(dbDataType, new Integer(dsDataType));
063: }
064:
065: private static void addType(String dbms, String dbDataType,
066: int dsDataType) {
067: _types.put((dbms + "." + dbDataType).toLowerCase(),
068: new Integer(dsDataType));
069: }
070:
071: public static int toDSDataType(String dbDataType) {
072: return toDSDataType(dbDataType, -1, -1);
073: }
074:
075: public static int toDSDataType(String dbms, String dbDataType) {
076: return toDSDataType(dbms, dbDataType, -1, -1);
077: }
078:
079: public static int toDSDataType(String dbDataType, int precision,
080: int scale) {
081: try {
082: if (dbDataType.equalsIgnoreCase("number") && scale < 1)
083: return DataStore.DATATYPE_INT;
084: else
085: return ((Integer) _types.get(dbDataType.toLowerCase()))
086: .intValue();
087: } catch (Exception e) {
088: return DataStore.DATATYPE_STRING;
089: }
090:
091: }
092:
093: public static int toDSDataType(String dbms, String dbDataType,
094: int precision, int scale) {
095: try {
096: if (dbms == null)
097: return toDSDataType(dbDataType, precision, scale);
098:
099: if (dbDataType.equalsIgnoreCase("number") && scale < 1)
100: return DataStore.DATATYPE_INT;
101: else {
102: dbms = dbms.toLowerCase();
103: dbDataType = dbDataType.toLowerCase();
104: Integer type = (Integer) _types.get(dbms + "."
105: + dbDataType);
106: if (type == null)
107: type = (Integer) _types.get(dbDataType);
108: if (type == null)
109: return DataStore.DATATYPE_STRING;
110: else
111: return type.intValue();
112: }
113: } catch (Exception e) {
114: return DataStore.DATATYPE_STRING;
115: }
116:
117: }
118: }
|