001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.CreateViewConstantAction
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.DataDescriptorGenerator;
027: import org.apache.derby.iapi.sql.dictionary.DataDictionary;
028: import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
029: import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList;
030: import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
031: import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
032: import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;
033: import org.apache.derby.iapi.sql.depend.DependencyManager;
034: import org.apache.derby.iapi.store.access.TransactionController;
035:
036: import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
037:
038: import org.apache.derby.iapi.sql.depend.Provider;
039: import org.apache.derby.iapi.sql.depend.ProviderInfo;
040:
041: import org.apache.derby.iapi.reference.SQLState;
042:
043: import org.apache.derby.iapi.sql.Activation;
044:
045: import org.apache.derby.iapi.error.StandardException;
046:
047: import org.apache.derby.iapi.services.sanity.SanityManager;
048:
049: import org.apache.derby.catalog.UUID;
050:
051: /**
052: * This class describes actions that are ALWAYS performed for a
053: * CREATE VIEW Statement at Execution time.
054: *
055: * @author Jerry Brenner.
056: */
057:
058: class CreateViewConstantAction extends DDLConstantAction {
059:
060: private final String tableName;
061: private final String schemaName;
062: private final String viewText;
063: private final int tableType;
064: private final int checkOption;
065: private final ColumnInfo[] columnInfo;
066: private final ProviderInfo[] providerInfo;
067: private final UUID compSchemaId;
068:
069: // CONSTRUCTORS
070: /**
071: * Make the ConstantAction for a CREATE VIEW statement.
072: *
073: * @param schemaName name for the schema that view lives in.
074: * @param tableName Name of table.
075: * @param tableType Type of table (e.g., BASE).
076: * @param viewText Text of query expression for view definition
077: * @param checkOption Check option type
078: * @param columnInfo Information on all the columns in the table.
079: * @param providerInfo Information on all the Providers
080: * @param compSchemaId Compilation Schema Id
081: * (REMIND tableDescriptor ignored)
082: */
083: CreateViewConstantAction(String schemaName, String tableName,
084: int tableType, String viewText, int checkOption,
085: ColumnInfo[] columnInfo, ProviderInfo[] providerInfo,
086: UUID compSchemaId) {
087: this .schemaName = schemaName;
088: this .tableName = tableName;
089: this .tableType = tableType;
090: this .viewText = viewText;
091: this .checkOption = checkOption;
092: this .columnInfo = columnInfo;
093: this .providerInfo = providerInfo;
094: this .compSchemaId = compSchemaId;
095:
096: if (SanityManager.DEBUG) {
097: SanityManager.ASSERT(schemaName != null,
098: "Schema name is null");
099: }
100: }
101:
102: // OBJECT METHODS
103:
104: public String toString() {
105: return constructToString("CREATE VIEW ", tableName);
106: }
107:
108: // INTERFACE METHODS
109:
110: /**
111: * This is the guts of the Execution-time logic for CREATE TABLE.
112: *
113: * @see ConstantAction#executeConstantAction
114: *
115: * @exception StandardException Thrown on failure
116: */
117: public void executeConstantAction(Activation activation)
118: throws StandardException {
119: TableDescriptor td;
120: UUID toid;
121: SchemaDescriptor schemaDescriptor;
122: ColumnDescriptor columnDescriptor;
123: ViewDescriptor vd;
124:
125: LanguageConnectionContext lcc = activation
126: .getLanguageConnectionContext();
127: DataDictionary dd = lcc.getDataDictionary();
128: DependencyManager dm = dd.getDependencyManager();
129: TransactionController tc = lcc.getTransactionExecute();
130:
131: /*
132: ** Inform the data dictionary that we are about to write to it.
133: ** There are several calls to data dictionary "get" methods here
134: ** that might be done in "read" mode in the data dictionary, but
135: ** it seemed safer to do this whole operation in "write" mode.
136: **
137: ** We tell the data dictionary we're done writing at the end of
138: ** the transaction.
139: */
140: dd.startWriting(lcc);
141:
142: SchemaDescriptor sd = DDLConstantAction
143: .getSchemaDescriptorForCreate(dd, activation,
144: schemaName);
145:
146: /* Create a new table descriptor.
147: * (Pass in row locking, even though meaningless for views.)
148: */
149: DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
150: td = ddg.newTableDescriptor(tableName, sd, tableType,
151: TableDescriptor.ROW_LOCK_GRANULARITY);
152:
153: dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM,
154: false, tc);
155: toid = td.getUUID();
156:
157: // for each column, stuff system.column
158: ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length];
159: int index = 1;
160: for (int ix = 0; ix < columnInfo.length; ix++) {
161: columnDescriptor = new ColumnDescriptor(
162: columnInfo[ix].name, index++,
163: columnInfo[ix].dataType,
164: columnInfo[ix].defaultValue,
165: columnInfo[ix].defaultInfo, td, (UUID) null,
166: columnInfo[ix].autoincStart,
167: columnInfo[ix].autoincInc);
168: cdlArray[ix] = columnDescriptor;
169: }
170:
171: dd.addDescriptorArray(cdlArray, td,
172: DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
173:
174: // add columns to the column descriptor list.
175: ColumnDescriptorList cdl = td.getColumnDescriptorList();
176: for (int i = 0; i < cdlArray.length; i++)
177: cdl.add(cdlArray[i]);
178:
179: /* Get and add a view descriptor */
180: vd = ddg.newViewDescriptor(toid, tableName, viewText,
181: checkOption, (compSchemaId == null) ? lcc
182: .getDefaultSchema().getUUID() : compSchemaId);
183:
184: for (int ix = 0; ix < providerInfo.length; ix++) {
185: /* We should always be able to find the Provider */
186: try {
187: Provider provider = (Provider) providerInfo[ix]
188: .getDependableFinder().getDependable(
189: providerInfo[ix].getObjectId());
190: if (provider == null) //see beetle 4444
191: {
192: throw StandardException.newException(
193: SQLState.LANG_OBJECT_NOT_FOUND, "OBJECT",
194: providerInfo[ix].getObjectId());
195: }
196: dm.addDependency(vd, provider, lcc.getContextManager());
197: } catch (java.sql.SQLException te) {
198: // we should allow timeout to be thrown
199: throw StandardException.plainWrapException(te);
200: }
201: }
202: //store view's dependency on various privileges in the dependeny system
203: storeViewTriggerDependenciesOnPrivileges(activation, vd);
204:
205: dd.addDescriptor(vd, sd, DataDictionary.SYSVIEWS_CATALOG_NUM,
206: true, tc);
207: }
208: }
|