db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bpendle...@apache.org
Subject svn commit: r795459 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/AlterTableNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
Date Sun, 19 Jul 2009 01:29:22 GMT
Author: bpendleton
Date: Sun Jul 19 01:29:22 2009
New Revision: 795459

URL: http://svn.apache.org/viewvc?rev=795459&view=rev
Log:
DERBY-4244: ALTER TABLE ASSERT in ADD COLUMN with autocommit off

This patch was contributed by Eranda Sooriyabandara (070468D at gmail dot com)

An ALTER TABLE ... ADD COLUMN statement which specifies to add a column to
a table may also include any column constraints which apply to that column.
For example, to add a column to a table and at the same time indicate that
the column should be the primary key of the table, this statement is used:

   alter table t0 add column c2 int not null default 0 primary key; 

If the table already contains a primary key, this statement fails, as the
table cannot have more than one primary key. However, the check for the
duplicate primary key was occurring at execution time, *after* the column
had already been added to the table. Then, during that same transaction,
subsequent ALTER TABLE statements affecting that table failed with internal
ASSERT messages because the data dictionary data structures were inconsistent.

This patch adds logic to the compilation processing of the ALTER TABLE
statement to check for a duplicate PRIMARY KEY constraint at compile
time, before the ALTER TABLE statement has begun execution. This way, when
such a duplicate constraint is rejected, the rejection occurs prior to
execution of the table, allowing for more straightforward cleanup.

The patch also re-enables a portion of the AlterTableTest, which had been
disabled due to this problem.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java?rev=795459&r1=795458&r2=795459&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java
Sun Jul 19 01:29:22 2009
@@ -32,6 +32,7 @@
 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
 
 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
+import org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList;
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
@@ -41,6 +42,7 @@
 
 import org.apache.derby.impl.sql.execute.ColumnInfo;
 import org.apache.derby.impl.sql.execute.ConstraintConstantAction;
+import org.apache.derby.impl.sql.execute.CreateConstraintConstantAction;
 
 /**
  * A AlterTableNode represents a DDL statement that alters a table.
@@ -511,6 +513,31 @@
 
 			tableElementList.genConstraintActions(false, conActions, getRelativeName(), schemaDescriptor,
 												  getDataDictionary());
+
+			for (int conIndex = 0; conIndex < conActions.length; conIndex++)
+			{
+				ConstraintConstantAction cca = conActions[conIndex];
+
+				if (cca instanceof CreateConstraintConstantAction)
+				{
+					int constraintType = cca.getConstraintType();
+					if (constraintType == DataDictionary.PRIMARYKEY_CONSTRAINT)
+					{
+						DataDictionary dd = getDataDictionary();
+						// Check to see if a constraint of the same type 
+						// already exists
+						ConstraintDescriptorList cdl = 
+                                dd.getConstraintDescriptors(baseTable);
+
+						if (cdl.getPrimaryKey() != null)
+						{
+							throw StandardException.newException(
+                                    SQLState.LANG_ADD_PRIMARY_KEY_FAILED1, 
+                                    baseTable.getQualifiedName());
+						}
+					}
+				}
+			}
 		}
 	}
 	  

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java?rev=795459&r1=795458&r2=795459&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
Sun Jul 19 01:29:22 2009
@@ -113,6 +113,7 @@
     public void testAddColumn() throws Exception {
         Statement st = createStatement();
         createTestObjects(st);
+        conn.commit();
 
         // add column negative tests alter a non-existing table
         assertStatementError("42Y55", st,
@@ -202,18 +203,6 @@
             JDBC.assertFullResultSet(rs, new String[][]{{"1"}, {"2"}});
         }
 
-        // DERBY-4244 (START)
-        // Without these lines, this test fails a few lines later when
-        // it makes yet another attempt to add column c2 to table t0. This
-        // bug is logged as DERBY-4244. When that problem is fixed, this
-        // section could be removed.
-        conn.commit();
-        st.executeUpdate("drop table t0");
-        st.executeUpdate(
-                "create table t0(c1 int not null constraint p1 primary key)");
-        conn.commit();
-        // DERBY-4244 (END)
-
         // add non-nullable column to 0 row table and verify
         st.executeUpdate("alter table t0 add column c2 int not null default 0");
         st.executeUpdate("insert into t0 values (1, default)");



Mime
View raw message