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 org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import org.dbunit.dataset.datatype.ClobDataType;
27  import org.dbunit.dataset.datatype.TypeCastException;
28  
29  import java.io.IOException;
30  import java.io.Writer;
31  import java.sql.Connection;
32  import java.sql.PreparedStatement;
33  import java.sql.ResultSet;
34  import java.sql.SQLException;
35  
36  /**
37   * @author Manuel Laflamme
38   * @author Last changed by: $Author$
39   * @version $Revision$ $Date$
40   * @since Jan 12, 2004
41   */
42  public class OracleClobDataType extends ClobDataType
43  {
44  
45      /**
46       * Logger for this class
47       */
48      private static final Logger logger = LoggerFactory.getLogger(OracleClobDataType.class);
49  
50      public Object getSqlValue(int column, ResultSet resultSet)
51              throws SQLException, TypeCastException
52      {
53      	if(logger.isDebugEnabled())
54      		logger.debug("getSqlValue(column={}, resultSet={}) - start", new Integer(column), resultSet);
55  
56          return typeCast(resultSet.getClob(column));
57      }
58  
59      public void setSqlValue(Object value, int column, PreparedStatement statement)
60              throws SQLException, TypeCastException
61      {
62      	if(logger.isDebugEnabled())
63      		logger.debug("setSqlValue(value={}, column={}, statement={}) - start",
64      				new Object[]{value, new Integer(column), statement} );
65  
66          statement.setObject(column, getClob(value, statement.getConnection()));
67      }
68  
69      protected Object getClob(Object value, Connection connection) throws TypeCastException
70      {
71          logger.debug("getClob(value={}, connection={}) - start", value, connection);
72  
73          oracle.sql.CLOB tempClob = null;
74          try
75          {
76              tempClob = oracle.sql.CLOB.createTemporary(connection, true, oracle.sql.CLOB.DURATION_SESSION);
77              tempClob.open(oracle.sql.CLOB.MODE_READWRITE);
78              Writer tempClobWriter = tempClob.getCharacterOutputStream();
79  
80              // Write the data into the temporary CLOB
81              tempClobWriter.write((String)typeCast(value));
82  
83              // Flush and close the stream
84              tempClobWriter.flush();
85              tempClobWriter.close();
86  
87              // Close the temporary CLOB
88              tempClob.close();
89          }
90          catch (IOException e)
91          {
92              // JH_TODO: shouldn't freeTemporary be called in finally {} ?
93              // It wasn't done like that in the original reflection-styled DbUnit code.
94              freeTemporaryClob(tempClob);
95              throw new TypeCastException(value, this, e);
96          }
97          catch (SQLException e)
98          {
99              freeTemporaryClob(tempClob);
100             throw new TypeCastException(value, this, e);
101         }
102 
103         return tempClob;
104     }
105 
106 
107     protected void freeTemporaryClob(oracle.sql.CLOB tempClob) throws TypeCastException
108     {
109         logger.debug("freeTemporaryClob(tempClob={}) - start", tempClob);
110 
111         if (tempClob == null)
112         {
113             return;
114         }
115 
116         try
117         {
118             tempClob.freeTemporary();
119         }
120         catch (SQLException e)
121         {
122             throw new TypeCastException("error freeing Oracle CLOB", e);
123         }
124     }
125 
126 }