commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wspe...@apache.org
Subject svn commit: r1482088 - in /commons/proper/dbutils/branches/2_0/src: main/java/org/apache/commons/dbutils2/BatchExecutor.java main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
Date Mon, 13 May 2013 20:27:58 GMT
Author: wspeirs
Date: Mon May 13 20:27:57 2013
New Revision: 1482088

URL: http://svn.apache.org/r1482088
Log:
Applied DBUTILS-108 to 2.0 branch

Added:
    commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
    commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
Modified:
    commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java

Modified: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java?rev=1482088&r1=1482087&r2=1482088&view=diff
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
(original)
+++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
Mon May 13 20:27:57 2013
@@ -17,6 +17,7 @@
 package org.apache.commons.dbutils2;
 
 import java.sql.Connection;
+import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Types;
 
@@ -43,6 +44,14 @@ public class BatchExecutor extends Abstr
         super(conn, sql);
         this.closeConn = closeConnection;
     }
+    
+    /**
+     * Returns the close connection flag.
+     * @return close connection flag.
+     */
+    boolean getCloseConn() {
+        return closeConn;
+    }
 
     /**
      * Binds a parameter name to a value for a given statement.
@@ -111,6 +120,9 @@ public class BatchExecutor extends Abstr
      * @see org.apache.commons.dbutils2.UpdateExecutor#execute()
      */
     public int[] execute() throws SQLException {
+        // throw an exception if there are unmapped parameters
+        this.throwIfUnmappedParams();
+
         try {
             return getStatement().executeBatch();
         } catch (SQLException e) {

Added: commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java?rev=1482088&view=auto
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
(added)
+++ commons/proper/dbutils/branches/2_0/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
Mon May 13 20:27:57 2013
@@ -0,0 +1,67 @@
+package org.apache.commons.dbutils2;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+
+public class BatchInsertExecutor extends BatchExecutor {
+
+    /**
+     * Constructs a BatchInsertExecutor given a connection and SQL statement.
+     * 
+     * @param conn The connection to use during execution.
+     * @param sql The SQL statement.
+     * @param closeConnection If the connection should be closed or not.
+     * @throws SQLException thrown if there is an error during execution.
+     */
+    BatchInsertExecutor(final Connection conn, final String sql, final boolean closeConnection)
throws SQLException {
+        super(conn, sql, closeConnection);
+    }
+
+    /**
+     * Calls batch after checking the parameters to ensure nothing is null.
+     * 
+     * @return the result generated by the handler.
+     * @throws SQLException If there are database or parameter errors.
+     * @see org.apache.commons.dbutils2.UpdateExecutor#execute()
+     */
+    public <T> T execute(final ResultSetHandler<T> handler) throws SQLException
{
+        // throw an exception if there are unmapped parameters
+        this.throwIfUnmappedParams();
+
+        // make sure our handler is not null
+        if (handler == null) {
+            if (getCloseConn()) {
+                close(getConnection());
+            }
+            throw new SQLException("Null ResultSetHandler");
+        }
+
+        ResultSet resultSet = null;
+        
+        try {
+            // execute the query and get the keys, wrapping them
+            getStatement().executeBatch();
+            resultSet = this.wrap(getStatement().getGeneratedKeys());
+            
+            // execute the handler
+            return handler.handle(resultSet);
+        } catch (SQLException e) {
+            rethrow(e);
+        } finally {
+            try {
+                close(resultSet);
+            } finally {
+                close(getStatement());
+                if (getCloseConn()) {
+                    close(getConnection());
+                }
+            }
+        }
+
+        // we get here only if something is thrown
+        return null;
+    }
+
+}

Added: commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java?rev=1482088&view=auto
==============================================================================
--- commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
(added)
+++ commons/proper/dbutils/branches/2_0/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
Mon May 13 20:27:57 2013
@@ -0,0 +1,72 @@
+/*
+ * 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.commons.dbutils2;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+import org.apache.commons.dbutils2.handlers.ArrayListHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+
+public class BatchInsertExecutorTest {
+
+    private BatchInsertExecutor executor;
+    
+    @Mock private ResultSetHandler<Object> handler;
+    @Mock private Connection conn;
+    @Mock private PreparedStatement stmt;
+    @Mock private ResultSet resultSet;
+
+    @Before
+    public void setup() throws SQLException {
+        MockitoAnnotations.initMocks(this);
+        
+        when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
+        when(stmt.getGeneratedKeys()).thenReturn(resultSet);
+        when(handler.handle(any(ResultSet.class))).thenReturn(new Object());
+    }
+    
+    protected void createExecutor(String sql) throws Exception {
+        executor = new BatchInsertExecutor(conn, sql, true);
+    }
+    
+    @Test
+    public void testGoodSQL() throws Exception {
+        createExecutor("insert into blah");
+        
+        executor.addBatch();
+        Object ret = executor.execute(handler);
+        
+        assertNotNull(ret);
+        verify(handler, times(1)).handle(resultSet);
+        verify(conn, times(1)).close();
+        verify(stmt, times(1)).close();
+    }
+    
+}



Mime
View raw message