001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021: package org.dbunit.ext.oracle;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.datatype.ClobDataType;
027: import org.dbunit.dataset.datatype.TypeCastException;
028:
029: import java.io.IOException;
030: import java.io.Writer;
031: import java.lang.reflect.InvocationTargetException;
032: import java.lang.reflect.Method;
033: import java.sql.Connection;
034: import java.sql.PreparedStatement;
035: import java.sql.ResultSet;
036: import java.sql.SQLException;
037:
038: /**
039: * @author Manuel Laflamme
040: * @version $Revision: 554 $
041: * @since Jan 12, 2004
042: */
043: public class OracleClobDataType extends ClobDataType {
044:
045: /**
046: * Logger for this class
047: */
048: private static final Logger logger = LoggerFactory
049: .getLogger(OracleClobDataType.class);
050:
051: private static final Integer DURATION_SESSION = new Integer(1);
052: // private static final Integer DURATION_CALL = new Integer(2);
053: // private static final Integer MODE_READONLY = new Integer(0);
054: private static final Integer MODE_READWRITE = new Integer(1);
055:
056: public Object getSqlValue(int column, ResultSet resultSet)
057: throws SQLException, TypeCastException {
058: logger.debug("getSqlValue(column=" + column + ", resultSet="
059: + resultSet + ") - start");
060:
061: return typeCast(resultSet.getClob(column));
062: }
063:
064: public void setSqlValue(Object value, int column,
065: PreparedStatement statement) throws SQLException,
066: TypeCastException {
067: logger.debug("setSqlValue(value=" + value + ", column="
068: + column + ", statement=" + statement + ") - start");
069:
070: statement.setObject(column, getClob(value, statement
071: .getConnection()));
072: }
073:
074: protected Object getClob(Object value, Connection connection)
075: throws TypeCastException {
076: logger.debug("getClob(value=" + value + ", connection="
077: + connection + ") - start");
078:
079: Object tempClob = null;
080: try {
081: Class aClobClass = Class.forName("oracle.sql.CLOB");
082:
083: // Create new temporary CLOB
084: Method createTemporaryMethod = aClobClass.getMethod(
085: "createTemporary", new Class[] { Connection.class,
086: Boolean.TYPE, Integer.TYPE });
087: tempClob = createTemporaryMethod.invoke(null, new Object[] {
088: connection, Boolean.TRUE, DURATION_SESSION });
089:
090: // Open the temporary CLOB in readwrite mode to enable writing
091: Method openMethod = aClobClass.getMethod("open",
092: new Class[] { Integer.TYPE });
093: openMethod
094: .invoke(tempClob, new Object[] { MODE_READWRITE });
095:
096: // Get the output stream to write
097: Method getCharacterOutputStreamMethod = tempClob
098: .getClass()
099: .getMethod("getCharacterOutputStream", new Class[0]);
100: Writer tempClobWriter = (Writer) getCharacterOutputStreamMethod
101: .invoke(tempClob, new Object[0]);
102:
103: // Write the data into the temporary CLOB
104: tempClobWriter.write((String) typeCast(value));
105:
106: // Flush and close the stream
107: tempClobWriter.flush();
108: tempClobWriter.close();
109:
110: // Close the temporary CLOB
111: Method closeMethod = tempClob.getClass().getMethod("close",
112: new Class[0]);
113: closeMethod.invoke(tempClob, new Object[0]);
114: } catch (IllegalAccessException e) {
115: logger.error("getClob()", e);
116:
117: freeTemporaryClob(tempClob);
118: throw new TypeCastException(value, this , e);
119: } catch (NoSuchMethodException e) {
120: logger.error("getClob()", e);
121:
122: freeTemporaryClob(tempClob);
123: throw new TypeCastException(value, this , e);
124: } catch (IOException e) {
125: logger.error("getClob()", e);
126:
127: freeTemporaryClob(tempClob);
128: throw new TypeCastException(value, this , e);
129: } catch (InvocationTargetException e) {
130: logger.error("getClob()", e);
131:
132: freeTemporaryClob(tempClob);
133: throw new TypeCastException(value, this , e
134: .getTargetException());
135: } catch (ClassNotFoundException e) {
136: logger.error("getClob()", e);
137:
138: freeTemporaryClob(tempClob);
139: throw new TypeCastException(value, this , e);
140: }
141:
142: return tempClob;
143: }
144:
145: protected void freeTemporaryClob(Object tempClob)
146: throws TypeCastException {
147: logger.debug("freeTemporaryClob(tempClob=" + tempClob
148: + ") - start");
149:
150: if (tempClob == null) {
151: return;
152: }
153:
154: try {
155: Method freeTemporaryMethod = tempClob.getClass().getMethod(
156: "freeTemporary", new Class[0]);
157: freeTemporaryMethod.invoke(tempClob, new Object[0]);
158: } catch (NoSuchMethodException e) {
159: logger.error("freeTemporaryClob()", e);
160:
161: throw new TypeCastException("Error freeing Oracle CLOB", e);
162: } catch (IllegalAccessException e) {
163: logger.error("freeTemporaryClob()", e);
164:
165: throw new TypeCastException("Error freeing Oracle CLOB", e);
166: } catch (InvocationTargetException e) {
167: logger.error("freeTemporaryClob()", e);
168:
169: throw new TypeCastException("Error freeing Oracle CLOB", e
170: .getTargetException());
171: }
172: }
173:
174: }
|