Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / MyDerby-10.3 / java / engine / org / apache / derby / impl / sql / execute / DropTableConstantAction.java
diff --git a/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/sql/execute/DropTableConstantAction.java b/JMCR-Stable/real-world application/MyDerby-10.3/java/engine/org/apache/derby/impl/sql/execute/DropTableConstantAction.java
new file mode 100644 (file)
index 0000000..60aa2d2
--- /dev/null
@@ -0,0 +1,386 @@
+/*\r
+\r
+   Derby - Class org.apache.derby.impl.sql.execute.DropTableConstantAction\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.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;\r
+import org.apache.derby.iapi.sql.StatementType;\r
+\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.ConglomerateDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList;\r
+import org.apache.derby.iapi.sql.dictionary.DataDictionary;\r
+import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor;\r
+import org.apache.derby.iapi.sql.dictionary.GenericDescriptorList;\r
+import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor;\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.TriggerDescriptor;\r
+\r
+import org.apache.derby.iapi.sql.depend.DependencyManager;\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.store.access.TransactionController;\r
+\r
+import org.apache.derby.catalog.UUID;\r
+\r
+import java.util.Enumeration;\r
+\r
+/**\r
+ *     This class  describes actions that are ALWAYS performed for a\r
+ *     DROP TABLE Statement at Execution time.\r
+ *\r
+ */\r
+\r
+class DropTableConstantAction extends DDLSingleTableConstantAction\r
+{\r
+\r
+       private final long                              conglomerateNumber;\r
+       private final String                            fullTableName;\r
+       private final String                            tableName;\r
+       private final SchemaDescriptor  sd;\r
+       private final boolean   cascade;                \r
+\r
+       // CONSTRUCTORS\r
+\r
+\r
+       /**\r
+        *      Make the ConstantAction for a DROP TABLE statement.\r
+        *\r
+        *\r
+        *      @param  fullTableName           Fully qualified table name\r
+        *      @param  tableName                       Table name.\r
+        *      @param  sd                                      Schema that table lives in.\r
+        *  @param  conglomerateNumber  Conglomerate number for heap\r
+        *  @param  tableId                             UUID for table\r
+        *  @param  behavior                    drop behavior: RESTRICT, CASCADE or default\r
+        *\r
+        */\r
+       DropTableConstantAction(\r
+                                                               String                          fullTableName,\r
+                                                               String                          tableName,\r
+                                                               SchemaDescriptor        sd,\r
+                                                               long                            conglomerateNumber,\r
+                                                               UUID                            tableId,\r
+                                                               int                                     behavior)\r
+       {\r
+               super(tableId);\r
+               this.fullTableName = fullTableName;\r
+               this.tableName = tableName;\r
+               this.sd = sd;\r
+               this.conglomerateNumber = conglomerateNumber;\r
+               this.cascade = (behavior == StatementType.DROP_CASCADE);\r
+\r
+               if (SanityManager.DEBUG)\r
+               {\r
+                       SanityManager.ASSERT(sd != null, "SchemaDescriptor is null");\r
+               }\r
+       }\r
+\r
+       // OBJECT METHODS\r
+\r
+       public  String  toString()\r
+       {\r
+               // Do not put this under SanityManager.DEBUG - it is needed for\r
+               // error reporting.\r
+               return "DROP TABLE " + fullTableName;\r
+       }\r
+\r
+       // INTERFACE METHODS\r
+\r
+\r
+       /**\r
+        *      This is the guts of the Execution-time logic for DROP TABLE.\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 tableID;\r
+               ConglomerateDescriptor[] cds;\r
+\r
+               LanguageConnectionContext lcc = activation.getLanguageConnectionContext();\r
+               DataDictionary dd = lcc.getDataDictionary();\r
+               DependencyManager dm = dd.getDependencyManager();\r
+               TransactionController tc = lcc.getTransactionExecute();\r
+\r
+               if ((sd != null) && sd.getSchemaName().equals(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME)) {\r
+                       td = lcc.getTableDescriptorForDeclaredGlobalTempTable(tableName); //check if this is a temp table before checking data dictionary\r
+\r
+                       if (td == null) //td null here means it is not a temporary table. Look for table in physical SESSION schema\r
+                               td = dd.getTableDescriptor(tableName, sd);\r
+\r
+                       if (td == null) //td null means tableName is not a temp table and it is not a physical table in SESSION schema\r
+                       {\r
+                               throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);\r
+                       }\r
+\r
+                       if (td.getTableType() ==  TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {\r
+                               dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);\r
+                               tc.dropConglomerate(td.getHeapConglomerateId());\r
+                               lcc.dropDeclaredGlobalTempTable(tableName);\r
+                               return;\r
+                       }\r
+    }\r
+\r
+               /* Lock the table before we access the data dictionary\r
+                * to prevent deadlocks.\r
+                *\r
+                * Note that for DROP TABLE replayed at Targets during REFRESH,\r
+                * the conglomerateNumber will be 0. That's ok. During REFRESH,\r
+                * we don't need to lock the conglomerate.\r
+                */\r
+               if ( conglomerateNumber != 0 ) { lockTableForDDL(tc, conglomerateNumber, true); }\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
+               /* Get the table descriptor. */\r
+               td = dd.getTableDescriptor(tableId);\r
+\r
+               if (td == null)\r
+               {\r
+                       throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);\r
+               }\r
+\r
+               /* Get an exclusive table lock on the table. */\r
+               long heapId = td.getHeapConglomerateId();\r
+               lockTableForDDL(tc, heapId, true);\r
+\r
+               /* Drop the triggers */\r
+               GenericDescriptorList tdl = dd.getTriggerDescriptors(td);\r
+               Enumeration descs = tdl.elements();\r
+               while (descs.hasMoreElements())\r
+               {\r
+                       TriggerDescriptor trd = (TriggerDescriptor) descs.nextElement();\r
+            trd.drop(lcc);\r
+               }\r
+\r
+               /* Drop all defaults */\r
+               ColumnDescriptorList cdl = td.getColumnDescriptorList();\r
+               int                                      cdlSize = cdl.size();\r
+               \r
+               for (int index = 0; index < cdlSize; index++)\r
+               {\r
+                       ColumnDescriptor cd = (ColumnDescriptor) cdl.elementAt(index);\r
+\r
+                       // If column has a default we drop the default and\r
+                       // any dependencies\r
+                       if (cd.getDefaultInfo() != null)\r
+                       {\r
+                               DefaultDescriptor defaultDesc = cd.getDefaultDescriptor(dd);\r
+                               dm.clearDependencies(lcc, defaultDesc);\r
+                       }\r
+               }\r
+\r
+               /* Drop the columns */\r
+               dd.dropAllColumnDescriptors(tableId, tc);\r
+\r
+               /* Drop all table and column permission descriptors */\r
+               dd.dropAllTableAndColPermDescriptors(tableId, tc);\r
+\r
+               /* Drop the constraints */\r
+               dropAllConstraintDescriptors(td, activation);\r
+\r
+               /*\r
+               ** Drop all the conglomerates.  Drop the heap last, because the\r
+               ** store needs it for locking the indexes when they are dropped.\r
+               */\r
+               cds = td.getConglomerateDescriptors();\r
+\r
+               long[] dropped = new long[cds.length - 1];\r
+               int numDropped = 0;\r
+               for (int index = 0; index < cds.length; index++)\r
+               {\r
+                       ConglomerateDescriptor cd = cds[index];\r
+\r
+                       /* if it's for an index, since similar indexes share one\r
+                        * conglomerate, we only drop the conglomerate once\r
+                        */\r
+                       if (cd.getConglomerateNumber() != heapId)\r
+                       {\r
+                               long thisConglom = cd.getConglomerateNumber();\r
+\r
+                               int i;\r
+                               for (i = 0; i < numDropped; i++)\r
+                               {\r
+                                       if (dropped[i] == thisConglom)\r
+                                               break;\r
+                               }\r
+                               if (i == numDropped)    // not dropped\r
+                               {\r
+                                       dropped[numDropped++] = thisConglom;\r
+                                       tc.dropConglomerate(thisConglom);\r
+                                       dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);\r
+                               }\r
+                       }\r
+               }\r
+\r
+               /* Prepare all dependents to invalidate.  (This is there chance\r
+                * to say that they can't be invalidated.  For example, an open\r
+                * cursor referencing a table/view that the user is attempting to\r
+                * drop.) If no one objects, then invalidate any dependent objects.\r
+                * We check for invalidation before we drop the table descriptor\r
+                * since the table descriptor may be looked up as part of\r
+                * decoding tuples in SYSDEPENDS.\r
+                */\r
+\r
+               dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);\r
+\r
+               /* Drop the table */\r
+               dd.dropTableDescriptor(td, sd, tc);\r
+\r
+               /* Drop the conglomerate descriptors */\r
+               dd.dropAllConglomerateDescriptors(td, tc);\r
+\r
+               /* Drop the store element at last, to prevent dangling reference\r
+                * for open cursor, beetle 4393.\r
+                */\r
+               tc.dropConglomerate(heapId);\r
+\r
+       }\r
+\r
+       private void dropAllConstraintDescriptors(TableDescriptor td, Activation activation)\r
+               throws StandardException\r
+       {\r
+               ConstraintDescriptor                            cd;\r
+               ConstraintDescriptorList                        cdl;\r
+               ConstraintDescriptor                            fkcd;\r
+               ConstraintDescriptorList                        fkcdl;\r
+               LanguageConnectionContext                       lcc = activation.getLanguageConnectionContext();\r
+               DataDictionary dd = lcc.getDataDictionary();\r
+               DependencyManager dm = dd.getDependencyManager();\r
+               TransactionController tc = lcc.getTransactionExecute();\r
+\r
+               cdl = dd.getConstraintDescriptors(td);\r
+               \r
+               /*\r
+               ** First go, don't drop unique or primary keys.\r
+               ** This will ensure that self-referential constraints\r
+               ** will work ok, even if not cascading.\r
+               */\r
+               /* The current element will be deleted underneath\r
+                * the loop, so we only increment the counter when\r
+                * skipping an element. (HACK!)\r
+                */\r
+               for(int index = 0; index < cdl.size(); )\r
+               {\r
+                       cd = cdl.elementAt(index);\r
+                       if (cd instanceof ReferencedKeyConstraintDescriptor)\r
+                       {\r
+                               index++;\r
+                               continue;\r
+                       }\r
+\r
+                       dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);\r
+                       cd.drop(lcc, true);\r
+               }\r
+\r
+               /*\r
+               ** Referenced keys (unique or pk) constraints only\r
+               */\r
+               /* The current element will be deleted underneath\r
+                * the loop. (HACK!)\r
+                */\r
+               while (cdl.size() > 0)\r
+               {\r
+                       cd = cdl.elementAt(0);\r
+                       if (SanityManager.DEBUG)\r
+                       {\r
+                               if (!(cd instanceof ReferencedKeyConstraintDescriptor))\r
+                               {\r
+                                       SanityManager.THROWASSERT("Constraint descriptor not an instance of " +\r
+                                       "ReferencedKeyConstraintDescriptor as expected.  Is a "+ cd.getClass().getName());\r
+                               }\r
+                       }\r
+\r
+                       /*\r
+                       ** Drop the referenced constraint (after we got\r
+                       ** the primary keys) now.  Do this prior to\r
+                       ** droping the referenced keys to avoid performing\r
+                       ** a lot of extra work updating the referencedcount\r
+                       ** field of sys.sysconstraints.\r
+                       **\r
+                       ** Pass in false to dropConstraintsAndIndex so it\r
+                       ** doesn't clear dependencies, we'll do that ourselves.\r
+                       */\r
+                       cd.drop(lcc, false);\r
+\r
+                       /*\r
+                       ** If we are going to cascade, get all the\r
+                       ** referencing foreign keys and zap them first.\r
+                       */\r
+                       if (cascade)\r
+                       {\r
+                               /*\r
+                               ** Go to the system tables to get the foreign keys\r
+                               ** to be safe\r
+                               */\r
+\r
+                               fkcdl = dd.getForeignKeys(cd.getUUID());\r
+\r
+                               /*\r
+                               ** For each FK that references this key, drop\r
+                               ** it.\r
+                               */\r
+                               for(int inner = 0; inner < fkcdl.size(); inner++)\r
+                               {\r
+                                       fkcd = (ConstraintDescriptor) fkcdl.elementAt(inner);\r
+                                       dm.invalidateFor(fkcd, DependencyManager.DROP_CONSTRAINT, lcc);\r
+                                       fkcd.drop(lcc, true);\r
+                                       activation.addWarning(\r
+                                               StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED,\r
+                                                       fkcd.getConstraintName(),\r
+                                                       fkcd.getTableDescriptor().getName()));\r
+                               }\r
+                       }\r
+\r
+                       /*\r
+                       ** Now that we got rid of the fks (if we were cascading), it is \r
+                       ** ok to do an invalidate for.\r
+                       */\r
+                       dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);\r
+                       dm.clearDependencies(lcc, cd);\r
+               }\r
+       }\r
+\r
+}\r