Source for org.jfree.report.modules.misc.tablemodel.TypeMapper

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: TypeMapper.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.misc.tablemodel;
  32: 
  33: import java.sql.Blob;
  34: import java.sql.Clob;
  35: import java.sql.Ref;
  36: import java.sql.ResultSetMetaData;
  37: import java.sql.SQLException;
  38: import java.sql.Struct;
  39: import java.sql.Time;
  40: import java.sql.Timestamp;
  41: import java.sql.Types;
  42: 
  43: import org.jfree.util.ObjectUtilities;
  44: 
  45: /**
  46:  * @author $Author: tmorgner $
  47:  * @version $Id: TypeMapper.java 3525 2007-10-16 11:43:48Z tmorgner $
  48:  */
  49: public class TypeMapper
  50: {
  51:   private static final Class byteArrayClass = byte[].class;
  52: 
  53:   private static Class mapSQLType (final int t)
  54:   {
  55:     switch (t)
  56:     {
  57:       case Types.ARRAY:
  58:         return Object[].class;
  59:       case Types.BIGINT:
  60:         return Long.class;
  61:       case Types.BINARY:
  62:         return byteArrayClass;
  63:       case Types.BIT:
  64:         return Boolean.class;
  65:       case Types.BLOB:
  66:         return Blob.class;
  67:       case 16: // Types.BOOLEAN was not part of JDK1.2.2
  68:         return Boolean.class;
  69:       case Types.CHAR:
  70:         return String.class;
  71:       case Types.CLOB:
  72:         return Clob.class;
  73:       case 70: // Types.DATALINK was not part of JDK 1.2.2
  74:         return Object.class;
  75:       case Types.DATE:
  76:         return java.sql.Date.class;
  77:       case Types.DECIMAL:
  78:         return java.math.BigDecimal.class;
  79:       case Types.DISTINCT:
  80:         return Object.class;
  81:       case Types.DOUBLE:
  82:         return Double.class;
  83:       case Types.FLOAT:
  84:         return Double.class;
  85:       case Types.INTEGER:
  86:         return Integer.class;
  87:       case Types.JAVA_OBJECT:
  88:         return Object.class;
  89:       case Types.LONGVARBINARY:
  90:         return byteArrayClass;
  91:       case Types.LONGVARCHAR:
  92:         return String.class;
  93:       case Types.NULL:
  94:         return Object.class;
  95:       case Types.NUMERIC:
  96:         return java.math.BigDecimal.class;
  97:       case Types.OTHER:
  98:         return Object.class;
  99:       case Types.REAL:
 100:         return Float.class;
 101:       case Types.REF:
 102:         return Ref.class;
 103:       case Types.SMALLINT:
 104:         return Short.class;
 105:       case Types.STRUCT:
 106:         return Struct.class;
 107:       case Types.TIME:
 108:         return Time.class;
 109:       case Types.TIMESTAMP:
 110:         return Timestamp.class;
 111:       case Types.TINYINT:
 112:         return Byte.class;
 113:       case Types.VARBINARY:
 114:         return byteArrayClass;
 115:       case Types.VARCHAR:
 116:         return String.class;
 117:       default:
 118:         return Object.class;
 119:     }
 120:   }
 121: 
 122:   public static Class[] mapTypes (final ResultSetMetaData rsmd)
 123:   {
 124:     final Class[] types;
 125:     try
 126:     {
 127:       types = new Class[rsmd.getColumnCount()];
 128:     }
 129:     catch (SQLException sqle)
 130:     {
 131:       return null;
 132:     }
 133: 
 134:     final ClassLoader cl = ObjectUtilities.getClassLoader(TypeMapper.class);
 135:     for (int i = 0; i < types.length; i++)
 136:     {
 137:       try
 138:       {
 139:         try
 140:         {
 141:           final String tn = rsmd.getColumnClassName(i + 1);
 142:           types[i] = cl.loadClass(tn);
 143:         }
 144:         catch (Exception oops)
 145:         {
 146:           final int colType = rsmd.getColumnType(i + 1);
 147:           types[i] = mapSQLType(colType);
 148:         }
 149:       }
 150:       catch (Exception e)
 151:       {
 152:         types[i] = Object.class;
 153:       }
 154:     }
 155: 
 156:     return types;
 157:   }
 158: 
 159:   private TypeMapper ()
 160:   {
 161:   }
 162: }