KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLVarchar
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.DataValueDescriptor;
28 import org.apache.derby.iapi.types.StringDataValue;
29 import org.apache.derby.iapi.reference.SQLState;
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.services.io.FormatIdUtil;
33 import org.apache.derby.iapi.services.io.StoredFormatIds;
34
35 import org.apache.derby.iapi.services.sanity.SanityManager;
36 import org.apache.derby.iapi.util.StringUtil;
37
38 /**
39  * SQLVarchar satisfies the DataValueDescriptor
40  * interfaces (i.e., OrderableDataType). It implements a String holder,
41  * e.g. for storing a column value; it can be specified
42  * when constructed to not allow nulls. Nullability cannot be changed
43  * after construction.
44  * <p>
45  * Because OrderableDataType is a subclass of DataType,
46  * SQLVarchar can play a role in either a DataType/ValueRow
47  * or a OrderableDataType/KeyRow, interchangeably.
48  *
49  * SQLVarchar is mostly the same as SQLChar, so it is implemented as a
50  * subclass of SQLChar. Only those methods with different behavior are
51  * implemented here.
52  */

53 public class SQLVarchar
54     extends SQLChar
55 {
56
57     /*
58      * DataValueDescriptor interface.
59      *
60      */

61
62     public String JavaDoc getTypeName()
63     {
64         return TypeId.VARCHAR_NAME;
65     }
66
67     /*
68      * DataValueDescriptor interface
69      */

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

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

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

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

112
113     public SQLVarchar()
114     {
115     }
116
117     public SQLVarchar(String JavaDoc val)
118     {
119         super(val);
120     }
121
122     /**
123      * Normalization method - this method may be called when putting
124      * a value into a SQLVarchar, for example, when inserting into a SQLVarchar
125      * column. See NormalizeResultSet in execution.
126      *
127      * @param desiredType The type to normalize the source column to
128      * @param source The value to normalize
129      *
130      *
131      * @exception StandardException Thrown for null into
132      * non-nullable column, and for
133      * truncation error
134      */

135
136     public void normalize(
137                 DataTypeDescriptor desiredType,
138                 DataValueDescriptor source)
139                     throws StandardException
140     {
141         normalize(desiredType, source.getString());
142     }
143
144     protected void normalize(DataTypeDescriptor desiredType, String JavaDoc sourceValue)
145         throws StandardException
146     {
147
148         int desiredWidth = desiredType.getMaximumWidth();
149
150         int sourceWidth = sourceValue.length();
151
152         /*
153         ** If the input is already the right length, no normalization is
154         ** necessary.
155         **
156         ** It's OK for a Varchar value to be shorter than the desired width.
157         ** This can happen, for example, if you insert a 3-character Varchar
158         ** value into a 10-character Varchar column. Just return the value
159         ** in this case.
160         */

161
162         if (sourceWidth > desiredWidth) {
163
164             hasNonBlankChars(sourceValue, desiredWidth, sourceWidth);
165
166             /*
167             ** No non-blank characters will be truncated. Truncate the blanks
168             ** to the desired width.
169             */

170             sourceValue = sourceValue.substring(0, desiredWidth);
171         }
172
173         setValue(sourceValue);
174     }
175
176
177     /*
178      * DataValueDescriptor interface
179      */

180
181     /* @see DataValueDescriptor#typePrecedence */
182     public int typePrecedence()
183     {
184         return TypeId.VARCHAR_PRECEDENCE;
185     }
186     
187     /**
188      * returns the reasonable minimum amount by
189      * which the array can grow . See readExternal.
190      * when we know that the array needs to grow by at least
191      * one byte, it is not performant to grow by just one byte
192      * instead this amount is used to provide a resonable growby size.
193      * @return minimum reasonable growby size
194      */

195     protected final int growBy()
196     {
197         return RETURN_SPACE_THRESHOLD; //seems reasonable for a varchar or clob
198
}
199 }
200
Popular Tags