KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

96     public boolean convertible(TypeId otherType,
97                                boolean forDataTypeFunction)
98     {
99         if (otherType.isStringTypeId()&&
100             (!otherType.isLongConcatableTypeId()))
101         {
102             return true;
103         }
104         
105         int otherJDBCTypeId = otherType.getJDBCTypeId();
106
107         /*
108         ** At this point, we have only date/time. If
109         ** same type, convert always ok.
110         */

111         if (otherJDBCTypeId == Types.TIMESTAMP)
112         {
113             return true;
114         }
115
116         /*
117         ** Otherwise, we can convert timestamp to
118         ** date or time only.
119         */

120         return ((otherJDBCTypeId == Types.DATE) ||
121                 (otherJDBCTypeId == Types.TIME));
122     }
123
124         /**
125          * Tell whether this type (timestamp) is compatible with the given type.
126          *
127          * @param otherType The TypeId of the other type.
128          */

129     public boolean compatible(TypeId otherType)
130     {
131         if (otherType.isStringTypeId() &&
132             (!otherType.isLongConcatableTypeId()))
133         {
134             return true;
135         }
136
137         /*
138         ** Both are timestamp datatypes and hence compatible.
139         */

140         return (getStoredFormatIdFromTypeId() ==
141                 otherType.getTypeFormatId());
142     }
143
144     /**
145      * User types are storable into other user types that they
146      * are assignable to. The other type must be a subclass of
147      * this type, or implement this type as one of its interfaces.
148      *
149      * Built-in types are also storable into user types when the built-in
150      * type's corresponding Java type is assignable to the user type.
151      *
152      * @param otherType the type of the instance to store into this type.
153      * @param cf A ClassFactory
154      * @return true if otherType is storable into this type, else false.
155      */

156     public boolean storable(TypeId otherType, ClassFactory cf)
157     {
158         int otherJDBCTypeId = otherType.getJDBCTypeId();
159
160         if (otherJDBCTypeId == Types.TIMESTAMP ||
161             (otherJDBCTypeId == Types.CHAR) ||
162             (otherJDBCTypeId == Types.VARCHAR))
163         {
164             return true;
165         }
166
167         return cf.getClassInspector().assignableTo(
168                otherType.getCorrespondingJavaTypeName(),
169                "java.sql.Timestamp");
170     }
171
172     /** @see TypeCompiler#interfaceName */
173     public String JavaDoc interfaceName()
174     {
175         return ClassName.DateTimeDataValue;
176     }
177             
178     /**
179      * @see TypeCompiler#getCorrespondingPrimitiveTypeName
180      */

181
182     public String JavaDoc getCorrespondingPrimitiveTypeName()
183     {
184         return "java.sql.Timestamp";
185     }
186
187     /**
188      * @see TypeCompiler#getCastToCharWidth
189      */

190     public int getCastToCharWidth(DataTypeDescriptor dts)
191     {
192         return 26; // DATE TIME.milliseconds (extra few for good measure)
193
}
194
195     public double estimatedMemoryUsage(DataTypeDescriptor dtd)
196     {
197         return 12.0;
198     }
199     protected String JavaDoc nullMethodName()
200     {
201         return "getNullTimestamp";
202     }
203 }
204
Popular Tags