Mercurial > hg > monetdb-java
comparison src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java @ 0:a5a898f6886c
Copy of MonetDB java directory changeset e6e32756ad31.
author | Sjoerd Mullender <sjoerd@acm.org> |
---|---|
date | Wed, 21 Sep 2016 09:34:48 +0200 (2016-09-21) |
parents | |
children | 7e0d71a22677 e092fa8d9ab7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a5a898f6886c |
---|---|
1 /* | |
2 * This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
5 * | |
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V. | |
7 */ | |
8 | |
9 package nl.cwi.monetdb.jdbc; | |
10 | |
11 import java.sql.*; | |
12 import java.io.*; | |
13 | |
14 /** | |
15 * The MonetClob class implements the {@link java.sql.Clob} interface. Because | |
16 * MonetDB/SQL currently has no support for streams, this class is a | |
17 * shallow wrapper of a {@link StringBuilder}. It is more or less supplied to | |
18 * enable an application that depends on it to run. It may be obvious | |
19 * that it is a real resource expensive workaround that contradicts the | |
20 * sole reason for a Clob: avoidance of huge resource consumption. | |
21 * <b>Use of this class is highly discouraged.</b> | |
22 * | |
23 * @author Fabian Groffen | |
24 */ | |
25 public class MonetClob implements Clob { | |
26 | |
27 private StringBuilder buf; | |
28 | |
29 protected MonetClob(String in) { | |
30 buf = new StringBuilder(in); | |
31 } | |
32 | |
33 //== begin interface Clob | |
34 | |
35 /** | |
36 * This method frees the Clob object and releases the resources the | |
37 * resources that it holds. The object is invalid once the free | |
38 * method is called. | |
39 * | |
40 * After free has been called, any attempt to invoke a method other | |
41 * than free will result in a SQLException being thrown. If free is | |
42 * called multiple times, the subsequent calls to free are treated | |
43 * as a no-op. | |
44 */ | |
45 @Override | |
46 public void free() { | |
47 buf = null; | |
48 } | |
49 | |
50 /** | |
51 * Retrieves the CLOB value designated by this Clob object as an | |
52 * ascii stream. | |
53 * | |
54 * @return a java.io.InputStream object containing the CLOB data | |
55 * @throws SQLFeatureNotSupportedException this JDBC driver does | |
56 * not support this method | |
57 */ | |
58 @Override | |
59 public InputStream getAsciiStream() throws SQLException { | |
60 throw new SQLFeatureNotSupportedException("Operation getAsciiStream() currently not supported", "0A000"); | |
61 } | |
62 | |
63 /** | |
64 * Retrieves the CLOB value designated by this Clob object as a | |
65 * java.io.Reader object (or as a stream of characters). | |
66 * | |
67 * @return a java.io.Reader object containing the CLOB data | |
68 * @throws SQLFeatureNotSupportedException this JDBC driver does | |
69 * not support this method | |
70 */ | |
71 @Override | |
72 public Reader getCharacterStream() throws SQLException { | |
73 throw new SQLFeatureNotSupportedException("Operation getCharacterStream() currently not supported", "0A000"); | |
74 } | |
75 | |
76 /** | |
77 * Returns a Reader object that contains a partial Clob value, | |
78 * starting with the character specified by pos, which is length | |
79 * characters in length. | |
80 * | |
81 * @param pos the offset to the first character of the partial value | |
82 * to be retrieved. The first character in the Clob is at | |
83 * position 1. | |
84 * @param length the length in characters of the partial value to be | |
85 * retrieved. | |
86 * @return Reader through which the partial Clob value can be read. | |
87 * @throws SQLFeatureNotSupportedException this JDBC driver does | |
88 * not support this method | |
89 */ | |
90 @Override | |
91 public Reader getCharacterStream(long pos, long length) throws SQLException { | |
92 throw new SQLFeatureNotSupportedException("Operation getCharacterStream(long, long) currently not supported", "0A000"); | |
93 } | |
94 | |
95 /** | |
96 * Retrieves a copy of the specified substring in the CLOB value | |
97 * designated by this Clob object. The substring begins at | |
98 * position pos and has up to length consecutive characters. | |
99 * | |
100 * @param pos the first character of the substring to be | |
101 * extracted. The first character is at position 1. | |
102 * @param length the number of consecutive characters to be copied | |
103 * @return a String that is the specified substring in the | |
104 * CLOB value designated by this Clob object | |
105 * @throws SQLException if there is an error accessing the | |
106 * CLOB value | |
107 */ | |
108 @Override | |
109 public String getSubString(long pos, int length) throws SQLException { | |
110 if (buf == null) | |
111 throw new SQLException("This Clob has been freed", "M1M20"); | |
112 try { | |
113 return buf.substring((int)(pos - 1), (int)(pos - 1 + length)); | |
114 } catch (IndexOutOfBoundsException e) { | |
115 throw new SQLException(e.getMessage()); | |
116 } | |
117 } | |
118 | |
119 /** | |
120 * Retrieves the number of characters in the CLOB value designated | |
121 * by this Clob object. | |
122 * | |
123 * @return length of the CLOB in characters | |
124 * @throws SQLException if there is an error accessing the length | |
125 * of the CLOB value | |
126 */ | |
127 @Override | |
128 public long length() throws SQLException { | |
129 if (buf == null) | |
130 throw new SQLException("This Clob has been freed", "M1M20"); | |
131 return (long)buf.length(); | |
132 } | |
133 | |
134 /** | |
135 * Retrieves the character position at which the specified Clob | |
136 * object searchstr appears in this Clob object. The search | |
137 * begins at position start. | |
138 * | |
139 * @param searchstr the Clob object for which to search | |
140 * @param start the position at which to begin searching; | |
141 * the first position is 1 | |
142 * @return the position at which the Clob object appears or | |
143 * -1 if it is not present; the first position is 1 | |
144 * @throws SQLException if there is an error accessing the | |
145 * CLOB value | |
146 */ | |
147 @Override | |
148 public long position(Clob searchstr, long start) throws SQLException { | |
149 return position(searchstr.getSubString(1L, (int)(searchstr.length())), start); | |
150 } | |
151 | |
152 /** | |
153 * Retrieves the character position at which the specified | |
154 * substring searchstr appears in the SQL CLOB value represented | |
155 * by this Clob object. The search begins at position start. | |
156 * | |
157 * @param searchstr the substring for which to search | |
158 * @param start the position at which to begin searching; | |
159 * the first position is 1 | |
160 * @return the position at which the substring appears or | |
161 * -1 if it is not present; the first position is 1 | |
162 * @throws SQLException if there is an error accessing the | |
163 * CLOB value | |
164 */ | |
165 @Override | |
166 public long position(String searchstr, long start) throws SQLException { | |
167 if (buf == null) | |
168 throw new SQLException("This Clob has been freed", "M1M20"); | |
169 return (long)(buf.indexOf(searchstr, (int)(start - 1))); | |
170 } | |
171 | |
172 @Override | |
173 public OutputStream setAsciiStream(long pos) throws SQLException { | |
174 if (buf == null) | |
175 throw new SQLException("This Clob has been freed", "M1M20"); | |
176 throw new SQLException("Operation setAsciiStream(long pos) currently not supported", "0A000"); | |
177 } | |
178 | |
179 @Override | |
180 public Writer setCharacterStream(long pos) throws SQLException { | |
181 if (buf == null) | |
182 throw new SQLException("This Clob has been freed", "M1M20"); | |
183 throw new SQLException("Operation setCharacterStream(long pos) currently not supported", "0A000"); | |
184 } | |
185 | |
186 /** | |
187 * Writes the given Java String to the CLOB value that this | |
188 * Clob object designates at the position pos. | |
189 * | |
190 * @param pos the position at which to start writing to the | |
191 * CLOB value that this Clob object represents | |
192 * @param str the string to be written to the CLOB value that | |
193 * this Clob designates | |
194 * @return the number of characters written | |
195 * @throws SQLException if there is an error accessing the | |
196 * CLOB value | |
197 */ | |
198 @Override | |
199 public int setString(long pos, String str) throws SQLException { | |
200 return setString(pos, str, 1, str.length()); | |
201 } | |
202 | |
203 /** | |
204 * Writes len characters of str, starting at character offset, | |
205 * to the CLOB value that this Clob represents. | |
206 * | |
207 * @param pos the position at which to start writing to this | |
208 * CLOB object | |
209 * @param str the string to be written to the CLOB value that | |
210 * this Clob object represents | |
211 * @param offset the offset into str to start reading the | |
212 * characters to be written | |
213 * @param len the number of characters to be written | |
214 * @return the number of characters written | |
215 * @throws SQLException if there is an error accessing the | |
216 * CLOB value | |
217 */ | |
218 @Override | |
219 public int setString(long pos, String str, int offset, int len) | |
220 throws SQLException | |
221 { | |
222 if (buf == null) | |
223 throw new SQLException("This Clob has been freed", "M1M20"); | |
224 | |
225 int buflen = buf.length(); | |
226 int retlen = Math.min(buflen, (int)(pos - 1 + len)); | |
227 | |
228 if (retlen > 0) { | |
229 buf.replace((int)(pos - 1), (int)(pos + retlen), str.substring(offset - 1, (offset + len))); | |
230 return retlen; | |
231 } else { | |
232 return 0; | |
233 } | |
234 } | |
235 | |
236 /** | |
237 * Truncates the CLOB value that this Clob designates to | |
238 * have a length of len characters. | |
239 * | |
240 * @param len the length, in bytes, to which the CLOB value | |
241 * should be truncated | |
242 * @throws SQLException if there is an error accessing the | |
243 * CLOB value | |
244 */ | |
245 @Override | |
246 public void truncate(long len) throws SQLException { | |
247 if (buf == null) | |
248 throw new SQLException("This Clob has been freed", "M1M20"); | |
249 // this command is a no-op | |
250 } | |
251 | |
252 /** | |
253 * Returns the String behind this Clob. This is a MonetClob | |
254 * extension that does not violate nor is described in the Clob | |
255 * interface. | |
256 * | |
257 * @return the String this Clob wraps. | |
258 */ | |
259 @Override | |
260 public String toString() { | |
261 if (buf == null) | |
262 return "<a freed MonetClob instance>"; | |
263 return buf.toString(); | |
264 } | |
265 } |