db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1440035 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ engine/org/apache/derby/iapi/jdbc/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ testing/org/apache/derbyTesting/junit/
Date Tue, 29 Jan 2013 18:02:00 GMT
Author: rhillegas
Date: Tue Jan 29 18:02:00 2013
New Revision: 1440035

URL: http://svn.apache.org/viewvc?rev=1440035&view=rev
Log:
DERBY-6000: Add JDBC 4.2 method Statement.executeLargeBatch() to embedded and client drivers.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/EngineStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
(original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
Tue Jan 29 18:02:00 2013
@@ -283,6 +283,10 @@ abstract class LogicalStatementEntity
     //
     ////////////////////////////////////////////////////////////////////
 
+    public  long[] executeLargeBatch() throws SQLException
+    {
+        return ((org.apache.derby.client.am.Statement) getPhysStmt()).executeLargeBatch();
+    }
     public  long executeLargeUpdate( String sql ) throws SQLException
     {
         return ((org.apache.derby.client.am.Statement) getPhysStmt()).executeLargeUpdate(
sql );

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java Tue Jan 29 18:02:00
2013
@@ -501,11 +501,11 @@ public class Statement implements java.s
         {
             synchronized (connection_) {
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceEntry(this, "executeUpdate", sql);
+                    agent_.logWriter_.traceEntry(this, "executeLargeUpdate", sql);
                 }
                 long updateValue = executeUpdateX(sql);
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);
+                    agent_.logWriter_.traceExit(this, "executeLargeUpdate", updateValue);
                 }
                 return updateValue;
             }
@@ -1175,6 +1175,27 @@ public class Statement implements java.s
         }
     }
 
+    // Added by JDBC 4.2
+    public long[] executeLargeBatch() throws SQLException, BatchUpdateException {
+        try
+        {
+            synchronized (connection_) {
+                if (agent_.loggingEnabled()) {
+                    agent_.logWriter_.traceEntry(this, "executeLargeBatch");
+                }
+                long[] updateCounts = executeBatchX();
+                if (agent_.loggingEnabled()) {
+                    agent_.logWriter_.traceExit(this, "executeLargeBatch", updateCounts);
+                }
+                return updateCounts;
+            }
+        }
+        catch ( SqlException se )
+        {
+            throw se.getSQLException();
+        }
+    }
+
     private long[] executeBatchX() throws SqlException, BatchUpdateException {
         checkForClosedStatement(); // Per jdbc spec (see java.sql.Statement.close() javadoc)
         clearWarningsX(); // Per jdbc spec 0.7, and getWarnings() javadoc
@@ -1308,12 +1329,12 @@ public class Statement implements java.s
         {
             synchronized (connection_) {
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceEntry(this, "executeUpdate", sql, autoGeneratedKeys);
+                    agent_.logWriter_.traceEntry(this, "executeLargeUpdate", sql, autoGeneratedKeys);
                 }
                 autoGeneratedKeys_ = autoGeneratedKeys;
                 long updateValue = executeUpdateX(sql);
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);
+                    agent_.logWriter_.traceExit(this, "executeLargeUpdate", updateValue);
                 }
                 return updateValue;
             }
@@ -1353,14 +1374,14 @@ public class Statement implements java.s
         {
             synchronized (connection_) {  
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceEntry(this, "executeUpdate", sql, columnIndexes);
+                    agent_.logWriter_.traceEntry(this, "executeLargeUpdate", sql, columnIndexes);
                 }
                 if (columnIndexes != null && columnIndexes.length > 0)
                     autoGeneratedKeys_ = Statement.RETURN_GENERATED_KEYS;
                 generatedKeysColumnIndexes_ = columnIndexes;
                 long updateValue = executeUpdateX(sql);
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);
+                    agent_.logWriter_.traceExit(this, "executeLargeUpdate", updateValue);
                 }
                 return updateValue;
             }
@@ -1400,14 +1421,14 @@ public class Statement implements java.s
         {
             synchronized (connection_) {
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceEntry(this, "executeUpdate", sql, columnNames);
+                    agent_.logWriter_.traceEntry(this, "executeLargeUpdate", sql, columnNames);
                 }
                 if (columnNames != null && columnNames.length > 0)
                     autoGeneratedKeys_ = Statement.RETURN_GENERATED_KEYS;
                 generatedKeysColumnNames_ = columnNames;
                 long updateValue = executeUpdateX(sql);
                 if (agent_.loggingEnabled()) {
-                    agent_.logWriter_.traceExit(this, "executeUpdate", updateValue);
+                    agent_.logWriter_.traceExit(this, "executeLargeUpdate", updateValue);
                 }
                 return updateValue;
             }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredStatement.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/BrokeredStatement.java Tue
