001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.compile.SetSchemaNode
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.compile;
023:
024: import org.apache.derby.iapi.reference.ClassName;
025: import org.apache.derby.iapi.services.classfile.VMOpcode;
026:
027: import org.apache.derby.iapi.services.context.ContextManager;
028: import org.apache.derby.iapi.services.compiler.MethodBuilder;
029: import org.apache.derby.iapi.services.sanity.SanityManager;
030:
031: import org.apache.derby.iapi.error.StandardException;
032: import org.apache.derby.iapi.sql.execute.ConstantAction;
033: import org.apache.derby.iapi.sql.Activation;
034: import org.apache.derby.iapi.sql.ResultSet;
035: import org.apache.derby.iapi.sql.StatementType;
036:
037: import org.apache.derby.impl.sql.compile.ActivationClassBuilder;
038:
039: import java.util.Vector;
040:
041: /**
042: * A SetSchemaNode is the root of a QueryTree that
043: * represents a SET SCHEMA statement. It isn't
044: * replicated, but it generates a ConstantAction
045: * because it is basically easier than generating
046: * the code from scratch.
047: *
048: * @author jamie
049: */
050:
051: public class SetSchemaNode extends MiscellaneousStatementNode {
052: private String name;
053: private int type;
054:
055: /**
056: * Initializer for a SetSchemaNode
057: *
058: * @param schemaName The name of the new schema
059: * @param type Type of schema name could be USER or dynamic parameter
060: *
061: */
062: public void init(Object schemaName, Object type) {
063: this .name = (String) schemaName;
064: if (type != null)
065: this .type = ((Integer) type).intValue();
066: }
067:
068: /**
069: * Convert this object to a String. See comments in QueryTreeNode.java
070: * for how this should be done for tree printing.
071: *
072: * @return This object as a String
073: */
074:
075: public String toString() {
076: if (SanityManager.DEBUG) {
077: return super .toString()
078: + (type == StatementType.SET_SCHEMA_USER ? "schemaName: \nUSER\n"
079: : (type == StatementType.SET_SCHEMA_DYNAMIC ? "schemaName: \n?\n"
080: : "schemaName: " + "\n" + name
081: + "\n"));
082: } else {
083: return "";
084: }
085: }
086:
087: public String statementToString() {
088: return "SET SCHEMA";
089: }
090:
091: /**
092: * Create the Constant information that will drive the guts of Execution.
093: *
094: * @exception StandardException Thrown on failure
095: */
096: public ConstantAction makeConstantAction() throws StandardException {
097: return getGenericConstantActionFactory()
098: .getSetSchemaConstantAction(name, type);
099: }
100:
101: /**
102: * Generate code, need to push parameters
103: *
104: * @param acb The ActivationClassBuilder for the class being built
105: * @param mb the method for the execute() method to be built
106: *
107: * @exception StandardException Thrown on error
108: */
109:
110: public void generate(ActivationClassBuilder acb, MethodBuilder mb)
111: throws StandardException {
112: //generate the parameters for the DYNAMIC SET SCHEMA
113: if (type == StatementType.SET_SCHEMA_DYNAMIC)
114: generateParameterValueSet(acb);
115:
116: // The generated java is the expression:
117: // return ResultSetFactory.getMiscResultSet(this )
118:
119: acb.pushGetResultSetFactoryExpression(mb);
120:
121: acb.pushThisAsActivation(mb); // first arg
122:
123: mb.callMethod(VMOpcode.INVOKEINTERFACE, (String) null,
124: "getMiscResultSet", ClassName.ResultSet, 1);
125: }
126:
127: /**
128: * Generate the code to create the ParameterValueSet, if necessary,
129: * when constructing the activation. Also generate the code to call
130: * a method that will throw an exception if we try to execute without
131: * all the parameters being set.
132: *
133: * @param acb The ActivationClassBuilder for the class we're building
134: */
135:
136: void generateParameterValueSet(ActivationClassBuilder acb)
137: throws StandardException {
138: Vector parameterList = getCompilerContext().getParameterList();
139: // parameter list size should be 1
140: if (SanityManager.DEBUG)
141: SanityManager.ASSERT(parameterList != null
142: && parameterList.size() == 1);
143:
144: ParameterNode.generateParameterValueSet(acb, 1, parameterList);
145: }
146:
147: /**
148: * Returns the type of activation this class
149: * generates.
150: *
151: * @return NEED_PARAM_ACTIVATION or
152: * NEED_NOTHING_ACTIVATION depending on params
153: *
154: */
155: int activationKind() {
156: Vector parameterList = getCompilerContext().getParameterList();
157: /*
158: ** We need parameters
159: ** only for those that have parameters.
160: */
161: if (type == StatementType.SET_SCHEMA_DYNAMIC) {
162: return StatementNode.NEED_PARAM_ACTIVATION;
163: } else {
164: return StatementNode.NEED_NOTHING_ACTIVATION;
165: }
166: }
167: }
|