KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.CurrentRowLocationNode
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.dictionary.DataDictionary;
25 import org.apache.derby.iapi.types.TypeId;
26 import org.apache.derby.iapi.types.DataValueDescriptor;
27 import org.apache.derby.iapi.types.DataTypeDescriptor;
28 import org.apache.derby.iapi.types.RefDataValue;
29 import org.apache.derby.iapi.types.RowLocation;
30 import org.apache.derby.iapi.sql.execute.CursorResultSet;
31
32 import org.apache.derby.iapi.services.compiler.ClassBuilder;
33 import org.apache.derby.iapi.services.compiler.MethodBuilder;
34 import org.apache.derby.iapi.services.compiler.LocalField;
35
36
37 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
38
39 import java.lang.reflect.Modifier JavaDoc;
40 import org.apache.derby.iapi.reference.ClassName;
41
42 import org.apache.derby.iapi.services.classfile.VMOpcode;
43
44 import org.apache.derby.iapi.error.StandardException;
45
46 import org.apache.derby.catalog.TypeDescriptor;
47
48 import java.util.Vector JavaDoc;
49
50 /**
51  * The CurrentRowLocation operator is used by DELETE and UPDATE to get the
52  * RowLocation of the current row for the target table. The bind() operations
53  * for DELETE and UPDATE add a column to the target list of the SelectNode
54  * that represents the ResultSet to be deleted or updated.
55  */

56
57 public class CurrentRowLocationNode extends ValueNode
58 {
59     /**
60      * Binding this expression means setting the result DataTypeServices.
61      * In this case, the result type is always the same.
62      *
63      * @param fromList The FROM list for the statement. This parameter
64      * is not used in this case.
65      * @param subqueryList The subquery list being built as we find SubqueryNodes
66      * @param aggregateVector The aggregate vector being built as we find AggregateNodes
67      *
68      * @return The new top of the expression tree.
69      *
70      * @exception StandardException Thrown on error
71      */

72
73     public ValueNode bindExpression(FromList fromList, SubqueryList subqueryList,
74                             Vector JavaDoc aggregateVector)
75                     throws StandardException
76     {
77         setType(new DataTypeDescriptor(TypeId.getBuiltInTypeId(TypeId.REF_NAME),
78                         false /* Not nullable */
79                     )
80                 );
81         return this;
82     }
83
84     /**
85      * CurrentRowLocationNode is used in updates and deletes. See generate() in
86      * UpdateNode and DeleteNode to get the full overview of generate(). This
87      * class is responsible for generating the method that will return the RowLocation
88      * for the next row to be updated or deleted.
89      *
90      * This routine will generate a method of the form:
91      *
92      * private SQLRef fieldx;
93      *
94      * ...
95      *
96      * public DataValueDescriptor exprx()
97      * throws StandardException
98      * {
99      * return fieldx = <SQLRefConstructor>(
100      * "result set member".getRowLocation(),
101      * fieldx);
102      * }
103      * and return the generated code:
104      * exprx()
105      *
106      * ("result set member" is a member of the generated class added by UpdateNode or
107      * DeleteNode.)
108      * This exprx function is used within another exprx function,
109      * and so doesn't need a static field or to be public; but
110      * at present, it has both.
111      *
112      * fieldx is a generated field that is initialized to null when the
113      * activation is constructed. getSQLRef will re-use fieldx on calls
114      * after the first call, rather than allocate a new SQLRef for each call.
115      *
116      * @param acb The ExpressionClassBuilder for the class being built
117      *
118      *
119      * @exception StandardException Thrown on error
120      */

121     public void generateExpression(ExpressionClassBuilder acb,
122                                             MethodBuilder mbex)
123                                     throws StandardException
124     {
125         /* Generate a new method */
126         /* only used within the other exprFuns, so can be private */
127         MethodBuilder mb = acb.newGeneratedFun(ClassName.DataValueDescriptor, Modifier.PROTECTED);
128         
129         /* Allocate an object for re-use to hold the result of the operator */
130         LocalField field =
131             acb.newFieldDeclaration(Modifier.PRIVATE, ClassName.RefDataValue);
132
133
134         /* Fill in the body of the method
135          * generates:
136          * return TypeFactory.getSQLRef(this.ROWLOCATIONSCANRESULTSET.getRowLocation());
137          * and adds it to exprFun
138          */

139
140         mb.pushThis();
141         mb.getField((String JavaDoc)null, acb.getRowLocationScanResultSetName(), ClassName.CursorResultSet);
142         mb.callMethod(VMOpcode.INVOKEINTERFACE, (String JavaDoc) null, "getRowLocation", ClassName.RowLocation, 0);
143
144
145         acb.generateDataValue(mb, getTypeCompiler(), field);
146
147         /*
148         ** Store the result of the method call in the field, so we can re-use
149         ** the object.
150         */

151         mb.putField(field);
152
153         /* Stuff the full expression into a return statement and add that to the
154          * body of the new method.
155          */

156         mb.methodReturn();
157
158         // complete the method
159
mb.complete();
160
161         /* Generate the call to the new method */
162         mbex.pushThis();
163         mbex.callMethod(VMOpcode.INVOKEVIRTUAL, (String JavaDoc) null, mb.getName(), ClassName.DataValueDescriptor, 0);
164     }
165     
166     protected boolean isEquivalent(ValueNode o)
167     {
168         return false;
169     }
170 }
171
Popular Tags