KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > types > SQLClob


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLClob
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.types;
23
24 import org.apache.derby.iapi.types.DataValueDescriptor;
25 import org.apache.derby.iapi.types.TypeId;
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.reference.SQLState;
29 import org.apache.derby.iapi.services.io.StoredFormatIds;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import java.sql.Blob JavaDoc;
34 import java.sql.Clob JavaDoc;
35 import java.sql.Date JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.Time JavaDoc;
38 import java.sql.Timestamp JavaDoc;
39 import java.util.Calendar JavaDoc;
40
41
42 /**
43  * SQLClob uses SQLVarchar by inheritance.
44  * It satisfies the DataValueDescriptor interfaces (i.e., OrderableDataType). It implements a String
45  * holder, e.g. for storing a column value; it can be specified
46  * when constructed to not allow nulls. Nullability cannot be changed
47  * after construction.
48  * <p>
49  * Because OrderableDataType is a subclass of DataType,
50  * SQLLongvarchar can play a role in either a DataType/ValueRow
51  * or a OrderableDataType/KeyRow, interchangeably.
52  */

53 public class SQLClob
54     extends SQLVarchar
55 {
56     /*
57      * DataValueDescriptor interface.
58      *
59      * These are actually all implemented in the super-class, but we need
60      * to duplicate some of them here so they can be called by byte-code
61      * generation, which needs to know the class the method appears in.
62      */

63
64     public String JavaDoc getTypeName()
65     {
66         return TypeId.CLOB_NAME;
67     }
68
69     /*
70      * DataValueDescriptor interface
71      */

72
73     /** @see DataValueDescriptor#getClone */
74     public DataValueDescriptor getClone()
75     {
76         try
77         {
78             return new SQLClob(getString());
79         }
80         catch (StandardException se)
81         {
82             if (SanityManager.DEBUG)
83                 SanityManager.THROWASSERT("Unexpected exception " + se);
84             return null;
85         }
86     }
87
88     /**
89      * @see DataValueDescriptor#getNewNull
90      *
91      */

92     public DataValueDescriptor getNewNull()
93     {
94         return new SQLClob();
95     }
96
97     /*
98      * Storable interface, implies Externalizable, TypedFormat
99      */

100
101     /**
102         Return my format identifier.
103
104         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
105     */

106     public int getTypeFormatId() {
107         return StoredFormatIds.SQL_CLOB_ID;
108     }
109
110     /*
111      * constructors
112      */

113
114     public SQLClob()
115     {
116     }
117
118     public SQLClob(String JavaDoc val)
119     {
120         super(val);
121     }
122
123     /*
124      * DataValueDescriptor interface
125      */

126
127     /* @see DataValueDescriptor#typePrecedence */
128     public int typePrecedence()
129     {
130         return TypeId.CLOB_PRECEDENCE;
131     }
132
133     /*
134     ** disable conversions to/from most types for CLOB.
135     ** TEMP - real fix is to re-work class hierachy so
136     ** that CLOB is towards the root, not at the leaf.
137     */

138
139     public Object JavaDoc getObject() throws StandardException
140     {
141         throw dataTypeConversion("java.lang.Object");
142     }
143
144     public boolean getBoolean() throws StandardException
145     {
146         throw dataTypeConversion("boolean");
147     }
148
149     public byte getByte() throws StandardException
150     {
151         throw dataTypeConversion("byte");
152     }
153
154     public short getShort() throws StandardException
155     {
156         throw dataTypeConversion("short");
157     }
158
159     public int getInt() throws StandardException
160     {
161         throw dataTypeConversion("int");
162     }
163
164     public long getLong() throws StandardException
165     {
166         throw dataTypeConversion("long");
167     }
168
169     public float getFloat() throws StandardException
170     {
171         throw dataTypeConversion("float");
172     }
173
174     public double getDouble() throws StandardException
175     {
176         throw dataTypeConversion("double");
177     }
178     public int typeToBigDecimal() throws StandardException
179     {
180         throw dataTypeConversion("java.math.BigDecimal");
181     }
182     public byte[] getBytes() throws StandardException
183     {
184         throw dataTypeConversion("byte[]");
185     }
186
187     public Date JavaDoc getDate(java.util.Calendar JavaDoc cal) throws StandardException
188     {
189         throw dataTypeConversion("java.sql.Date");
190     }
191
192     public Time JavaDoc getTime(java.util.Calendar JavaDoc cal) throws StandardException
193     {
194         throw dataTypeConversion("java.sql.Time");
195     }
196
197     public Timestamp JavaDoc getTimestamp(java.util.Calendar JavaDoc cal) throws StandardException
198     {
199         throw dataTypeConversion("java.sql.Timestamp");
200     }
201     
202     /**
203      * Gets a trace representation of the CLOB for debugging.
204      *
205      * @return a trace representation of the CLOB.
206      */

207     public final String JavaDoc getTraceString() throws StandardException {
208         // Check if the value is SQL NULL.
209
if (isNull()) {
210             return "NULL";
211         }
212
213         // Check if we have a stream.
214
if (getStream() != null) {
215             return ("CLOB(" + getStream().toString() + ")");
216         }
217
218         return ("CLOB(" + getLength() + ")");
219     }
220     
221     /**
222      * Normalization method - this method may be called when putting
223      * a value into a SQLClob, for example, when inserting into a SQLClob
224      * column. See NormalizeResultSet in execution.
225      * Per the SQL standard ,if the clob column is not big enough to
226      * hold the value being inserted,truncation error will result
227      * if there are trailing non-blanks. Truncation of trailing blanks
228      * is allowed.
229      * @param desiredType The type to normalize the source column to
230      * @param sourceValue The value to normalize
231      *
232      *
233      * @exception StandardException Thrown for null into
234      * non-nullable column, and for
235      * truncation error
236      */

237
238     public void normalize(
239                 DataTypeDescriptor desiredType,
240                 DataValueDescriptor sourceValue)
241                     throws StandardException
242     {
243         // if sourceValue is of type clob, and has a stream,
244
// dont materialize it here (as the goal of using a stream is to
245
// not have to materialize whole object in memory in the server),
246
// but instead truncation checks will be done when data is streamed in.
247
// (see ReaderToUTF8Stream)
248
// if sourceValue is not a stream, then follow the same
249
// protocol as varchar type for normalization
250
if( sourceValue instanceof SQLClob)
251         {
252             SQLClob clob = (SQLClob)sourceValue;
253             if (clob.stream != null)
254             {
255                 copyState(clob);
256                 return;
257             }
258         }
259         
260         super.normalize(desiredType,sourceValue);
261     }
262
263     public void setValue(Time JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
264     {
265         throwLangSetMismatch("java.sql.Time");
266     }
267     
268     public void setValue(Timestamp JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
269     {
270         throwLangSetMismatch("java.sql.Timestamp");
271     }
272     
273     public void setValue(Date JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
274     {
275         throwLangSetMismatch("java.sql.Date");
276     }
277     
278     public void setBigDecimal(Number JavaDoc bigDecimal) throws StandardException
279     {
280         throwLangSetMismatch("java.math.BigDecimal");
281     }
282
283     public void setValue(int theValue) throws StandardException
284     {
285         throwLangSetMismatch("int");
286     }
287
288     public void setValue(double theValue) throws StandardException
289     {
290         throwLangSetMismatch("double");
291     }
292
293     public void setValue(float theValue) throws StandardException
294     {
295         throwLangSetMismatch("float");
296     }
297  
298     public void setValue(short theValue) throws StandardException
299     {
300         throwLangSetMismatch("short");
301     }
302
303     public void setValue(long theValue) throws StandardException
304     {
305         throwLangSetMismatch("long");
306     }
307
308
309     public void setValue(byte theValue) throws StandardException
310     {
311         throwLangSetMismatch("byte");
312     }
313
314     public void setValue(boolean theValue) throws StandardException
315     {
316         throwLangSetMismatch("boolean");
317     }
318
319     public void setValue(byte[] theValue) throws StandardException
320     {
321         throwLangSetMismatch("byte[]");
322     }
323     
324     /**
325      * Set the value from an non-null Java.sql.Clob object.
326      */

327     final void setObject(Object JavaDoc theValue)
328         throws StandardException
329     {
330         Clob JavaDoc vc = (Clob JavaDoc) theValue;
331         
332         try {
333             long vcl = vc.length();
334             if (vcl < 0L || vcl > Integer.MAX_VALUE)
335                 throw this.outOfRange();
336             
337             setValue(new ReaderToUTF8Stream(vc.getCharacterStream(),
338                     (int) vcl, 0, TypeId.CLOB_NAME), (int) vcl);
339             
340         } catch (SQLException JavaDoc e) {
341             throw dataTypeConversion("DAN-438-tmp");
342        }
343     }
344 }
345
Popular Tags