db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1738368 - in /db/derby/code/trunk/java: build/org/apache/derbyBuild/lastgoodjarcontents/ engine/org/apache/derby/impl/sql/catalog/ engine/org/apache/derby/impl/sql/compile/ engine/org/apache/derby/impl/sql/execute/ engine/org/apache/derby/...
Date Sat, 09 Apr 2016 17:53:41 GMT
Author: rhillegas
Date: Sat Apr  9 17:53:40 2016
New Revision: 1738368

URL: http://svn.apache.org/viewvc?rev=1738368&view=rev
Log:
DERBY-6882: Add ALTER TABLE ALTER COLUMN ... SET GENERATED functionality, which switches an
identity column from ALWAYS to BY DEFAULT and vice-versa.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java
  (with props)
Modified:
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyTesting.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyclient.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbynet.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyTesting.jar.lastcontents
    db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyclient.jar.lastcontents
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/SYSCOLUMNSRowFactory.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ColumnInfo.java
    db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
    db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/UpgradeRun.java

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -1410,10 +1410,10 @@ org.apache.derby.mbeans.VersionMBean.cla
 org.apache.derby.osgi.EmbeddedActivator.class
 org.apache.derby.security.DatabasePermission.class
 org.apache.derby.security.SystemPermission.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.error.ExceptionSeverity.class
 org.apache.derby.shared.common.error.MessageUtils.class
 org.apache.derby.shared.common.error.ShutdownException.class
-org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.reference.JDBC40Translation.class
 org.apache.derby.shared.common.reference.MessageId.class
 org.apache.derby.shared.common.reference.SQLState.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyTesting.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyTesting.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyTesting.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyTesting.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -715,6 +715,7 @@ org.apache.derbyTesting.functionTests.te
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_10.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_11.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_12.class
+org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_13.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_2.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_3.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_4.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyclient.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyclient.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyclient.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbyclient.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -139,7 +139,6 @@ org.apache.derby.client.net.Typdef.class
 org.apache.derby.client.net.Utf8CcsidManager.class
 org.apache.derby.iapi.error.ExceptionSeverity.class
 org.apache.derby.iapi.reference.Attribute.class
-org.apache.derby.iapi.reference.DRDAConstants.class
 org.apache.derby.iapi.reference.JDBC40Translation.class
 org.apache.derby.iapi.services.info.JVMInfo.class
 org.apache.derby.iapi.services.info.ProductGenusNames.class
@@ -164,11 +163,11 @@ org.apache.derby.jdbc.ClientDriver40.cla
 org.apache.derby.jdbc.ClientXADataSource.class
 org.apache.derby.jdbc.ClientXADataSource40.class
 org.apache.derby.jdbc.ClientXADataSourceInterface.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.error.ExceptionSeverity.class
 org.apache.derby.shared.common.error.ExceptionUtil.class
-org.apache.derby.shared.common.error.ShutdownException.class
 org.apache.derby.shared.common.error.MessageUtils.class
-org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
+org.apache.derby.shared.common.error.ShutdownException.class
 org.apache.derby.shared.common.i18n.MessageUtil.class
 org.apache.derby.shared.common.reference.Attribute.class
 org.apache.derby.shared.common.reference.JDBC40Translation.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbynet.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbynet.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbynet.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derbynet.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -45,7 +45,7 @@ org.apache.derby.impl.drda.memCheck.clas
 org.apache.derby.impl.tools.sysinfo.Main.class
 org.apache.derby.impl.tools.sysinfo.ZipInfoProperties.class
 org.apache.derby.mbeans.drda.NetworkServerMBean.class
-org.apache.derby.shared.common.reference.MessageId.class
 org.apache.derby.shared.common.error.MessageUtils.class
 org.apache.derby.shared.common.error.ShutdownException.class
+org.apache.derby.shared.common.reference.MessageId.class
 org.apache.derby.tools.sysinfo.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -1413,10 +1413,10 @@ org.apache.derby.mbeans.VersionMBean.cla
 org.apache.derby.osgi.EmbeddedActivator.class
 org.apache.derby.security.DatabasePermission.class
 org.apache.derby.security.SystemPermission.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.error.ExceptionSeverity.class
 org.apache.derby.shared.common.error.MessageUtils.class
 org.apache.derby.shared.common.error.ShutdownException.class
