KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLBit
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.reference.SQLState;
25 import org.apache.derby.iapi.reference.Limits;
26
27 import org.apache.derby.iapi.services.io.ArrayInputStream;
28
29 import org.apache.derby.iapi.types.DataTypeDescriptor;
30 import org.apache.derby.iapi.types.DataValueDescriptor;
31 import org.apache.derby.iapi.types.TypeId;
32 import org.apache.derby.iapi.types.BitDataValue;
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34 import org.apache.derby.iapi.types.ConcatableDataValue;
35 import org.apache.derby.iapi.types.VariableSizeDataValue;
36 import org.apache.derby.iapi.error.StandardException;
37
38 import org.apache.derby.iapi.services.io.FormatIdUtil;
39 import org.apache.derby.iapi.services.io.StoredFormatIds;
40 import org.apache.derby.iapi.services.io.StreamStorable;
41 import org.apache.derby.iapi.services.io.FormatIdInputStream;
42
43 import org.apache.derby.iapi.services.sanity.SanityManager;
44
45 import org.apache.derby.iapi.types.BooleanDataValue;
46 import org.apache.derby.iapi.types.StringDataValue;
47 import org.apache.derby.iapi.types.NumberDataValue;
48
49 import org.apache.derby.iapi.services.cache.ClassSize;
50 import org.apache.derby.iapi.util.StringUtil;
51
52 import org.apache.derby.iapi.types.SQLInteger;
53
54 import java.io.ObjectOutput JavaDoc;
55 import java.io.ObjectInput JavaDoc;
56 import java.io.IOException JavaDoc;
57 import java.io.InputStream JavaDoc;
58
59 import java.sql.ResultSet JavaDoc;
60 import java.sql.SQLException JavaDoc;
61
62 /**
63  * SQLBit represents the SQL type CHAR FOR BIT DATA
64  */

65 public class SQLBit
66     extends SQLBinary
67 {
68
69     /**
70      *
71      * @exception StandardException Thrown on error
72      */

73     public Object JavaDoc getObject() throws StandardException
74     {
75         return getBytes();
76     }
77
78
79     public String JavaDoc getTypeName()
80     {
81         return TypeId.BIT_NAME;
82     }
83
84     /**
85      * Return max memory usage for a SQL Bit
86      */

87     int getMaxMemoryUsage()
88     {
89         return Limits.DB2_CHAR_MAXWIDTH;
90     }
91
92     /*
93      * Storable interface, implies Externalizable, TypedFormat
94      */

95
96     /**
97         Return my format identifier.
98
99         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
100     */

101     public int getTypeFormatId()
102     {
103         return StoredFormatIds.SQL_BIT_ID;
104     }
105
106
107     /** @see DataValueDescriptor#getNewNull */
108     public DataValueDescriptor getNewNull()
109     {
110         return new SQLBit();
111     }
112
113     /**
114      * Obtain the value using getBytes. This works for all FOR BIT DATA types.
115      * Getting a stream is problematic as any other getXXX() call on the ResultSet
116      * will close the stream we fetched. Therefore we have to create the value in-memory
117      * as a byte array.
118      * @see DataValueDescriptor#setValueFromResultSet
119      *
120      * @exception SQLException Thrown on error
121      */

122     public final void setValueFromResultSet(ResultSet JavaDoc resultSet, int colNumber,
123                                       boolean isNullable)
124         throws SQLException JavaDoc
125     {
126             setValue(resultSet.getBytes(colNumber));
127     }
128
129     /*
130      * DataValueDescriptor interface
131      */

132
133     /** @see DataValueDescriptor#typePrecedence */
134     public int typePrecedence()
135     {
136         return TypeId.BIT_PRECEDENCE;
137     }
138     
139     /**
140      * Set the value from an non-null object.
141      */

142     final void setObject(Object JavaDoc theValue)
143         throws StandardException
144     {
145         setValue((byte[]) theValue);
146     }
147
148     /*
149      * constructors
150      */

151
152     /**
153         no-arg constructor, required by Formattable.
154     */

155     public SQLBit()
156     {
157     }
158
159     public SQLBit(byte[] val)
160     {
161         dataValue = val;
162     }
163
164     /**
165      * Normalization method - this method may be called when putting
166      * a value into a SQLBit, for example, when inserting into a SQLBit
167      * column. See NormalizeResultSet in execution.
168      *
169      * @param desiredType The type to normalize the source column to
170      * @param source The value to normalize
171      *
172      * @exception StandardException Thrown for null into
173      * non-nullable column, and for
174      * truncation error
175      */

176
177     public void normalize(
178                 DataTypeDescriptor desiredType,
179                 DataValueDescriptor source)
180                     throws StandardException
181     {
182         int desiredWidth = desiredType.getMaximumWidth();
183
184         ((SQLBinary) this).setValue(source.getBytes());
185         setWidth(desiredWidth, 0, true);
186     }
187
188     /**
189      * Set the width of the to the desired value. Used
190      * when CASTing. Ideally we'd recycle normalize(), but
191      * the behavior is different (we issue a warning instead
192      * of an error, and we aren't interested in nullability).
193      *
194      * @param desiredWidth the desired length
195      * @param desiredScale the desired scale (ignored)
196      * @param errorOnTrunc throw error on truncation
197      *
198      * @exception StandardException Thrown on non-zero truncation
199      * if errorOnTrunc is true
200      */

201     public void setWidth(int desiredWidth,
202             int desiredScale, // Ignored
203
boolean errorOnTrunc)
204             throws StandardException
205     {
206         /*
207         ** If the input is NULL, nothing to do.
208         */

209         if (getValue() == null)
210         {
211             return;
212         }
213
214         int sourceWidth = dataValue.length;
215
216         /*
217         ** If the input is shorter than the desired type,
218         ** then pad with blanks to the right length.
219         */

220         if (sourceWidth < desiredWidth)
221         {
222             byte[] actualData = new byte[desiredWidth];
223             System.arraycopy(dataValue, 0, actualData, 0, dataValue.length);
224             java.util.Arrays.fill(actualData, dataValue.length, actualData.length, SQLBinary.PAD);
225             dataValue = actualData;
226         }
227         /*
228         ** Truncation?
229         */

230         else if (sourceWidth > desiredWidth)
231         {
232             if (errorOnTrunc)
233             {
234                 // error if truncating non pad characters.
235
for (int i = desiredWidth; i < dataValue.length; i++) {
236
237                     if (dataValue[i] != SQLBinary.PAD)
238                         throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(),
239                                     StringUtil.formatForPrint(this.toString()),
240                                     String.valueOf(desiredWidth));
241                 }
242             }
243             //else
244
//{
245
// RESOLVE: when we have warnings, issue a warning if
246
// truncation of non-zero bits will occur
247
//}
248

249             /*
250             ** Truncate to the desired width.
251             */

252             byte[] shrunkData = new byte[desiredWidth];
253             System.arraycopy(dataValue, 0, shrunkData, 0, desiredWidth);
254             dataValue = shrunkData;
255
256         }
257     }
258
259
260
261
262
263 }
264
Popular Tags