Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/derby/iapi/types/SQLBlob.java


1   /*
2   
3      Derby - Class org.apache.derby.iapi.types.SQLBlob
4   
5      Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable.
6   
7      Licensed under the Apache License, Version 2.0 (the "License");
8      you may not use this file except in compliance with the License.
9      You may obtain a copy of the License at
10  
11        http://www.apache.org/licenses/LICENSE-2.0
12  
13     Unless required by applicable law or agreed to in writing, software
14     distributed under the License is distributed on an "AS IS" BASIS,
15     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16     See the License for the specific language governing permissions and
17     limitations under the License.
18  
19   */
20  
21  package org.apache.derby.iapi.types;
22  
23  import org.apache.derby.iapi.types.DataTypeDescriptor;
24  import org.apache.derby.iapi.types.DataValueDescriptor;
25  import org.apache.derby.iapi.types.TypeId;
26  import org.apache.derby.iapi.types.BitDataValue;
27  import org.apache.derby.iapi.types.DataValueDescriptor;
28  import org.apache.derby.iapi.reference.SQLState;
29  import org.apache.derby.iapi.error.StandardException;
30  
31  import org.apache.derby.iapi.types.Orderable;
32  
33  import org.apache.derby.iapi.services.io.FormatIdUtil;
34  import org.apache.derby.iapi.services.io.StoredFormatIds;
35  
36  import org.apache.derby.iapi.services.sanity.SanityManager;
37  
38  import org.apache.derby.iapi.types.BooleanDataValue;
39  import org.apache.derby.iapi.types.StringDataValue;
40  import org.apache.derby.iapi.types.NumberDataValue;
41  
42  import org.apache.derby.iapi.services.io.FormatableBitSet;
43  
44  import java.io.ObjectOutput;
45  import java.io.ObjectInput;
46  import java.io.IOException;
47  import java.sql.ResultSet;
48  import java.sql.SQLException;
49  import java.sql.PreparedStatement;
50  
51  /**
52   * SQLBlob satisfies the DataValueDescriptor,
53   * interfaces (i.e., OrderableDataType). 
54   * It uses the SQLLongVarbit implementation, which implements a String holder,
55   * e.g. for storing a column value; it can be specified
56   * when constructed to not allow nulls. Nullability cannot be changed
57   * after construction.
58   * <p>
59   * Because LOB types are not orderable, we'll override those
60   * methods...
61   *
62   */
63  public class SQLBlob extends SQLBinary
64  {
65  
66    /*
67     * constructors
68     */
69    public SQLBlob()
70          {
71          }
72  
73    public SQLBlob(byte[] val)
74          {
75        super(val);
76          }
77    
78    public String getTypeName()
79          {
80        return TypeId.BLOB_NAME;
81          }
82  
83      /**
84       * @see DataValueDescriptor#getNewNull
85       */
86    public DataValueDescriptor getNewNull()
87          {
88        return new SQLBlob();
89          }
90  
91    /**
92     * Normalization method - this method may be called when putting
93     * a value into a SQLBit, for example, when inserting into a SQLBit
94     * column.  See NormalizeResultSet in execution.
95     *
96     * @param desiredType  The type to normalize the source column to
97     * @param source    The value to normalize
98     *
99     * @exception StandardException        Thrown for null into
100    *                      non-nullable column, and for
101    *                      truncation error
102    */
103 
104   public void normalize(
105         DataTypeDescriptor desiredType,
106         DataValueDescriptor source)
107           throws StandardException
108   {
109     setValue(source);
110     setWidth(desiredType.getMaximumWidth(), 0, true);
111   }
112 
113     // The method setWidth is only(?) used to adopt the value
114     // to the casted domain/size. BLOBs behave different
115     // from the BIT types in that a (CAST (X'01' TO BLOB(1024)))
116     // does NOT pad the value to the maximal allowed datasize.
117     // That it is done for BIT is understandable, however,
118     // for BIT VARYING it is a bit confusing. Could be inheritence bug.
119     // Anyhow, here we just ignore the call, since there is no padding to be done.
120     // We do detect truncation, if the errorOnTrunc flag is set.
121     // DB2 does return a WARNING on CAST and ERROR on INSERT.
122   public DataValueDescriptor setWidth(int desiredWidth,  // ignored!
123       int desiredScale,  // Ignored 
124       boolean errorOnTrunc)
125       throws StandardException
126     {
127 
128     if (isNull())
129       return this;
130 
131     int sourceWidth = getLength();
132 
133         // need to truncate?
134         if (sourceWidth > desiredWidth) {
135             if (errorOnTrunc)
136                 throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(), 
137                                                      "XXXX",
138                                                      String.valueOf(desiredWidth));
139             else {
140                 /*
141                  * Truncate to the desired width.
142                  */
143         
144 
145         byte[] shrunkData = new byte[desiredWidth];
146         System.arraycopy(getBytes(), 0, shrunkData, 0, desiredWidth);
147         dataValue = shrunkData;
148             }
149         }
150 
151         return this;
152     }
153 
154     /**
155      Return my format identifier.
156            
157      @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
158   */
159   public int getTypeFormatId()
160         {
161       return StoredFormatIds.SQL_BLOB_ID;
162         }
163 
164   /** 
165    * @see DataValueDescriptor#setValueFromResultSet 
166    *
167    * @exception SQLException    Thrown on error
168    */
169   public void setValueFromResultSet(ResultSet resultSet, int colNumber,
170                     boolean isNullable)
171     throws SQLException
172   {
173       stream = resultSet.getBinaryStream(colNumber);
174       streamLength = -1; // unknown
175       dataValue = null;
176   }
177 
178 
179 
180   /*
181    * DataValueDescriptor interface
182    */
183         
184   /** @see DataValueDescriptor#typePrecedence */
185   public int typePrecedence()
186     {
187       return TypeId.BLOB_PRECEDENCE; // not really used
188     }
189 
190     public void setInto(PreparedStatement ps, int position)
191     throws SQLException, StandardException
192   {
193     if (isNull()) {
194       ps.setBlob(position, null);    
195       return;
196     }
197 
198     // This may cause problems for streaming blobs, by materializing the whole blob.
199     ps.setBytes(position, getBytes());
200     }
201 }
202 
203