001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.UserDefinedTypeCompiler
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.DataValueFactory;
030: import org.apache.derby.iapi.types.TypeId;
031:
032: import org.apache.derby.iapi.sql.compile.TypeCompiler;
033:
034: import org.apache.derby.iapi.services.sanity.SanityManager;
035: import org.apache.derby.iapi.services.compiler.LocalField;
036: import org.apache.derby.iapi.services.compiler.MethodBuilder;
037:
038: import org.apache.derby.iapi.reference.ClassName;
039:
040: public class UserDefinedTypeCompiler extends BaseTypeCompiler {
041: /* TypeCompiler methods */
042:
043: /**
044: * User types are comparable to other user types only if
045: * (for now) they are the same type and are being used to
046: * implement some JDBC type. This is sufficient for
047: * date/time types; it may be generalized later for e.g.
048: * comparison of any user type with one of its subtypes.
049: *
050: * @param otherType the type of the instance to compare with this type.
051: * @param forEquals True if this is an = or <> comparison, false
052: * otherwise.
053: * @param cf A ClassFactory
054: * @return true if otherType is comparable to this type, else false.
055: */
056: public boolean comparable(TypeId otherType, boolean forEquals,
057: ClassFactory cf) {
058: if (forEquals)
059: return true;
060:
061: try {
062: Class this Class = cf.getClassInspector().getClass(
063: getTypeId().getCorrespondingJavaTypeName());
064:
065: return java.lang.Comparable.class
066: .isAssignableFrom(this Class);
067:
068: } catch (ClassNotFoundException cnfe) {
069: return false;
070: }
071: }
072:
073: /**
074: * User types are convertible to other user types only if
075: * (for now) they are the same type and are being used to
076: * implement some JDBC type. This is sufficient for
077: * date/time types; it may be generalized later for e.g.
078: * comparison of any user type with one of its subtypes.
079: *
080: * @param otherType
081: * @param forDataTypeFunction
082: * @return true if otherType is convertible to this type, else false.
083: *
084: *@see TypeCompiler#convertible
085: */
086: public boolean convertible(TypeId otherType,
087: boolean forDataTypeFunction) {
088: /*
089: ** We are a user defined type, we are
090: ** going to have to let the client find out
091: ** the hard way.
092: */
093: return true;
094: }
095:
096: /** @see TypeCompiler#compatible */
097: public boolean compatible(TypeId otherType) {
098: return convertible(otherType, false);
099: }
100:
101: /**
102: * User types are storable into other user types that they
103: * are assignable to. The other type must be a subclass of
104: * this type, or implement this type as one of its interfaces.
105: *
106: * Built-in types are also storable into user types when the built-in
107: * type's corresponding Java type is assignable to the user type.
108: *
109: * @param otherType the type of the instance to store into this type.
110: * @param cf A ClassFactory
111: * @return true if otherType is storable into this type, else false.
112: */
113: public boolean storable(TypeId otherType, ClassFactory cf) {
114: return cf.getClassInspector().assignableTo(
115: otherType.getCorrespondingJavaTypeName(),
116: getTypeId().getCorrespondingJavaTypeName());
117: }
118:
119: /** @see TypeCompiler#interfaceName */
120: public String interfaceName() {
121: return ClassName.UserDataValue;
122: }
123:
124: /**
125: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
126: */
127:
128: public String getCorrespondingPrimitiveTypeName() {
129: return getTypeId().getCorrespondingJavaTypeName();
130: }
131:
132: /**
133: * @see TypeCompiler#getCastToCharWidth
134: */
135: public int getCastToCharWidth(DataTypeDescriptor dts) {
136: // This is the maximum maximum width for user types
137: return -1;
138: }
139:
140: protected String nullMethodName() {
141: return "getNullObject";
142: }
143:
144: public void generateDataValue(MethodBuilder mb, LocalField field) {
145: // cast the value to an object for method resolution
146: mb.upCast("java.lang.Object");
147:
148: super.generateDataValue(mb, field);
149: }
150:
151: }
|