KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.UserDefinedTypeCompiler
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.error.StandardException;
27
28 import org.apache.derby.iapi.types.DataTypeDescriptor;
29 import org.apache.derby.iapi.types.DataValueFactory;
30 import org.apache.derby.iapi.types.TypeId;
31
32 import org.apache.derby.iapi.sql.compile.TypeCompiler;
33
34 import org.apache.derby.iapi.services.sanity.SanityManager;
35 import org.apache.derby.iapi.services.compiler.LocalField;
36 import org.apache.derby.iapi.services.compiler.MethodBuilder;
37
38 import org.apache.derby.iapi.reference.ClassName;
39
40 public class UserDefinedTypeCompiler extends BaseTypeCompiler
41 {
42     /* TypeCompiler methods */
43
44     /**
45      * User types are comparable to other user types only if
46      * (for now) they are the same type and are being used to
47      * implement some JDBC type. This is sufficient for
48      * date/time types; it may be generalized later for e.g.
49      * comparison of any user type with one of its subtypes.
50      *
51      * @param otherType the type of the instance to compare with this type.
52      * @param forEquals True if this is an = or <> comparison, false
53      * otherwise.
54      * @param cf A ClassFactory
55      * @return true if otherType is comparable to this type, else false.
56      */

57     public boolean comparable(TypeId otherType,
58                               boolean forEquals,
59                               ClassFactory cf)
60     {
61         if (forEquals)
62             return true;
63
64         try {
65             Class JavaDoc thisClass = cf.getClassInspector().getClass(
66                                     getTypeId().getCorrespondingJavaTypeName());
67
68             return java.lang.Comparable JavaDoc.class.isAssignableFrom(thisClass);
69
70         } catch (ClassNotFoundException JavaDoc cnfe) {
71             return false;
72         }
73     }
74
75     /**
76      * User types are convertible to other user types only if
77      * (for now) they are the same type and are being used to
78      * implement some JDBC type. This is sufficient for
79      * date/time types; it may be generalized later for e.g.
80      * comparison of any user type with one of its subtypes.
81      *
82      * @param otherType
83      * @param forDataTypeFunction
84      * @return true if otherType is convertible to this type, else false.
85      *
86      *@see TypeCompiler#convertible
87      */

88     public boolean convertible(TypeId otherType, boolean forDataTypeFunction)
89     {
90         /*
91         ** We are a user defined type, we are
92         ** going to have to let the client find out
93         ** the hard way.
94         */

95         return true;
96     }
97
98      /** @see TypeCompiler#compatible */
99     public boolean compatible(TypeId otherType)
100     {
101         return convertible(otherType, false);
102     }
103
104     /**
105      * User types are storable into other user types that they
106      * are assignable to. The other type must be a subclass of
107      * this type, or implement this type as one of its interfaces.
108      *
109      * Built-in types are also storable into user types when the built-in
110      * type's corresponding Java type is assignable to the user type.
111      *
112      * @param otherType the type of the instance to store into this type.
113      * @param cf A ClassFactory
114      * @return true if otherType is storable into this type, else false.
115      */

116     public boolean storable(TypeId otherType, ClassFactory cf)
117     {
118         return cf.getClassInspector().assignableTo(
119                otherType.getCorrespondingJavaTypeName(),
120                getTypeId().getCorrespondingJavaTypeName());
121     }
122
123     /** @see TypeCompiler#interfaceName */
124     public String JavaDoc interfaceName()
125     {
126         return ClassName.UserDataValue;
127     }
128             
129     /**
130      * @see TypeCompiler#getCorrespondingPrimitiveTypeName
131      */

132
133     public String JavaDoc getCorrespondingPrimitiveTypeName()
134     {
135         return getTypeId().getCorrespondingJavaTypeName();
136     }
137
138     /**
139      * @see TypeCompiler#getCastToCharWidth
140      */

141     public int getCastToCharWidth(DataTypeDescriptor dts)
142     {
143         // This is the maximum maximum width for user types
144
return -1;
145     }
146
147     protected String JavaDoc nullMethodName()
148     {
149         return "getNullObject";
150     }
151
152     public void generateDataValue(MethodBuilder mb,
153                                         LocalField field)
154     {
155         // cast the value to an object for method resolution
156
mb.upCast("java.lang.Object");
157
158         super.generateDataValue(mb, field);
159     }
160
161         
162
163 }
164
Popular Tags