001    /**
002    
003     * ========================================
004    
005     * JFreeReport : a free Java report library
006    
007     * ========================================
008    
009     *
010    
011     * Project Info:  http://reporting.pentaho.org/
012    
013     *
014    
015     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
016    
017     *
018    
019     * This library is free software; you can redistribute it and/or modify it under the terms
020    
021     * of the GNU Lesser General Public License as published by the Free Software Foundation;
022    
023     * either version 2.1 of the License, or (at your option) any later version.
024    
025     *
026    
027     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
028    
029     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
030    
031     * See the GNU Lesser General Public License for more details.
032    
033     *
034    
035     * You should have received a copy of the GNU Lesser General Public License along with this
036    
037     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
038    
039     * Boston, MA 02111-1307, USA.
040    
041     *
042    
043     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
044    
045     * in the United States and other countries.]
046    
047     *
048    
049     * ------------
050    
051     * $Id: TypeMapper.java,v 1.8 2007/04/01 18:49:32 taqua Exp $
052    
053     * ------------
054    
055     * (C) Copyright 2000-2005, by Object Refinery Limited.
056    
057     * (C) Copyright 2005-2007, by Pentaho Corporation.
058    
059     */
060    
061    package org.jfree.report.modules.misc.tablemodel;
062    
063    
064    
065    import java.sql.Blob;
066    
067    import java.sql.Clob;
068    
069    import java.sql.Ref;
070    
071    import java.sql.ResultSetMetaData;
072    
073    import java.sql.SQLException;
074    
075    import java.sql.Struct;
076    
077    import java.sql.Time;
078    
079    import java.sql.Timestamp;
080    
081    import java.sql.Types;
082    
083    
084    
085    import org.jfree.util.ObjectUtilities;
086    
087    
088    
089    /**
090    
091     * @author $Author: taqua $
092    
093     * @version $Id: TypeMapper.java,v 1.8 2007/04/01 18:49:32 taqua Exp $
094    
095     */
096    
097    public class TypeMapper
098    
099    {
100    
101      private static Class byteArrayClass = (new byte[0]).getClass();
102    
103    
104    
105      private static Class mapSQLType (final int t)
106    
107      {
108    
109        switch (t)
110    
111        {
112    
113          case Types.ARRAY:
114    
115            return (new Object[0]).getClass();
116    
117          case Types.BIGINT:
118    
119            return Long.class;
120    
121          case Types.BINARY:
122    
123            return byteArrayClass;
124    
125          case Types.BIT:
126    
127            return Boolean.class;
128    
129          case Types.BLOB:
130    
131            return Blob.class;
132    
133          case 16: // Types.BOOLEAN was not part of JDK1.2.2
134    
135            return Boolean.class;
136    
137          case Types.CHAR:
138    
139            return String.class;
140    
141          case Types.CLOB:
142    
143            return Clob.class;
144    
145          case 70: // Types.DATALINK was not part of JDK 1.2.2
146    
147            return Object.class;
148    
149          case Types.DATE:
150    
151            return java.sql.Date.class;
152    
153          case Types.DECIMAL:
154    
155            return java.math.BigDecimal.class;
156    
157          case Types.DISTINCT:
158    
159            return Object.class;
160    
161          case Types.DOUBLE:
162    
163            return Double.class;
164    
165          case Types.FLOAT:
166    
167            return Double.class;
168    
169          case Types.INTEGER:
170    
171            return Integer.class;
172    
173          case Types.JAVA_OBJECT:
174    
175            return Object.class;
176    
177          case Types.LONGVARBINARY:
178    
179            return byteArrayClass;
180    
181          case Types.LONGVARCHAR:
182    
183            return String.class;
184    
185          case Types.NULL:
186    
187            return Object.class;
188    
189          case Types.NUMERIC:
190    
191            return java.math.BigDecimal.class;
192    
193          case Types.OTHER:
194    
195            return Object.class;
196    
197          case Types.REAL:
198    
199            return Float.class;
200    
201          case Types.REF:
202    
203            return Ref.class;
204    
205          case Types.SMALLINT:
206    
207            return Short.class;
208    
209          case Types.STRUCT:
210    
211            return Struct.class;
212    
213          case Types.TIME:
214    
215            return Time.class;
216    
217          case Types.TIMESTAMP:
218    
219            return Timestamp.class;
220    
221          case Types.TINYINT:
222    
223            return Byte.class;
224    
225          case Types.VARBINARY:
226    
227            return byteArrayClass;
228    
229          case Types.VARCHAR:
230    
231            return String.class;
232    
233          default:
234    
235            return Object.class;
236    
237        }
238    
239      }
240    
241    
242    
243      public static Class[] mapTypes (final ResultSetMetaData rsmd)
244    
245      {
246    
247        final Class[] types;
248    
249        try
250    
251        {
252    
253          types = new Class[rsmd.getColumnCount()];
254    
255        }
256    
257        catch (SQLException sqle)
258    
259        {
260    
261          return null;
262    
263        }
264    
265    
266    
267        final ClassLoader cl = ObjectUtilities.getClassLoader(TypeMapper.class);
268    
269        for (int i = 0; i < types.length; i++)
270    
271        {
272    
273          try
274    
275          {
276    
277            try
278    
279            {
280    
281              final String tn = rsmd.getColumnClassName(i + 1);
282    
283              types[i] = cl.loadClass(tn);
284    
285            }
286    
287            catch (Exception oops)
288    
289            {
290    
291              final int colType = rsmd.getColumnType(i + 1);
292    
293              types[i] = mapSQLType(colType);
294    
295            }
296    
297          }
298    
299          catch (Exception e)
300    
301          {
302    
303            types[i] = Object.class;
304    
305          }
306    
307        }
308    
309    
310    
311        return types;
312    
313      }
314    
315    
316    
317      private TypeMapper ()
318    
319      {
320    
321      }
322    
323    }
324