001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.DateTypeCompiler
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 org.apache.derby.iapi.reference.ClassName;
038:
039: import java.sql.Types;
040:
041: public class DateTypeCompiler 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: TypeCompiler otherTC = getTypeCompiler(otherType);
058:
059: // Long types cannot be compared
060: if (otherType.isLongConcatableTypeId())
061: return false;
062:
063: if (otherJDBCTypeId == Types.DATE || otherType.isStringTypeId())
064: return true;
065:
066: /* User types know the rules for what can be compared to them */
067: if (otherType.userType()) {
068: return otherTC.comparable(getTypeId(), forEquals, cf);
069: }
070:
071: return false;
072: }
073:
074: /**
075: * User types are convertible to other user types only if
076: * (for now) they are the same type and are being used to
077: * implement some JDBC type. This is sufficient for
078: * date/time types; it may be generalized later for e.g.
079: * comparison of any user type with one of its subtypes.
080: *
081: * @see TypeCompiler#convertible
082: */
083: public boolean convertible(TypeId otherType,
084: boolean forDataTypeFunction) {
085:
086: if (otherType.isStringTypeId()
087: && (!otherType.isLongConcatableTypeId())) {
088: return true;
089: }
090:
091: return (getStoredFormatIdFromTypeId() == otherType
092: .getTypeFormatId());
093:
094: }
095:
096: /**
097: * Tell whether this type (date) is compatible with the given type.
098: *
099: * @param otherType The TypeId of the other type.
100: */
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.DATE
121: || (otherJDBCTypeId == Types.CHAR)
122: || (otherJDBCTypeId == Types.VARCHAR)) {
123: return true;
124: }
125:
126: return cf.getClassInspector().assignableTo(
127: otherType.getCorrespondingJavaTypeName(),
128: "java.sql.Date");
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.Date";
142: }
143:
144: /**
145: * @see TypeCompiler#getCastToCharWidth
146: */
147: public int getCastToCharWidth(DataTypeDescriptor dts) {
148: return 10;
149: }
150:
151: protected String nullMethodName() {
152: return "getNullDate";
153: }
154: }
|