001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.CharTypeCompiler
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.StringDataValue;
033: import org.apache.derby.iapi.types.DataValueDescriptor;
034: import org.apache.derby.iapi.types.TypeId;
035: import org.apache.derby.iapi.types.DataTypeDescriptor;
036:
037: import org.apache.derby.iapi.sql.compile.TypeCompiler;
038:
039: import org.apache.derby.iapi.reference.ClassName;
040: import org.apache.derby.iapi.reference.SQLState;
041:
042: import org.apache.derby.iapi.util.StringUtil;
043:
044: import java.sql.Types;
045: import org.apache.derby.iapi.reference.JDBC20Translation;
046:
047: /**
048: * This class implements TypeCompiler for the SQL char datatypes.
049: *
050: * @author Jeff Lichtman
051: */
052:
053: public final class CharTypeCompiler extends BaseTypeCompiler {
054: /**
055: * Tell whether this type (char) can be compared to the given type.
056: * Long types can not be compared.
057: * VARCHAR AND CHAR can be compared to CHAR/VARCHAR/DATE/TIME/TIMESTAMP
058: *
059: *
060: * @param otherType The TypeId of the other type.
061: */
062:
063: public boolean comparable(TypeId otherType, boolean forEquals,
064: ClassFactory cf) {
065:
066: // Long Types cannot be compared
067: if (getTypeId().isLongConcatableTypeId()
068: || otherType.isLongConcatableTypeId())
069: return false;
070:
071: // CHAR and VARCHAR can compare to Strings or DATE/TIME/TIMESTAMP
072: if ((otherType.isStringTypeId()
073: || otherType.isDateTimeTimeStampTypeID() || otherType
074: .isBooleanTypeId()))
075: return true;
076:
077: TypeCompiler otherTC = getTypeCompiler(otherType);
078: return (otherType.userType() && otherTC.comparable(getTypeId(),
079: forEquals, cf));
080: }
081:
082: /**
083: * Tell whether this type (char) can be converted to the given type.
084: *
085: * @see TypeCompiler#convertible
086: */
087: public boolean convertible(TypeId otherType,
088: boolean forDataTypeFunction) {
089: // LONGVARCHAR can only be converted from character types
090: // or CLOB.
091: if (getTypeId().isLongVarcharTypeId()) {
092: return (otherType.isStringTypeId());
093: }
094:
095: // The double function can convert CHAR and VARCHAR
096: if (forDataTypeFunction && otherType.isDoubleTypeId())
097: return (getTypeId().isStringTypeId());
098:
099: // can't CAST to CHAR and VARCHAR from REAL or DOUBLE
100: // or binary types or XML
101: // all other types are ok.
102: if (otherType.isFloatingPointTypeId()
103: || otherType.isBitTypeId() || otherType.isBlobTypeId()
104: || otherType.isXMLTypeId())
105: return false;
106:
107: return true;
108: }
109:
110: /**
111: * Tell whether this type (char) is compatible with the given type.
112: *
113: * @param otherType The TypeId of the other type.
114: */
115: public boolean compatible(TypeId otherType) {
116: return (otherType.isStringTypeId() || (otherType
117: .isDateTimeTimeStampTypeId() && !getTypeId()
118: .isLongVarcharTypeId()));
119:
120: }
121:
122: /**
123: * Tell whether this type (char) can be stored into from the given type.
124: *
125: * @param otherType The TypeId of the other type.
126: * @param cf A ClassFactory
127: */
128:
129: public boolean storable(TypeId otherType, ClassFactory cf) {
130: // Same rules as cast except we can't assign from numbers
131: if (convertible(otherType, false) && !otherType.isBlobTypeId()
132: && !otherType.isNumericTypeId())
133: return true;
134:
135: /*
136: ** If the other type is user-defined, use the java types to determine
137: ** assignability.
138: */
139: return userTypeStorable(getTypeId(), otherType, cf);
140: }
141:
142: /** @see TypeCompiler#interfaceName */
143: public String interfaceName() {
144: return ClassName.StringDataValue;
145: }
146:
147: /**
148: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
149: */
150:
151: public String getCorrespondingPrimitiveTypeName() {
152: /* Only numerics and booleans get mapped to Java primitives */
153: return "java.lang.String";
154: }
155:
156: /**
157: * @see TypeCompiler#getCastToCharWidth
158: */
159: public int getCastToCharWidth(DataTypeDescriptor dts) {
160: return dts.getMaximumWidth();
161: }
162:
163: /** @see TypeCompiler#getMatchingNationalCharTypeName */
164: public String getMatchingNationalCharTypeName() {
165: int formatId = getStoredFormatIdFromTypeId();
166: switch (formatId) {
167: case StoredFormatIds.CHAR_TYPE_ID:
168: case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
169: return TypeId.NATIONAL_CHAR_NAME;
170:
171: case StoredFormatIds.LONGVARCHAR_TYPE_ID:
172: case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
173: return TypeId.NATIONAL_LONGVARCHAR_NAME;
174:
175: case StoredFormatIds.VARCHAR_TYPE_ID:
176: case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
177: return TypeId.NATIONAL_VARCHAR_NAME;
178:
179: default:
180: if (SanityManager.DEBUG) {
181: SanityManager
182: .THROWASSERT("unexpected formatId in getMatchingNationalCharTypeName() - "
183: + formatId);
184: }
185: return null;
186: }
187: }
188:
189: protected String nullMethodName() {
190: int formatId = getStoredFormatIdFromTypeId();
191: switch (formatId) {
192: case StoredFormatIds.CHAR_TYPE_ID:
193: return "getNullChar";
194:
195: case StoredFormatIds.LONGVARCHAR_TYPE_ID:
196: return "getNullLongvarchar";
197:
198: case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
199: return "getNullNationalChar";
200:
201: case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
202: return "getNullNationalLongvarchar";
203:
204: case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
205: return "getNullNationalVarchar";
206:
207: case StoredFormatIds.VARCHAR_TYPE_ID:
208: return "getNullVarchar";
209:
210: default:
211: if (SanityManager.DEBUG) {
212: SanityManager
213: .THROWASSERT("unexpected formatId in nullMethodName() - "
214: + formatId);
215: }
216: return null;
217: }
218: }
219:
220: protected String dataValueMethodName() {
221: int formatId = getStoredFormatIdFromTypeId();
222: switch (formatId) {
223: case StoredFormatIds.CHAR_TYPE_ID:
224: return "getCharDataValue";
225:
226: case StoredFormatIds.LONGVARCHAR_TYPE_ID:
227: return "getLongvarcharDataValue";
228:
229: case StoredFormatIds.NATIONAL_CHAR_TYPE_ID:
230: return "getNationalCharDataValue";
231:
232: case StoredFormatIds.NATIONAL_LONGVARCHAR_TYPE_ID:
233: return "getNationalLongvarcharDataValue";
234:
235: case StoredFormatIds.NATIONAL_VARCHAR_TYPE_ID:
236: return "getNationalVarcharDataValue";
237:
238: case StoredFormatIds.VARCHAR_TYPE_ID:
239: return "getVarcharDataValue";
240:
241: default:
242: if (SanityManager.DEBUG) {
243: SanityManager
244: .THROWASSERT("unexpected formatId in dataValueMethodName() - "
245: + formatId);
246: }
247: return null;
248: }
249: }
250: }
|