001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.BitTypeCompiler
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.services.sanity.SanityManager;
027:
028: import org.apache.derby.iapi.services.io.StoredFormatIds;
029:
030: import org.apache.derby.iapi.error.StandardException;
031:
032: import org.apache.derby.iapi.types.BitDataValue;
033: import org.apache.derby.iapi.types.DataValueFactory;
034: import org.apache.derby.iapi.types.TypeId;
035:
036: import org.apache.derby.iapi.types.DataTypeDescriptor;
037:
038: import org.apache.derby.iapi.sql.compile.TypeCompiler;
039:
040: import org.apache.derby.iapi.reference.ClassName;
041:
042: import java.sql.Types;
043: import org.apache.derby.iapi.reference.JDBC20Translation;
044:
045: /**
046: * This class implements TypeCompiler for the SQL BIT datatype.
047: *
048: * @author Jamie
049: */
050:
051: public class BitTypeCompiler extends BaseTypeCompiler {
052: /**
053: * Tell whether this type (bit) can be compared to the given type. //
054: *
055: * Bit Types can only be compared to Bit Types.
056: * Long Bit Types can not be compared
057: * @param otherType The TypeId of the other type.
058: */
059:
060: public boolean comparable(TypeId otherType, boolean forEquals,
061: ClassFactory cf) {
062:
063: if (getTypeId().isLongConcatableTypeId()
064: || otherType.isLongConcatableTypeId())
065: return false;
066:
067: TypeCompiler otherTC = getTypeCompiler(otherType);
068: return (otherType.isBitTypeId() || (otherType.userType() && otherTC
069: .comparable(getTypeId(), forEquals, cf)));
070: }
071:
072: /**
073: * Tell whether this type (bit) can be converted to the given type.
074: *
075: * @see TypeCompiler#convertible
076: */
077: public boolean convertible(TypeId otherType,
078: boolean forDataTypeFunction) {
079:
080: return (otherType.isBitTypeId() || otherType.isBlobTypeId()
081: || otherType.isBooleanTypeId() || otherType.userType());
082: }
083:
084: /**
085: * Tell whether this type (bit) is compatible with the given type.
086: *
087: * @param otherType The TypeId of the other type.
088: */
089: public boolean compatible(TypeId otherType) {
090: if (otherType.isBlobTypeId())
091: return false;
092: return (otherType.isBitTypeId());
093: }
094:
095: /**
096: * Tell whether this type (bit) can be stored into from the given type.
097: *
098: * @param otherType The TypeId of the other type.
099: * @param cf A ClassFactory
100: */
101:
102: public boolean storable(TypeId otherType, ClassFactory cf) {
103: if (otherType.isBlobTypeId())
104: return false;
105: if (otherType.isBitTypeId()) {
106: return true;
107: }
108:
109: /*
110: ** If the other type is user-defined, use the java types to determine
111: ** assignability.
112: */
113: return userTypeStorable(this .getTypeId(), otherType, cf);
114: }
115:
116: /** @see TypeCompiler#interfaceName */
117: public String interfaceName() {
118: // may need to return different for Blob
119: // however, since it the nullMethodName()
120: // does not operate on a BitTypeCompiler object?
121: // it should?
122: return ClassName.BitDataValue;
123: }
124:
125: /**
126: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
127: */
128:
129: public String getCorrespondingPrimitiveTypeName() {
130: return "byte[]";
131: }
132:
133: /**
134: * @see TypeCompiler#getCastToCharWidth
135: */
136: public int getCastToCharWidth(DataTypeDescriptor dts) {
137: return dts.getMaximumWidth();
138: }
139:
140: protected String nullMethodName() {
141: int formatId = getStoredFormatIdFromTypeId();
142: switch (formatId) {
143: case StoredFormatIds.BIT_TYPE_ID:
144: return "getNullBit";
145:
146: case StoredFormatIds.LONGVARBIT_TYPE_ID:
147: return "getNullLongVarbit";
148:
149: case StoredFormatIds.VARBIT_TYPE_ID:
150: return "getNullVarbit";
151:
152: default:
153: if (SanityManager.DEBUG) {
154: SanityManager
155: .THROWASSERT("unexpected formatId in nullMethodName() - "
156: + formatId);
157: }
158: return null;
159: }
160: }
161:
162: protected String dataValueMethodName() {
163: int formatId = getStoredFormatIdFromTypeId();
164: switch (formatId) {
165: case StoredFormatIds.BIT_TYPE_ID:
166: return "getBitDataValue";
167:
168: case StoredFormatIds.LONGVARBIT_TYPE_ID:
169: return "getLongVarbitDataValue";
170:
171: case StoredFormatIds.VARBIT_TYPE_ID:
172: return "getVarbitDataValue";
173:
174: default:
175: if (SanityManager.DEBUG) {
176: SanityManager
177: .THROWASSERT("unexpected formatId in dataValueMethodName() - "
178: + formatId);
179: }
180: return null;
181: }
182: }
183: }
|