View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.ext.oracle;
22  
23  import java.sql.Types;
24  import java.util.Arrays;
25  import java.util.Collection;
26  
27  import org.dbunit.dataset.datatype.BinaryStreamDataType;
28  import org.dbunit.dataset.datatype.DataType;
29  import org.dbunit.dataset.datatype.DataTypeException;
30  import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
31  import org.dbunit.dataset.datatype.StringDataType;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Specialized factory that recognizes Oracle data types.
37   * @author manuel.laflamme
38   * @since Jul 17, 2003
39   * @version $Revision$
40   */
41  public class OracleDataTypeFactory extends DefaultDataTypeFactory
42  {
43  
44      /**
45       * Logger for this class
46       */
47      private static final Logger logger = LoggerFactory.getLogger(OracleDataTypeFactory.class);
48      /**
49       * Database product names supported.
50       */
51      private static final Collection DATABASE_PRODUCTS = Arrays.asList(new String[] {"oracle"});
52  
53      public static final DataType ORACLE_BLOB = new OracleBlobDataType();
54      public static final DataType ORACLE_CLOB = new OracleClobDataType();
55      public static final DataType ORACLE_NCLOB = new OracleNClobDataType();
56      public static final DataType ORACLE_XMLTYPE = new OracleXMLTypeDataType();
57      public static final DataType ORACLE_SDO_GEOMETRY_TYPE = new OracleSdoGeometryDataType();
58      
59      public static final DataType LONG_RAW = new BinaryStreamDataType(
60              "LONG RAW", Types.LONGVARBINARY);
61      
62      public static final DataType ROWID_TYPE = new StringDataType("ROWID", Types.OTHER);
63  
64      /**
65       * @see org.dbunit.dataset.datatype.IDbProductRelatable#getValidDbProducts()
66       */
67      public Collection getValidDbProducts()
68      {
69        return DATABASE_PRODUCTS;
70      }
71  
72      public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException
73      {
74      	if(logger.isDebugEnabled())
75      		logger.debug("createDataType(sqlType={}, sqlTypeName={}) - start", String.valueOf(sqlType), sqlTypeName);
76  
77          // Map Oracle DATE to TIMESTAMP
78          if (sqlType == Types.DATE)
79          {
80              return DataType.TIMESTAMP;
81          }
82  
83          // TIMESTAMP
84          if (sqlTypeName.startsWith("TIMESTAMP"))
85          {
86              return DataType.TIMESTAMP;
87          }
88  
89          // XMLTYPE
90          if ("XMLTYPE".equals(sqlTypeName) || "SYS.XMLTYPE".equals(sqlTypeName))
91          {
92              return ORACLE_XMLTYPE;
93          }
94  
95          // BLOB
96          if ("BLOB".equals(sqlTypeName))
97          {
98              return ORACLE_BLOB;
99          }
100 
101         // CLOB
102         if ("CLOB".equals(sqlTypeName))
103         {
104             return ORACLE_CLOB;
105         }
106         
107         // NCLOB
108         if  ("NCLOB".equals(sqlTypeName))
109         {
110             return ORACLE_NCLOB;
111         }
112 
113         // NVARCHAR2
114         if ("NVARCHAR2".equals(sqlTypeName))
115         {
116             return DataType.VARCHAR;
117         }
118 
119 		// NCHAR
120         if (sqlTypeName.startsWith("NCHAR"))
121         {
122             return DataType.CHAR;
123         }
124 
125         // FLOAT
126         if ("FLOAT".equals(sqlTypeName))
127         {
128             return DataType.FLOAT;
129         }
130 
131         // LONG RAW
132         if (LONG_RAW.toString().equals(sqlTypeName))
133         {
134             return LONG_RAW;
135         }
136 
137         // BINARY_DOUBLE/BINARY_FLOAT
138         // Note that you have to configure your driver appropriate:
139         // Oracle-specific property to support IEEE floating-point is enabled setting the following property
140         // <value>SetFloatAndDoubleUseBinary=true</value>
141         if ("BINARY_DOUBLE".equals(sqlTypeName)) 
142         {
143             return DataType.DOUBLE;
144         }
145         if ("BINARY_FLOAT".equals(sqlTypeName)) 
146         {
147             return DataType.FLOAT;
148         }
149 
150         // ROWID
151         if ("ROWID".equals(sqlTypeName))
152         {
153             return ROWID_TYPE;
154         }
155 
156         // SDO_GEOMETRY
157         if ("SDO_GEOMETRY".equals(sqlTypeName) || "MDSYS.SDO_GEOMETRY".equals(sqlTypeName))
158         {
159             return ORACLE_SDO_GEOMETRY_TYPE;
160         }
161 
162         return super.createDataType(sqlType, sqlTypeName);
163     }
164 }
165 
166