KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > TextType


1 //$Id: TextType.java,v 1.2 2004/09/25 11:22:19 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.IOException JavaDoc;
5 import java.io.Reader JavaDoc;
6 import java.io.StringReader JavaDoc;
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.SQLException JavaDoc;
10 import java.sql.Types JavaDoc;
11
12 import org.hibernate.HibernateException;
13
14 /**
15  * <tt>text</tt>: A type that maps an SQL CLOB to a Java String.
16  * @author Gavin King, Bertrand Renuart
17  */

18 public class TextType extends ImmutableType {
19
20     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
21         String JavaDoc str = (String JavaDoc) value;
22         st.setCharacterStream( index, new StringReader JavaDoc(str), str.length() );
23     }
24
25     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
26
27             // Retrieve the value of the designated column in the current row of this
28
// ResultSet object as a java.io.Reader object
29
Reader JavaDoc charReader = rs.getCharacterStream(name);
30
31             // if the corresponding SQL value is NULL, the reader we got is NULL as well
32
if (charReader==null) return null;
33
34             // Fetch Reader content up to the end - and put characters in a StringBuffer
35
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
36             try {
37                 char[] buffer = new char[2048];
38                 while (true) {
39                     int amountRead = charReader.read(buffer, 0, buffer.length);
40                     if ( amountRead == -1 ) break;
41                     sb.append(buffer, 0, amountRead);
42                 }
43             }
44             catch (IOException JavaDoc ioe) {
45                 throw new HibernateException( "IOException occurred reading text", ioe );
46             }
47             finally {
48                 try {
49                     charReader.close();
50                 }
51                 catch (IOException JavaDoc e) {
52                     throw new HibernateException( "IOException occurred closing stream", e );
53                 }
54             }
55
56             // Return StringBuffer content as a large String
57
return sb.toString();
58     }
59
60     public int sqlType() {
61         return Types.CLOB; //or Types.LONGVARCHAR?
62
}
63
64     public Class JavaDoc getReturnedClass() {
65         return String JavaDoc.class;
66     }
67
68     public String JavaDoc getName() { return "text"; }
69
70     public String JavaDoc toString(Object JavaDoc val) {
71         return (String JavaDoc) val;
72     }
73     public Object JavaDoc fromStringValue(String JavaDoc xml) {
74         return xml;
75     }
76
77 }
78
79
80
81
82
83
84
85
Popular Tags