KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.SetSchemaNode
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.reference.ClassName;
25 import org.apache.derby.iapi.services.classfile.VMOpcode;
26
27 import org.apache.derby.iapi.services.context.ContextManager;
28 import org.apache.derby.iapi.services.compiler.MethodBuilder;
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import org.apache.derby.iapi.error.StandardException;
32 import org.apache.derby.iapi.sql.execute.ConstantAction;
33 import org.apache.derby.iapi.sql.Activation;
34 import org.apache.derby.iapi.sql.ResultSet;
35 import org.apache.derby.iapi.sql.StatementType;
36
37 import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
38
39 import java.util.Vector JavaDoc;
40
41
42 /**
43  * A SetSchemaNode is the root of a QueryTree that
44  * represents a SET SCHEMA statement. It isn't
45  * replicated, but it generates a ConstantAction
46  * because it is basically easier than generating
47  * the code from scratch.
48  *
49  * @author jamie
50  */

51
52 public class SetSchemaNode extends MiscellaneousStatementNode
53 {
54     private String JavaDoc name;
55     private int type;
56     
57     /**
58      * Initializer for a SetSchemaNode
59      *
60      * @param schemaName The name of the new schema
61      * @param type Type of schema name could be USER or dynamic parameter
62      *
63      */

64     public void init(Object JavaDoc schemaName, Object JavaDoc type)
65     {
66         this.name = (String JavaDoc) schemaName;
67         if (type != null)
68             this.type = ((Integer JavaDoc)type).intValue();
69     }
70
71     /**
72      * Convert this object to a String. See comments in QueryTreeNode.java
73      * for how this should be done for tree printing.
74      *
75      * @return This object as a String
76      */

77
78     public String JavaDoc toString()
79     {
80         if (SanityManager.DEBUG)
81         {
82             return super.toString() +
83                 (type == StatementType.SET_SCHEMA_USER ? "schemaName: \nUSER\n" :
84                 (type == StatementType.SET_SCHEMA_DYNAMIC ? "schemaName: \n?\n" :
85                     "schemaName: " + "\n" + name + "\n"));
86         }
87         else
88         {
89             return "";
90         }
91     }
92
93     public String JavaDoc statementToString()
94     {
95         return "SET SCHEMA";
96     }
97
98     /**
99      * Create the Constant information that will drive the guts of Execution.
100      *
101      * @exception StandardException Thrown on failure
102      */

103     public ConstantAction makeConstantAction() throws StandardException
104     {
105         return getGenericConstantActionFactory().getSetSchemaConstantAction(name, type);
106     }
107     /**
108      * Generate code, need to push parameters
109      *
110      * @param acb The ActivationClassBuilder for the class being built
111      * @param mb the method for the execute() method to be built
112      *
113      * @exception StandardException Thrown on error
114      */

115
116     public void generate(ActivationClassBuilder acb,
117                                 MethodBuilder mb)
118                             throws StandardException
119     {
120         //generate the parameters for the DYNAMIC SET SCHEMA
121
if (type == StatementType.SET_SCHEMA_DYNAMIC)
122             generateParameterValueSet(acb);
123
124         // The generated java is the expression:
125
// return ResultSetFactory.getMiscResultSet(this )
126

127         acb.pushGetResultSetFactoryExpression(mb);
128
129         acb.pushThisAsActivation(mb); // first arg
130

131         mb.callMethod(VMOpcode.INVOKEINTERFACE, (String JavaDoc) null, "getMiscResultSet",
132                         ClassName.ResultSet, 1);
133     }
134     /**
135      * Generate the code to create the ParameterValueSet, if necessary,
136      * when constructing the activation. Also generate the code to call
137      * a method that will throw an exception if we try to execute without
138      * all the parameters being set.
139      *
140      * @param acb The ActivationClassBuilder for the class we're building
141      */

142
143     void generateParameterValueSet(ActivationClassBuilder acb)
144         throws StandardException
145     {
146         Vector JavaDoc parameterList = getCompilerContext().getParameterList();
147         // parameter list size should be 1
148
if (SanityManager.DEBUG)
149             SanityManager.ASSERT(parameterList != null && parameterList.size() == 1);
150             
151         ParameterNode.generateParameterValueSet ( acb, 1, parameterList);
152     }
153
154     /**
155      * Returns the type of activation this class
156      * generates.
157      *
158      * @return NEED_PARAM_ACTIVATION or
159      * NEED_NOTHING_ACTIVATION depending on params
160      *
161      */

162     int activationKind()
163     {
164         Vector JavaDoc parameterList = getCompilerContext().getParameterList();
165         /*
166         ** We need parameters
167         ** only for those that have parameters.
168         */

169         if (type == StatementType.SET_SCHEMA_DYNAMIC)
170         {
171             return StatementNode.NEED_PARAM_ACTIVATION;
172         }
173         else
174         {
175             return StatementNode.NEED_NOTHING_ACTIVATION;
176         }
177     }
178 }
179
Popular Tags