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: */package org.jfree.report.modules.misc.tablemodel;
031:
032: import java.sql.Blob;
033: import java.sql.Clob;
034: import java.sql.Ref;
035: import java.sql.ResultSetMetaData;
036: import java.sql.SQLException;
037: import java.sql.Struct;
038: import java.sql.Time;
039: import java.sql.Timestamp;
040: import java.sql.Types;
041:
042: import org.jfree.util.ObjectUtilities;
043:
044: /**
045: * @author $Author: tmorgner $
046: * @version $Id: TypeMapper.java 3525 2007-10-16 11:43:48Z tmorgner $
047: */
048: public class TypeMapper {
049: private static final Class byteArrayClass = byte[].class;
050:
051: private static Class mapSQLType(final int t) {
052: switch (t) {
053: case Types.ARRAY:
054: return Object[].class;
055: case Types.BIGINT:
056: return Long.class;
057: case Types.BINARY:
058: return byteArrayClass;
059: case Types.BIT:
060: return Boolean.class;
061: case Types.BLOB:
062: return Blob.class;
063: case 16: // Types.BOOLEAN was not part of JDK1.2.2
064: return Boolean.class;
065: case Types.CHAR:
066: return String.class;
067: case Types.CLOB:
068: return Clob.class;
069: case 70: // Types.DATALINK was not part of JDK 1.2.2
070: return Object.class;
071: case Types.DATE:
072: return java.sql.Date.class;
073: case Types.DECIMAL:
074: return java.math.BigDecimal.class;
075: case Types.DISTINCT:
076: return Object.class;
077: case Types.DOUBLE:
078: return Double.class;
079: case Types.FLOAT:
080: return Double.class;
081: case Types.INTEGER:
082: return Integer.class;
083: case Types.JAVA_OBJECT:
084: return Object.class;
085: case Types.LONGVARBINARY:
086: return byteArrayClass;
087: case Types.LONGVARCHAR:
088: return String.class;
089: case Types.NULL:
090: return Object.class;
091: case Types.NUMERIC:
092: return java.math.BigDecimal.class;
093: case Types.OTHER:
094: return Object.class;
095: case Types.REAL:
096: return Float.class;
097: case Types.REF:
098: return Ref.class;
099: case Types.SMALLINT:
100: return Short.class;
101: case Types.STRUCT:
102: return Struct.class;
103: case Types.TIME:
104: return Time.class;
105: case Types.TIMESTAMP:
106: return Timestamp.class;
107: case Types.TINYINT:
108: return Byte.class;
109: case Types.VARBINARY:
110: return byteArrayClass;
111: case Types.VARCHAR:
112: return String.class;
113: default:
114: return Object.class;
115: }
116: }
117:
118: public static Class[] mapTypes(final ResultSetMetaData rsmd) {
119: final Class[] types;
120: try {
121: types = new Class[rsmd.getColumnCount()];
122: } catch (SQLException sqle) {
123: return null;
124: }
125:
126: final ClassLoader cl = ObjectUtilities
127: .getClassLoader(TypeMapper.class);
128: for (int i = 0; i < types.length; i++) {
129: try {
130: try {
131: final String tn = rsmd.getColumnClassName(i + 1);
132: types[i] = cl.loadClass(tn);
133: } catch (Exception oops) {
134: final int colType = rsmd.getColumnType(i + 1);
135: types[i] = mapSQLType(colType);
136: }
137: } catch (Exception e) {
138: types[i] = Object.class;
139: }
140: }
141:
142: return types;
143: }
144:
145: private TypeMapper() {
146: }
147: }
|