db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r719656 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang: GeneratedColumnsHelper.java GeneratedColumnsTest.java
Date Fri, 21 Nov 2008 17:51:45 GMT
Author: rhillegas
Date: Fri Nov 21 09:51:44 2008
New Revision: 719656

URL: http://svn.apache.org/viewvc?rev=719656&view=rev
Log:
DERBY-481: Add tests to verify that you can't use updatable ResultSets to corrupt generated
columns.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsHelper.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsTest.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsHelper.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsHelper.java?rev=719656&r1=719655&r2=719656&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsHelper.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/GeneratedColumnsHelper.java
Fri Nov 21 09:51:44 2008
@@ -126,6 +126,14 @@
         ps.close();
     }
     
+	protected	static	ResultSet	executeQuery( Statement stmt, String text )
+		throws SQLException
+	{
+		println( "Executing '" + text + "'" );
+
+        return stmt.executeQuery( text );
+	}
+
     /**
      * Prepare a statement and report its sql text.
      */
@@ -160,6 +168,42 @@
     }
 
     /**
+     * Assert that the in-place update raises the expected error.
+     */
+    protected void    expectUpdateRowError( ResultSet rs, String sqlState )
+        throws Exception
+    {
+        println( "\nExpecting " + sqlState + " when updating row" );
+
+        try {
+            rs.updateRow();
+            fail( "Expected error: " + sqlState );
+        }
+        catch (SQLException se)
+        {
+            assertSQLState( sqlState, se );
+        }
+    }
+
+    /**
+     * Assert that the in-place insert raises the expected error.
+     */
+    protected void    expectInsertRowError( ResultSet rs, String sqlState )
+        throws Exception
+    {
+        println( "\nExpecting " + sqlState + " when inserting row" );
+
+        try {
+            rs.insertRow();
+            fail( "Expected error: " + sqlState );
+        }
+        catch (SQLException se)
+        {
+            assertSQLState( sqlState, se );
+        }
+    }
+
+    /**
      * Assert that the statement text, when executed, raises a warning.
      */
     protected void    expectExecutionWarning( Connection conn, String sqlState, String query
)

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=719656&r1=719655&r2=719656&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
Fri Nov 21 09:51:44 2008
@@ -4450,6 +4450,129 @@
 
     }
 
+    /**
+     * <p>
+     * Test that you can't use updatable ResultSets to corrupt generated columns.
+     * </p>
+     */
+    public  void    test_025_basicUpdatableResultSets()
+        throws Exception
+    {
+        Connection  conn = getConnection();
+
+        //
+        // Setup
+        //
+        conn.setAutoCommit( false );
+        goodStatement
+            (
+             conn,
+             "create table t_urs_1 ( a int, b generated always as ( -a ) )"
+             );
+        goodStatement
+            (
+             conn,
+             "insert into t_urs_1( a ) values ( 1 )"
+             );
+        conn.commit();
+        Statement   stmt = conn.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE
);
+        ResultSet   rs = null;
+        
+        //
+        // Verify that updates to the updatable column trigger the generation clause.
+        //
+        rs = executeQuery( stmt, "select * from t_urs_1 for update" );
+        rs.next();
+        println( "Initially ( a, b ) = ( " + rs.getInt( 1 ) + ", " + rs.getInt( 2 ) + " )"
);
+        rs.updateInt( 1, 2 );
+        rs.updateRow();
+        rs.close();
+        conn.commit();
+        assertResults
+            (
+             conn,
+             "select * from t_urs_1 order by a",
+             new String[][]
+             {
+                 { "2", "-2" },
+             },
+             false
+             );
+        conn.commit();
+
+        //
+        // Verify that updates to the generated column raise an exception.
+        //
+        rs = executeQuery( stmt, "select * from t_urs_1 for update" );
+        rs.next();
+        println( "Initially ( a, b ) = ( " + rs.getInt( 1 ) + ", " + rs.getInt( 2 ) + " )"
);
+        rs.updateInt( 2, 2 );
+        expectUpdateRowError( rs, CANT_OVERRIDE_GENERATION_CLAUSE );
+        rs.close();
+        conn.commit();
+        assertResults
+            (
+             conn,
+             "select * from t_urs_1 order by a",
+             new String[][]
+             {
+                 { "2", "-2" },
+             },
+             false
+             );
+        conn.commit();
+
+        //
+        // Verify that inserts succeed and trigger the generation clause if they
+        // just poke values into non-generated columns.
+        //
+        rs = executeQuery( stmt, "select * from t_urs_1 for update" );
+        rs.next();
+        rs.moveToInsertRow();
+        rs.updateInt( 1, 10 );
+        rs.insertRow();
+        rs.close();
+        conn.commit();
+        assertResults
+            (
+             conn,
+             "select * from t_urs_1 order by a",
+             new String[][]
+             {
+                 { "2", "-2" },
+                 { "10", "-10" },
+             },
+             false
+             );
+        conn.commit();
+
+        //
+        // Verify that in-place inserts raise an exception if you try to poke a
+        // value into a generated column.
+        //
+        rs = executeQuery( stmt, "select * from t_urs_1 for update" );
+        rs.next();
+        rs.moveToInsertRow();
+        rs.updateInt( 2, 10 );
+        expectInsertRowError( rs, CANT_OVERRIDE_GENERATION_CLAUSE );
+        rs.close();
+        conn.commit();
+        assertResults
+            (
+             conn,
+             "select * from t_urs_1 order by a",
+             new String[][]
+             {
+                 { "2", "-2" },
+                 { "10", "-10" },
+             },
+             false
+             );
+        conn.commit();
+
+        stmt.close();
+    }
+
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // MINIONS



Mime
View raw message