Debian Codegraph @

src/main/org/h2/jdbc/JdbcClob.java

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