001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: TypeMapper.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    package org.jfree.report.modules.misc.tablemodel;
032    
033    import java.sql.Blob;
034    import java.sql.Clob;
035    import java.sql.Ref;
036    import java.sql.ResultSetMetaData;
037    import java.sql.SQLException;
038    import java.sql.Struct;
039    import java.sql.Time;
040    import java.sql.Timestamp;
041    import java.sql.Types;
042    
043    import org.jfree.util.ObjectUtilities;
044    
045    /**
046     * @author $Author: tmorgner $
047     * @version $Id: TypeMapper.java 3525 2007-10-16 11:43:48Z tmorgner $
048     */
049    public class TypeMapper
050    {
051      private static final Class byteArrayClass = byte[].class;
052    
053      private static Class mapSQLType (final int t)
054      {
055        switch (t)
056        {
057          case Types.ARRAY:
058            return Object[].class;
059          case Types.BIGINT:
060            return Long.class;
061          case Types.BINARY:
062            return byteArrayClass;
063          case Types.BIT:
064            return Boolean.class;
065          case Types.BLOB:
066            return Blob.class;
067          case 16: // Types.BOOLEAN was not part of JDK1.2.2
068            return Boolean.class;
069          case Types.CHAR:
070            return String.class;
071          case Types.CLOB:
072            return Clob.class;
073          case 70: // Types.DATALINK was not part of JDK 1.2.2
074            return Object.class;
075          case Types.DATE:
076            return java.sql.Date.class;
077          case Types.DECIMAL:
078            return java.math.BigDecimal.class;
079          case Types.DISTINCT:
080            return Object.class;
081          case Types.DOUBLE:
082            return Double.class;
083          case Types.FLOAT:
084            return Double.class;
085          case Types.INTEGER:
086            return Integer.class;
087          case Types.JAVA_OBJECT:
088            return Object.class;
089          case Types.LONGVARBINARY:
090            return byteArrayClass;
091          case Types.LONGVARCHAR:
092            return String.class;
093          case Types.NULL:
094            return Object.class;
095          case Types.NUMERIC:
096            return java.math.BigDecimal.class;
097          case Types.OTHER:
098            return Object.class;
099          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    }