--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.CreateViewConstantAction\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.sql.execute.ConstantAction;\r
+\r
+import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator;\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList;\r
+import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.TableDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;\r
+import org.apache.derby.iapi.sql.depend.DependencyManager;\r
+import org.apache.derby.iapi.store.access.TransactionController;\r
+\r
+import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;\r
+\r
+import org.apache.derby.iapi.sql.depend.Provider;\r
+import org.apache.derby.iapi.sql.depend.ProviderInfo;\r
+\r
+import org.apache.derby.iapi.reference.SQLState;\r
+\r
+import org.apache.derby.iapi.sql.Activation;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.catalog.UUID;\r
+\r
+/**\r
+ * This class describes actions that are ALWAYS performed for a\r
+ * CREATE VIEW Statement at Execution time.\r
+ * A view is represented as:\r
+ * <UL>\r
+ * <LI> TableDescriptor with the name of the view and type VIEW_TYPE\r
+ * <LI> Set of ColumnDescriptor's for the column names and types\r
+ * <LI> ViewDescriptor describing the SQL query that makes up the view.\r
+ * </UL>\r
+ * Dependencies are created as:\r
+ * <UL>\r
+ * <LI> ViewDescriptor depends on the Providers that its compiled\r
+ * query depends on.\r
+ * <LI> ViewDescriptor depends on the privileges required to execute the view.\r
+ * </UL>\r
+ * Note there are no dependencies created between the ViewDescriptor, TableDecriptor\r
+ * and the ColumnDescriptor's.\r
+ *\r
+ */\r
+\r
+class CreateViewConstantAction extends DDLConstantAction\r
+{\r
+ \r
+ private final String tableName;\r
+ private final String schemaName;\r
+ private final String viewText;\r
+ private final int tableType;\r
+ private final int checkOption;\r
+ private final ColumnInfo[] columnInfo;\r
+ private final ProviderInfo[] providerInfo;\r
+ private final UUID compSchemaId;\r
+ \r
+ // CONSTRUCTORS\r
+ /**\r
+ * Make the ConstantAction for a CREATE VIEW statement.\r
+ *\r
+ * @param schemaName name for the schema that view lives in.\r
+ * @param tableName Name of view.\r
+ * @param tableType Type of table (ie. TableDescriptor.VIEW_TYPE).\r
+ * @param viewText Text of query expression for view definition\r
+ * @param checkOption Check option type\r
+ * @param columnInfo Information on all the columns in the table.\r
+ * @param providerInfo Information on all the Providers\r
+ * @param compSchemaId Compilation Schema Id\r
+ */\r
+ CreateViewConstantAction(\r
+ String schemaName,\r
+ String tableName,\r
+ int tableType,\r
+ String viewText,\r
+ int checkOption,\r
+ ColumnInfo[] columnInfo,\r
+ ProviderInfo[] providerInfo,\r
+ UUID compSchemaId)\r
+ {\r
+ this.schemaName = schemaName;\r
+ this.tableName = tableName;\r
+ this.tableType = tableType;\r
+ this.viewText = viewText;\r
+ this.checkOption = checkOption;\r
+ this.columnInfo = columnInfo;\r
+ this.providerInfo = providerInfo;\r
+ this.compSchemaId = compSchemaId;\r
+\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.ASSERT(schemaName != null, "Schema name is null");\r
+ }\r
+ }\r
+\r
+ // OBJECT METHODS\r
+\r
+ public String toString()\r
+ {\r
+ return constructToString("CREATE VIEW ", tableName);\r
+ }\r
+\r
+ // INTERFACE METHODS\r
+\r
+\r
+ /**\r
+ * This is the guts of the Execution-time logic for CREATE VIEW.\r
+ *\r
+ * @see ConstantAction#executeConstantAction\r
+ *\r
+ * @exception StandardException Thrown on failure\r
+ */\r
+ public void executeConstantAction( Activation activation )\r
+ throws StandardException\r
+ {\r
+ TableDescriptor td;\r
+ UUID toid;\r
+ ColumnDescriptor columnDescriptor;\r
+ ViewDescriptor vd;\r
+\r
+ LanguageConnectionContext lcc = activation.getLanguageConnectionContext();\r
+ DataDictionary dd = lcc.getDataDictionary();\r
+ DependencyManager dm = dd.getDependencyManager();\r
+ TransactionController tc = lcc.getTransactionExecute();\r
+\r
+ /*\r
+ ** Inform the data dictionary that we are about to write to it.\r
+ ** There are several calls to data dictionary "get" methods here\r
+ ** that might be done in "read" mode in the data dictionary, but\r
+ ** it seemed safer to do this whole operation in "write" mode.\r
+ **\r
+ ** We tell the data dictionary we're done writing at the end of\r
+ ** the transaction.\r
+ */\r
+ dd.startWriting(lcc);\r
+\r
+ SchemaDescriptor sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName);\r
+\r
+ /* Create a new table descriptor.\r
+ * (Pass in row locking, even though meaningless for views.)\r
+ */\r
+ DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();\r
+ td = ddg.newTableDescriptor(tableName,\r
+ sd,\r
+ tableType,\r
+ TableDescriptor.ROW_LOCK_GRANULARITY);\r
+\r
+ dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);\r
+ toid = td.getUUID();\r
+\r
+ // for each column, stuff system.column\r
+ ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length];\r
+ int index = 1;\r
+ for (int ix = 0; ix < columnInfo.length; ix++)\r
+ {\r
+ columnDescriptor = new ColumnDescriptor(\r
+ columnInfo[ix].name,\r
+ index++,\r
+ columnInfo[ix].dataType,\r
+ columnInfo[ix].defaultValue,\r
+ columnInfo[ix].defaultInfo,\r
+ td,\r
+ (UUID) null,\r
+ columnInfo[ix].autoincStart,\r
+ columnInfo[ix].autoincInc\r
+ );\r
+ cdlArray[ix] = columnDescriptor;\r
+ }\r
+\r
+ dd.addDescriptorArray(cdlArray, td,\r
+ DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);\r
+\r
+ // add columns to the column descriptor list.\r
+ ColumnDescriptorList cdl = td.getColumnDescriptorList();\r
+ for (int i = 0; i < cdlArray.length; i++)\r
+ cdl.add(cdlArray[i]);\r
+\r
+ /* Get and add a view descriptor */\r
+ vd = ddg.newViewDescriptor(toid, tableName, viewText, \r
+ checkOption, \r
+ (compSchemaId == null) ?\r
+ lcc.getDefaultSchema().getUUID() :\r
+ compSchemaId);\r
+\r
+ for (int ix = 0; ix < providerInfo.length; ix++)\r
+ {\r
+ /* We should always be able to find the Provider */\r
+ Provider provider = (Provider) providerInfo[ix].\r
+ getDependableFinder().\r
+ getDependable(dd,\r
+ providerInfo[ix].getObjectId());\r
+ dm.addDependency(vd, provider, lcc.getContextManager());\r
+ }\r
+ //store view's dependency on various privileges in the dependeny system\r
+ storeViewTriggerDependenciesOnPrivileges(activation, vd);\r
+\r
+ dd.addDescriptor(vd, sd, DataDictionary.SYSVIEWS_CATALOG_NUM, true, tc);\r
+ }\r
+}\r