KickJava   Java API By Example, From Geeks To Geeks.

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


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

60 public class SQLNationalVarchar
61     extends SQLVarchar
62 {
63
64     public String JavaDoc getTypeName()
65     {
66         return TypeId.NATIONAL_VARCHAR_NAME;
67     }
68
69     /**
70      * Compare two SQLChars. This method will be overriden in the
71      * National char wrappers so that the appropriate comparison
72      * is done.
73      *
74      * @exception StandardException Thrown on error
75      */

76      protected int stringCompare(SQLChar char1, SQLChar char2)
77          throws StandardException
78      {
79          return char1.stringCollatorCompare(char2);
80      }
81
82     /*
83      * DataValueDescriptor interface
84      */

85
86     /** @see DataValueDescriptor#getClone */
87     public DataValueDescriptor getClone()
88     {
89         try
90         {
91             /* NOTE: We pass instance variables for locale info
92              * because we only call methods when we know that we
93              * will need locale info.
94              */

95             return new SQLNationalVarchar(getString(), getLocaleFinder());
96         }
97         catch (StandardException se)
98         {
99             if (SanityManager.DEBUG)
100                 SanityManager.THROWASSERT("Unexpected exception " + se);
101             return null;
102         }
103     }
104
105     /**
106      * @see DataValueDescriptor#getNewNull
107      *
108      */

109     public DataValueDescriptor getNewNull()
110     {
111         /* NOTE: We pass instance variables for locale info
112          * because we only call methods when we know that we
113          * will need locale info.
114          */

115         SQLNationalVarchar result = new SQLNationalVarchar();
116         result.setLocaleFinder(getLocaleFinder());
117         return result;
118     }
119
120
121     /*
122      * Storable interface, implies Externalizable, TypedFormat
123      */

124
125     /**
126         Return my format identifier.
127
128         @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId
129     */

130     public int getTypeFormatId() {
131         return StoredFormatIds.SQL_NATIONAL_VARCHAR_ID;
132     }
133
134     /*
135      * constructors
136      */

137
138     public SQLNationalVarchar()
139     {
140     }
141
142     public SQLNationalVarchar(String JavaDoc val, LocaleFinder localeFinder)
143     {
144         super(val);
145         setLocaleFinder(localeFinder);
146     }
147
148     /**
149      * @see DataValueDescriptor#getDate
150      * @exception StandardException thrown on failure to convert
151      */

152     public Date JavaDoc getDate( Calendar JavaDoc cal) throws StandardException
153     {
154         return nationalGetDate(cal);
155     }
156
157     /**
158      * @see DataValueDescriptor#getTime
159      * @exception StandardException thrown on failure to convert
160      */

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

170     public Timestamp JavaDoc getTimestamp( Calendar JavaDoc cal) throws StandardException
171     {
172         return nationalGetTimestamp(cal);
173     }
174
175     /*
176      * DataValueDescriptor interface
177      */

178
179     /* @see DataValueDescriptor#typePrecedence */
180     public int typePrecedence()
181     {
182         return TypeId.NATIONAL_VARCHAR_PRECEDENCE;
183     }
184
185     /**
186      * Get a SQLVarchar for a built-in string function.
187      * (Could be either a SQLVarchar or SQLNationalVarchar.)
188      *
189      * @return a SQLVarchar or SQLNationalVarchar.
190      */

191     protected StringDataValue getNewVarchar()
192     {
193         return new SQLNationalVarchar();
194     }
195
196     /**
197      * Return whether or not this is a national character datatype.
198      */

199     protected boolean isNationalString()
200     {
201         return true;
202     }
203
204     /**
205      * @see DataValueDescriptor#setValue
206      *
207      * @exception StandardException Thrown on error
208      */

209     public void setValue(Date JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
210     {
211         setValue(getDateFormat( cal).format(theValue));
212     }
213
214     /**
215      * @see DataValueDescriptor#setValue
216      *
217      * @exception StandardException Thrown on error
218      */

219     public void setValue(Time JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
220     {
221         setValue(getTimeFormat( cal).format(theValue));
222     }
223
224     /**
225      * @see DataValueDescriptor#setValue
226      *
227      * @exception StandardException Thrown on error
228      */

229     public void setValue(Timestamp JavaDoc theValue, Calendar JavaDoc cal) throws StandardException
230     {
231         setValue(getTimestampFormat(cal).format(theValue));
232     }
233
234     public void normalize(
235                 DataTypeDescriptor desiredType,
236                 DataValueDescriptor source)
237                     throws StandardException
238     {
239         normalize(desiredType, ((DataType) source).getNationalString(getLocaleFinder()));
240     }
241     protected void setFrom(DataValueDescriptor theValue) throws StandardException {
242
243         setValue(((DataType) theValue).getNationalString(getLocaleFinder()));
244     }
245
246     /** @see java.lang.Object#hashCode */
247     public int hashCode()
248     {
249         return nationalHashCode();
250     }
251 }
252
Popular Tags