001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.XMLTypeCompiler
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: import org.apache.derby.iapi.services.sanity.SanityManager;
026: import org.apache.derby.iapi.services.io.StoredFormatIds;
027: import org.apache.derby.iapi.sql.compile.TypeCompiler;
028:
029: import org.apache.derby.iapi.error.StandardException;
030:
031: import org.apache.derby.iapi.types.TypeId;
032: import org.apache.derby.iapi.types.DataTypeDescriptor;
033:
034: import org.apache.derby.iapi.reference.ClassName;
035:
036: /**
037: * This class implements TypeCompiler for the XML type.
038: */
039:
040: public class XMLTypeCompiler extends BaseTypeCompiler {
041: /**
042: * Tell whether this type (XML) can be compared to the given type.
043: * Says SQL/XML[2003] spec:
044: *
045: * 4.2.2 XML comparison and assignment
046: * "XML values are not comparable."
047: *
048: * @param otherType The TypeId of the other type.
049: */
050: public boolean comparable(TypeId otherType, boolean forEquals,
051: ClassFactory cs) {
052: // An XML value cannot be compared to any type--
053: // not even to other XML values.
054: return false;
055: }
056:
057: /**
058: * Tell whether this type (XML) can be converted to the given type.
059: *
060: * An XML value can't be converted to any other type, per
061: * SQL/XML[2003] 6.3 <cast specification>
062: *
063: * @see TypeCompiler#convertible
064: */
065: public boolean convertible(TypeId otherType,
066: boolean forDataTypeFunction) {
067: // An XML value cannot be converted to any non-XML type. If
068: // user wants to convert an XML value to a string, then
069: // s/he must use the provided SQL/XML serialization operator
070: // (namely, XMLSERIALIZE).
071: return otherType.isXMLTypeId();
072: }
073:
074: /**
075: * Tell whether this type (XML) is compatible with the given type.
076: *
077: * @param otherType The TypeId of the other type.
078: */
079: public boolean compatible(TypeId otherType) {
080: // An XML value is not compatible (i.e. cannot be "coalesced")
081: // into any non-XML type.
082: return otherType.isXMLTypeId();
083: }
084:
085: /**
086: * Tell whether this type (XML) can be stored into from the given type.
087: * Only XML values can be stored into an XML type, per SQL/XML spec:
088: *
089: * 4.2.2 XML comparison and assignment
090: * Values of XML type are assignable to sites of XML type.
091: *
092: * @param otherType The TypeId of the other type.
093: * @param cf A ClassFactory
094: */
095: public boolean storable(TypeId otherType, ClassFactory cf) {
096: // The only type of value that can be stored as XML
097: // is an XML value. Strings are not allowed. If
098: // the user wants to store a string value as XML,
099: // s/he must use the provided XML parse operator
100: // (namely, XMLPARSE) to parse the string into
101: // XML.
102: return otherType.isXMLTypeId();
103: }
104:
105: /**
106: * @see TypeCompiler#interfaceName
107: */
108: public String interfaceName() {
109: return ClassName.XMLDataValue;
110: }
111:
112: /**
113: * @see TypeCompiler#getCorrespondingPrimitiveTypeName
114: */
115: public String getCorrespondingPrimitiveTypeName() {
116: int formatId = getStoredFormatIdFromTypeId();
117: if (formatId == StoredFormatIds.XML_TYPE_ID)
118: return "org.apache.derby.iapi.types.XML";
119:
120: if (SanityManager.DEBUG) {
121: SanityManager
122: .THROWASSERT("unexpected formatId in getCorrespondingPrimitiveTypeName(): "
123: + formatId);
124: }
125:
126: return null;
127: }
128:
129: /**
130: * @see TypeCompiler#getCastToCharWidth
131: *
132: * While it is true XML values can't be cast to char, this method
133: * can get called before we finish type checking--so we return a dummy
134: * value here and let the type check throw the appropriate error.
135: */
136: public int getCastToCharWidth(DataTypeDescriptor dts) {
137: return -1;
138: }
139:
140: /**
141: * @see BaseTypeCompiler#nullMethodName
142: */
143: protected String nullMethodName() {
144: int formatId = getStoredFormatIdFromTypeId();
145: if (formatId == StoredFormatIds.XML_TYPE_ID)
146: return "getNullXML";
147:
148: if (SanityManager.DEBUG) {
149: SanityManager
150: .THROWASSERT("unexpected formatId in nullMethodName(): "
151: + formatId);
152: }
153:
154: return null;
155: }
156:
157: /**
158: * @see BaseTypeCompiler#dataValueMethodName
159: */
160: protected String dataValueMethodName() {
161: int formatId = getStoredFormatIdFromTypeId();
162: if (formatId == StoredFormatIds.XML_TYPE_ID)
163: return "getXMLDataValue";
164:
165: if (SanityManager.DEBUG) {
166: SanityManager
167: .THROWASSERT("unexpected formatId in dataValueMethodName() - "
168: + formatId);
169: }
170:
171: return null;
172: }
173: }
|