001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Nicolas Modrzyk.
021: * Contributor(s): Emmanuel Cecchet.
022: */package org.continuent.sequoia.common.protocol;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.OutputStream;
026: import java.io.Serializable;
027: import java.io.StringReader;
028: import java.io.Writer;
029: import java.sql.SQLException;
030:
031: import org.continuent.sequoia.common.exceptions.NotImplementedException;
032:
033: /**
034: * The representation (mapping) in the Java <sup><small>TM </small> </sup>
035: * programming language of an SQL <code>CLOB</code> value. An SQL
036: * <code>CLOB</code> is a built-in type that stores a Character Large Object
037: * as a column value in a row of a database table. By default drivers implement
038: * <code>Clob</code> using an SQL <code>locator(CLOB)</code>, which means
039: * that a <code>Clob</code> object contains a logical pointer to the SQL
040: * <code>CLOB</code> data rather than the data itself. A <code>Clob</code>
041: * object is valid for the duration of the transaction in which is was created.
042: * <p>
043: * Methods in the interfaces {@link java.sql.ResultSet},
044: * {@link java.sql.CallableStatement}, and {@link java.sql.PreparedStatement},
045: * such as <code>getClob</code> and <code>setClob</code> allow a programmer
046: * to access an SQL <code>CLOB</code> value. The <code>Clob</code> interface
047: * provides methods for getting the length of an SQL <code>CLOB</code>
048: * (Character Large Object) value, for materializing a <code>CLOB</code> value
049: * on the client, and for determining the position of a pattern of bytes within
050: * a <code>CLOB</code> value. In addition, this interface has methods for
051: * updating a <code>CLOB</code> value.
052: *
053: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
054: * @author <a href="mailto:Emmanuel.Cecchet@emicnetworks.fr">Emmanuel Cecchet
055: * </a>
056: * @since JDK 1.2
057: */
058: public class StringClob implements java.sql.Clob, Serializable {
059: private static final long serialVersionUID = 1832832422588968988L;
060:
061: /** The data represented as a string of this <code>CLOB</code> */
062: private String stringData = null;
063:
064: /**
065: * Creates a new <code>Clob</code> instance.
066: *
067: * @param data a <code>String</code> of character data
068: */
069: public StringClob(String data) {
070: stringData = data;
071: }
072:
073: /**
074: * Returns the size of the <code>CLOB</code> value designated by this
075: * <code>Clob</code> object
076: *
077: * @return length of the <code>CLOB</code> value that this <code>clob</code>
078: * represents
079: * @exception SQLException if there is an error accessing the length of the
080: * <code>CLOB</code>
081: * @since JDK 1.2
082: */
083: public long length() throws SQLException {
084: return stringData.length();
085: }
086:
087: /**
088: * Retrieves the <code>CLOB</code> value designated by this
089: * <code>Clob</code> instance as a stream.
090: *
091: * @return a stream containing the <code>CLOB</code> data
092: * @exception SQLException if there is an error accessing the
093: * <code>CLOB</code> value
094: * @since JDK 1.2
095: */
096: public java.io.InputStream getAsciiStream() throws SQLException {
097: return new ByteArrayInputStream(stringData.getBytes());
098: }
099:
100: /**
101: * Materializes the <code>CLOB</code> value designated by this <Code>object
102: * as a stream of Unicode character.
103: *
104: * @return A reader object with all the data in the <code>CLOB</code> value
105: * designated by this clob object as unicode characters.
106: * @exception SQLException if there is an error accessing the
107: * <code>CLOB</code> value
108: */
109: public java.io.Reader getCharacterStream() throws SQLException {
110: return new StringReader(stringData);
111: }
112:
113: /**
114: * @see java.sql.Clob#getSubString(long, int)
115: */
116: public String getSubString(long sqlPos, int wantedLength)
117: throws SQLException {
118: ByteArrayBlob.checkSQLRangeIsSupported(sqlPos, wantedLength);
119: // SQL counts from one, whereas real languages count from zero
120: int endIndex = Math.min(
121: // no further than what we have
122: stringData.length() - 1,
123: // no further than what is asked
124: (int) (sqlPos - 1) + wantedLength - 1);
125: return stringData.substring((int) sqlPos - 1, endIndex);
126: }
127:
128: /**
129: * Retrieves the character position at which the specified string
130: * <code>searchstr</code> begins within the <code>CLOB</code> value that
131: * this <code>Clob</code> object represents. The search for
132: * <code>searchstr</code> begins at position <code>start</code>.
133: *
134: * @param searchstr the byte array for which to search
135: * @param start the position at which to begin searching; the first position
136: * is 1
137: * @return the position at which the pattern appears, else -1
138: * @exception SQLException if there is an error accessing the
139: * <code>CLOB</code>
140: * @since JDK 1.2
141: */
142: public long position(String searchstr, long start)
143: throws SQLException {
144: return stringData.indexOf(searchstr, (int) start);
145: }
146:
147: /**
148: * Retrieves the character position at which the specified <code>Clob</code>
149: * object <code>searchstr</code> begins within the <code>CLOB</code> value
150: * that this <code>Clob</code> object represents. The search for
151: * <code>searchstr</code> begins at position <code>start</code>.
152: *
153: * @param searchstr the byte array for which to search
154: * @param start the position at which to begin searching; the first position
155: * is 1
156: * @return the position at which the pattern appears, else -1
157: * @exception SQLException if there is an error accessing the
158: * <code>CLOB</code>
159: * @since JDK 1.2
160: */
161: public long position(java.sql.Clob searchstr, long start)
162: throws SQLException {
163: return position(searchstr.getSubString(0, (int) searchstr
164: .length()), (int) start);
165: }
166:
167: // -------------------------- JDBC 3.0 -----------------------------------
168:
169: /**
170: * Retrieves a stream to be used to write Ascii characters to the CLOB value
171: * that this Clob object represents, starting at position pos.
172: *
173: * @param pos the position where to start the stream
174: * @return the ascii outputstream to this <code>clob</code> object
175: * @throws SQLException if there is an error accessing the <code>clob</code>
176: */
177: public OutputStream setAsciiStream(long pos) throws SQLException {
178: throw new NotImplementedException("setAsciiStream");
179: }
180:
181: /**
182: * Retrieves a stream to be used to write a stream of Unicode characters to
183: * the CLOB value that this Clob object represents, at position pos.
184: *
185: * @param pos the position where to start the writer
186: * @return the writer to this <code>clob</code> object
187: * @throws SQLException if there is an error accessing the <code>clob</code>
188: */
189: public Writer setCharacterStream(long pos) throws SQLException {
190: throw new NotImplementedException("setCharacterStream");
191: }
192:
193: /**
194: * Writes the given Java String to the CLOB value that this Clob object
195: * designates at the position pos.
196: *
197: * @param pos the position where to set the string
198: * @param str string to insert in the <code>clob</code>
199: * @return return value
200: * @throws SQLException if there is an error accessing the <code>clob</code>
201: */
202: public int setString(long pos, String str) throws SQLException {
203: throw new NotImplementedException("setString");
204: }
205:
206: /**
207: * Writes len characters of str, starting at character offset, to the CLOB
208: * value that this Clob represents.
209: *
210: * @param pos the position
211: * @param str the string
212: * @param offset the offset
213: * @param len the length
214: * @return return value
215: * @throws SQLException if there is an error accessing the <code>clob</code>
216: */
217: public int setString(long pos, String str, int offset, int len)
218: throws SQLException {
219: throw new NotImplementedException("setString");
220: }
221:
222: /**
223: * Truncates the CLOB value that this Clob designates to have a length of len
224: * characters.
225: *
226: * @param len the length
227: * @throws SQLException if there is an error accessing the <code>clob</code>
228: */
229: public void truncate(long len) throws SQLException {
230: throw new NotImplementedException("truncate");
231: }
232: }
|