KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.SpecialFunctionNode
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.CompilerContext;
25
26 import org.apache.derby.iapi.types.DataTypeDescriptor;
27
28 import org.apache.derby.iapi.services.compiler.MethodBuilder;
29 import org.apache.derby.iapi.services.compiler.LocalField;
30
31 import org.apache.derby.iapi.services.sanity.SanityManager;
32
33 import org.apache.derby.iapi.store.access.Qualifier;
34
35 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
36
37 import java.lang.reflect.Modifier JavaDoc;
38
39 import org.apache.derby.iapi.error.StandardException;
40 import org.apache.derby.iapi.reference.ClassName;
41 import org.apache.derby.iapi.services.classfile.VMOpcode;
42 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
43
44
45 import java.sql.Types JavaDoc;
46
47 import java.util.Vector JavaDoc;
48
49 /**
50      SpecialFunctionNode handles system SQL functions.
51      A function value is either obtained by a method
52      call off the LanguageConnectionContext or Activation.
53      LanguageConnectionContext functions are state related to the connection.
54      Activation functions are those related to the statement execution.
55
56      Each SQL function takes no arguments and returns a SQLvalue.
57      <P>
58      Functions supported:
59      <UL>
60      <LI> USER
61      <LI> CURRENT_USER
62      <LI> SESSION_USER
63      <LI> SYSTEM_USER
64      <LI> CURRENT SCHEMA
65      <LI> CURRENT ISOLATION
66      <LI> IDENTITY_VAL_LOCAL
67
68      </UL>
69
70
71     <P>
72
73      This node is used rather than some use of MethodCallNode for
74      runtime performance. MethodCallNode does not provide a fast access
75      to the current language connection or activatation, since it is geared
76      towards user defined routines.
77
78
79 */

80 public class SpecialFunctionNode extends ValueNode
81 {
82     /**
83         Name of SQL function
84     */

85     String JavaDoc sqlName;
86
87     /**
88         Java method name
89     */

90     private String JavaDoc methodName;
91
92     /**
93         Return type of Java method.
94     */

95     private String JavaDoc methodType;
96
97     /**
98     */

99     //private boolean isActivationCall;
100

101     /**
102      * Binding this special function means setting the result DataTypeServices.
103      * In this case, the result type is based on the operation requested.
104      *
105      * @param fromList The FROM list for the statement. This parameter
106      * is not used in this case.
107      * @param subqueryList The subquery list being built as we find
108      * SubqueryNodes. Not used in this case.
109      * @param aggregateVector The aggregate vector being built as we find
110      * AggregateNodes. Not used in this case.
111      *
112      * @return The new top of the expression tree.
113      *
114      * @exception StandardException Thrown on error
115      */

116     public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList,
117                             Vector JavaDoc aggregateVector)
118                     throws StandardException
119     { DataTypeDescriptor dtd;
120         int nodeType = getNodeType();
121         switch (nodeType)
122         {
123         case C_NodeTypes.USER_NODE:
124         case C_NodeTypes.CURRENT_USER_NODE:
125         case C_NodeTypes.SESSION_USER_NODE:
126         case C_NodeTypes.SYSTEM_USER_NODE:
127             switch (nodeType)
128             {
129                 case C_NodeTypes.USER_NODE: sqlName = "USER"; break;
130                 case C_NodeTypes.CURRENT_USER_NODE: sqlName = "CURRENT_USER"; break;
131                 case C_NodeTypes.SESSION_USER_NODE: sqlName = "SESSION_USER"; break;
132                 case C_NodeTypes.SYSTEM_USER_NODE: sqlName = "SYSTEM_USER"; break;
133             }
134             methodName = "getAuthorizationId";
135             methodType = "java.lang.String";
136             dtd = DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR, false, 128);
137             break;
138
139         case C_NodeTypes.CURRENT_SCHEMA_NODE:
140             sqlName = "CURRENT SCHEMA";
141             methodName = "getCurrentSchemaName";
142             methodType = "java.lang.String";
143             dtd = DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.VARCHAR, false, 128);
144             break;
145
146         case C_NodeTypes.IDENTITY_VAL_NODE:
147             sqlName = "IDENTITY_VAL_LOCAL";
148             methodName = "getIdentityValue";
149             methodType = "java.lang.Long";
150             dtd = DataTypeDescriptor.getSQLDataTypeDescriptor("java.math.BigDecimal", 31, 0, true, 31);
151             break;
152
153         case C_NodeTypes.CURRENT_ISOLATION_NODE:
154             sqlName = "CURRENT ISOLATION";
155             methodName = "getCurrentIsolationLevelStr";
156             methodType = "java.lang.String";
157             dtd = DataTypeDescriptor.getBuiltInDataTypeDescriptor(Types.CHAR, 2);
158             break;
159         default:
160             if (SanityManager.DEBUG)
161             {
162                 SanityManager.THROWASSERT("Invalid type for SpecialFunctionNode " + nodeType);
163             }
164             dtd = null;
165             break;
166         }
167
168         checkReliability(sqlName, CompilerContext.USER_ILLEGAL );
169         setType(dtd);
170
171         return this;
172     }
173
174     /**
175      * Return the variant type for the underlying expression.
176        All supported special functions are QUERY_INVARIANT
177
178      *
179      * @return The variant type for the underlying expression.
180      */

181     protected int getOrderableVariantType()
182     {
183         return Qualifier.QUERY_INVARIANT;
184     }
185
186     /**
187         Generate an expression that returns a DataValueDescriptor and
188         calls a method off the language connection or the activation.
189      *
190      * @param acb The ExpressionClassBuilder for the class being built
191      * @param mb The method the code to place the code
192      *
193      *
194      * @exception StandardException Thrown on error
195      */

196     public void generateExpression(ExpressionClassBuilder acb,
197                                             MethodBuilder mb)
198                                     throws StandardException
199     {
200         mb.pushThis();
201         mb.callMethod(VMOpcode.INVOKEINTERFACE, ClassName.Activation, "getLanguageConnectionContext",
202                                              ClassName.LanguageConnectionContext, 0);
203
204         mb.callMethod(VMOpcode.INVOKEINTERFACE, (String JavaDoc) null, methodName, methodType, 0);
205
206         String JavaDoc fieldType = getTypeCompiler().interfaceName();
207         LocalField field = acb.newFieldDeclaration(Modifier.PRIVATE, fieldType);
208
209         acb.generateDataValue(mb, getTypeCompiler(), field);
210     }
211
212     /*
213         print the non-node subfields
214      */

215     public String JavaDoc toString() {
216         if (SanityManager.DEBUG)
217         {
218             return super.toString()+ sqlName;
219         }
220         else
221         {
222             return "";
223         }
224     }
225         
226     protected boolean isEquivalent(ValueNode o)
227     {
228         if (isSameNodeType(o))
229         {
230             SpecialFunctionNode other = (SpecialFunctionNode)o;
231             return methodName.equals(other.methodName);
232         }
233         return false;
234     }
235 }
236
Popular Tags