KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > SimpleStringOperatorNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.SimpleStringOperatorNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
25
26 import org.apache.derby.iapi.error.StandardException;
27
28 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
29
30 import org.apache.derby.iapi.types.TypeId;
31 import org.apache.derby.iapi.types.DataTypeDescriptor;
32
33 import org.apache.derby.iapi.types.StringDataValue;
34
35 import org.apache.derby.iapi.reference.SQLState;
36 import org.apache.derby.iapi.reference.ClassName;
37
38 import java.sql.Types JavaDoc;
39
40 import java.util.Vector JavaDoc;
41
42 /**
43  * This node represents a unary upper or lower operator
44  *
45  * @author Jerry
46  */

47
48 public class SimpleStringOperatorNode extends UnaryOperatorNode
49 {
50     /**
51      * Initializer for a SimpleOperatorNode
52      *
53      * @param operand The operand
54      * @param methodName The method name
55      */

56
57     public void init(Object JavaDoc operand, Object JavaDoc methodName)
58     {
59         super.init(operand, methodName, methodName);
60     }
61
62     /**
63      * Bind this operator
64      *
65      * @param fromList The query's FROM list
66      * @param subqueryList The subquery list being built as we find SubqueryNodes
67      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
68      *
69      * @return The new top of the expression tree.
70      *
71      * @exception StandardException Thrown on error
72      */

73
74     public ValueNode bindExpression(
75         FromList fromList, SubqueryList subqueryList,
76         Vector JavaDoc aggregateVector)
77             throws StandardException
78     {
79         TypeId operandType;
80
81         super.bindExpression(fromList, subqueryList,
82                 aggregateVector);
83
84         /*
85         ** Check the type of the operand - this function is allowed only on
86         ** string value (char and bit) types.
87         */

88         operandType = operand.getTypeId();
89
90         switch (operandType.getJDBCTypeId())
91         {
92                 case Types.CHAR:
93                 case Types.VARCHAR:
94                 case Types.LONGVARCHAR:
95                 case Types.CLOB:
96                     break;
97                 case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT:
98                 case Types.OTHER:
99                 {
100                     throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE,
101                                         methodName,
102                                         operandType.getSQLTypeName());
103                 }
104
105                 default:
106                     operand = (ValueNode)
107                         getNodeFactory().getNode(
108                             C_NodeTypes.CAST_NODE,
109                             operand,
110                             DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR, true,
111                                           operand.getTypeCompiler().
112                                             getCastToCharWidth(
113                                                 operand.getTypeServices())),
114                             getContextManager());
115                     ((CastNode) operand).bindCastNodeOnly();
116                     operandType = operand.getTypeId();
117         }
118
119         /*
120         ** The result type of upper()/lower() is the type of the operand.
121         */

122
123         setType(new DataTypeDescriptor(operandType,
124                 operand.getTypeServices().isNullable(),
125                 operand.getTypeCompiler().
126                     getCastToCharWidth(operand.getTypeServices())
127                         )
128                 );
129
130         return this;
131     }
132
133     /**
134      * Bind a ? parameter operand of the upper/lower function.
135      *
136      * @exception StandardException Thrown on error
137      */

138
139     void bindParameter()
140             throws StandardException
141     {
142         /*
143         ** According to the SQL standard, if bit_length has a ? operand,
144         ** its type is bit varying with the implementation-defined maximum length
145         ** for a bit.
146         */

147
148         operand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR));
149     }
150
151     /**
152      * This is a length operator node. Overrides this method
153      * in UnaryOperatorNode for code generation purposes.
154      */

155     public String JavaDoc getReceiverInterfaceName() {
156         return ClassName.StringDataValue;
157     }
158
159     /**
160      * @see ValueNode#requiresTypeFromContext
161      */

162     public boolean requiresTypeFromContext()
163     {
164         //should return false because lower(?)/upper(?) are bound to varchar and hence don't
165
//require their type to be set from the context.
166
return false;
167     }
168 }
169
Popular Tags