db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r712840 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/ engine/org/apache/derby/impl/sql/execute/ testing/org/apache/derbyTesting/functionTests/tests/lang/
Date Mon, 10 Nov 2008 21:31:48 GMT
Author: rhillegas
Date: Mon Nov 10 13:31:47 2008
New Revision: 712840

URL: http://svn.apache.org/viewvc?rev=712840&view=rev
Log:
DERBY-481: Prevent users from dropping a function mentioned in a generation clause.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/AlterTableNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateViewNode.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java
    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/impl/sql/execute/CreateTableConstantAction.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.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=712840&r1=712839&r2=712840&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
Mon Nov 10 13:31:47 2008
@@ -507,6 +507,7 @@
 	  *	Generate the ColumnInfo argument for the constant action. Return the number of constraints.
 	  */
 	public	void	genColumnInfo()
+        throws StandardException
 	{
 		// for each column, stuff system.column
 		colInfos = new ColumnInfo[tableElementList.countNumberOfColumns()]; 

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateViewNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateViewNode.java?rev=712840&r1=712839&r2=712840&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateViewNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/CreateViewNode.java
Mon Nov 10 13:31:47 2008
@@ -357,6 +357,7 @@
 											 null,
 											 null,
 											 null,
+											 null,
 											 ColumnInfo.CREATE,
 											 0, 0, 0);
 		}

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java?rev=712840&r1=712839&r2=712840&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/TableElementList.java
Mon Nov 10 13:31:47 2008
@@ -430,7 +430,8 @@
 	 *
 	 * @return int		The number of constraints in the create table.
 	 */
-	public int genColumnInfos(ColumnInfo[] colInfos)
+	public int genColumnInfos( ColumnInfo[] colInfos)
+        throws StandardException
 	{
 		int	numConstraints = 0;
 		int size = size();
@@ -441,7 +442,7 @@
 			{
 				colInfos[index] = new ColumnInfo(
 								((TableElementNode) elementAt(index)).getName(),
-								null, null, null, null, null,
+								null, null, null, null, null, null,
 								ColumnInfo.DROP, 0, 0, 0);
 				break;
 			}
@@ -455,18 +456,35 @@
 						"ConstraintDefinitionNode");
 				}
 
-				/* Remember how many constraints that we've seen */
+				/* Remember how many constraints we've seen */
 				numConstraints++;
 				continue;
 			}
 
 			ColumnDefinitionNode coldef = (ColumnDefinitionNode) elementAt(index);
 
+            //
+            // Generated columns may depend on functions mentioned in their
+            // generation clauses.
+            //
+            ProviderList apl = null;
+            ProviderInfo[]	providerInfos = null;
+			if ( coldef.hasGenerationClause() )
+			{
+				apl = coldef.getGenerationClauseNode().getAuxiliaryProviderList();
+			}
+            if (apl != null && apl.size() > 0)
+            {
+                DependencyManager dm = getDataDictionary().getDependencyManager();
+                providerInfos = dm.getPersistentProviderInfos(apl);
+            }
+            
 			colInfos[index - numConstraints] = 
 				new ColumnInfo(coldef.getColumnName(),
 							   coldef.getType(),
 							   coldef.getDefaultValue(),
 							   coldef.getDefaultInfo(),
+							   providerInfos,
 							   (UUID) null,
 							   coldef.getOldDefaultUUID(),
 							   coldef.getAction(),

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=712840&r1=712839&r2=712840&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
Mon Nov 10 13:31:47 2008
@@ -1343,6 +1343,12 @@
 			updateNewColumnToDefault(activation, columnDescriptor, lcc);
 		}	
 
+        //
+        // Add dependencies. These can arise if a generated column depends
+        // on a user created function.
+        //
+        addColumnDependencies( lcc, dd, td, columnInfo[ix] );
+
 		// Update SYSCOLPERMS table which tracks the permissions granted
 		// at columns level. The sytem table has a bit map of all the columns
 		// in the user table to help determine which columns have the 

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=712840&r1=712839&r2=712840&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 Mon
Nov 10 13:31:47 2008
@@ -29,12 +29,13 @@
 
 import org.apache.derby.iapi.types.DataTypeDescriptor;
 import org.apache.derby.iapi.types.DataValueDescriptor;
-
+import org.apache.derby.iapi.sql.depend.ProviderInfo;
 import org.apache.derby.iapi.services.sanity.SanityManager;
 
 import org.apache.derby.catalog.DefaultInfo;
 import org.apache.derby.catalog.UUID;
 
