KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > CharTypeCompiler


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.CharTypeCompiler
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.impl.sql.compile;
23
24 import org.apache.derby.iapi.services.loader.ClassFactory;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.services.io.StoredFormatIds;
29
30 import org.apache.derby.iapi.error.StandardException;
31
32 import org.apache.derby.iapi.types.StringDataValue;
33 import org.apache.derby.iapi.types.DataValueDescriptor;
34 import org.apache.derby.iapi.types.TypeId;
35 import org.apache.derby.iapi.types.DataTypeDescriptor;
36
37 import org.apache.derby.iapi.sql.compile.TypeCompiler;
38
39 import org.apache.derby.iapi.reference.ClassName;
40 import org.apache.derby.iapi.reference.SQLState;
41
42 import org.apache.derby.iapi.util.StringUtil;
43
44 import java.sql.Types JavaDoc;
45 import org.apache.derby.iapi.reference.JDBC20Translation;
46
47 /**
48  * This class implements TypeCompiler for the SQL char datatypes.
49  *
50  * @author Jeff Lichtman
51  */

52
53 public final class CharTypeCompiler extends BaseTypeCompiler
54 {
55         /**
56          * Tell whether this type (char) can be compared to the given type.
57          * Long types can not be compared.
58          * VARCHAR AND CHAR can be compared to CHAR/VARCHAR/DATE/TIME/TIMESTAMP
59          *
60          *
61          * @param otherType The TypeId of the other type.
62          */

63
64         public boolean comparable(TypeId otherType,
65                                   boolean forEquals,
66                                   ClassFactory cf)
67         {
68                 
69             // Long Types cannot be compared
70
if (getTypeId().isLongConcatableTypeId() ||
71                 otherType.isLongConcatableTypeId())
72                 return false;
73             
74             // CHAR and VARCHAR can compare to Strings or DATE/TIME/TIMESTAMP
75
if((otherType.isStringTypeId() ||
76                 otherType.isDateTimeTimeStampTypeID() ||
77                 otherType.isBooleanTypeId()))
78                 return true;
79             
80             
81             TypeCompiler otherTC = getTypeCompiler(otherType);
82             return (otherType.userType() && otherTC.comparable(getTypeId(),
83                                                                forEquals, cf));
84         }
85     
86     
87        /**
88          * Tell whether this type (char) can be converted to the given type.
89          *
90          * @see TypeCompiler#convertible
91          */

92         public boolean convertible(TypeId otherType, boolean forDataTypeFunction)
93         {
94             // LONGVARCHAR can only be converted from character types
95
// or CLOB.
96
if (getTypeId().isLongVarcharTypeId())
97             {
98                 return (otherType.isStringTypeId());
99             }
100
101             // The double function can convert CHAR and VARCHAR
102
if (forDataTypeFunction && otherType.isDoubleTypeId())
103                 return (getTypeId().isStringTypeId());
104
105             // can't CAST to CHAR and VARCHAR from REAL or DOUBLE
106
// or binary types or XML
107
// all other types are ok.
108
if (otherType.isFloatingPointTypeId() || otherType.isBitTypeId() ||
109                 otherType.isBlobTypeId() || otherType.isXMLTypeId())
110                 return false;
111                         
112             return true;
113         }
114     
115
116
117     /**
118      * Tell whether this type (char) is compatible with the given type.
119      *
120      * @param otherType The TypeId of the other type.
121      */

122     public boolean compatible(TypeId otherType)
123     {
124         return (otherType.isStringTypeId() || (otherType.isDateTimeTimeStampTypeId() && !getTypeId().isLongVarcharTypeId()));
125         
126     }
127
128         /**
129          * Tell whether this type (char) can be stored into from the given type.
130          *
131          * @param otherType The TypeId of the other type.
132          * @param cf A ClassFactory
133          */

134
135         public boolean storable(TypeId otherType, ClassFactory cf)
136         {
137                 // Same rules as cast except we can't assign from numbers
138
if (convertible(otherType,false) &&
139                     !otherType.isBlobTypeId() &&
140                     !otherType.isNumericTypeId())
141                         return true;
142
143                 /*
144                 ** If the other type is user-defined, use the java types to determine
145                 ** assignability.
146                 */

147                 return userTypeStorable(getTypeId(), otherType, cf);
148         }
149
150         /** @see TypeCompiler#interfaceName */
151         public String JavaDoc interfaceName()
152         {
153                 return ClassName.StringDataValue;
154         }
155
156         /**
157          * @see TypeCompiler#getCorrespondingPrimitiveTypeName
158          */

159
160         public String JavaDoc getCorrespondingPrimitiveTypeName()
161         {
162                 /* Only numerics and booleans get mapped to Java primitives */
163                 return "java.lang.String";
164         }
165
166         /**
167          * @see TypeCompiler#getCastToCharWidth
168          */

169         public int getCastToCharWidth(DataTypeDescriptor dts)
170         {
171                 return dts.getMaximumWidth();
172         }
173
174         /** @see TypeCompiler#getMatchingNationalCharTypeName */
175         public String JavaDoc getMatchingNationalCharTypeName()
176         {
177                 int formatId = getStoredFormatIdFromTypeId();
178                 switch (formatId)
179                 {
180                         case StoredFormatIds.CHAR_TYPE_ID:
181                         case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
182                                 return TypeId.NATIONAL_CHAR_NAME;
183
184                         case StoredFormatIds.LONGVARCHAR_TYPE_ID:
185                         case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
186                                 return TypeId.NATIONAL_LONGVARCHAR_NAME;
187
188                         case StoredFormatIds.VARCHAR_TYPE_ID:
189                         case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
190                                 return TypeId.NATIONAL_VARCHAR_NAME;
191
192                         default:
193                                 if (SanityManager.DEBUG)
194                                 {
195                                         SanityManager.THROWASSERT(
196                                                 "unexpected formatId in getMatchingNationalCharTypeName() - " + formatId);
197                                 }
198                                 return null;
199                 }
200         }
201
202
203         protected String JavaDoc nullMethodName()
204         {
205                 int formatId = getStoredFormatIdFromTypeId();
206                 switch (formatId)
207                 {
208                         case StoredFormatIds.CHAR_TYPE_ID:
209                                 return "getNullChar";
210
211                         case StoredFormatIds.LONGVARCHAR_TYPE_ID:
212                                 return "getNullLongvarchar";
213
214                         case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
215                                 return "getNullNationalChar";
216
217                         case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
218                                 return "getNullNationalLongvarchar";
219
220                         case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
221                                 return "getNullNationalVarchar";
222
223                         case StoredFormatIds.VARCHAR_TYPE_ID:
224                                 return "getNullVarchar";
225
226                         default:
227                                 if (SanityManager.DEBUG)
228                                 {
229                                         SanityManager.THROWASSERT(
230                                                 "unexpected formatId in nullMethodName() - " + formatId);
231                                 }
232                                 return null;
233                 }
234         }
235
236         protected String JavaDoc dataValueMethodName()
237         {
238                 int formatId = getStoredFormatIdFromTypeId();
239                 switch (formatId)
240                 {
241                         case StoredFormatIds.CHAR_TYPE_ID:
242                                 return "getCharDataValue";
243
244                         case StoredFormatIds.LONGVARCHAR_TYPE_ID:
245                                 return "getLongvarcharDataValue";
246
247                         case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
248                                 return "getNationalCharDataValue";
249
250                         case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
251                                 return "getNationalLongvarcharDataValue";
252
253                         case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
254                                 return "getNationalVarcharDataValue";
255
256                         case StoredFormatIds.VARCHAR_TYPE_ID:
257                                 return "getVarcharDataValue";
258
259                         default:
260                                 if (SanityManager.DEBUG)
261                                 {
262                                         SanityManager.THROWASSERT(
263                                                 "unexpected formatId in dataValueMethodName() - " + formatId);
264                                 }
265                                 return null;
266                 }
267         }
268 }
269
Popular Tags