KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.iapi.types.SQLNationalChar
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.StringDataValue;
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29 import org.apache.derby.iapi.types.BooleanDataValue;
30 import org.apache.derby.iapi.reference.SQLState;
31
32 import org.apache.derby.iapi.types.Orderable;
33
34 import org.apache.derby.iapi.services.io.Storable;
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.error.StandardException;
39 import org.apache.derby.iapi.services.sanity.SanityManager;
40
41 import org.apache.derby.iapi.util.StringUtil;
42
43 import org.apache.derby.iapi.services.i18n.LocaleFinder;
44
45 import java.io.ObjectOutput JavaDoc;
46 import java.io.ObjectInput JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.UTFDataFormatException JavaDoc;
49 import java.io.EOFException JavaDoc;
50 import java.sql.Date JavaDoc;
51 import java.sql.ResultSet JavaDoc;
52 import java.sql.SQLException JavaDoc;
53 import java.sql.Time JavaDoc;
54 import java.sql.Timestamp JavaDoc;
55 import java.util.Locale JavaDoc;
56 import java.util.Calendar JavaDoc;
57
58 /**
59  * SQLNationalChar satisfies the DataValueDescriptor
60  * interfaces (i.e., OrderableDataType). It implements an String holder,
61  * e.g. for storing a column value; it can be specified
62  * when constructed to not allow nulls. Nullability cannot be changed
63  * after construction.
64  * <p>
65  * Because OrderableDataType is a subclass of DataType,
66  * SQLNationalChar can play a role in either a DataType/ValueRow
67  * or a OrderableDataType/KeyRow, interchangeably.
68  */

69 public class SQLNationalChar
70     extends SQLChar
71 {
72
73     /*
74      * DataValueDescriptor interface.
75      *
76      * These are actually all implemented in the super-class, but we need
77      * to duplicate some of them here so they can be called by byte-code
78      * generation, which needs to know the class the method appears in.
79      */

80
81     public String JavaDoc getTypeName()
82     {
83         return TypeId.NATIONAL_CHAR_NAME;
84     }
85
86     /*
87      * Storable interface, implies Externalizable, TypedFormat
88      */

89
90     /**
91         Return my format identifier.
92
93         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
94     */

95     public int getTypeFormatId() {
96         return StoredFormatIds.SQL_NATIONAL_CHAR_ID;
97     }
98
99     /*
100      * DataValueDescriptor interface
101      */

102
103     /** @see DataValueDescriptor#getClone */
104     public DataValueDescriptor getClone()
105     {
106         try
107         {
108             /* NOTE: We pass instance variables for locale info
109              * because we only call methods when we know that we
110              * will need locale info.
111              */

112             return new SQLNationalChar(getString(), getLocaleFinder());
113         }
114         catch (StandardException se)
115         {
116             if (SanityManager.DEBUG)
117                 SanityManager.THROWASSERT("Unexpected exception " + se);
118             return null;
119         }
120     }
121
122     /**
123      * @see DataValueDescriptor#getNewNull
124      *
125      */

126     public DataValueDescriptor getNewNull()
127     {
128         /* NOTE: We pass instance variables for locale info
129          * because we only call methods when we know that we
130          * will need locale info.
131          */

132         SQLNationalChar result = new SQLNationalChar();
133         result.setLocaleFinder(getLocaleFinder());
134         return result;
135     }
136
137     /*
138      * class interface
139      */

140
141     /*
142      * constructors
143      */

144
145     /**
146         no-arg constructor, required by Formattable.
147     */

148     public SQLNationalChar()
149     {
150     }
151
152     public SQLNationalChar(String JavaDoc val, LocaleFinder localeFinder)
153     {
154         super(val);
155         setLocaleFinder(localeFinder);
156     }
157
158     /**
159      * @see DataValueDescriptor#getDate
160      * @exception StandardException thrown on failure to convert
161      */

162     public Date JavaDoc getDate( Calendar JavaDoc cal) throws StandardException
163     {
164         return nationalGetDate(cal);
165     }
166
167     /**
168      * @see DataValueDescriptor#getTime
169      * @exception StandardException thrown on failure to convert
170      */

171     public Time JavaDoc getTime( Calendar JavaDoc cal) throws StandardException
172     {
173         return nationalGetTime(cal);
174     }
175
176     /**
177      * @see DataValueDescriptor#getTimestamp
178      * @exception StandardException thrown on failure to convert
179      */

180     public Timestamp JavaDoc getTimestamp( Calendar JavaDoc cal) throws StandardException
181     {
182         return nationalGetTimestamp(cal);
183     }
184
185     /**
186      * @see DataValueDescriptor#setValue
187      *
188      * @exception StandardException Thrown on error
189      */

190     public void setValue(Date JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
191     {
192         setValue(getDateFormat( cal).format(theValue));
193     }
194
195     /**
196      * @see DataValueDescriptor#setValue
197      *
198      * @exception StandardException Thrown on error
199      */

200     public void setValue(Time JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
201     {
202         setValue(getTimeFormat( cal).format(theValue));
203     }
204
205     /**
206      * @see DataValueDescriptor#setValue
207      *
208      * @exception StandardException Thrown on error
209      */

210     public void setValue(Timestamp JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
211     {
212         setValue(getTimestampFormat(cal).format(theValue));
213     }
214
215     /*
216      * DataValueDescriptor interface
217      */

218
219     /** @see DataValueDescriptor#typePrecedence */
220     public int typePrecedence()
221     {
222         return TypeId.NATIONAL_CHAR_PRECEDENCE;
223     }
224
225
226     /**
227      * Compare two SQLChars. This method will be overriden in the
228      * National char wrappers so that the appropriate comparison
229      * is done.
230      *
231      * @exception StandardException Thrown on error
232      */

233      protected int stringCompare(SQLChar char1, SQLChar char2)
234          throws StandardException
235      {
236          return char1.stringCollatorCompare(char2);
237      }
238
239     /**
240      * Get a SQLVarchar for a built-in string function.
241      * (Could be either a SQLVarchar or SQLNationalVarchar.)
242      *
243      * @return a SQLVarchar or SQLNationalVarchar.
244      */

245     protected StringDataValue getNewVarchar() throws StandardException
246     {
247         SQLNationalVarchar result = new SQLNationalVarchar();
248         result.setLocaleFinder(getLocaleFinder());
249         return result;
250     }
251
252     /**
253      * Normalization method - this method may be called when putting
254      * a value into a SQLChar, for example, when inserting into a SQLChar
255      * column. See NormalizeResultSet in execution.
256      *
257      * @param desiredType The type to normalize the source column to
258      * @param source The value to normalize
259      *
260      * @exception StandardException Thrown for null into
261      * non-nullable column, and for
262      * truncation error
263      */

264
265     public void normalize(
266                 DataTypeDescriptor desiredType,
267                 DataValueDescriptor source)
268                     throws StandardException
269     {
270
271         normalize(desiredType, ((DataType) source).getNationalString(getLocaleFinder()));
272
273     }
274     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
275
276         setValue(((DataType) theValue).getNationalString(getLocaleFinder()));
277     }
278
279     /**
280      * Return whether or not this is a national character datatype.
281      */

282     protected boolean isNationalString()
283     {
284         return true;
285     }
286
287     /** @see java.lang.Object#hashCode */
288     public int hashCode()
289     {
290         return nationalHashCode();
291     }
292 }
293
Popular Tags