KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLBlob
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.DataTypeDescriptor;
25 import org.apache.derby.iapi.types.DataValueDescriptor;
26 import org.apache.derby.iapi.types.TypeId;
27 import org.apache.derby.iapi.types.BitDataValue;
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29 import org.apache.derby.iapi.reference.SQLState;
30 import org.apache.derby.iapi.reference.Limits;
31 import org.apache.derby.iapi.error.StandardException;
32
33 import org.apache.derby.iapi.types.Orderable;
34
35 import org.apache.derby.iapi.services.io.FormatIdUtil;
36 import org.apache.derby.iapi.services.io.StoredFormatIds;
37
38 import org.apache.derby.iapi.services.sanity.SanityManager;
39
40 import org.apache.derby.iapi.types.BooleanDataValue;
41 import org.apache.derby.iapi.types.StringDataValue;
42 import org.apache.derby.iapi.types.NumberDataValue;
43
44 import org.apache.derby.iapi.services.io.FormatableBitSet;
45
46 import java.io.ObjectOutput JavaDoc;
47 import java.io.ObjectInput JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.sql.Blob JavaDoc;
50 import java.sql.ResultSet JavaDoc;
51 import java.sql.SQLException JavaDoc;
52 import java.sql.PreparedStatement JavaDoc;
53
54 /**
55  * SQLBlob satisfies the DataValueDescriptor,
56  * interfaces (i.e., OrderableDataType).
57  * It uses the SQLLongVarbit implementation, which implements a String holder,
58  * e.g. for storing a column value; it can be specified
59  * when constructed to not allow nulls. Nullability cannot be changed
60  * after construction.
61  * <p>
62  * Because LOB types are not orderable, we'll override those
63  * methods...
64  *
65  */

66 public class SQLBlob extends SQLBinary
67 {
68
69     /*
70      * constructors
71      */

72     public SQLBlob()
73         {
74         }
75
76     public SQLBlob(byte[] val)
77         {
78             super(val);
79         }
80     
81     public String JavaDoc getTypeName()
82         {
83             return TypeId.BLOB_NAME;
84         }
85
86     /**
87      * Return max memory usage for a SQL Blob
88      */

89     int getMaxMemoryUsage()
90     {
91         return Limits.DB2_LOB_MAXWIDTH;
92     }
93
94     /**
95      * @see DataValueDescriptor#getNewNull
96      */

97     public DataValueDescriptor getNewNull()
98         {
99             return new SQLBlob();
100         }
101
102     /**
103      * Normalization method - this method may be called when putting
104      * a value into a SQLBit, for example, when inserting into a SQLBit
105      * column. See NormalizeResultSet in execution.
106      *
107      * @param desiredType The type to normalize the source column to
108      * @param source The value to normalize
109      *
110      * @exception StandardException Thrown for null into
111      * non-nullable column, and for
112      * truncation error
113      */

114
115     public void normalize(
116                 DataTypeDescriptor desiredType,
117                 DataValueDescriptor source)
118                     throws StandardException
119     {
120         setValue(source);
121         setWidth(desiredType.getMaximumWidth(), 0, true);
122     }
123
124     // The method setWidth is only(?) used to adopt the value
125
// to the casted domain/size. BLOBs behave different
126
// from the BIT types in that a (CAST (X'01' TO BLOB(1024)))
127
// does NOT pad the value to the maximal allowed datasize.
128
// That it is done for BIT is understandable, however,
129
// for BIT VARYING it is a bit confusing. Could be inheritence bug.
130
// Anyhow, here we just ignore the call, since there is no padding to be done.
131
// We do detect truncation, if the errorOnTrunc flag is set.
132
// DB2 does return a WARNING on CAST and ERROR on INSERT.
133
public void setWidth(int desiredWidth, // ignored!
134
int desiredScale, // Ignored
135
boolean errorOnTrunc)
136             throws StandardException
137     {
138
139         // Input is null, so there's nothing to do.
140
if (isNull())
141             return;
142
143         // Input is a stream with unknown length. The length will be checked
144
// while reading the stream.
145
if (isLengthLess()) {
146             return;
147         }
148
149         int sourceWidth = getLength();
150
151         // need to truncate?
152
if (sourceWidth > desiredWidth) {
153             if (errorOnTrunc)
154                 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(),
155                                                      "XXXX",
156                                                      String.valueOf(desiredWidth));
157             else {
158                 /*
159                  * Truncate to the desired width.
160                  */

161                 
162
163                 byte[] shrunkData = new byte[desiredWidth];
164                 System.arraycopy(getBytes(), 0, shrunkData, 0, desiredWidth);
165                 dataValue = shrunkData;
166             }
167         }
168     }
169
170     /**
171      * Gets a trace representation of the BLOB for debugging.
172      *
173      * @return a trace representation of the BLOB.
174      */

175     public final String JavaDoc getTraceString() throws StandardException {
176         // Check if the value is SQL NULL.
177
if (isNull()) {
178             return "NULL";
179         }
180
181         // Check if we have a stream.
182
if (getStream() != null) {
183             return ("BLOB(" + getStream().toString() + ")");
184         }
185
186         return ("BLOB(" + getLength() + ")");
187     }
188
189     /**
190        Return my format identifier.
191            
192        @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
193     */

194     public int getTypeFormatId()
195         {
196             return StoredFormatIds.SQL_BLOB_ID;
197         }
198
199     /**
200      * @see DataValueDescriptor#setValueFromResultSet
201      *
202      * @exception SQLException Thrown on error
203      * @throws StandardException
204      */

205     public void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
206                                       boolean isNullable)
207         throws SQLException JavaDoc, StandardException
208     {
209         Blob JavaDoc blob = resultSet.getBlob(colNumber);
210         if (blob == null)
211             setToNull();
212         else
213             setObject(blob);
214     }
215
216
217
218     /*
219      * DataValueDescriptor interface
220      */

221         
222     /** @see DataValueDescriptor#typePrecedence */
223     public int typePrecedence()
224         {
225             return TypeId.BLOB_PRECEDENCE; // not really used
226
}
227
228     public void setInto(PreparedStatement JavaDoc ps, int position)
229         throws SQLException JavaDoc, StandardException
230     {
231         if (isNull()) {
232             ps.setBlob(position, null);
233             return;
234         }
235
236         // This may cause problems for streaming blobs, by materializing the whole blob.
237
ps.setBytes(position, getBytes());
238     }
239     
240     /**
241      * Set the value from an non-null object.
242      */

243     final void setObject(Object JavaDoc theValue)
244         throws StandardException
245     {
246         Blob JavaDoc vb = (Blob JavaDoc) theValue;
247         
248         try {
249             long vbl = vb.length();
250             if (vbl < 0L || vbl > Integer.MAX_VALUE)
251                 throw this.outOfRange();
252             
253             setValue(new RawToBinaryFormatStream(
254                     vb.getBinaryStream(), (int) vbl),
255                     (int) vbl);
256             
257         } catch (SQLException JavaDoc e) {
258             throw dataTypeConversion("DAN-438-tmp");
259        }
260     }
261
262     /**
263      * Tell if this blob is length less.
264      *
265      * @return <code>true</code> if the length of the blob is not known,
266      * <code>false</code> otherwise
267      */

268     private final boolean isLengthLess() {
269         return (stream != null && streamValueLength < 0);
270     }
271 }
272
273
274
Popular Tags