001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.SimpleStringOperatorNode
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.sql.compile.C_NodeTypes;
025:
026: import org.apache.derby.iapi.error.StandardException;
027:
028: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
029:
030: import org.apache.derby.iapi.types.TypeId;
031: import org.apache.derby.iapi.types.DataTypeDescriptor;
032:
033: import org.apache.derby.iapi.types.StringDataValue;
034:
035: import org.apache.derby.iapi.reference.SQLState;
036: import org.apache.derby.iapi.reference.ClassName;
037:
038: import java.sql.Types;
039:
040: import java.util.Vector;
041:
042: /**
043: * This node represents a unary upper or lower operator
044: *
045: * @author Jerry
046: */
047:
048: public class SimpleStringOperatorNode extends UnaryOperatorNode {
049: /**
050: * Initializer for a SimpleOperatorNode
051: *
052: * @param operand The operand
053: * @param methodName The method name
054: */
055:
056: public void init(Object operand, Object methodName) {
057: super .init(operand, methodName, methodName);
058: }
059:
060: /**
061: * Bind this operator
062: *
063: * @param fromList The query's FROM list
064: * @param subqueryList The subquery list being built as we find SubqueryNodes
065: * @param aggregateVector The aggregate vector being built as we find AggregateNodes
066: *
067: * @return The new top of the expression tree.
068: *
069: * @exception StandardException Thrown on error
070: */
071:
072: public ValueNode bindExpression(FromList fromList,
073: SubqueryList subqueryList, Vector aggregateVector)
074: throws StandardException {
075: TypeId operandType;
076:
077: super .bindExpression(fromList, subqueryList, aggregateVector);
078:
079: /*
080: ** Check the type of the operand - this function is allowed only on
081: ** string value (char and bit) types.
082: */
083: operandType = operand.getTypeId();
084:
085: switch (operandType.getJDBCTypeId()) {
086: case Types.CHAR:
087: case Types.VARCHAR:
088: case Types.LONGVARCHAR:
089: case Types.CLOB:
090: break;
091: case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT:
092: case Types.OTHER: {
093: throw StandardException.newException(
094: SQLState.LANG_UNARY_FUNCTION_BAD_TYPE, methodName,
095: operandType.getSQLTypeName());
096: }
097:
098: default:
099: operand = (ValueNode) getNodeFactory()
100: .getNode(
101: C_NodeTypes.CAST_NODE,
102: operand,
103: DataTypeDescriptor
104: .getBuiltInDataTypeDescriptor(
105: Types.VARCHAR,
106: true,
107: operand
108: .getTypeCompiler()
109: .getCastToCharWidth(
110: operand
111: .getTypeServices())),
112: getContextManager());
113: ((CastNode) operand).bindCastNodeOnly();
114: operandType = operand.getTypeId();
115: }
116:
117: /*
118: ** The result type of upper()/lower() is the type of the operand.
119: */
120:
121: setType(new DataTypeDescriptor(operandType, operand
122: .getTypeServices().isNullable(), operand
123: .getTypeCompiler().getCastToCharWidth(
124: operand.getTypeServices())));
125:
126: return this ;
127: }
128:
129: /**
130: * Bind a ? parameter operand of the upper/lower function.
131: *
132: * @exception StandardException Thrown on error
133: */
134:
135: void bindParameter() throws StandardException {
136: /*
137: ** According to the SQL standard, if bit_length has a ? operand,
138: ** its type is bit varying with the implementation-defined maximum length
139: ** for a bit.
140: */
141:
142: operand.setType(DataTypeDescriptor
143: .getBuiltInDataTypeDescriptor(Types.VARCHAR));
144: }
145:
146: /**
147: * This is a length operator node. Overrides this method
148: * in UnaryOperatorNode for code generation purposes.
149: */
150: public String getReceiverInterfaceName() {
151: return ClassName.StringDataValue;
152: }
153:
154: /**
155: * @see ValueNode#requiresTypeFromContext
156: */
157: public boolean requiresTypeFromContext() {
158: //should return false because lower(?)/upper(?) are bound to varchar and hence don't
159: //require their type to be set from the context.
160: return false;
161: }
162: }
|