KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > ext > oracle > OracleClobDataType


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.dbunit.dataset.datatype.ClobDataType;
24 import org.dbunit.dataset.datatype.TypeCastException;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Writer JavaDoc;
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.PreparedStatement JavaDoc;
32 import java.sql.ResultSet JavaDoc;
33 import java.sql.SQLException JavaDoc;
34
35 /**
36  * @author Manuel Laflamme
37  * @version $Revision: 1.3 $
38  * @since Jan 12, 2004
39  */

40 public class OracleClobDataType extends ClobDataType
41 {
42     private static final Integer JavaDoc DURATION_SESSION = new Integer JavaDoc(1);
43 // private static final Integer DURATION_CALL = new Integer(2);
44
// private static final Integer MODE_READONLY = new Integer(0);
45
private static final Integer JavaDoc MODE_READWRITE = new Integer JavaDoc(1);
46
47     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
48             throws SQLException JavaDoc, TypeCastException
49     {
50         return typeCast(resultSet.getClob(column));
51     }
52
53     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
54             throws SQLException JavaDoc, TypeCastException
55     {
56         statement.setObject(column, getClob(value, statement.getConnection()));
57     }
58
59     private Object JavaDoc getClob(Object JavaDoc value, Connection JavaDoc connection)
60             throws TypeCastException
61     {
62         Object JavaDoc tempClob = null;
63         try
64         {
65             Class JavaDoc aClobClass = Class.forName("oracle.sql.CLOB");
66
67             // Create new temporary CLOB
68
Method JavaDoc createTemporaryMethod = aClobClass.getMethod("createTemporary",
69                     new Class JavaDoc[]{Connection JavaDoc.class, Boolean.TYPE, Integer.TYPE});
70             tempClob = createTemporaryMethod.invoke(null,
71                     new Object JavaDoc[]{connection, Boolean.TRUE, DURATION_SESSION});
72
73             // Open the temporary CLOB in readwrite mode to enable writing
74
Method JavaDoc openMethod = aClobClass.getMethod("open", new Class JavaDoc[]{Integer.TYPE});
75             openMethod.invoke(tempClob, new Object JavaDoc[]{MODE_READWRITE});
76
77             // Get the output stream to write
78
Method JavaDoc getCharacterOutputStreamMethod = tempClob.getClass().getMethod(
79                     "getCharacterOutputStream", new Class JavaDoc[0]);
80             Writer JavaDoc tempClobWriter = (Writer JavaDoc)getCharacterOutputStreamMethod.invoke(
81                     tempClob, new Object JavaDoc[0]);
82
83             // Write the data into the temporary CLOB
84
tempClobWriter.write((String JavaDoc)typeCast(value));
85
86             // Flush and close the stream
87
tempClobWriter.flush();
88             tempClobWriter.close();
89
90             // Close the temporary CLOB
91
Method JavaDoc closeMethod = tempClob.getClass().getMethod(
92                     "close", new Class JavaDoc[0]);
93             closeMethod.invoke(tempClob, new Object JavaDoc[0]);
94         }
95         catch (IllegalAccessException JavaDoc e)
96         {
97             freeTemporaryClob(tempClob);
98             throw new TypeCastException(value, this, e);
99         }
100         catch (NoSuchMethodException JavaDoc e)
101         {
102             freeTemporaryClob(tempClob);
103             throw new TypeCastException(value, this, e);
104         }
105         catch (IOException JavaDoc e)
106         {
107             freeTemporaryClob(tempClob);
108             throw new TypeCastException(value, this, e);
109         }
110         catch (InvocationTargetException JavaDoc e)
111         {
112             freeTemporaryClob(tempClob);
113             throw new TypeCastException(value, this, e.getTargetException());
114         }
115         catch (ClassNotFoundException JavaDoc e)
116         {
117             freeTemporaryClob(tempClob);
118             throw new TypeCastException(value, this, e);
119         }
120
121         return tempClob;
122     }
123
124
125     private void freeTemporaryClob(Object JavaDoc tempClob) throws TypeCastException
126     {
127         if (tempClob == null)
128         {
129             return;
130         }
131
132         try
133         {
134             Method JavaDoc freeTemporaryMethod = tempClob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]);
135             freeTemporaryMethod.invoke(tempClob, new Object JavaDoc[0]);
136         }
137         catch (NoSuchMethodException JavaDoc e)
138         {
139             throw new TypeCastException("Error freeing Oracle CLOB", e);
140         }
141         catch (IllegalAccessException JavaDoc e)
142         {
143             throw new TypeCastException("Error freeing Oracle CLOB", e);
144         }
145         catch (InvocationTargetException JavaDoc e)
146         {
147             throw new TypeCastException("Error freeing Oracle CLOB", e.getTargetException());
148         }
149     }
150
151 }
152
Popular Tags