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