001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.TimeTypeCompiler
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.DataTypeDescriptor;
030: import org.apache.derby.iapi.types.DateTimeDataValue;
031: import org.apache.derby.iapi.types.DataValueFactory;
032: import org.apache.derby.iapi.types.TypeId;
033:
034: import org.apache.derby.iapi.sql.compile.TypeCompiler;
035:
036: import org.apache.derby.iapi.services.sanity.SanityManager;
037:
038: import java.sql.Types;
039: import org.apache.derby.iapi.reference.ClassName;
040:
041: public class TimeTypeCompiler extends BaseTypeCompiler {
042: /* TypeCompiler methods */
043:
044: /**
045: * Dates are comparable to timestamps and to comparable
046: * user types.
047: *
048: * @param otherType the type of the instance to compare with this type.
049: * @param forEquals True if this is an = or <> comparison, false
050: * otherwise.
051: * @param cf A ClassFactory
052: * @return true if otherType is comparable to this type, else false.
053: */
054: public boolean comparable(TypeId otherType, boolean forEquals,
055: ClassFactory cf) {
056: int otherJDBCTypeId = otherType.getJDBCTypeId();
057:
058: // Long types cannot be compared
059: if (otherType.isLongConcatableTypeId())
060: return false;
061:
062: if (otherJDBCTypeId == Types.TIME || otherType.isStringTypeId())
063: return true;
064:
065: TypeCompiler otherTC = getTypeCompiler(otherType);
066:
067: /* User types know the rules for what can be compared to them */
068: if (otherType.userType()) {
069: return otherTC.comparable(getTypeId(), forEquals, cf);
070: }
071:
072: return false;
073: }
074:
075: /**
076: * User types are convertible to other user types only if
077: * (for now) they are the same type and are being used to
078: * implement some JDBC type. This is sufficient for
079: * date/time types; it may be generalized later for e.g.
080: * comparison of any user type with one of its subtypes.
081: *
082: * @see TypeCompiler#convertible
083: */
084: public boolean convertible(TypeId otherType,
085: boolean forDataTypeFunction) {
086:
087: if (otherType.isStringTypeId() && (!otherType.isLOBTypeId())
088: && !otherType.isLongVarcharTypeId()) {
089: return true;
090: }
091:
092: /*
093: ** If same type, convert always ok.
094: */
095: return (getStoredFormatIdFromTypeId() == otherType
096: .getTypeFormatId());
097:
098: }
099:
100: /** @see TypeCompiler#compatible */
101: public boolean compatible(TypeId otherType) {
102: return convertible(otherType, false);
103: }
104:
105: /**
106: * User types are storable into other user types that they
107: * are assignable to. The other type must be a subclass of
108: * this type, or implement this type as one of its interfaces.
109: *
110: * Built-in types are also storable into user types when the built-in
111: * type's corresponding Java type is assignable to the user type.
112: *
113: * @param otherType the type of the instance to store into this type.
114: * @param cf A ClassFactory
115: * @return true if otherType is storable into this type, else false.
116: */
117: public boolean storable(TypeId otherType, ClassFactory cf) {
118: int otherJDBCTypeId = otherType.getJDBCTypeId();
119:
120: if (otherJDBCTypeId == Types.TIME
121: || (otherJDBCTypeId == Types.CHAR)
122: || (otherJDBCTypeId == Types.VARCHAR)) {
123: return true;
124: }
125:
126: return cf.getClassInspector().assignableTo(
127: otherType.getCorrespondingJavaTypeName(),
128: "java.sql.Time");
129: }
130:
131: /** @see TypeCompiler#interfaceName */
132: public String interfaceName() {
133: return ClassName.DateTimeDataValue;
134: }
135:
136: /**
137: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
138: */
139:
140: public String getCorrespondingPrimitiveTypeName() {
141: return "java.sql.Time";
142: }
143:
144: /**
145: * @see TypeCompiler#getCastToCharWidth
146: */
147: public int getCastToCharWidth(DataTypeDescriptor dts) {
148: return 8;
149: }
150:
151: public double estimatedMemoryUsage(DataTypeDescriptor dtd) {
152: return 12.0;
153: }
154:
155: protected String nullMethodName() {
156: return "getNullTime";
157: }
158: }
|