001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.TimestampTypeCompiler
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.services.loader.ClassFactory;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.types.DataTypeDescriptor;
029: import org.apache.derby.iapi.types.DateTimeDataValue;
030: import org.apache.derby.iapi.types.DataValueFactory;
031: import org.apache.derby.iapi.types.TypeId;
032:
033: import org.apache.derby.iapi.sql.compile.TypeCompiler;
034:
035: import org.apache.derby.iapi.services.sanity.SanityManager;
036:
037: import java.sql.Types;
038: import org.apache.derby.iapi.reference.ClassName;
039:
040: public class TimestampTypeCompiler extends BaseTypeCompiler {
041: /* TypeCompiler methods */
042:
043: /**
044: * Timestamps are comparable to timestamps and to comparable
045: * user types.
046: *
047: * @param otherType the type of the instance to compare with this type.
048: * @param forEquals True if this is an = or <> comparison, false
049: * otherwise.
050: * @param cf A ClassFactory
051: * @return true if otherType is comparable to this type, else false.
052: */
053: public boolean comparable(TypeId otherType, boolean forEquals,
054: ClassFactory cf) {
055: return comparable(getTypeId(), otherType, forEquals, cf);
056: }
057:
058: boolean comparable(TypeId leftType, TypeId otherType,
059: boolean forEquals, ClassFactory cf) {
060:
061: int otherJDBCTypeId = otherType.getJDBCTypeId();
062:
063: // Long types cannot be compared
064: if (otherType.isLongConcatableTypeId())
065: return false;
066:
067: TypeCompiler otherTC = getTypeCompiler(otherType);
068: if (otherJDBCTypeId == Types.TIMESTAMP
069: || otherType.isStringTypeId())
070: return true;
071:
072: /* User types know the rules for what can be compared to them */
073: if (otherType.userType()) {
074: return otherTC.comparable(getTypeId(), forEquals, cf);
075: }
076:
077: return false;
078: }
079:
080: /**
081: * User types are convertible to other user types only if
082: * (for now) they are the same type and are being used to
083: * implement some JDBC type. This is sufficient for
084: * date/time types; it may be generalized later for e.g.
085: * comparison of any user type with one of its subtypes.
086: *
087: * @see TypeCompiler#convertible
088: *
089: */
090: public boolean convertible(TypeId otherType,
091: boolean forDataTypeFunction) {
092: if (otherType.isStringTypeId()
093: && (!otherType.isLongConcatableTypeId())) {
094: return true;
095: }
096:
097: int otherJDBCTypeId = otherType.getJDBCTypeId();
098:
099: /*
100: ** At this point, we have only date/time. If
101: ** same type, convert always ok.
102: */
103: if (otherJDBCTypeId == Types.TIMESTAMP) {
104: return true;
105: }
106:
107: /*
108: ** Otherwise, we can convert timestamp to
109: ** date or time only.
110: */
111: return ((otherJDBCTypeId == Types.DATE) || (otherJDBCTypeId == Types.TIME));
112: }
113:
114: /**
115: * Tell whether this type (timestamp) is compatible with the given type.
116: *
117: * @param otherType The TypeId of the other type.
118: */
119: public boolean compatible(TypeId otherType) {
120: if (otherType.isStringTypeId()
121: && (!otherType.isLongConcatableTypeId())) {
122: return true;
123: }
124:
125: /*
126: ** Both are timestamp datatypes and hence compatible.
127: */
128: return (getStoredFormatIdFromTypeId() == otherType
129: .getTypeFormatId());
130: }
131:
132: /**
133: * User types are storable into other user types that they
134: * are assignable to. The other type must be a subclass of
135: * this type, or implement this type as one of its interfaces.
136: *
137: * Built-in types are also storable into user types when the built-in
138: * type's corresponding Java type is assignable to the user type.
139: *
140: * @param otherType the type of the instance to store into this type.
141: * @param cf A ClassFactory
142: * @return true if otherType is storable into this type, else false.
143: */
144: public boolean storable(TypeId otherType, ClassFactory cf) {
145: int otherJDBCTypeId = otherType.getJDBCTypeId();
146:
147: if (otherJDBCTypeId == Types.TIMESTAMP
148: || (otherJDBCTypeId == Types.CHAR)
149: || (otherJDBCTypeId == Types.VARCHAR)) {
150: return true;
151: }
152:
153: return cf.getClassInspector().assignableTo(
154: otherType.getCorrespondingJavaTypeName(),
155: "java.sql.Timestamp");
156: }
157:
158: /** @see TypeCompiler#interfaceName */
159: public String interfaceName() {
160: return ClassName.DateTimeDataValue;
161: }
162:
163: /**
164: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
165: */
166:
167: public String getCorrespondingPrimitiveTypeName() {
168: return "java.sql.Timestamp";
169: }
170:
171: /**
172: * @see TypeCompiler#getCastToCharWidth
173: */
174: public int getCastToCharWidth(DataTypeDescriptor dts) {
175: return 26; // DATE TIME.milliseconds (extra few for good measure)
176: }
177:
178: public double estimatedMemoryUsage(DataTypeDescriptor dtd) {
179: return 12.0;
180: }
181:
182: protected String nullMethodName() {
183: return "getNullTimestamp";
184: }
185: }
|