-org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.reference.JDBC40Translation.class
 org.apache.derby.shared.common.reference.MessageId.class
 org.apache.derby.shared.common.reference.SQLState.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyTesting.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyTesting.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyTesting.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyTesting.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -715,6 +715,7 @@ org.apache.derbyTesting.functionTests.te
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_10.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_11.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_12.class
+org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_13.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_2.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_3.class
 org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_4.class

Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyclient.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyclient.jar.lastcontents?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyclient.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derbyclient.jar.lastcontents
Sat Apr  9 17:53:40 2016
@@ -139,7 +139,6 @@ org.apache.derby.client.net.Typdef.class
 org.apache.derby.client.net.Utf8CcsidManager.class
 org.apache.derby.iapi.error.ExceptionSeverity.class
 org.apache.derby.iapi.reference.Attribute.class
-org.apache.derby.iapi.reference.DRDAConstants.class
 org.apache.derby.iapi.reference.JDBC40Translation.class
 org.apache.derby.iapi.services.info.JVMInfo.class
 org.apache.derby.iapi.services.info.ProductGenusNames.class
@@ -164,11 +163,11 @@ org.apache.derby.jdbc.ClientDriver40.cla
 org.apache.derby.jdbc.ClientXADataSource.class
 org.apache.derby.jdbc.ClientXADataSource40.class
 org.apache.derby.jdbc.ClientXADataSourceInterface.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
 org.apache.derby.shared.common.error.ExceptionSeverity.class
 org.apache.derby.shared.common.error.ExceptionUtil.class
-org.apache.derby.shared.common.error.ShutdownException.class
 org.apache.derby.shared.common.error.MessageUtils.class
-org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
+org.apache.derby.shared.common.error.ShutdownException.class
 org.apache.derby.shared.common.i18n.MessageUtil.class
 org.apache.derby.shared.common.reference.Attribute.class
 org.apache.derby.shared.common.reference.JDBC40Translation.class

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/SYSCOLUMNSRowFactory.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/SYSCOLUMNSRowFactory.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/SYSCOLUMNSRowFactory.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/catalog/SYSCOLUMNSRowFactory.java
Sat Apr  9 17:53:40 2016
@@ -222,9 +222,18 @@ public class SYSCOLUMNSRowFactory extend
 		/* 6th column is DEFAULTID (UUID - char(36)) */
 		row.setColumn(SYSCOLUMNS_COLUMNDEFAULTID, new SQLChar(defaultID));
 
