You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by th...@apache.org on 2017/07/07 02:07:03 UTC

[41/58] [abbrv] commons-dbutils git commit: Applied DBUTILS-108 to 2.0 branch

Applied DBUTILS-108 to 2.0 branch

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/dbutils/branches/2_0@1482088 13f79535-47bb-0310-9956-ffa450edef68


Project: http://git-wip-us.apache.org/repos/asf/commons-dbutils/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-dbutils/commit/e8727b69
Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/e8727b69
Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/e8727b69

Branch: refs/heads/2_0
Commit: e8727b69983ddfb247201dd69cd35940806051a5
Parents: 67728b0
Author: Bill Speirs <ws...@apache.org>
Authored: Mon May 13 20:27:57 2013 +0000
Committer: Bill Speirs <ws...@apache.org>
Committed: Mon May 13 20:27:57 2013 +0000

----------------------------------------------------------------------
 .../apache/commons/dbutils2/BatchExecutor.java  | 12 ++++
 .../commons/dbutils2/BatchInsertExecutor.java   | 67 ++++++++++++++++++
 .../dbutils2/BatchInsertExecutorTest.java       | 72 ++++++++++++++++++++
 3 files changed, 151 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/e8727b69/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
index e8b13a9..4ee5d16 100644
--- a/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
+++ b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java
@@ -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 AbstractExecutor<BatchExecutor> {
         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 AbstractExecutor<BatchExecutor> {
      * @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) {

http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/e8727b69/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java b/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
new file mode 100644
index 0000000..01f75d7
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/BatchInsertExecutor.java
@@ -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;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/e8727b69/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
new file mode 100644
index 0000000..7bb3a85
--- /dev/null
+++ b/src/test/java/org/apache/commons/dbutils2/BatchInsertExecutorTest.java
@@ -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();
+    }
+    
+}