+import org.apache.derby.iapi.services.io.FormatableArrayHolder;
 import org.apache.derby.iapi.services.io.FormatableHashtable;
 import org.apache.derby.iapi.services.io.FormatableIntHolder;
 import org.apache.derby.iapi.services.io.FormatableLongHolder;
@@ -70,6 +71,7 @@
 	public	String						name;
 	public	DataTypeDescriptor			dataType;
 	public	DefaultInfo					defaultInfo;
+    public    ProviderInfo[]            providers;
 	public	DataValueDescriptor			defaultValue;
 	public	UUID						newDefaultUUID;
 	public	UUID						oldDefaultUUID;
@@ -107,6 +109,7 @@
 	 *  @param dataType		Column type.
 	 *  @param defaultValue	Column default value.
 	 *  @param defaultInfo	Column default info.
+	 *  @param providers   Array of providers that this column depends on.
 	 *  @param newDefaultUUID	New UUID for default.
 	 *  @param oldDefaultUUID	Old UUID for default.
 	 *	@param action		Action (create, modify default, etc.)
@@ -120,6 +123,7 @@
 					   DataTypeDescriptor			dataType,
 					   DataValueDescriptor			defaultValue,
 					   DefaultInfo					defaultInfo,
+					   ProviderInfo[]					providers,
 					   UUID							newDefaultUUID,
 					   UUID							oldDefaultUUID,
 					   int							action,
@@ -131,6 +135,7 @@
 		this.dataType = dataType;
 		this.defaultValue = defaultValue;
 		this.defaultInfo = defaultInfo;
+        this.providers = providers;
 		this.newDefaultUUID = newDefaultUUID;
 		this.oldDefaultUUID = oldDefaultUUID;
 		this.action = action;
@@ -173,6 +178,12 @@
 		{
 			autoincInc = autoincStart = 0;
 		}
+
+        FormatableArrayHolder   fah = (FormatableArrayHolder) fh.get( "providers" );
+        if ( fah != null )
+        {
+            providers = (ProviderInfo[]) fah.getArray( ProviderInfo.class );
+        }
 	}
 
 	/**
@@ -200,6 +211,13 @@
 			fh.putLong("autoincStart", autoincStart);
 			fh.putLong("autoincInc", autoincInc);
 		}
+
+        if ( providers != null )
+        {
+            FormatableArrayHolder   fah = new FormatableArrayHolder( providers );
+            fh.put( "providers", fah );
+        }
+        
 		out.writeObject(fh);
 	}
  

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CreateTableConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CreateTableConstantAction.java?rev=712840&r1=712839&r2=712840&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CreateTableConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/CreateTableConstantAction.java
Mon Nov 10 13:31:47 2008
@@ -22,7 +22,6 @@
 package org.apache.derby.impl.sql.execute;
 
 import org.apache.derby.iapi.sql.execute.ConstantAction;
-
 import org.apache.derby.iapi.store.access.TransactionController;
 
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
@@ -49,6 +48,7 @@
 
 import org.apache.derby.iapi.services.sanity.SanityManager;
 
+import org.apache.derby.catalog.DependableFinder;
 import org.apache.derby.catalog.UUID;
 
 import org.apache.derby.catalog.types.DefaultInfoImpl;
@@ -357,6 +357,16 @@
 				}
 			}
 		}
+
+        //
+        // Add dependencies. These can arise if a generated column depends
+        // on a user created function.
+        //
+		for (int ix = 0; ix < columnInfo.length; ix++)
+		{
+            addColumnDependencies( lcc, dd, td, columnInfo[ ix ] );
+        }
+        
 		if ( tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE )
 		{
 			lcc.addDeclaredGlobalTempTable(td);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java?rev=712840&r1=712839&r2=712840&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/execute/DDLConstantAction.java
Mon Nov 10 13:31:47 2008
@@ -24,18 +24,24 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.derby.catalog.DependableFinder;
 import org.apache.derby.catalog.UUID;
 import org.apache.derby.iapi.error.StandardException;
 import org.apache.derby.iapi.reference.SQLState;
 import org.apache.derby.iapi.services.sanity.SanityManager;
+import org.apache.derby.iapi.services.context.ContextManager;
 import org.apache.derby.iapi.sql.Activation;
 import org.apache.derby.iapi.sql.conn.Authorizer;
 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
 import org.apache.derby.iapi.sql.depend.DependencyManager;
 import org.apache.derby.iapi.sql.depend.Dependent;
+import org.apache.derby.iapi.sql.depend.Provider;
 import org.apache.derby.iapi.sql.depend.ProviderInfo;
 import org.apache.derby.iapi.sql.dictionary.ColPermsDescriptor;
+import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
+import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor;
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
+import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
 import org.apache.derby.iapi.sql.dictionary.PermissionsDescriptor;
 import org.apache.derby.iapi.sql.dictionary.RoleGrantDescriptor;
 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
@@ -764,6 +770,41 @@
 		return false;
 	}
 
+    /**
+     * Add dependencies of a column on providers. These can arise if a generated column depends
+     * on a user created function.
+     */
+    protected   void    addColumnDependencies
+        (
+         LanguageConnectionContext  lcc,
+         DataDictionary     dd,
+         TableDescriptor    td,
+         ColumnInfo         ci
+         )
+        throws StandardException
+    {
+        ProviderInfo[]  providers = ci.providers;
+
+        if ( providers != null )
+        {
+            DependencyManager   dm = dd.getDependencyManager();
+            ContextManager      cm = lcc.getContextManager();
+            int                         providerCount = providers.length;
+            ColumnDescriptor    cd = td.getColumnDescriptor( ci.name );
+            DefaultDescriptor   defDesc = cd.getDefaultDescriptor( dd );
+
+            for ( int px = 0; px < providerCount; px++ )
+            {
+                ProviderInfo            pi = providers[ px ];
+                DependableFinder    finder = pi.getDependableFinder();
+                UUID                        providerID = pi.getObjectId();
+                Provider                    provider = (Provider) finder.getDependable( dd,
providerID );
+
+                dm.addDependency( defDesc, provider, cm );
+            }   // end loop through providers
+        }
+    }
+
 	/**
 	 * Mutable Boolean wrapper, initially false
 	 */

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.java?rev=712840&r1=712839&r2=712840&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.java
Mon Nov 10 13:31:47 2008
@@ -3225,6 +3225,131 @@
              );
     }
 