-		if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.CREATE_AUTOINCREMENT
||
-				autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_INC_VALUE)
-		{//user is adding an autoinc column or is changing the increment value of autoinc column
+		if (
+            (autoinc_create_or_modify_Start_Increment ==
+             ColumnDefinitionNode.CREATE_AUTOINCREMENT) ||
+            (autoinc_create_or_modify_Start_Increment ==
+             ColumnDefinitionNode.MODIFY_AUTOINCREMENT_INC_VALUE) ||
+            (autoinc_create_or_modify_Start_Increment ==
+             ColumnDefinitionNode.MODIFY_AUTOINCREMENT_ALWAYS_VS_DEFAULT)
+            )
+		{
+            //user is adding an autoinc column
+            // or is changing the increment value of autoinc column
+            // or is changing an autoinc column between ALWAYS and DEFAULT.
 			// This code also gets run when ALTER TABLE DROP COLUMN
 			// is used to drop a column other than the autoinc
 			// column, and the autoinc column gets removed from

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ColumnDefinitionNode.java
Sat Apr  9 17:53:40 2016
@@ -86,6 +86,8 @@ public class ColumnDefinitionNode extend
 	//MODIFY_AUTOINCREMENT_INC_VALUE - this column definition is for
 	//alter table command to change the increment value of the column
 	public static final int MODIFY_AUTOINCREMENT_INC_VALUE = 2;
+	//alter table command to change the ALWAYS vs DEFAULT nature of an autoinc column
+	public static final int MODIFY_AUTOINCREMENT_ALWAYS_VS_DEFAULT = 3;
 	
 	/**
      * Constructor for a ColumnDefinitionNode
@@ -668,7 +670,7 @@ public class ColumnDefinitionNode extend
 	}
 
 
-	private static DefaultInfoImpl createDefaultInfoOfAutoInc(){
+	protected static DefaultInfoImpl createDefaultInfoOfAutoInc(){
 		return new DefaultInfoImpl(true,
 					   null, 
 					   null);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
Sat Apr  9 17:53:40 2016
@@ -54,6 +54,8 @@ class ModifyColumnNode extends ColumnDef
     final static int K_MODIFY_COLUMN_CONSTRAINT = 2;
     final static int K_MODIFY_COLUMN_CONSTRAINT_NOT_NULL = 3;
     final static int K_DROP_COLUMN = 4;
+    final static int K_MODIFY_COLUMN_GENERATED_ALWAYS = 5;
+    final static int K_MODIFY_COLUMN_GENERATED_BY_DEFAULT = 6;
 
     /**
      * This class is used to hold logically different objects for
@@ -317,6 +319,12 @@ class ModifyColumnNode extends ColumnDef
             case K_DROP_COLUMN:
                 return ColumnInfo.DROP;
 
+            case K_MODIFY_COLUMN_GENERATED_ALWAYS:
+                return ColumnInfo.MODIFY_COLUMN_GENERATED_ALWAYS;
+
+            case K_MODIFY_COLUMN_GENERATED_BY_DEFAULT:
+                return ColumnInfo.MODIFY_COLUMN_GENERATED_BY_DEFAULT;
+
             default:
                 if (SanityManager.DEBUG) {
                     SanityManager.THROWASSERT("Unexpected type = " + kind);
@@ -418,6 +426,28 @@ class ModifyColumnNode extends ColumnDef
 	{
 		ColumnDescriptor cd;
 
+		// only autoincrement columns can have their generation property changed
+        if (
+            (kind == K_MODIFY_COLUMN_GENERATED_ALWAYS) ||
+            (kind == K_MODIFY_COLUMN_GENERATED_BY_DEFAULT)
+            )
+		{
+			cd = getLocalColumnDescriptor(name, td);
+			if (!cd.isAutoincrement())
+			{
+				throw StandardException.newException(SQLState.LANG_AI_CANNOT_ALTER_IDENTITYNESS,
+						getColumnName());
+			}
+
+            if (kind == K_MODIFY_COLUMN_GENERATED_BY_DEFAULT)
+            {
+                defaultInfo = createDefaultInfoOfAutoInc();
+            }
+
+            // nothing more to do here
+            return;
+		}
+
 		// a column that has an autoincrement default can't be made nullable
         if (kind == K_MODIFY_COLUMN_CONSTRAINT)
 		{

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/sqlgrammar.jj Sat Apr
 9 17:53:40 2016
@@ -13658,6 +13658,7 @@ columnAlterClause(String columnName) thr
 	long				autoIncrementIncrement = 1;
 	long				autoIncrementRestartWith = 1;
 	DataTypeDescriptor	typeDescriptor = null;
+	int                 alterKind;
 }
 {
 	LOOKAHEAD( {getToken(2).kind == DATA} )
@@ -13672,6 +13673,19 @@ columnAlterClause(String columnName) thr
                                  getContextManager());
 	}
 |
+	LOOKAHEAD( {getToken(2).kind == GENERATED} )
+	<SET> <GENERATED> alterKind = alterGeneratedColumn()
+	{
+		checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, "ALTER TABLE ALTER COLUMN SET GENERATED");
+        return
+            new ModifyColumnNode(alterKind,
+                                 columnName,
+                                 null,
+                                 null,
+                                 null,
+                                 getContextManager());
+	}
+|
 	LOOKAHEAD( {getToken(2).kind == INCREMENT} )
 	<SET> <INCREMENT> <BY> autoIncrementIncrement = exactNumber()
 	{
@@ -13760,6 +13774,28 @@ columnAlterClause(String columnName) thr
 	}
 }
 
+/*
+ * Various variants of the ALTER TABLE ALTER COLUMN SET GENERATED statement.
+ *
+ * By the time we get here, we've parsed
+ *    ALTER TABLE ALTER COLUMN SET GENERATED
+ */
+int
+alterGeneratedColumn() throws StandardException :
+{}
+{
+	<BY> <_DEFAULT>
+    {
+        return ModifyColumnNode.K_MODIFY_COLUMN_GENERATED_BY_DEFAULT;
+	}
+|
+	<ALWAYS>
+    {
+        return ModifyColumnNode.K_MODIFY_COLUMN_GENERATED_ALWAYS;
+	}
+}
+
+
 StatementNode
 dropTableConstraintDefinitionCore(TableName tableName, TableElementList tableElementList,
int[] changeType, int[] behavior) throws StandardException :
 {

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/AlterTableConstantAction.java
Sat Apr  9 17:53:40 2016
@@ -532,6 +532,13 @@ class AlterTableConstantAction extends D
 				{
                     dropColumnFromTable(columnInfo[ix].name);
 				}
+				else if (
+                         (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_GENERATED_ALWAYS)
||
+                         (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_GENERATED_BY_DEFAULT)
+                         )
+				{
+                    modifyIdentityState(ix);
+				}
 				else if (SanityManager.DEBUG)
 				{
 					SanityManager.THROWASSERT(
@@ -2243,6 +2250,48 @@ class AlterTableConstantAction extends D
         }
 	}
 	
+	/**
+	 * Change an identity from ALWAYS to BY DEFAULT (or vice versa)
+	 * 
+	 * @param       ix 		the index of the column specfication in the ALTER 
+	 *						statement-- currently we allow only one.
+	 * @exception	StandardException, thrown on error.
+	 */
+    private void modifyIdentityState(int ix)
+			throws StandardException						 
+	{
+		ColumnDescriptor oldColumnDescriptor = td.getColumnDescriptor(columnInfo[ix].name);
+		int columnPosition = oldColumnDescriptor.getPosition();
+        boolean wasGeneratedAlways = oldColumnDescriptor.isAutoincAlways();
+        boolean willBeGeneratedAlways =
+          (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_GENERATED_ALWAYS);
+
+        // nothing to do if the identity column already behaves the right way
+        if (wasGeneratedAlways == willBeGeneratedAlways) { return; }
+
+        UUID defaultUUID = willBeGeneratedAlways ? null : dd.getUUIDFactory().createUUID();
+
+        /* Get a ColumnDescriptor reflecting the new default */
+		ColumnDescriptor newColumnDescriptor = new ColumnDescriptor
+          (
+           columnInfo[ix].name,
+           columnPosition,
+           oldColumnDescriptor.getType(),
+           columnInfo[ix].defaultValue,
+           columnInfo[ix].defaultInfo,
+           td,
+           defaultUUID,
+           oldColumnDescriptor.getAutoincStart(),
+           oldColumnDescriptor.getAutoincInc(),
+           ColumnDefinitionNode.MODIFY_AUTOINCREMENT_ALWAYS_VS_DEFAULT
+           );
+
+		// Update the ColumnDescriptor with new default info
+		dd.dropColumnDescriptor(td.getUUID(), columnInfo[ix].name, tc);
+		dd.addDescriptor(newColumnDescriptor, td,
+						 DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
+    }
+  
     /**
      * routine to process compress table or ALTER TABLE <t> DROP COLUMN <c>;
      * <p>

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ColumnInfo.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ColumnInfo.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ColumnInfo.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/ColumnInfo.java Sat
Apr  9 17:53:40 2016
@@ -91,6 +91,10 @@ public class ColumnInfo implements Forma
 	//This indicates column is for ALTER TABLE to change the increment value of autoinc column

 	public static final int MODIFY_COLUMN_DEFAULT_INCREMENT	= 6;
 	public static final int MODIFY_COLUMN_DEFAULT_VALUE	= 7;
+	//This change the generation property of autoinc columns
+	public static final int MODIFY_COLUMN_GENERATED_ALWAYS	= 8;
+	public static final int MODIFY_COLUMN_GENERATED_BY_DEFAULT	= 9;
+
 	// CONSTRUCTORS
 
 	/**

Modified: db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/loc/messages.xml Sat Apr  9 17:53:40
2016
@@ -2974,6 +2974,12 @@ Guide.
             </msg>
 
             <msg>
+                <name>42Z29</name>
+                <text>Column '{0}' is not an identity column, so its generation state
cannot be changed. </text>
+                <arg>columnName</arg>
+            </msg>
+
+            <msg>
                 <name>42Z50</name>
                 <text>Internal Error: Unable to generate code for {0}.</text>
                 <arg>queryTreeNodeIdentifier</arg>

Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/reference/SQLState.java
Sat Apr  9 17:53:40 2016
@@ -1078,6 +1078,7 @@ public interface SQLState {
 	String LANG_AI_CANNOT_ADD_AI_TO_NULLABLE						   = "42Z27";
 	// String LANG_BUILT_IN_ALIAS_NAME						   = "42Z28";
 	// RUNTIMESTATISTICS
+	String LANG_AI_CANNOT_ALTER_IDENTITYNESS						   = "42Z29";
 	String LANG_TIME_SPENT_THIS										   = "42Z30.U";
 	String LANG_TIME_SPENT_THIS_AND_BELOW							   = "42Z31.U";
 	String LANG_TOTAL_TIME_BREAKDOWN								   = "42Z32.U";

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=1738368&r1=1738367&r2=1738368&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
Sat Apr  9 17:53:40 2016
@@ -41,6 +41,10 @@ import org.apache.derbyTesting.junit.Tes
 
 public final class AlterTableTest extends BaseJDBCTestCase {
 
+    private static final String CANNOT_ALTER_NON_IDENTITY_COLUMN = "42Z29";
+    private static final String CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN = "42Z23";
+    private static final String DUPLICATE_KEY = "23505";
+
     /**
      * Public constructor required for running test as standalone JUnit.
      */
@@ -4577,5 +4581,270 @@ public final class AlterTableTest extend
             assertEquals( expected[ i ], actual[ i ] );
         }
     }
+
+    /**
+     * Test that identity columns can be switched between
+     * ALWAYS and DEFAULT modes.
+     */
+    public void test_6882() throws Exception
+    {
+        Connection conn = getConnection();
+
+        // initial schema
+        goodStatement
+          (
+           conn,
+           "create table t_6882\n" +
+           "(\n" +
+           "  a int generated always as identity,\n" +
+           "  b int\n" +
+           ")\n"
+           );
+        goodStatement
+          (
+           conn,
+           "create table s_6882\n" +
+           "(\n" +
+           "  a int generated by default as identity,\n" +
+           "  b int\n" +
+           ")\n"
+           );
+        goodStatement
+          (
+           conn,
+           "create table u_6882\n" +
+           "(\n" +
+           "  a int,\n" +
+           "  b int\n" +
+           ")\n"
+           );
+
+        // can't change the ALWAYS/DEFAULT state of a non-identity column
+        assertCompileError
+          (
+           CANNOT_ALTER_NON_IDENTITY_COLUMN,
+           "alter table u_6882 alter column a set generated by default"
+           );
+        assertCompileError
+          (
+           CANNOT_ALTER_NON_IDENTITY_COLUMN,
+           "alter table u_6882 alter column a set generated always"
+           );
+
+        // add some good tuples
+        goodStatement(conn, "insert into t_6882(b) values (100)");
+        goodStatement(conn, "insert into s_6882(b) values (100)");
+
+        //
+        // Demonstrate that t_6882 is ALWAYS and s_6882 is BY DEFAULT.
+        //
+        
+        assertCompileError
+          (
+           CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+           "insert into t_6882(a, b) values (-1, -100)"
+           );
+        goodStatement(conn, "insert into s_6882(a, b) values (-1, -100)");
+        assertResults
+          (
+           conn,
+           "select * from t_6882 order by a",
+           new String[][]
+           {
+             { "1", "100" },
+           },
+           false
+           );
+        assertResults
+          (
+           conn,
+           "select * from s_6882 order by a",
+           new String[][]
+           {
+             { "-1", "-100" },
+             { "1", "100" },
+           },
+           false
+           );
+
+        //
+        // Now swap the ALWAYS/DEFAULT state of the tables
+        //
+        
+        goodStatement(conn, "alter table t_6882 alter column a set generated by default");
+        goodStatement(conn, "alter table s_6882 alter column a set generated always");
+
+        goodStatement(conn, "insert into t_6882(a, b) values (-2, -200)");
+        assertCompileError
+          (
+           CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+           "insert into s_6882(a, b) values (-2, -200)"
+           );
+
+        goodStatement(conn, "insert into t_6882(b) values (200)");
+        goodStatement(conn, "insert into s_6882(b) values (200)");
+
+        assertResults
+          (
+           conn,
+           "select * from t_6882 order by a",
+           new String[][]
+           {
+             { "-2", "-200" },
+             { "1", "100" },
+             { "2", "200" },
+           },
+           false
+           );
+        assertResults
+          (
+           conn,
+           "select * from s_6882 order by a",
+           new String[][]
+           {
+             { "-1", "-100" },
+             { "1", "100" },
+             { "2", "200" },
+           },
+           false
+           );
+
+        //
+        // Swap the states again.
+        //
+        
+        goodStatement(conn, "alter table t_6882 alter column a set generated always");
+        goodStatement(conn, "alter table s_6882 alter column a set generated by default");
+        
+        assertCompileError
+          (
+           CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+           "insert into t_6882(a, b) values (-3, -300)"
+           );
+        goodStatement(conn, "insert into s_6882(a, b) values (-3, -300)");
+
+        goodStatement(conn, "insert into t_6882(b) values (300)");
+        goodStatement(conn, "insert into s_6882(b) values (300)");
+
+        assertResults
+          (
+           conn,
+           "select * from t_6882 order by a",
+           new String[][]
+           {
+             { "-2", "-200" },
+             { "1", "100" },
+             { "2", "200" },
+             { "3", "300" },
+           },
+           false
+           );
+        assertResults
+          (
+           conn,
+           "select * from s_6882 order by a",
+           new String[][]
+           {
+             { "-3", "-300" },
+             { "-1", "-100" },
+             { "1", "100" },
+             { "2", "200" },
+             { "3", "300" },
+           },
+           false
+           );
+
+        // drop schema
+        goodStatement(conn, "drop table u_6882");
+        goodStatement(conn, "drop table s_6882");
+        goodStatement(conn, "drop table t_6882");
+    }
+
+    /**
+     * Second test for feature 6882. Verify that you
+     * can use the SET GENERATED feature in order to
+     * import data into a newly created table which has
+     * an ALWAYS generated column and a primary key.
+     */
+    public void test_6882_import() throws Exception
+    {
+        Connection conn = getConnection();
+
+        // initial schema
+        goodStatement
+          (
+           conn,
+           "create table source_6882\n" +
+           "(\n" +
+           "  a int generated always as identity primary key,\n" +
+           "  b int\n" +
+           ")\n"
+           );
+        goodStatement
+          (
+           conn,
+           "create table target_6882\n" +
+           "(\n" +
+           "  a int generated always as identity primary key,\n" +
+           "  b int\n" +
+           ")\n"
+           );
+
+        // populate the source table
+        goodStatement(conn, "insert into source_6882(b) values (100), (200), (300)");
+
+        // can't populate the target table yet because its key
+        // is generated ALWAYS
+        assertCompileError
+          (
+           CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+           "insert into target_6882 select * from source_6882"
+           );
+
+        // alter the target table to allow the bulk insert to succeed
+        goodStatement(conn, "alter table target_6882 alter column a set generated by default");
+
+        // now the bulk insert works
+        goodStatement(conn, "insert into target_6882 select * from source_6882");
+
+        // set the target table back to its original, intended shape
+        goodStatement(conn, "alter table target_6882 alter column a set generated always");
+
+        // insert now fails because the sequence generator starts
+        // at 1 and there is already a key with that value
+        try (PreparedStatement ps = conn.prepareStatement("insert into target_6882(b) values
(400)"))
+        {
+            assertPreparedStatementError
+              (
+               DUPLICATE_KEY,
+               ps
+               );
+        }
+        
+        // move the generator's start value forward
+        goodStatement(conn, "alter table target_6882 alter column a restart with 4");
+
+        // now the insert succeeds
+        goodStatement(conn, "insert into target_6882(b) values (400)");
+
+        // verify the results
+        assertResults
+          (
+           conn,
+           "select * from target_6882 order by a",
+           new String[][]
+           {
+             { "1", "100" },
+             { "2", "200" },
+             { "3", "300" },
+             { "4", "400" },
+           },
+           false
+           );
+
+        // drop schema
+        goodStatement(conn, "drop table target_6882");
+        goodStatement(conn, "drop table source_6882");
+    }
     
 }

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java?rev=1738368&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java
Sat Apr  9 17:53:40 2016
@@ -0,0 +1,229 @@
+/*
+
+Derby - Class org.apache.derbyTesting.functionTests.tests.upgradeTests.Changes10_13
+
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+package org.apache.derbyTesting.functionTests.tests.upgradeTests;
+
+import java.sql.SQLException;
+import java.sql.Statement;
+import junit.framework.Test;
+import org.apache.derbyTesting.junit.BaseTestSuite;
+import org.apache.derbyTesting.junit.JDBC;
+
+
+/**
+ * Upgrade test cases for 10.13.
+ */
+public class Changes10_13 extends UpgradeChange
+{
+
+    //////////////////////////////////////////////////////////////////
+    //
+    // CONSTANTS
+    //
+    //////////////////////////////////////////////////////////////////
+
+    private static final String SYNTAX_ERROR = "42X01";
+    private static final String UPGRADE_REQUIRED = "XCL47";
+    private static final String CANNOT_ALTER_NON_IDENTITY_COLUMN = "42Z29";
+    private static final String CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN = "42Z23";
+
+    //////////////////////////////////////////////////////////////////
+    //
+    // CONSTRUCTOR
+    //
+    //////////////////////////////////////////////////////////////////
+
+    public Changes10_13(String name) {
+        super(name);
+    }
+
+    //////////////////////////////////////////////////////////////////
+    //
+    // JUnit BEHAVIOR
+    //
+    //////////////////////////////////////////////////////////////////
+
+    /**
+     * Return the suite of tests to test the changes made in 10.13.
+     *
+     * @param phase an integer that indicates the current phase in
+     *              the upgrade test.
+     * @return the test suite created.
+     */
+    public static Test suite(int phase) {
+        return new BaseTestSuite(Changes10_13.class, "Upgrade test for 10.13");
+    }
+
+    //////////////////////////////////////////////////////////////////
+    //
+    // TESTS
+    //
+    //////////////////////////////////////////////////////////////////
+
+    /**
+     * Test the addition of support for changing identity columns
+     * from ALWAYS to BY DEFAULT and vice versa via
+     * an ALTER TABLE statement. DERBY-6882.
+     */
+    public void testAlterTableSetGenerated() throws SQLException {
+        Statement s = createStatement();
+
+        // GENERATED BY DEFAULT not supported prior to 10.1
+        if (!oldAtLeast(10, 1)) { return; }
+
+        // 10.11 upgraded all identity columns to be backed by sequences
+        boolean atLeast10_11 = oldAtLeast(10,11);
+        
+        switch (getPhase()) {
+            case PH_CREATE:
+                s.execute("create table t_always_6882(a int generated always as identity,
b int)");
+                s.execute("create table t_default_6882(a int generated by default as identity,
b int)");
+                s.execute("create table t_none_6882(a int, b int)");
+                s.execute("insert into t_always_6882(b) values (100)");
+                s.execute("insert into t_default_6882(b) values (100)");
+                assertCompileError
+                  (
+                   CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+                   "insert into t_always_6882(a, b) values (-1, -100)"
+                   );
+                s.execute("insert into t_default_6882(a, b) values (-1, -100)");
+                break;
+
+            case PH_SOFT_UPGRADE:
+                // We only support the SET GENERATED clause if the database
+                // is at level 10.11 or higher.
+                if (atLeast10_11)
+                {
+                    assertCompileError
+                      (
+                       CANNOT_ALTER_NON_IDENTITY_COLUMN,
+                       "alter table t_none_6882 alter column a set generated by default"
+                       );
+                    s.execute("alter table t_always_6882 alter column a set generated by
default");
+                    s.execute("alter table t_default_6882 alter column a set generated always");
+                    s.execute("insert into t_always_6882(a, b) values (-2, -200)");
+                    assertCompileError
+                      (
+                       CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+                       "insert into t_default_6882(a, b) values (-2, -200)"
+                       );
+                    s.execute("insert into t_always_6882(b) values (200)");
+                    s.execute("insert into t_default_6882(b) values (200)");
+                    JDBC.assertFullResultSet(
+                        s.executeQuery("select * from t_always_6882 order by a"),
+                        new String[][]
+                        {
+                          { "-2", "-200" },
+                          { "1", "100" },
+                          { "2", "200" },
+                        });
+                    JDBC.assertFullResultSet(
+                        s.executeQuery("select * from t_default_6882 order by a"),
+                        new String[][]
+                        {
+                          { "-1", "-100" },
+                          { "1", "100" },
+                          { "2", "200" },
+                        });
+                } else {
+                    assertCompileError
+                      (
+                       UPGRADE_REQUIRED,
+                       "alter table t_none_6882 alter column a set generated by default"
+                       );
+                    assertCompileError
+                      (
+                       UPGRADE_REQUIRED,
+                       "alter table t_always_6882 alter column a set generated by default"
+                       );
+                    assertCompileError
+                      (
+                       UPGRADE_REQUIRED,
+                       "alter table t_default_6882 alter column a set generated always"
+                       );
+                }
+                break;
+                
+            case PH_HARD_UPGRADE:
+                assertCompileError
+                  (
+                   CANNOT_ALTER_NON_IDENTITY_COLUMN,
+                   "alter table t_none_6882 alter column a set generated by default"
+                   );
+                s.execute("alter table t_always_6882 alter column a set generated always");
+                s.execute("alter table t_default_6882 alter column a set generated by default");
+                assertCompileError
+                  (
+                   CANNOT_MODIFY_ALWAYS_IDENTITY_COLUMN,
+                   "insert into t_always_6882(a, b) values (-3, -300)"
+                   );
+                s.execute("insert into t_default_6882(a, b) values (-3, -300)");
+                s.execute("insert into t_always_6882(b) values (300)");
+                s.execute("insert into t_default_6882(b) values (300)");
+
+                String[][] alwaysResults;
+                String[][] defaultResults;
+                if (atLeast10_11)
+                {
+                    alwaysResults =  new String[][]  
+                      {
+                        { "-2", "-200" },
+                        { "1", "100" },
+                        { "2", "200" },
+                        { "3", "300" },
+                      };
+                    defaultResults = new String[][]
+                      {
+                        { "-3", "-300" },
+                        { "-1", "-100" },
+                        { "1", "100" },
+                        { "2", "200" },
+                        { "3", "300" },
+                      };
+                }
+                else
+                {
+                    alwaysResults = new String[][]  
+                      {
+                        { "1", "100" },
+                        { "2", "300" },
+                      };
+                    defaultResults = new String[][]
+                      {
+                        { "-3", "-300" },
+                        { "-1", "-100" },
+                        { "1", "100" },
+                        { "2", "300" },
+                      };
+                }
+
+                JDBC.assertFullResultSet
+                  (
+                   s.executeQuery("select * from t_always_6882 order by a"),
+                   alwaysResults
+                   );
+                JDBC.assertFullResultSet
+                  (
+                   s.executeQuery("select * from t_default_6882 order by a"),
+                   defaultResults);
+                break;
+        };
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/Changes10_13.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/UpgradeRun.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/UpgradeRun.java?rev=1738368&r1=1738367&r2=1738368&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/UpgradeRun.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/upgradeTests/UpgradeRun.java
Sat Apr  9 17:53:40 2016
@@ -195,6 +195,8 @@ class UpgradeRun extends UpgradeClassLoa
                     suite.addTest(Changes10_11.suite(phase));
                 if (oldMinor < 12)
                     suite.addTest(Changes10_12.suite(phase));
+                if (oldMinor < 13)
+                    suite.addTest(Changes10_13.suite(phase));
             }
             
             // Add DatabaseMetaData tests. Since metadata




Mime
View raw message