KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.DateTypeCompiler
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.DateTimeDataValue;
30 import org.apache.derby.iapi.types.DataValueFactory;
31 import org.apache.derby.iapi.types.TypeId;
32
33 import org.apache.derby.iapi.sql.compile.TypeCompiler;
34
35 import org.apache.derby.iapi.services.sanity.SanityManager;
36
37 import org.apache.derby.iapi.reference.ClassName;
38
39 import java.sql.Types JavaDoc;
40
41 public class DateTypeCompiler 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         TypeCompiler otherTC = getTypeCompiler(otherType);
61
62         // Long types cannot be compared
63
if (otherType.isLongConcatableTypeId())
64             return false;
65
66         if (otherJDBCTypeId == Types.DATE || otherType.isStringTypeId())
67             return true;
68
69         /* User types know the rules for what can be compared to them */
70         if (otherType.userType())
71         {
72             return otherTC.comparable(getTypeId(), forEquals, cf);
73         }
74
75         return false;
76     }
77
78     /**
79      * User types are convertible to other user types only if
80      * (for now) they are the same type and are being used to
81      * implement some JDBC type. This is sufficient for
82      * date/time types; it may be generalized later for e.g.
83      * comparison of any user type with one of its subtypes.
84      *
85      * @see TypeCompiler#convertible
86      */

87     public boolean convertible(TypeId otherType, boolean forDataTypeFunction)
88     {
89
90
91         if (otherType.isStringTypeId() &&
92             (!otherType.isLongConcatableTypeId()))
93         {
94             return true;
95         }
96
97         return (getStoredFormatIdFromTypeId() ==
98                 otherType.getTypeFormatId());
99            
100     }
101
102         /**
103          * Tell whether this type (date) is compatible with the given type.
104          *
105          * @param otherType The TypeId of the other type.
106          */

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

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

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

158     public int getCastToCharWidth(DataTypeDescriptor dts)
159     {
160         return 10;
161     }
162
163     protected String JavaDoc nullMethodName()
164     {
165         return "getNullDate";
166     }
167 }
168
Popular Tags