+    /**
+     * <p>
+     * Test that generation clauses block the dropping of routines that they
+     * depend on.
+     * </p>
+     */
+    public  void    test_021_dropFunction()
+        throws Exception
+    {
+        Connection  conn = getConnection();
+
+        goodStatement
+            (
+             conn,
+             "create function f_fd_minus\n" +
+             "(\n" +
+             "    a int\n" +
+             ")\n" +
+             "returns int\n" +
+             "language java\n" +
+             "deterministic\n" +
+             "parameter style java\n" +
+             "no sql\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest.minus'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create function f_fd_minus_2\n" +
+             "(\n" +
+             "    a int\n" +
+             ")\n" +
+             "returns int\n" +
+             "language java\n" +
+             "deterministic\n" +
+             "parameter style java\n" +
+             "no sql\n" +
+             "external name 'org.apache.derbyTesting.functionTests.tests.lang.GeneratedColumnsTest.minus'\n"
+             );
+        goodStatement
+            (
+             conn,
+             "create table t_fd_1( a int generated always as ( f_fd_minus( b ) ), b int )"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into t_fd_1( b ) values ( 1 )"
+             );
+        
+        //
+        // Verify that the generation clause blocks our dropping the function.
+        //
+        expectExecutionError
+            (
+             conn,
+             OPERATION_FORBIDDEN,
+             "drop function f_fd_minus"
+             );
+        goodStatement
+            (
+             conn,
+             "alter table t_fd_1 drop column a"
+             );
+        goodStatement
+            (
+             conn,
+             "drop function f_fd_minus"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into t_fd_1( b ) values ( 1 )"
+             );
+        
+        //
+        // Verify that same behavior in case the generated column was added via
+        // an ALTER TABLE statement.
+        //
+        goodStatement
+            (
+             conn,
+             "alter table t_fd_1 add column c int generated always as ( f_fd_minus_2( b )
)"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into t_fd_1( b ) values ( 1 )"
+             );
+        expectExecutionError
+            (
+             conn,
+             OPERATION_FORBIDDEN,
+             "drop function f_fd_minus_2"
+             );
+        goodStatement
+            (
+             conn,
+             "alter table t_fd_1 drop column c"
+             );
+        goodStatement
+            (
+             conn,
+             "drop function f_fd_minus_2"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into t_fd_1( b ) values ( 1 )"
+             );
+        assertResults
+            (
+             conn,
+             "select * from t_fd_1 order by b",
+             new String[][]
+             {
+                 { "1", },
+                 { "1", },
+                 { "1", },
+                 { "1", },
+             },
+             false
+             );
+    }
+
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // MINIONS



Mime
View raw message