KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLVarbit
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 import org.apache.derby.iapi.util.StringUtil;
46
47 import java.io.ObjectOutput JavaDoc;
48 import java.io.ObjectInput JavaDoc;
49 import java.io.IOException JavaDoc;
50
51 /**
52  * SQLVarbit represents the SQL type VARCHAR FOR BIT DATA
53  * It is an extension of SQLBit and is virtually indistinguishable
54  * other than normalization.
55  */

56 public class SQLVarbit extends SQLBit
57 {
58
59
60     public String JavaDoc getTypeName()
61     {
62         return TypeId.VARBIT_NAME;
63     }
64
65     /**
66      * Return max memory usage for a SQL Varbit
67      */

68     int getMaxMemoryUsage()
69     {
70         return Limits.DB2_VARCHAR_MAXWIDTH;
71     }
72
73     /**
74      * @see DataValueDescriptor#getNewNull
75      */

76     public DataValueDescriptor getNewNull()
77     {
78         return new SQLVarbit();
79     }
80
81     /**
82         Return my format identifier.
83
84         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
85     */

86     public int getTypeFormatId()
87     {
88         return StoredFormatIds.SQL_VARBIT_ID;
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         int desiredWidth = desiredType.getMaximumWidth();
110
111         byte[] sourceData = source.getBytes();
112         setValue(sourceData);
113         if (sourceData.length > desiredWidth)
114             setWidth(desiredWidth, 0, true);
115     }
116
117     /**
118      * Set the width of the to the desired value. Used
119      * when CASTing. Ideally we'd recycle normalize(), but
120      * the behavior is different (we issue a warning instead
121      * of an error, and we aren't interested in nullability).
122      *
123      * @param desiredWidth the desired length
124      * @param desiredScale the desired scale (ignored)
125      * @param errorOnTrunc throw error on truncation
126      *
127      * @exception StandardException Thrown on non-zero truncation
128      * if errorOnTrunc is true
129      */

130     public void setWidth(int desiredWidth,
131             int desiredScale, // Ignored
132
boolean errorOnTrunc)
133             throws StandardException
134     {
135         /*
136         ** If the input is NULL, nothing to do.
137         */

138         if (getValue() == null)
139         {
140             return;
141         }
142
143         int sourceWidth = dataValue.length;
144
145         if (sourceWidth > desiredWidth)
146         {
147             if (errorOnTrunc)
148             {
149                 // error if truncating non pad characters.
150
for (int i = desiredWidth; i < dataValue.length; i++) {
151
152                     if (dataValue[i] != SQLBinary.PAD)
153                         throw StandardException.newException(SQLState.LANG_STRING_TRUNCATION, getTypeName(),
154                                     StringUtil.formatForPrint(this.toString()),
155                                     String.valueOf(desiredWidth));
156                 }
157             }
158             //else
159
//{
160
// RESOLVE: when we have warnings, issue a warning if
161
// truncation of non-zero bits will occur
162
//}
163

164             /*
165             ** Truncate to the desired width.
166             */

167             byte[] shrunkData = new byte[desiredWidth];
168             System.arraycopy(dataValue, 0, shrunkData, 0, desiredWidth);
169             dataValue = shrunkData;
170
171         }
172     }
173
174
175     /*
176      * Column interface
177      */

178
179
180     /*
181      * class interface
182      */

183
184     /*
185      * constructors
186      */

187     public SQLVarbit()
188     {
189     }
190
191     public SQLVarbit(byte[] val)
192     {
193         super(val);
194     }
195
196     /*
197      * DataValueDescriptor interface
198      */

199
200     /** @see DataValueDescriptor#typePrecedence */
201     public int typePrecedence()
202     {
203         return TypeId.VARBIT_PRECEDENCE;
204     }
205 }
206
Popular Tags