db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1558871 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
Date Thu, 16 Jan 2014 18:32:16 GMT
Author: rhillegas
Date: Thu Jan 16 18:32:15 2014
New Revision: 1558871

URL: http://svn.apache.org/r1558871
Log:
DERBY-3155: Make the INSERT column list optional in the WHEN NOT MATCHED clause of a MERGE
statement provided that the number of values in the VALUES subclause equals the number of
columns in the target table; commit derby-3155-12-aa-canOmitInsertColumnList.diff.

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

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java?rev=1558871&r1=1558870&r2=1558871&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/MatchingClauseNode.java
Thu Jan 16 18:32:15 2014
@@ -41,6 +41,7 @@ import org.apache.derby.iapi.sql.ResultD
 import org.apache.derby.iapi.sql.compile.CompilerContext;
 import org.apache.derby.iapi.sql.compile.Visitor;
 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor;
+import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList;
 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
 import org.apache.derby.iapi.sql.execute.ConstantAction;
@@ -780,13 +781,16 @@ public class MatchingClauseNode extends 
          )
         throws StandardException
     {
+        TableDescriptor td = targetTable.getTableDescriptor();
+
+        // construct a full insert column list if insert columns weren't specified
+        if ( _insertColumns == null )  { _insertColumns = buildFullColumnList( td ); }
+
         if ( _insertColumns.size() != _insertValues.size() )
         {
             throw StandardException.newException( SQLState.LANG_DB2_INVALID_COLS_SPECIFIED
); 
         }
         
-        TableDescriptor td = targetTable.getTableDescriptor();
-
         // forbid illegal values for identity columns
         for ( int i = 0; i <_insertValues.size(); i++ )
         {
@@ -816,6 +820,35 @@ public class MatchingClauseNode extends 
         _insertValues.replaceOrForbidDefaults( targetTable.getTableDescriptor(), _insertColumns,
true );
         bindExpressions( _insertValues, fullFromList );
     }
+
+    /**
+     * <p>
+     * Build the full column list for a table.
+     * </p>
+     */
+    private ResultColumnList    buildFullColumnList( TableDescriptor td )
+        throws StandardException
+    {
+        ResultColumnList    result = new ResultColumnList( getContextManager() );
+		ColumnDescriptorList cdl = td.getColumnDescriptorList();
+		int					 cdlSize = cdl.size();
+
+		for ( int index = 0; index < cdlSize; index++ )
+		{
+            ColumnDescriptor colDesc = cdl.elementAt( index );
+            ColumnReference columnRef = new ColumnReference( colDesc.getColumnName(), null,
getContextManager() );
+            ResultColumn    resultColumn = new ResultColumn
+                (
+                 columnRef,
+                 null,
+                 getContextManager()
+                 );
+            
+            result.addResultColumn( resultColumn );
+        }
+
+        return result;
+    }
     
     /**
      * <p>

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java?rev=1558871&r1=1558870&r2=1558871&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/MergeStatementTest.java
Thu Jan 16 18:32:15 2014
@@ -4299,6 +4299,82 @@ public class MergeStatementTest extends 
         goodStatement( dboConnection, "drop table t1_024" );
     }
     
+    /**
+     * <p>
+     * Verify that the INSERT list can be omitted.
+     * </p>
+     */
+    public  void    test_025_noInsertList()
+        throws Exception
+    {
+        Connection  dboConnection = openUserConnection( TEST_DBO );
+
+        //
+        // create schema
+        //
+        goodStatement
+            (
+             dboConnection,
+             "create table t1_025( a int, b int )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "create table t2_025( a int, b int )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "insert into t1_025 values ( 1, 100 ), ( 2, 200 )"
+             );
+        goodStatement
+            (
+             dboConnection,
+             "insert into t2_025 values ( 1, 100 ), ( 3, 300 )"
+             );
+
+        //
+        // Omitting the INSERT column list is OK as long as this is
+        // a short-hand for the full column list.
+        //
+        goodStatement
+            (
+             dboConnection,
+             "merge into t1_025\n" +
+             "using t2_025 on t1_025.a = t2_025.a\n" +
+             "when not matched then insert values ( t2_025.a, t2_025.b )\n"
+             );
+        assertResults
+            (
+             dboConnection,
+             "select * from t1_025 order by a",
+             new String[][]
+             {
+                 { "1", "100" },
+                 { "2", "200" },
+                 { "3", "300" },
+             },
+             false
+             );
+
+        //
+        // Fails because the omitted INSERT column list implies that a value
+        // must be supplied for every column in the table.
+        //
+        expectCompilationError
+            ( dboConnection, COLUMN_COUNT_MISMATCH,
+              "merge into t1_025\n" +
+              "using t2_025 on t1_025.a = t2_025.a\n" +
+              "when not matched then insert values ( t2_025.a )\n"
+              );
+
+        //
+        // drop schema
+        //
+        goodStatement( dboConnection, "drop table t2_025" );
+        goodStatement( dboConnection, "drop table t1_025" );
+    }
+
     ///////////////////////////////////////////////////////////////////////////////////
     //
     // ROUTINES



Mime
View raw message