KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TimeTypeCompiler
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.DataTypeDescriptor;
30 import org.apache.derby.iapi.types.DateTimeDataValue;
31 import org.apache.derby.iapi.types.DataValueFactory;
32 import org.apache.derby.iapi.types.TypeId;
33
34 import org.apache.derby.iapi.sql.compile.TypeCompiler;
35
36 import org.apache.derby.iapi.services.sanity.SanityManager;
37
38 import java.sql.Types JavaDoc;
39 import org.apache.derby.iapi.reference.ClassName;
40
41 public class TimeTypeCompiler extends BaseTypeCompiler
42 {
43     /* TypeCompiler methods */
44
45     /**
46      * Dates are comparable to timestamps and to comparable
47      * user types.
48      *
49      * @param otherType the type of the instance to compare with this type.
50      * @param forEquals True if this is an = or <> comparison, false
51      * otherwise.
52      * @param cf A ClassFactory
53      * @return true if otherType is comparable to this type, else false.
54      */

55     public boolean comparable(TypeId otherType,
56                               boolean forEquals,
57                               ClassFactory cf)
58     {
59         int otherJDBCTypeId = otherType.getJDBCTypeId();
60
61         // Long types cannot be compared
62
if (otherType.isLongConcatableTypeId())
63             return false;
64
65         if (otherJDBCTypeId == Types.TIME || otherType.isStringTypeId())
66             return true;
67
68         TypeCompiler otherTC = getTypeCompiler(otherType);
69
70         /* User types know the rules for what can be compared to them */
71         if (otherType.userType())
72         {
73             return otherTC.comparable(getTypeId(), forEquals, cf);
74         }
75
76         return false;
77     }
78
79     /**
80      * User types are convertible to other user types only if
81      * (for now) they are the same type and are being used to
82      * implement some JDBC type. This is sufficient for
83      * date/time types; it may be generalized later for e.g.
84      * comparison of any user type with one of its subtypes.
85      *
86      * @see TypeCompiler#convertible
87      */

88     public boolean convertible(TypeId otherType,
89                                boolean forDataTypeFunction)
90     {
91
92         if (otherType.isStringTypeId() &&
93             (!otherType.isLOBTypeId()) &&
94             !otherType.isLongVarcharTypeId())
95         {
96             return true;
97         }
98
99
100         /*
101         ** If same type, convert always ok.
102         */

103         return (getStoredFormatIdFromTypeId() ==
104                 otherType.getTypeFormatId());
105            
106     }
107
108     /** @see TypeCompiler#compatible */
109     public boolean compatible(TypeId otherType)
110     {
111         return convertible(otherType,false);
112     }
113
114     /**
115      * User types are storable into other user types that they
116      * are assignable to. The other type must be a subclass of
117      * this type, or implement this type as one of its interfaces.
118      *
119      * Built-in types are also storable into user types when the built-in
120      * type's corresponding Java type is assignable to the user type.
121      *
122      * @param otherType the type of the instance to store into this type.
123      * @param cf A ClassFactory
124      * @return true if otherType is storable into this type, else false.
125      */

126     public boolean storable(TypeId otherType, ClassFactory cf)
127     {
128         int otherJDBCTypeId = otherType.getJDBCTypeId();
129
130         if (otherJDBCTypeId == Types.TIME ||
131             (otherJDBCTypeId == Types.CHAR) ||
132             (otherJDBCTypeId == Types.VARCHAR))
133         {
134             return true;
135         }
136
137         return cf.getClassInspector().assignableTo(
138                otherType.getCorrespondingJavaTypeName(),
139                "java.sql.Time");
140     }
141
142     /** @see TypeCompiler#interfaceName */
143     public String JavaDoc interfaceName()
144     {
145         return ClassName.DateTimeDataValue;
146     }
147             
148     /**
149      * @see TypeCompiler#getCorrespondingPrimitiveTypeName
150      */

151
152     public String JavaDoc getCorrespondingPrimitiveTypeName()
153     {
154         return "java.sql.Time";
155     }
156
157     /**
158      * @see TypeCompiler#getCastToCharWidth
159      */

160     public int getCastToCharWidth(DataTypeDescriptor dts)
161     {
162         return 8;
163     }
164
165     public double estimatedMemoryUsage(DataTypeDescriptor dtd)
166     {
167         return 12.0;
168     }
169
170     protected String JavaDoc nullMethodName()
171     {
172         return "getNullTime";
173     }
174 }
175
Popular Tags