001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.SetSchemaConstantAction
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.execute;
023:
024: import org.apache.derby.iapi.sql.execute.ConstantAction;
025:
026: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
027: import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
028: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
029: import org.apache.derby.iapi.sql.ParameterValueSet;
030: import org.apache.derby.iapi.sql.StatementType;
031:
032: import org.apache.derby.iapi.types.DataValueDescriptor;
033:
034: import org.apache.derby.iapi.reference.Limits;
035:
036: import org.apache.derby.iapi.error.StandardException;
037:
038: import org.apache.derby.iapi.sql.Activation;
039:
040: import org.apache.derby.iapi.services.sanity.SanityManager;
041:
042: import org.apache.derby.iapi.reference.SQLState;
043:
044: /**
045: * This class describes actions that are ALWAYS performed for a
046: * SET SCHEMA Statement at Execution time.
047: *
048: * @author jamie
049: */
050:
051: class SetSchemaConstantAction extends GenericConstantAction {
052:
053: private final String schemaName;
054: private final int type;
055:
056: // CONSTRUCTORS
057:
058: /**
059: * Make the ConstantAction for a SET SCHEMA statement.
060: *
061: * @param schemaName Name of schema.
062: * @param type type of set schema (e.g. SET_SCHEMA_DYNAMIC, SET_SCHEMA_USER)
063: */
064: SetSchemaConstantAction(String schemaName, int type) {
065: this .schemaName = schemaName;
066: this .type = type;
067: }
068:
069: ///////////////////////////////////////////////
070: //
071: // OBJECT SHADOWS
072: //
073: ///////////////////////////////////////////////
074:
075: public String toString() {
076: // Do not put this under SanityManager.DEBUG - it is needed for
077: // error reporting.
078: // if the error happens after we have figured out the schema name for
079: // dynamic we want to use it rather than ?
080: return "SET SCHEMA "
081: + ((type == StatementType.SET_SCHEMA_USER) ? "USER"
082: : ((type == StatementType.SET_SCHEMA_DYNAMIC && schemaName == null) ? "?"
083: : schemaName));
084: }
085:
086: // INTERFACE METHODS
087:
088: /**
089: * This is the guts of the Execution-time logic for CREATE SCHEMA.
090: *
091: * @see ConstantAction#executeConstantAction
092: *
093: * @exception StandardException Thrown on failure
094: */
095: public void executeConstantAction(Activation activation)
096: throws StandardException {
097: LanguageConnectionContext lcc;
098: DataDictionary dd;
099:
100: // find the language context.
101: lcc = activation.getLanguageConnectionContext();
102:
103: dd = lcc.getDataDictionary();
104: String this SchemaName = schemaName;
105: if (type == StatementType.SET_SCHEMA_DYNAMIC) {
106: ParameterValueSet pvs = activation.getParameterValueSet();
107: DataValueDescriptor dvs = pvs.getParameter(0);
108: this SchemaName = dvs.getString();
109: //null parameter is not allowed
110: if (this SchemaName == null
111: || this SchemaName.length() > Limits.MAX_IDENTIFIER_LENGTH)
112: throw StandardException.newException(
113: SQLState.LANG_DB2_REPLACEMENT_ERROR,
114: "CURRENT SCHEMA");
115: } else if (type == StatementType.SET_SCHEMA_USER) {
116: this SchemaName = lcc.getAuthorizationId();
117: }
118: // if schemaName is null, sd will be null and default schema will be used
119: SchemaDescriptor sd = dd.getSchemaDescriptor(this SchemaName,
120: null, true);
121: lcc.setDefaultSchema(sd);
122: }
123: }
|