File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcClob.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbc;
8
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.OutputStream;
12 import java.io.PipedInputStream;
13 import java.io.PipedOutputStream;
14 import java.io.Reader;
15 import java.io.StringReader;
16 import java.io.StringWriter;
17 import java.io.Writer;
18 import java.sql.Clob;
19 //## Java 1.6 ##
20 import java.sql.NClob;
21 //*/
22 import java.sql.SQLException;
23 import org.h2.constant.ErrorCode;
24 import org.h2.engine.Constants;
25 import org.h2.message.DbException;
26 import org.h2.message.TraceObject;
27 import org.h2.util.Task;
28 import org.h2.util.IOUtils;
29 import org.h2.value.Value;
30
31 /**
32 * Represents a CLOB value.
33 */
34 public class JdbcClob extends TraceObject implements Clob
35 //## Java 1.6 ##
36 , NClob
37 //*/
38 {
39
40 Value value;
41 private final JdbcConnection conn;
42
43 /**
44 * INTERNAL
45 */
46 public JdbcClob(JdbcConnection conn, Value value, int id) {
47 setTrace(conn.getSession().getTrace(), TraceObject.CLOB, id);
48 this.conn = conn;
49 this.value = value;
50 }
51
52 /**
53 * Returns the length.
54 *
55 * @return the length
56 */
57 public long length() throws SQLException {
58 try {
59 debugCodeCall("length");
60 checkClosed();
61 if (value.getType() == Value.CLOB) {
62 long precision = value.getPrecision();
63 if (precision > 0) {
64 return precision;
65 }
66 }
67 return IOUtils.copyAndCloseInput(value.getReader(), null, Long.MAX_VALUE);
68 } catch (Exception e) {
69 throw logAndConvert(e);
70 }
71 }
72
73 /**
74 * [Not supported] Truncates the object.
75 */
76 public void truncate(long len) throws SQLException {
77 throw unsupported("LOB update");
78 }
79
80 /**
81 * Returns the input stream.
82 *
83 * @return the input stream
84 */
85 public InputStream getAsciiStream() throws SQLException {
86 try {
87 debugCodeCall("getAsciiStream");
88 checkClosed();
89 String s = value.getString();
90 return IOUtils.getInputStreamFromString(s);
91 } catch (Exception e) {
92 throw logAndConvert(e);
93 }
94 }
95
96 /**
97 * [Not supported] Returns an output stream.
98 */
99 public OutputStream setAsciiStream(long pos) throws SQLException {
100 throw unsupported("LOB update");
101 }
102
103 /**
104 * Returns the reader.
105 *
106 * @return the reader
107 */
108 public Reader getCharacterStream() throws SQLException {
109 try {
110 debugCodeCall("getCharacterStream");
111 checkClosed();
112 return value.getReader();
113 } catch (Exception e) {
114 throw logAndConvert(e);
115 }
116 }
117
118 /**
119 * Get a writer to update the Clob. This is only supported for new, empty
120 * Clob objects that were created with Connection.createClob() or
121 * createNClob(). The Clob is created in a separate thread, and the object
122 * is only updated when Writer.close() is called. The position must be 1,
123 * meaning the whole Clob data is set.
124 *
125 * @param pos where to start writing (the first character is at position 1)
126 * @return a writer
127 */
128 public Writer setCharacterStream(long pos) throws SQLException {
129 try {
130 if (isDebugEnabled()) {
131 debugCodeCall("setCharacterStream(" + pos + ");");
132 }
133 checkClosed();
134 if (pos != 1) {
135 throw DbException.getInvalidValueException("pos", pos);
136 }
137 if (value.getPrecision() != 0) {
138 throw DbException.getInvalidValueException("length", value.getPrecision());
139 }
140 final JdbcConnection c = conn;
141 // PipedReader / PipedWriter are a lot slower
142 // than PipedInputStream / PipedOutputStream
143 // (Sun/Oracle Java 1.6.0_20)
144 final PipedInputStream in = new PipedInputStream();
145 final Task task = new Task() {
146 public void call() {
147 value = c.createClob(IOUtils.getReader(in), -1);
148 }
149 };
150 PipedOutputStream out = new PipedOutputStream(in) {
151 public void close() throws IOException {
152 super.close();
153 try {
154 task.get();
155 } catch (Exception e) {
156 throw DbException.convertToIOException(e);
157 }
158 }
159 };
160 task.execute();
161 return IOUtils.getBufferedWriter(out);
162 } catch (Exception e) {
163 throw logAndConvert(e);
164 }
165 }
166
167 /**
168 * Returns a substring.
169 *
170 * @param pos the position (the first character is at position 1)
171 * @param length the number of characters
172 * @return the string
173 */
174 public String getSubString(long pos, int length) throws SQLException {
175 try {
176 if (isDebugEnabled()) {
177 debugCode("getSubString(" + pos + ", " + length + ");");
178 }
179 checkClosed();
180 if (pos < 1) {
181 throw DbException.getInvalidValueException("pos", pos);
182 }
183 if (length < 0) {
184 throw DbException.getInvalidValueException("length", length);
185 }
186 StringWriter writer = new StringWriter(Math.min(Constants.IO_BUFFER_SIZE, length));
187 Reader reader = value.getReader();
188 try {
189 IOUtils.skipFully(reader, pos - 1);
190 IOUtils.copyAndCloseInput(reader, writer, length);
191 } finally {
192 reader.close();
193 }
194 return writer.toString();
195 } catch (Exception e) {
196 throw logAndConvert(e);
197 }
198 }
199
200 /**
201 * Fills the Clob. This is only supported for new, empty Clob objects that
202 * were created with Connection.createClob() or createNClob(). The position
203 * must be 1, meaning the whole Clob data is set.
204 *
205 * @param pos where to start writing (the first character is at position 1)
206 * @param str the string to add
207 * @return the length of the added text
208 */
209 public int setString(long pos, String str) throws SQLException {
210 try {
211 if (isDebugEnabled()) {
212 debugCode("setString(" + pos + ", " + quote(str) + ");");
213 }
214 checkClosed();
215 if (pos != 1) {
216 throw DbException.getInvalidValueException("pos", pos);
217 } else if (str == null) {
218 throw DbException.getInvalidValueException("str", str);
219 }
220 value = conn.createClob(new StringReader(str), -1);
221 return str.length();
222 } catch (Exception e) {
223 throw logAndConvert(e);
224 }
225 }
226
227 /**
228 * [Not supported] Sets a substring.
229 */
230 public int setString(long pos, String str, int offset, int len) throws SQLException {
231 throw unsupported("LOB update");
232 }
233
234 /**
235 * [Not supported] Searches a pattern and return the position.
236 */
237 public long position(String pattern, long start) throws SQLException {
238 throw unsupported("LOB search");
239 }
240
241 /**
242 * [Not supported] Searches a pattern and return the position.
243 */
244 public long position(Clob clobPattern, long start) throws SQLException {
245 throw unsupported("LOB search");
246 }
247
248 /**
249 * Release all resources of this object.
250 */
251 public void free() {
252 debugCodeCall("free");
253 value = null;
254 }
255
256 /**
257 * [Not supported] Returns the reader, starting from an offset.
258 */
259 public Reader getCharacterStream(long pos, long length) throws SQLException {
260 throw unsupported("LOB subset");
261 }
262
263 private void checkClosed() {
264 conn.checkClosed();
265 if (value == null) {
266 throw DbException.get(ErrorCode.OBJECT_CLOSED);
267 }
268 }
269
270 /**
271 * INTERNAL
272 */
273 public String toString() {
274 return getTraceObjectName() + ": " + (value == null ? "null" : value.getTraceSQL());
275 }
276
277 }
278