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.PreparedStatement;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.sql.SQLXML;
27  import java.sql.Types;
28  
29  import org.dbunit.dataset.datatype.BlobDataType;
30  import org.dbunit.dataset.datatype.TypeCastException;
31  
32  import oracle.jdbc.OraclePreparedStatement;
33  import oracle.jdbc.OracleResultSet;
34  
35  /**
36   *
37   * TODO UnitTests are completely missing
38   * 
39   * @author Phil Barr
40   * @author Last changed by: $Author$
41   * @version $Revision$ $Date$
42   * @since 2.4.0
43   */
44  public class OracleXMLTypeDataType extends BlobDataType
45  {
46      OracleXMLTypeDataType()
47      {
48          super("SQLXML", Types.SQLXML);
49      }
50  
51      @Override
52      public Object getSqlValue(int column, ResultSet resultSet)
53              throws SQLException, TypeCastException
54      {
55          byte[] data = null;
56          OracleResultSet oracleResultSet = (OracleResultSet) resultSet;
57          SQLXML sqlXml = oracleResultSet.getSQLXML(column);
58          if (sqlXml != null)
59          {
60              data = sqlXml.getString().getBytes();
61          }
62  
63          // return the byte data (using typeCast to cast it to Base64 notation)
64          return typeCast(data);
65      }
66  
67      @Override
68      public void setSqlValue(Object value, int column,
69              PreparedStatement statement) throws SQLException, TypeCastException
70      {
71          OraclePreparedStatement oraclePreparedStatement =
72                  (OraclePreparedStatement) statement;
73          SQLXML sqlXmlValue =
74                  oraclePreparedStatement.getConnection().createSQLXML();
75          // XML document in the parameter is Base64 encoded (it is entered in XML
76          // parameter
77          sqlXmlValue.setString(new String((byte[]) typeCast(value)));
78          oraclePreparedStatement.setSQLXML(column, sqlXmlValue);
79      }
80  
81      @Override
82      public String getSqlTypeName()
83      {
84          return "SYS.XMLTYPE";
85      }
86  }