OracleXMLTypeDataType.java

  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. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.SQLXML;
  26. import java.sql.Types;

  27. import org.dbunit.dataset.datatype.BlobDataType;
  28. import org.dbunit.dataset.datatype.TypeCastException;

  29. import oracle.jdbc.OraclePreparedStatement;
  30. import oracle.jdbc.OracleResultSet;

  31. /**
  32.  *
  33.  * TODO UnitTests are completely missing
  34.  *
  35.  * @author Phil Barr
  36.  * @author Last changed by: $Author$
  37.  * @version $Revision$ $Date$
  38.  * @since 2.4.0
  39.  */
  40. public class OracleXMLTypeDataType extends BlobDataType
  41. {
  42.     OracleXMLTypeDataType()
  43.     {
  44.         super("SQLXML", Types.SQLXML);
  45.     }

  46.     @Override
  47.     public Object getSqlValue(int column, ResultSet resultSet)
  48.             throws SQLException, TypeCastException
  49.     {
  50.         byte[] data = null;
  51.         OracleResultSet oracleResultSet = resultSet.unwrap(OracleResultSet.class);
  52.         SQLXML sqlXml = oracleResultSet.getSQLXML(column);
  53.         if (sqlXml != null)
  54.         {
  55.             data = sqlXml.getString().getBytes();
  56.         }

  57.         // return the byte data (using typeCast to cast it to Base64 notation)
  58.         return typeCast(data);
  59.     }

  60.     @Override
  61.     public void setSqlValue(Object value, int column,
  62.             PreparedStatement statement) throws SQLException, TypeCastException
  63.     {
  64.         OraclePreparedStatement oraclePreparedStatement = statement.unwrap(OraclePreparedStatement.class);
  65.         SQLXML sqlXmlValue =
  66.                 oraclePreparedStatement.getConnection().createSQLXML();
  67.         // XML document in the parameter is Base64 encoded (it is entered in XML
  68.         // parameter
  69.         sqlXmlValue.setString(new String((byte[]) typeCast(value)));
  70.         oraclePreparedStatement.setSQLXML(column, sqlXmlValue);
  71.     }

  72.     @Override
  73.     public String getSqlTypeName()
  74.     {
  75.         return "SYS.XMLTYPE";
  76.     }
  77. }