KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.LengthOperatorNode
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.services.sanity.SanityManager;
25
26 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
27
28 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
29 import org.apache.derby.iapi.types.TypeId;
30 import org.apache.derby.iapi.types.DataTypeDescriptor;
31 import org.apache.derby.iapi.types.ConcatableDataValue;
32 import org.apache.derby.iapi.sql.compile.TypeCompiler;
33 import org.apache.derby.iapi.reference.SQLState;
34 import org.apache.derby.iapi.error.StandardException;
35 import org.apache.derby.iapi.reference.ClassName;
36 import org.apache.derby.iapi.reference.JDBC20Translation;
37
38 import java.sql.Types JavaDoc;
39
40 import java.util.Vector JavaDoc;
41
42 /**
43  * This node represents a unary XXX_length operator
44  *
45  * @author Jeff Lichtman
46  */

47
48 public final class LengthOperatorNode extends UnaryOperatorNode
49 {
50     private int parameterType;
51     private int parameterWidth;
52
53     public void setNodeType(int nodeType)
54     {
55         String JavaDoc operator = null;
56         String JavaDoc methodName = null;
57
58         if (nodeType == C_NodeTypes.CHAR_LENGTH_OPERATOR_NODE)
59         {
60                 operator = "char_length";
61                 methodName = "charLength";
62                 parameterType = Types.VARCHAR;
63                 parameterWidth = TypeId.VARCHAR_MAXWIDTH;
64         }
65         else
66         {
67                 if (SanityManager.DEBUG)
68                 {
69                     SanityManager.THROWASSERT(
70                         "Unexpected nodeType = " + nodeType);
71                 }
72         }
73         setOperator(operator);
74         setMethodName(methodName);
75         super.setNodeType(nodeType);
76     }
77
78     /**
79      * Bind this operator
80      *
81      * @param fromList The query's FROM list
82      * @param subqueryList The subquery list being built as we find SubqueryNodes
83      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
84      *
85      * @return The new top of the expression tree.
86      *
87      * @exception StandardException Thrown on error
88      */

89
90     public ValueNode bindExpression(
91         FromList fromList, SubqueryList subqueryList,
92         Vector JavaDoc aggregateVector)
93             throws StandardException
94     {
95         TypeId operandType;
96
97         super.bindExpression(fromList, subqueryList,
98                 aggregateVector);
99
100         /*
101         ** Check the type of the operand - this function is allowed only on
102         ** string value types.
103         */

104         operandType = operand.getTypeId();
105         switch (operandType.getJDBCTypeId())
106         {
107                 case Types.CHAR:
108                 case Types.VARCHAR:
109                 case Types.BINARY:
110                 case Types.VARBINARY:
111                 case Types.LONGVARBINARY:
112                 case Types.LONGVARCHAR:
113                 case JDBC20Translation.SQL_TYPES_BLOB:
114                 case JDBC20Translation.SQL_TYPES_CLOB:
115                     break;
116             
117                 default:
118                     throw StandardException.newException(SQLState.LANG_UNARY_FUNCTION_BAD_TYPE,
119                                             getOperatorString(),
120                                             operandType.getSQLTypeName());
121         }
122
123         /*
124         ** The result type of XXX_length is int.
125         */

126         setType(new DataTypeDescriptor(
127                             TypeId.INTEGER_ID,
128                             operand.getTypeServices().isNullable()
129                         )
130                 );
131         return this;
132     }
133
134     /**
135      * Bind a ? parameter operand of the XXX_length function.
136      *
137      * @exception StandardException Thrown on error
138      */

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

148
149         operand.setType(DataTypeDescriptor.getBuiltInDataTypeDescriptor(parameterType, true,
150                                                 parameterWidth));
151     }
152
153     /**
154      * This is a length operator node. Overrides this method
155      * in UnaryOperatorNode for code generation purposes.
156      */

157     public String JavaDoc getReceiverInterfaceName() {
158         return ClassName.ConcatableDataValue;
159     }
160 }
161
Popular Tags