Jan 29 18:02:00 2013
@@ -624,6 +624,10 @@ public class BrokeredStatement implement
     //
     ////////////////////////////////////////////////////////////////////
 
+    public  long[] executeLargeBatch() throws SQLException
+    {
+        return ((EngineStatement) getStatement()).executeLargeBatch();
+    }
     public  long executeLargeUpdate( String sql ) throws SQLException
     {
         return ((EngineStatement) getStatement()).executeLargeUpdate( sql );

Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/EngineStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/EngineStatement.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/EngineStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/jdbc/EngineStatement.java Tue Jan
29 18:02:00 2013
@@ -67,6 +67,7 @@ public interface EngineStatement extends
     //
     ////////////////////////////////////////////////////////////////////
 
+    public  long[] executeLargeBatch() throws SQLException;
     public  long executeLargeUpdate( String sql ) throws SQLException;
     public  long executeLargeUpdate( String sql, int autoGeneratedKeys) throws SQLException;
     public  long executeLargeUpdate( String sql, int[] columnIndexes ) throws SQLException;

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedStatement.java Tue Jan
29 18:02:00 2013
@@ -1015,10 +1015,21 @@ public class EmbedStatement extends Conn
      * driver does not support batch statements
      */
     public int[] executeBatch() throws SQLException {
+        return Util.squashLongs( executeLargeBatch() );
+    }
+    
+    /**
+     * JDBC 4.2
+     * 
+     * Submit a batch of commands to the database for execution.
+     * This method is optional. For use with
+     * statements which may touch more than Integer.MAX_VALUE rows.
+     */
+    public long[] executeLargeBatch() throws SQLException {
 		checkExecStatus();
 		synchronized (getConnectionSynchronization()) 
 		{
-                        setupContextStack();
+            setupContextStack();
 			int i = 0;
 			// As per the jdbc 2.0 specs, close the statement object's current resultset
 			// if one is open.
@@ -1035,7 +1046,7 @@ public class EmbedStatement extends Conn
 			else
 				size = stmts.size();
 
-			int[] returnUpdateCountForBatch = new int[size];
+			long[] returnUpdateCountForBatch = new long[size];
 
 			SQLException sqle;
 			try {
@@ -1048,7 +1059,7 @@ public class EmbedStatement extends Conn
                     InterruptStatus.throwIf(lcc);
 					if (executeBatchElement(stmts.get(i)))
 						throw newSQLException(SQLState.RESULTSET_RETURN_NOT_ALLOWED);
-					returnUpdateCountForBatch[i] = getUpdateCount();
+					returnUpdateCountForBatch[ i ] = getLargeUpdateCount();
 				}
 
                 InterruptStatus.restoreIntrFlagIfSeen(lcc);
@@ -1067,12 +1078,11 @@ public class EmbedStatement extends Conn
 				restoreContextStack();
 			}
 
-			int successfulUpdateCount[] = new int[i];
+			long[] successfulUpdateCount = new long[ i ];
             System.arraycopy(returnUpdateCountForBatch, 0, successfulUpdateCount, 0, i);
 
-			SQLException batch =
-			new java.sql.BatchUpdateException(sqle.getMessage(), sqle.getSQLState(),
-									sqle.getErrorCode(), successfulUpdateCount);
+			SQLException batch = Util.newBatchUpdateException
+                ( sqle.getMessage(), sqle.getSQLState(),sqle.getErrorCode(), successfulUpdateCount
);
 
 			batch.setNextException(sqle);
 			batch.initCause(sqle);

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/Util.java Tue Jan 29 18:02:00
2013
@@ -334,6 +334,24 @@ public abstract class Util  {
                 StandardException.getSeverityFromIdentifier(SQLState.TYPE_MISMATCH));
 	}
 
+    /** Create the correct BatchUpdateException depending on whether this is Java 8 or lower
*/
+    static  SQLException    newBatchUpdateException
+        ( String message, String sqlState, int errorCode, long[] updateCounts )
+    {
+        return new java.sql.BatchUpdateException
+            ( message, sqlState, errorCode, squashLongs( updateCounts ) );
+    }
+
+    /** Squash an array of longs into an array of ints */
+    public static  int[]   squashLongs( long[] longs )
+    {
+        int count = (longs == null) ? 0 : longs.length;
+        int[]   ints = new int[ count ];
+        for ( int i = 0; i < count; i++ ) { ints[ i ] = (int) longs[ i ]; }
+
+        return ints;
+    }
+
     /**
      * Create an {@code IOException} that wraps another {@code Throwable}.
      *

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
Tue Jan 29 18:02:00 2013
@@ -380,6 +380,9 @@ public class StatementTest
 
         largeUpdateTest( sw, (long) Integer.MAX_VALUE );
         largeUpdateTest( sw, 0L);
+
+        largeBatchTest( sw, (long) Integer.MAX_VALUE );
+        largeBatchTest( sw, 0L);
     }
     private static  void    largeUpdateTest( StatementWrapper sw, long rowCountBase )
         throws Exception
@@ -433,6 +436,46 @@ public class StatementTest
         assertEquals( (int) expected, sw.getWrappedStatement().getUpdateCount() );
         assertEquals( longAnswer, sw.getLargeUpdateCount() );
     }
+    private static  void    largeBatchTest( StatementWrapper sw, long rowCountBase )
+        throws Exception
+    {
+        println( "Large batch test with rowCountBase = " + rowCountBase );
+        
+        // poke the rowCountBase into the engine. all returned row counts will be
+        // increased by this amount
+        sw.getWrappedStatement().clearBatch();
+        sw.getWrappedStatement().execute( "call setRowCountBase( " + rowCountBase + " )"
);
+
+        long[]  expectedResult = new long[] { rowCountBase + 1L, rowCountBase + 1L, rowCountBase
+ 2L };
+
+        createBatch( sw );
+        assertEquals( sw.getWrappedStatement().executeBatch(), squashLongs( expectedResult
) );
+
+        createBatch( sw );
+        assertEquals( sw.executeLargeBatch(), expectedResult );
+    }
+    private static  void    createBatch( StatementTest.StatementWrapper sw )
+        throws Exception
+    {
+        sw.getWrappedStatement().clearBatch();
+        truncate( sw );
+        sw.getWrappedStatement().addBatch( "insert into bigintTable( col2 ) values ( 1 )"
);
+        sw.getWrappedStatement().addBatch( "update bigintTable set col2 = 2" );
+        sw.getWrappedStatement().addBatch( "insert into bigintTable( col2 ) values ( 3 ),
( 4 )" );
+    }
+    private static  void    truncate( StatementTest.StatementWrapper sw )
+        throws Exception
+    {
+        sw.getWrappedStatement().execute( "truncate table bigintTable" );
+    }
+    private static  int[]   squashLongs( long[] longs )
+    {
+        int count = (longs == null) ? 0 : longs.length;
+        int[]   ints = new int[ count ];
+        for ( int i = 0; i < count; i++ ) { ints[ i ] = (int) longs[ i ]; }
+
+        return ints;
+    }
     
     /**
      * Create test suite for StatementTest.
@@ -471,6 +514,15 @@ public class StatementTest
         public  Statement   getWrappedStatement() { return _wrappedStatement; }
 
         // New methods added by JDBC 4.2
+        public  long[] executeLargeBatch() throws SQLException
+        {
+            return ((long[]) invoke
+                (
+                 "executeLargeBatch",
+                 new Class[] {},
+                 new Object[] {}
+                 ));
+        }
         public  long executeLargeUpdate( String sql ) throws SQLException
         {
             return ((Long) invoke

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=1440035&r1=1440034&r2=1440035&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java Tue Jan
29 18:02:00 2013
@@ -468,6 +468,34 @@ public abstract class BaseTestCase
     }
     
     /**
+     * <p>
+     * Assert the equivalence of two int arrays.
+     * </p>
+     */
+    public  static  void    assertEquals( int[] expected, int[] actual )
+    {
+        assertEquals( expected.length, actual.length );
+        for ( int i = 0; i < expected.length; i++ )
+        {
+            assertEquals( Integer.toString( i ), expected[ i ], actual[ i ] );
+        }
+    }
+
+    /**
+     * <p>
+     * Assert the equivalence of two long arrays.
+     * </p>
+     */
+    public  static  void    assertEquals( long[] expected, long[] actual )
+    {
+        assertEquals( expected.length, actual.length );
+        for ( int i = 0; i < expected.length; i++ )
+        {
+            assertEquals( Integer.toString( i ), expected[ i ], actual[ i ] );
+        }
+    }
+
+    /**
      * Assert that two files in the filesystem are identical.
      * 
      * @param file1 the first file to compare



Mime
View raw message