KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.BaseTypeCompiler
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.reference.SQLState;
25 import org.apache.derby.iapi.services.loader.ClassFactory;
26
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.error.StandardException;
30 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
31
32 import org.apache.derby.iapi.sql.compile.TypeCompiler;
33
34 import org.apache.derby.iapi.types.BitDataValue;
35 import org.apache.derby.iapi.types.DataValueFactory;
36 import org.apache.derby.iapi.types.DataValueDescriptor;
37 import org.apache.derby.iapi.types.TypeId;
38
39 import org.apache.derby.iapi.types.DataTypeDescriptor;
40
41 import org.apache.derby.iapi.reference.SQLState;
42 import org.apache.derby.iapi.reference.ClassName;
43
44 import org.apache.derby.iapi.services.compiler.LocalField;
45 import org.apache.derby.iapi.services.compiler.MethodBuilder;
46
47 import org.apache.derby.iapi.services.classfile.VMOpcode;
48
49 import java.sql.Types JavaDoc;
50
51 /**
52  * This is the base implementation of TypeCompiler
53  *
54  * @author Jeff
55  */

56
57 abstract class BaseTypeCompiler implements TypeCompiler
58 {
59     private TypeId correspondingTypeId;
60
61     /**
62      * Get the method name for getting out the corresponding primitive
63      * Java type.
64      *
65      * @return String The method call name for getting the
66      * corresponding primitive Java type.
67      */

68     public String JavaDoc getPrimitiveMethodName()
69     {
70         if (SanityManager.DEBUG)
71         {
72             SanityManager.THROWASSERT("getPrimitiveMethodName not applicable for " +
73                                       getClass().toString());
74         }
75         return null;
76     }
77
78     /** @see TypeCompiler#getMatchingNationalCharTypeName */
79     public String JavaDoc getMatchingNationalCharTypeName()
80     {
81         return TypeId.NATIONAL_CHAR_NAME;
82     }
83
84
85     /**
86      * @see TypeCompiler#resolveArithmeticOperation
87      *
88      * @exception StandardException Thrown on error
89      */

90     public DataTypeDescriptor
91     resolveArithmeticOperation(DataTypeDescriptor leftType,
92                                 DataTypeDescriptor rightType,
93                                 String JavaDoc operator)
94                             throws StandardException
95     {
96         throw StandardException.newException(SQLState.LANG_BINARY_OPERATOR_NOT_SUPPORTED,
97                                         operator,
98                                         leftType.getTypeId().getSQLTypeName(),
99                                         rightType.getTypeId().getSQLTypeName()
100                                         );
101     }
102
103     /** @see TypeCompiler#generateNull */
104
105     public void generateNull(MethodBuilder mb)
106     {
107         mb.callMethod(VMOpcode.INVOKEINTERFACE, (String JavaDoc) null,
108                                     nullMethodName(),
109                                     interfaceName(),
110                                     1);
111     }
112
113     /** @see TypeCompiler#generateDataValue */
114     public void generateDataValue(MethodBuilder mb,
115                                         LocalField field)
116     {
117         String JavaDoc interfaceName = interfaceName();
118
119         // push the second argument
120

121         /* If fieldName is null, then there is no
122          * reusable wrapper (null), else we
123          * reuse the field.
124          */

125         if (field == null)
126         {
127             mb.pushNull(interfaceName);
128         }
129         else
130         {
131             mb.getField(field);
132         }
133
134
135         mb.callMethod(VMOpcode.INVOKEINTERFACE, (String JavaDoc) null,
136                             dataValueMethodName(),
137                             interfaceName,
138                             2);
139
140         if (field != null)
141         {
142             /* Store the result of the method call in the field,
143              * so we can re-use the wrapper.
144              */

145             mb.putField(field);
146         }
147     }
148
149     protected abstract String JavaDoc nullMethodName();
150
151     /**
152         Return the method name to get a Derby DataValueDescriptor
153         object of the correct type. This implementation returns "getDataValue".
154     */

155     protected String JavaDoc dataValueMethodName()
156     {
157         return "getDataValue";
158     }
159
160     
161     /**
162      * Determine whether thisType is storable in otherType due to otherType
163      * being a user type.
164      *
165      * @param thisType The TypeId of the value to be stored
166      * @param otherType The TypeId of the value to be stored in
167      *
168      * @return true if thisType is storable in otherType
169      */

170     protected boolean userTypeStorable(TypeId thisType,
171                             TypeId otherType,
172                             ClassFactory cf)
173     {
174         /*
175         ** If the other type is user-defined, use the java types to determine
176         ** assignability.
177         */

178         if (otherType.userType())
179         {
180             return cf.getClassInspector().assignableTo(
181                     thisType.getCorrespondingJavaTypeName(),
182                     otherType.getCorrespondingJavaTypeName());
183         }
184
185         return false;
186     }
187
188     /**
189      * Tell whether this numeric type can be compared to the given type.
190      *
191      * @param otherType The TypeId of the other type.
192      */

193
194     public boolean numberComparable(TypeId otherType,
195                                     boolean forEquals,
196                                     ClassFactory cf)
197     {
198         TypeCompiler otherTC = getTypeCompiler(otherType);
199
200         /* Numbers can be compared to other numbers,
201          * boolean and objects
202          */

203         return otherType.isNumericTypeId() ||
204                 otherType.isBooleanTypeId() ||
205                 (otherType.userType() && otherTC.comparable(otherType,
206                                                             forEquals,
207                                                             cf));
208     }
209
210     
211     /**
212      * Tell whether this numeric type can be converted to the given type.
213      *
214      * @param otherType The TypeId of the other type.
215      * @param forDataTypeFunction was this called from a scalarFunction like
216      * CHAR() or DOUBLE()
217      */

218     public boolean numberConvertible(TypeId otherType,
219                                      boolean forDataTypeFunction)
220     {
221
222         // Can't convert numbers to long types
223
if (otherType.isLongConcatableTypeId())
224             return false;
225
226         // Numbers can only be converted to other numbers,
227
// and CHAR, (not VARCHARS or LONGVARCHAR).
228
// Only with the CHAR() or VARCHAR()function can they be converted.
229
boolean retval =((otherType.isNumericTypeId()) ||
230                          (otherType.isBooleanTypeId()) ||
231                          (otherType.userType()));
232
233         // For CHAR Conversions, function can convert
234
// Floating types
235
if (forDataTypeFunction)
236             retval = retval ||
237                 (otherType.isFixedStringTypeId() &&
238                 (getTypeId().isFloatingPointTypeId()));
239        
240         retval = retval ||
241             (otherType.isFixedStringTypeId() &&
242              (!getTypeId().isFloatingPointTypeId()));
243         
244         return retval;
245
246     }
247
248     /**
249      * Tell whether this numeric type can be stored into from the given type.
250      *
251      * @param thisType The TypeId of this type
252      * @param otherType The TypeId of the other type.
253      * @param cf A ClassFactory
254      */

255
256     public boolean numberStorable(TypeId thisType,
257                                     TypeId otherType,
258                                     ClassFactory cf)
259     {
260         /*
261         ** Numbers can be stored into from other number types.
262         ** Also, user types with compatible classes can be stored into numbers.
263         */

264         if ((otherType.isNumericTypeId()) ||
265             (otherType.isBooleanTypeId()))
266             return true;
267
268         /*
269         ** If the other type is user-defined, use the java types to determine
270         ** assignability.
271         */

272         return userTypeStorable(thisType, otherType, cf);
273     }
274
275
276     /**
277      * Get the TypeId that corresponds to this TypeCompiler.
278      */

279     protected TypeId getTypeId()
280     {
281         return correspondingTypeId;
282     }
283
284     /**
285      * Get the TypeCompiler that corresponds to the given TypeId.
286      */

287     protected TypeCompiler getTypeCompiler(TypeId typeId)
288     {
289         return TypeCompilerFactoryImpl.staticGetTypeCompiler(typeId);
290     }
291
292     /**
293      * Set the TypeCompiler that corresponds to the given TypeId.
294      */

295     void setTypeId(TypeId typeId)
296     {
297         correspondingTypeId = typeId;
298     }
299
300     /**
301      * Get the StoredFormatId from the corresponding
302      * TypeId.
303      *
304      * @return The StoredFormatId from the corresponding
305      * TypeId.
306      */

307     protected int getStoredFormatIdFromTypeId()
308     {
309         return getTypeId().getTypeFormatId();
310     }
311
312
313 }
314
315
316
317
318
319
Popular Tags