001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.CreateSchemaConstantAction
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.store.access.TransactionController;
027:
028: import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;
029: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
030: import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
031: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
032:
033: import org.apache.derby.iapi.reference.SQLState;
034:
035: import org.apache.derby.iapi.sql.Activation;
036:
037: import org.apache.derby.iapi.error.StandardException;
038:
039: import org.apache.derby.iapi.services.sanity.SanityManager;
040:
041: import org.apache.derby.catalog.UUID;
042:
043: /**
044: * This class describes actions that are ALWAYS performed for a
045: * CREATE SCHEMA Statement at Execution time.
046: *
047: * @author jamie
048: */
049:
050: class CreateSchemaConstantAction extends DDLConstantAction {
051:
052: private final String aid; // authorization id
053: private final String schemaName;
054:
055: // CONSTRUCTORS
056:
057: /**
058: * Make the ConstantAction for a CREATE SCHEMA statement.
059: * When executed, will set the default schema to the
060: * new schema if the setToDefault parameter is set to
061: * true.
062: *
063: * @param schemaName Name of table.
064: * @param aid Authorizaton id
065: */
066: CreateSchemaConstantAction(String schemaName, String aid) {
067: this .schemaName = schemaName;
068: this .aid = aid;
069: }
070:
071: ///////////////////////////////////////////////
072: //
073: // OBJECT SHADOWS
074: //
075: ///////////////////////////////////////////////
076:
077: public String toString() {
078: // Do not put this under SanityManager.DEBUG - it is needed for
079: // error reporting.
080: return "CREATE SCHEMA " + schemaName;
081: }
082:
083: // INTERFACE METHODS
084:
085: /**
086: * This is the guts of the Execution-time logic for CREATE SCHEMA.
087: *
088: * @see ConstantAction#executeConstantAction
089: *
090: * @exception StandardException Thrown on failure
091: */
092: public void executeConstantAction(Activation activation)
093: throws StandardException {
094: LanguageConnectionContext lcc = activation
095: .getLanguageConnectionContext();
096: DataDictionary dd = lcc.getDataDictionary();
097: TransactionController tc = lcc.getTransactionExecute();
098: DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
099:
100: SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, lcc
101: .getTransactionExecute(), false);
102:
103: //if the schema descriptor is an in-memory schema, we donot throw schema already exists exception for it.
104: //This is to handle in-memory SESSION schema for temp tables
105: if ((sd != null) && (sd.getUUID() != null)) {
106: throw StandardException.newException(
107: SQLState.LANG_OBJECT_ALREADY_EXISTS, "Schema",
108: schemaName);
109: }
110:
111: UUID tmpSchemaId = dd.getUUIDFactory().createUUID();
112:
113: /*
114: ** AID defaults to connection authorization if not
115: ** specified in CREATE SCHEMA (if we had module
116: ** authorizations, that would be the first check
117: ** for default, then session aid).
118: */
119: String this Aid = aid;
120: if (this Aid == null) {
121: this Aid = lcc.getAuthorizationId();
122: }
123:
124: /*
125: ** Inform the data dictionary that we are about to write to it.
126: ** There are several calls to data dictionary "get" methods here
127: ** that might be done in "read" mode in the data dictionary, but
128: ** it seemed safer to do this whole operation in "write" mode.
129: **
130: ** We tell the data dictionary we're done writing at the end of
131: ** the transaction.
132: */
133: dd.startWriting(lcc);
134:
135: sd = ddg.newSchemaDescriptor(schemaName, this Aid, tmpSchemaId);
136:
137: dd.addDescriptor(sd, null,
138: DataDictionary.SYSSCHEMAS_CATALOG_NUM, false, tc);
139: }
140: }
|