Add missing svn:eol-style properties git-svn-id: https://svn.apache.org/repos/asf/commons/proper/dbutils/branches/2_0@1457567 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/d2ce0836 Tree: http://git-wip-us.apache.org/repos/asf/commons-dbutils/tree/d2ce0836 Diff: http://git-wip-us.apache.org/repos/asf/commons-dbutils/diff/d2ce0836 Branch: refs/heads/2_0 Commit: d2ce08361fb0c760d55e3d97737d27c396c9e8ff Parents: 9928367 Author: Sebastian Bazley <[email protected]> Authored: Sun Mar 17 21:29:20 2013 +0000 Committer: Sebastian Bazley <[email protected]> Committed: Sun Mar 17 21:29:20 2013 +0000 ---------------------------------------------------------------------- .../commons/dbutils2/AbstractExecutor.java | 646 +++++++++---------- .../apache/commons/dbutils2/BatchExecutor.java | 248 +++---- .../commons/dbutils2/GenerousBeanProcessor.java | 142 ++-- .../apache/commons/dbutils2/InsertExecutor.java | 228 +++---- .../apache/commons/dbutils2/QueryExecutor.java | 178 ++--- .../apache/commons/dbutils2/UpdateExecutor.java | 138 ++-- .../commons/dbutils2/AbstractExecutorTest.java | 250 +++---- .../commons/dbutils2/BatchExecutorTest.java | 138 ++-- .../dbutils2/GenerousBeanProcessorTest.java | 232 +++---- .../commons/dbutils2/InsertExecutorTest.java | 188 +++--- .../commons/dbutils2/QueryExecutorTest.java | 188 +++--- .../commons/dbutils2/UpdateExecutorTest.java | 154 ++--- 12 files changed, 1365 insertions(+), 1365 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/main/java/org/apache/commons/dbutils2/AbstractExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/AbstractExecutor.java b/src/main/java/org/apache/commons/dbutils2/AbstractExecutor.java index db15bab..25a9ab8 100644 --- a/src/main/java/org/apache/commons/dbutils2/AbstractExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/AbstractExecutor.java @@ -1,323 +1,323 @@ -/* - * 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 java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Types; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Abstract class for executing a query, insert, update, or batch. - * - * @since 2.0 - * @author William Speirs <[email protected]> - */ -abstract class AbstractExecutor<T extends AbstractExecutor<T>> { - - private static final String COLON = ":"; // TODO: change this to any character - - private final Connection conn; - private final String sql; - private final PreparedStatement stmt; - - private final Map<String, List<Integer>> paramPosMap; - private final Map<String, Object> paramValueMap; - private Integer currentPosition = Integer.valueOf(0); - - public AbstractExecutor(final Connection conn, final String sql) throws SQLException { - this.conn = conn; - this.sql = sql; - this.paramPosMap = new HashMap<String, List<Integer>>(); - this.paramValueMap = new HashMap<String, Object>(); - - final Pattern paramPattern = Pattern.compile("(:\\w+)"); - final Matcher matcher = paramPattern.matcher(sql); - - // go through finding params - while (matcher.find()) { - insertParamPosition(matcher.group().replace(COLON, "")); - } - - // replace all of the :names with ?, and create a prepared statement - stmt = conn.prepareStatement(sql.replaceAll(":\\w+", "\\?")); - } - - /** - * Helper method to insert params and the current position into the map. - * @param param the SQL param. - */ - private void insertParamPosition(final String param) { - List<Integer> posList = paramPosMap.get(param); - - // create a new list if we need to - if (posList == null) { - posList = new ArrayList<Integer>(); - paramPosMap.put(param, posList); - } - - // increment first, so we match SQL numbering - posList.add(++currentPosition); - } - - /** - * Gets the SQL statement that was passed into the constructor. - * @return the SQL statement passed into the constructor. - */ - protected String getSql() { - return sql; - } - - /** - * Returns the underlying prepared statement. - * @return the underlying prepared statement. - */ - protected PreparedStatement getStatement() { - return stmt; - } - - /** - * Returns the underlying connection. - * @return the underlying connection. - */ - protected Connection getConnection() { - return conn; - } - - /** - * Throws an exception if there are unmapped params. - * @throws SQLException if there are unmapped params. - */ - protected void throwIfUnmappedParams() throws SQLException { - if (paramPosMap.size() != 0) { - final Set<String> unmappedParams = paramPosMap.keySet(); - final StringBuilder sb = new StringBuilder("There are unbound parameters: "); - - for (String param:unmappedParams) { - sb.append(param); - sb.append(", "); - } - - // remove the last comma - sb.delete(sb.length() - 2, sb.length()); - - // throw our exception - throw new SQLException(sb.toString()); - } - } - - /** - * Binds a named parameter to a value. - * - * @param name the name of the parameter in the SQL statement. - * @param value the value of the parameter in the SQL statement. - * @return this execution object to provide the fluent style. - * @throws SQLException thrown if the parameter is not found, already bound, or there is an issue binding it. - */ - public T bind(final String name, final Object value) throws SQLException { - return bind(name, value, true); - } - - /** - * Binds null to a parameter. - * Types.VARCHAR is used as the type's parameter. - * This usually works, but fails with some Oracle and MS SQL drivers. - * @param name the name of the parameter. - * @return this execution object to provide the fluent style. - * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. - */ - public T bindNull(final String name) throws SQLException { - return bindNull(name, Types.VARCHAR, true); - } - - /** - * Binds null to a parameter, specifying the parameter's type. - * @param name the name of the parameter. - * @param sqlType the type of the parameter. - * @return this execution object to provide the fluent style. - * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. - */ - public T bindNull(final String name, final int sqlType) throws SQLException { - return bindNull(name, sqlType, true); - } - - /** - * Given a param name and sqlType, binds a null to that parameter. - * @param name the name of the parameter. - * @param sqlType the type of the parameter. - * @param removeFromPosMap if the param should be removed from the pos map. - * @return this - * @throws SQLException if there is an SQLException during binding. - */ - protected T bindNull(String name, int sqlType, boolean removeFromPosMap) throws SQLException { - name = name.replace(COLON, ""); // so we can take ":name" or "name" - - final List<Integer> pos = removeFromPosMap ? paramPosMap.remove(name) : paramPosMap.get(name); - - if (pos == null) { - throw new SQLException(name + " is not found in the SQL statement"); - } - - // go through and bind all of the positions for this name - for (Integer p:pos) { - stmt.setNull(p.intValue(), sqlType); - } - - // add the param and value to our map - paramValueMap.put(name, null); - - // suppressed because the casting will always work here - @SuppressWarnings("unchecked") - final T ret = (T) this; - - return ret; - } - - /** - * Binds value to name, but does not do the bookkeeping. - * @param name the parameter name. - * @param value the value. - * @param removeFromPosMap if the param should be removed from the pos map. - * @return this - * @throws SQLException if there is an SQLException during binding. - */ - protected T bind(String name, final Object value, boolean removeFromPosMap) throws SQLException { - name = name.replace(COLON, ""); // so we can take ":name" or "name" - - final List<Integer> pos = removeFromPosMap ? paramPosMap.remove(name) : paramPosMap.get(name); - - if (pos == null) { - throw new SQLException(name + " is not found in the SQL statement"); - } - - // go through and bind all of the positions for this name - for (Integer p:pos) { - // TODO: need to figure out how to bind NULL - stmt.setObject(p.intValue(), value); - } - - // add the param and value to our map - paramValueMap.put(name, value); - - // suppressed because the casting will always work here - @SuppressWarnings("unchecked") - final T ret = (T) this; - - return ret; - } - - /** - * Used for batch calls so we can clear the map after the addBatch call. - */ - protected void clearValueMap() { - paramValueMap.clear(); - } - - /** - * Throws a new exception with a more informative error message. - * - * @param cause The original exception that will be chained to the new - * exception when it's rethrown. - * - * @throws SQLException if a database access error occurs - */ - protected void rethrow(SQLException cause) throws SQLException { - String causeMessage = cause.getMessage(); - - if (causeMessage == null) { - causeMessage = ""; - } - - final StringBuffer msg = new StringBuffer(causeMessage); - - msg.append(" Query: "); - msg.append(sql); - msg.append(" Parameters: "); - - // loop through adding the parameter to value mappings - for (Map.Entry<String, Object> param:paramValueMap.entrySet()) { - msg.append(param.getKey()); - msg.append("="); - msg.append(param.getValue()); - msg.append(" "); - } - - final SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode()); - e.setNextException(cause); - - throw e; - } - - /** - * Wrap the <code>ResultSet</code> in a decorator before processing it. This - * implementation returns the <code>ResultSet</code> it is given without any - * decoration. - * - * @param rs The <code>ResultSet</code> to decorate; never <code>null</code>. - * @return The <code>ResultSet</code> wrapped in some decorator. - */ - protected ResultSet wrap(ResultSet rs) { - return rs; - } - - /** - * Close a <code>Connection</code>. This implementation avoids closing if - * null and does <strong>not</strong> suppress any exceptions. Subclasses - * can override to provide special handling like logging. - * - * @param conn Connection to close - * @throws SQLException if a database access error occurs - */ - protected void close(Connection conn) throws SQLException { - DbUtils.close(conn); - } - - /** - * Close a <code>Statement</code>. This implementation avoids closing if - * null and does <strong>not</strong> suppress any exceptions. Subclasses - * can override to provide special handling like logging. - * - * @param stmt Statement to close - * @throws SQLException if a database access error occurs - */ - protected void close(Statement stmt) throws SQLException { - DbUtils.close(stmt); - } - - /** - * Close a <code>ResultSet</code>. This implementation avoids closing if - * null and does <strong>not</strong> suppress any exceptions. Subclasses - * can override to provide special handling like logging. - * - * @param rs ResultSet to close - * @throws SQLException if a database access error occurs - */ - protected void close(ResultSet rs) throws SQLException { - DbUtils.close(rs); - } - - -} +/* + * 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 java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Types; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Abstract class for executing a query, insert, update, or batch. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ +abstract class AbstractExecutor<T extends AbstractExecutor<T>> { + + private static final String COLON = ":"; // TODO: change this to any character + + private final Connection conn; + private final String sql; + private final PreparedStatement stmt; + + private final Map<String, List<Integer>> paramPosMap; + private final Map<String, Object> paramValueMap; + private Integer currentPosition = Integer.valueOf(0); + + public AbstractExecutor(final Connection conn, final String sql) throws SQLException { + this.conn = conn; + this.sql = sql; + this.paramPosMap = new HashMap<String, List<Integer>>(); + this.paramValueMap = new HashMap<String, Object>(); + + final Pattern paramPattern = Pattern.compile("(:\\w+)"); + final Matcher matcher = paramPattern.matcher(sql); + + // go through finding params + while (matcher.find()) { + insertParamPosition(matcher.group().replace(COLON, "")); + } + + // replace all of the :names with ?, and create a prepared statement + stmt = conn.prepareStatement(sql.replaceAll(":\\w+", "\\?")); + } + + /** + * Helper method to insert params and the current position into the map. + * @param param the SQL param. + */ + private void insertParamPosition(final String param) { + List<Integer> posList = paramPosMap.get(param); + + // create a new list if we need to + if (posList == null) { + posList = new ArrayList<Integer>(); + paramPosMap.put(param, posList); + } + + // increment first, so we match SQL numbering + posList.add(++currentPosition); + } + + /** + * Gets the SQL statement that was passed into the constructor. + * @return the SQL statement passed into the constructor. + */ + protected String getSql() { + return sql; + } + + /** + * Returns the underlying prepared statement. + * @return the underlying prepared statement. + */ + protected PreparedStatement getStatement() { + return stmt; + } + + /** + * Returns the underlying connection. + * @return the underlying connection. + */ + protected Connection getConnection() { + return conn; + } + + /** + * Throws an exception if there are unmapped params. + * @throws SQLException if there are unmapped params. + */ + protected void throwIfUnmappedParams() throws SQLException { + if (paramPosMap.size() != 0) { + final Set<String> unmappedParams = paramPosMap.keySet(); + final StringBuilder sb = new StringBuilder("There are unbound parameters: "); + + for (String param:unmappedParams) { + sb.append(param); + sb.append(", "); + } + + // remove the last comma + sb.delete(sb.length() - 2, sb.length()); + + // throw our exception + throw new SQLException(sb.toString()); + } + } + + /** + * Binds a named parameter to a value. + * + * @param name the name of the parameter in the SQL statement. + * @param value the value of the parameter in the SQL statement. + * @return this execution object to provide the fluent style. + * @throws SQLException thrown if the parameter is not found, already bound, or there is an issue binding it. + */ + public T bind(final String name, final Object value) throws SQLException { + return bind(name, value, true); + } + + /** + * Binds null to a parameter. + * Types.VARCHAR is used as the type's parameter. + * This usually works, but fails with some Oracle and MS SQL drivers. + * @param name the name of the parameter. + * @return this execution object to provide the fluent style. + * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. + */ + public T bindNull(final String name) throws SQLException { + return bindNull(name, Types.VARCHAR, true); + } + + /** + * Binds null to a parameter, specifying the parameter's type. + * @param name the name of the parameter. + * @param sqlType the type of the parameter. + * @return this execution object to provide the fluent style. + * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. + */ + public T bindNull(final String name, final int sqlType) throws SQLException { + return bindNull(name, sqlType, true); + } + + /** + * Given a param name and sqlType, binds a null to that parameter. + * @param name the name of the parameter. + * @param sqlType the type of the parameter. + * @param removeFromPosMap if the param should be removed from the pos map. + * @return this + * @throws SQLException if there is an SQLException during binding. + */ + protected T bindNull(String name, int sqlType, boolean removeFromPosMap) throws SQLException { + name = name.replace(COLON, ""); // so we can take ":name" or "name" + + final List<Integer> pos = removeFromPosMap ? paramPosMap.remove(name) : paramPosMap.get(name); + + if (pos == null) { + throw new SQLException(name + " is not found in the SQL statement"); + } + + // go through and bind all of the positions for this name + for (Integer p:pos) { + stmt.setNull(p.intValue(), sqlType); + } + + // add the param and value to our map + paramValueMap.put(name, null); + + // suppressed because the casting will always work here + @SuppressWarnings("unchecked") + final T ret = (T) this; + + return ret; + } + + /** + * Binds value to name, but does not do the bookkeeping. + * @param name the parameter name. + * @param value the value. + * @param removeFromPosMap if the param should be removed from the pos map. + * @return this + * @throws SQLException if there is an SQLException during binding. + */ + protected T bind(String name, final Object value, boolean removeFromPosMap) throws SQLException { + name = name.replace(COLON, ""); // so we can take ":name" or "name" + + final List<Integer> pos = removeFromPosMap ? paramPosMap.remove(name) : paramPosMap.get(name); + + if (pos == null) { + throw new SQLException(name + " is not found in the SQL statement"); + } + + // go through and bind all of the positions for this name + for (Integer p:pos) { + // TODO: need to figure out how to bind NULL + stmt.setObject(p.intValue(), value); + } + + // add the param and value to our map + paramValueMap.put(name, value); + + // suppressed because the casting will always work here + @SuppressWarnings("unchecked") + final T ret = (T) this; + + return ret; + } + + /** + * Used for batch calls so we can clear the map after the addBatch call. + */ + protected void clearValueMap() { + paramValueMap.clear(); + } + + /** + * Throws a new exception with a more informative error message. + * + * @param cause The original exception that will be chained to the new + * exception when it's rethrown. + * + * @throws SQLException if a database access error occurs + */ + protected void rethrow(SQLException cause) throws SQLException { + String causeMessage = cause.getMessage(); + + if (causeMessage == null) { + causeMessage = ""; + } + + final StringBuffer msg = new StringBuffer(causeMessage); + + msg.append(" Query: "); + msg.append(sql); + msg.append(" Parameters: "); + + // loop through adding the parameter to value mappings + for (Map.Entry<String, Object> param:paramValueMap.entrySet()) { + msg.append(param.getKey()); + msg.append("="); + msg.append(param.getValue()); + msg.append(" "); + } + + final SQLException e = new SQLException(msg.toString(), cause.getSQLState(), cause.getErrorCode()); + e.setNextException(cause); + + throw e; + } + + /** + * Wrap the <code>ResultSet</code> in a decorator before processing it. This + * implementation returns the <code>ResultSet</code> it is given without any + * decoration. + * + * @param rs The <code>ResultSet</code> to decorate; never <code>null</code>. + * @return The <code>ResultSet</code> wrapped in some decorator. + */ + protected ResultSet wrap(ResultSet rs) { + return rs; + } + + /** + * Close a <code>Connection</code>. This implementation avoids closing if + * null and does <strong>not</strong> suppress any exceptions. Subclasses + * can override to provide special handling like logging. + * + * @param conn Connection to close + * @throws SQLException if a database access error occurs + */ + protected void close(Connection conn) throws SQLException { + DbUtils.close(conn); + } + + /** + * Close a <code>Statement</code>. This implementation avoids closing if + * null and does <strong>not</strong> suppress any exceptions. Subclasses + * can override to provide special handling like logging. + * + * @param stmt Statement to close + * @throws SQLException if a database access error occurs + */ + protected void close(Statement stmt) throws SQLException { + DbUtils.close(stmt); + } + + /** + * Close a <code>ResultSet</code>. This implementation avoids closing if + * null and does <strong>not</strong> suppress any exceptions. Subclasses + * can override to provide special handling like logging. + * + * @param rs ResultSet to close + * @throws SQLException if a database access error occurs + */ + protected void close(ResultSet rs) throws SQLException { + DbUtils.close(rs); + } + + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/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 64f1679..d25d435 100644 --- a/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/BatchExecutor.java @@ -1,124 +1,124 @@ -/* - * 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 java.sql.Connection; -import java.sql.SQLException; -import java.sql.Types; - -/** - * This class provides the ability to execute a batch of statements. - * - * It is really just a facade to an array of UpdateExecutors. - * - * @since 2.0 - * @author William Speirs <[email protected]> - */ -public class BatchExecutor extends AbstractExecutor<BatchExecutor> { - - private final boolean closeConn; - - /** - * Constructs a BatchExecutor 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. - */ - BatchExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { - super(conn, sql); - this.closeConn = closeConnection; - } - - /** - * Binds a parameter name to a value for a given statement. - * @param name the name of the parameter. - * @param value the value to bind to the parameter. - * @return this object. - * @throws SQLException thrown if the statement number does not exist, or any other SQLException. - * @see org.apache.commons.dbutils2.UpdateExecutor#bind(String, Object) - */ - @Override - public BatchExecutor bind(final String name, final Object value) throws SQLException { - return bind(name, value, false); - } - - /** - * Binds null to a parameter. - * Types.VARCHAR is used as the type's parameter. - * This usually works, but fails with some Oracle and MS SQL drivers. - * @param name the name of the parameter. - * @return this execution object to provide the fluent style. - * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. - */ - @Override - public BatchExecutor bindNull(final String name) throws SQLException { - return bindNull(name, Types.VARCHAR, false); - } - - /** - * Binds null to a parameter, specifying the parameter's type. - * @param name the name of the parameter. - * @param sqlType the type of the parameter. - * @return this execution object to provide the fluent style. - * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. - */ - @Override - public BatchExecutor bindNull(final String name, final int sqlType) throws SQLException { - return bindNull(name, sqlType, false); - } - - /** - * Adds the statement to the batch after binding all of the parameters. - * @return this object. - * @throws SQLException if a SQLException is thrown during the addBatch() call. - * @see java.sql.PreparedStatement#addBatch() - */ - public BatchExecutor addBatch() throws SQLException { - try { - getStatement().addBatch(); - clearValueMap(); - } catch (SQLException e) { - rethrow(e); - } - - return this; - } - - /** - * Calls batch after checking the parameters to ensure nothing is null. - * @return an array containing the number of rows updated for each statement. - * @throws SQLException If there are database or parameter errors. - * @see org.apache.commons.dbutils2.UpdateExecutor#execute() - */ - public int[] execute() throws SQLException { - try { - return getStatement().executeBatch(); - } catch (SQLException e) { - rethrow(e); - } finally { - close(getStatement()); - if (closeConn) { - close(getConnection()); - } - } - - // we get here only if something is thrown - return null; - } - -} +/* + * 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 java.sql.Connection; +import java.sql.SQLException; +import java.sql.Types; + +/** + * This class provides the ability to execute a batch of statements. + * + * It is really just a facade to an array of UpdateExecutors. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ +public class BatchExecutor extends AbstractExecutor<BatchExecutor> { + + private final boolean closeConn; + + /** + * Constructs a BatchExecutor 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. + */ + BatchExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + super(conn, sql); + this.closeConn = closeConnection; + } + + /** + * Binds a parameter name to a value for a given statement. + * @param name the name of the parameter. + * @param value the value to bind to the parameter. + * @return this object. + * @throws SQLException thrown if the statement number does not exist, or any other SQLException. + * @see org.apache.commons.dbutils2.UpdateExecutor#bind(String, Object) + */ + @Override + public BatchExecutor bind(final String name, final Object value) throws SQLException { + return bind(name, value, false); + } + + /** + * Binds null to a parameter. + * Types.VARCHAR is used as the type's parameter. + * This usually works, but fails with some Oracle and MS SQL drivers. + * @param name the name of the parameter. + * @return this execution object to provide the fluent style. + * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. + */ + @Override + public BatchExecutor bindNull(final String name) throws SQLException { + return bindNull(name, Types.VARCHAR, false); + } + + /** + * Binds null to a parameter, specifying the parameter's type. + * @param name the name of the parameter. + * @param sqlType the type of the parameter. + * @return this execution object to provide the fluent style. + * @throws SQLException throw if the parameter is not found, already bound, or there is an issue binding null. + */ + @Override + public BatchExecutor bindNull(final String name, final int sqlType) throws SQLException { + return bindNull(name, sqlType, false); + } + + /** + * Adds the statement to the batch after binding all of the parameters. + * @return this object. + * @throws SQLException if a SQLException is thrown during the addBatch() call. + * @see java.sql.PreparedStatement#addBatch() + */ + public BatchExecutor addBatch() throws SQLException { + try { + getStatement().addBatch(); + clearValueMap(); + } catch (SQLException e) { + rethrow(e); + } + + return this; + } + + /** + * Calls batch after checking the parameters to ensure nothing is null. + * @return an array containing the number of rows updated for each statement. + * @throws SQLException If there are database or parameter errors. + * @see org.apache.commons.dbutils2.UpdateExecutor#execute() + */ + public int[] execute() throws SQLException { + try { + return getStatement().executeBatch(); + } catch (SQLException e) { + rethrow(e); + } finally { + close(getStatement()); + if (closeConn) { + close(getConnection()); + } + } + + // we get here only if something is thrown + return null; + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java b/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java index 0904da8..3b4fb45 100644 --- a/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java +++ b/src/main/java/org/apache/commons/dbutils2/GenerousBeanProcessor.java @@ -1,71 +1,71 @@ -/* - * 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 java.beans.PropertyDescriptor; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.util.Arrays; - - -/** - * Provides generous name matching (e.g. underscore-aware) from DB - * columns to Java Bean properties. - */ -public class GenerousBeanProcessor extends BeanProcessor { - - /** - * Default constructor. - */ - public GenerousBeanProcessor() { - super(); - } - - @Override - protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd, - final PropertyDescriptor[] props) throws SQLException { - - final int cols = rsmd.getColumnCount(); - final int[] columnToProperty = new int[cols + 1]; - Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND); - - for (int col = 1; col <= cols; col++) { - String columnName = rsmd.getColumnLabel(col); - - if (null == columnName || 0 == columnName.length()) { - columnName = rsmd.getColumnName(col); - } - - final String generousColumnName = columnName.replace("_", ""); - - for (int i = 0; i < props.length; i++) { - final String propName = props[i].getName(); - - // see if either the column name, or the generous one matches - if (columnName.equalsIgnoreCase(propName) || - generousColumnName.equalsIgnoreCase(propName)) { - columnToProperty[col] = i; - break; - } - } - } - - return columnToProperty; - } - -} +/* + * 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 java.beans.PropertyDescriptor; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.util.Arrays; + + +/** + * Provides generous name matching (e.g. underscore-aware) from DB + * columns to Java Bean properties. + */ +public class GenerousBeanProcessor extends BeanProcessor { + + /** + * Default constructor. + */ + public GenerousBeanProcessor() { + super(); + } + + @Override + protected int[] mapColumnsToProperties(final ResultSetMetaData rsmd, + final PropertyDescriptor[] props) throws SQLException { + + final int cols = rsmd.getColumnCount(); + final int[] columnToProperty = new int[cols + 1]; + Arrays.fill(columnToProperty, PROPERTY_NOT_FOUND); + + for (int col = 1; col <= cols; col++) { + String columnName = rsmd.getColumnLabel(col); + + if (null == columnName || 0 == columnName.length()) { + columnName = rsmd.getColumnName(col); + } + + final String generousColumnName = columnName.replace("_", ""); + + for (int i = 0; i < props.length; i++) { + final String propName = props[i].getName(); + + // see if either the column name, or the generous one matches + if (columnName.equalsIgnoreCase(propName) || + generousColumnName.equalsIgnoreCase(propName)) { + columnToProperty[col] = i; + break; + } + } + } + + return columnToProperty; + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java b/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java index 9b5cecb..73c1b30 100644 --- a/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/InsertExecutor.java @@ -1,114 +1,114 @@ -/* - * 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 java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; - - -/** - * Fluent class for executing inserts. - * - * @since 2.0 - * @author William Speirs <[email protected]> - */ -public class InsertExecutor extends AbstractExecutor<InsertExecutor> { - - private final boolean closeConn; - - /** - * Constructs an InsertExecutor 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. - */ - InsertExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { - super(conn, sql); - this.closeConn = closeConnection; - } - - /** - * Executes the given INSERT SQL statement. - * - * @param <T> the type returned by the ResultSetHandler. - * @param handler The handler used to create the result object from - * the <code>ResultSet</code> of auto-generated keys. - * - * @return An object generated by the handler. - * @throws SQLException If there are database or parameter errors. - */ - public <T> T execute(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 (closeConn) { - close(getConnection()); - } - throw new SQLException("Null ResultSetHandler"); - } - - try { - // execute the update - getStatement().executeUpdate(); - - // get the result set - final ResultSet resultSet = getStatement().getGeneratedKeys(); - - // run the handler over the results and return them - return handler.handle(resultSet); - } catch (SQLException e) { - this.rethrow(e); - } finally { - close(getStatement()); - if (closeConn) { - close(getConnection()); - } - } - - // we get here only if something is thrown - return null; - } - - /** - * Executes the given INSERT SQL statement. - * @return the number of rows updated. - * @throws SQLException If there are database or parameter errors. - */ - public int execute() throws SQLException { - // throw an exception if there are unmapped parameters - this.throwIfUnmappedParams(); - - try { - // execute the insert - return getStatement().executeUpdate(); - } catch (SQLException e) { - this.rethrow(e); - } finally { - close(getStatement()); - if (closeConn) { - close(getConnection()); - } - } - - return 0; // only get here on an error - } - -} +/* + * 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 java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + + +/** + * Fluent class for executing inserts. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ +public class InsertExecutor extends AbstractExecutor<InsertExecutor> { + + private final boolean closeConn; + + /** + * Constructs an InsertExecutor 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. + */ + InsertExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + super(conn, sql); + this.closeConn = closeConnection; + } + + /** + * Executes the given INSERT SQL statement. + * + * @param <T> the type returned by the ResultSetHandler. + * @param handler The handler used to create the result object from + * the <code>ResultSet</code> of auto-generated keys. + * + * @return An object generated by the handler. + * @throws SQLException If there are database or parameter errors. + */ + public <T> T execute(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 (closeConn) { + close(getConnection()); + } + throw new SQLException("Null ResultSetHandler"); + } + + try { + // execute the update + getStatement().executeUpdate(); + + // get the result set + final ResultSet resultSet = getStatement().getGeneratedKeys(); + + // run the handler over the results and return them + return handler.handle(resultSet); + } catch (SQLException e) { + this.rethrow(e); + } finally { + close(getStatement()); + if (closeConn) { + close(getConnection()); + } + } + + // we get here only if something is thrown + return null; + } + + /** + * Executes the given INSERT SQL statement. + * @return the number of rows updated. + * @throws SQLException If there are database or parameter errors. + */ + public int execute() throws SQLException { + // throw an exception if there are unmapped parameters + this.throwIfUnmappedParams(); + + try { + // execute the insert + return getStatement().executeUpdate(); + } catch (SQLException e) { + this.rethrow(e); + } finally { + close(getStatement()); + if (closeConn) { + close(getConnection()); + } + } + + return 0; // only get here on an error + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java b/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java index 11bd8aa..0b29dc3 100644 --- a/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/QueryExecutor.java @@ -1,89 +1,89 @@ -/* - * 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 java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * Fluent class for executing a query. - * - * @since 2.0 - * @author William Speirs <[email protected]> - */ -class QueryExecutor extends AbstractExecutor<QueryExecutor> { - - private final boolean closeConn; - - /** - * Constructs a QueryExecutor 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. - */ - QueryExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { - super(conn, sql); - this.closeConn = closeConnection; - } - - /** - * Calls query after checking the parameters to ensure nothing is null. - * - * @param handler The handler that converts the results into an object. - * - * @return The results of the query. - * @throws SQLException If there are database or parameter errors. - */ - public <T> T execute(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 (closeConn) { - close(getConnection()); - } - throw new SQLException("Null ResultSetHandler"); - } - - ResultSet resultSet = null; - - try { - // execute the query, wrapping it - resultSet = this.wrap(getStatement().executeQuery()); - // execute the handler - return handler.handle(resultSet); - } catch (SQLException e) { - // rethrow our exception printing more information - this.rethrow(e); - } finally { - try { - close(resultSet); - } finally { - close(getStatement()); - if (closeConn) { - close(getConnection()); - } - } - } - - // we get here only if something is thrown - return null; - } -} +/* + * 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 java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Fluent class for executing a query. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ +class QueryExecutor extends AbstractExecutor<QueryExecutor> { + + private final boolean closeConn; + + /** + * Constructs a QueryExecutor 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. + */ + QueryExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + super(conn, sql); + this.closeConn = closeConnection; + } + + /** + * Calls query after checking the parameters to ensure nothing is null. + * + * @param handler The handler that converts the results into an object. + * + * @return The results of the query. + * @throws SQLException If there are database or parameter errors. + */ + public <T> T execute(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 (closeConn) { + close(getConnection()); + } + throw new SQLException("Null ResultSetHandler"); + } + + ResultSet resultSet = null; + + try { + // execute the query, wrapping it + resultSet = this.wrap(getStatement().executeQuery()); + // execute the handler + return handler.handle(resultSet); + } catch (SQLException e) { + // rethrow our exception printing more information + this.rethrow(e); + } finally { + try { + close(resultSet); + } finally { + close(getStatement()); + if (closeConn) { + close(getConnection()); + } + } + } + + // we get here only if something is thrown + return null; + } +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java b/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java index 882aa65..580736b 100644 --- a/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java +++ b/src/main/java/org/apache/commons/dbutils2/UpdateExecutor.java @@ -1,69 +1,69 @@ -/* - * 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 java.sql.Connection; -import java.sql.SQLException; - -/** - * Fluent class for executing updates. - * - * @since 2.0 - * @author William Speirs <[email protected]> - */ -public class UpdateExecutor extends AbstractExecutor<UpdateExecutor> { - - private final boolean closeConn; - - /** - * Constructs an UpdateExecutor 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. - */ - UpdateExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { - super(conn, sql); - this.closeConn = closeConnection; - } - - /** - * Calls update after checking the parameters to ensure nothing is null. - * @return The number of rows updated. - * @throws SQLException If there are database or parameter errors. - */ - public int execute() throws SQLException { - // throw an exception if there are unmapped parameters - this.throwIfUnmappedParams(); - - try { - return getStatement().executeUpdate(); - } catch (SQLException e) { - this.rethrow(e); - - } finally { - close(getStatement()); - if (closeConn) { - close(getConnection()); - } - } - - // we get here only if something is thrown - return 0; - } - -} +/* + * 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 java.sql.Connection; +import java.sql.SQLException; + +/** + * Fluent class for executing updates. + * + * @since 2.0 + * @author William Speirs <[email protected]> + */ +public class UpdateExecutor extends AbstractExecutor<UpdateExecutor> { + + private final boolean closeConn; + + /** + * Constructs an UpdateExecutor 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. + */ + UpdateExecutor(final Connection conn, final String sql, final boolean closeConnection) throws SQLException { + super(conn, sql); + this.closeConn = closeConnection; + } + + /** + * Calls update after checking the parameters to ensure nothing is null. + * @return The number of rows updated. + * @throws SQLException If there are database or parameter errors. + */ + public int execute() throws SQLException { + // throw an exception if there are unmapped parameters + this.throwIfUnmappedParams(); + + try { + return getStatement().executeUpdate(); + } catch (SQLException e) { + this.rethrow(e); + + } finally { + close(getStatement()); + if (closeConn) { + close(getConnection()); + } + } + + // we get here only if something is thrown + return 0; + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java index 1d7c270..ff8e6d1 100644 --- a/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java +++ b/src/test/java/org/apache/commons/dbutils2/AbstractExecutorTest.java @@ -1,125 +1,125 @@ -/* - * 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.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -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.SQLException; -import java.sql.Types; - -import org.apache.commons.dbutils2.AbstractExecutor; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - - -public class AbstractExecutorTest { - - @SuppressWarnings("rawtypes") // don't care about this in the unit test - private AbstractExecutor executor; - - @Mock private Connection conn; - @Mock private PreparedStatement stmt; - - @Before - public void setup() throws SQLException { - MockitoAnnotations.initMocks(this); - - when(conn.prepareStatement(any(String.class))).thenReturn(stmt); - } - - @SuppressWarnings("rawtypes") - public void createExecutor(String sql) throws SQLException { - executor = new AbstractExecutor(conn, sql) { }; - } - - @Test - public void testGoodSql() throws SQLException { - createExecutor("select * from blah :first = first and :last=last and phone=:phone"); - - verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last and phone=?"); - - executor.bind("first", "first_name") - .bind(":last", "last_name") - .bind("phone", Integer.valueOf(12345)); - - verify(stmt, times(1)).setObject(1, "first_name"); - verify(stmt, times(1)).setObject(2, "last_name"); - verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); - - executor.throwIfUnmappedParams(); - } - - @SuppressWarnings("boxing") // test code - @Test - public void testNoParamsSql() throws SQLException { - createExecutor("select * from blah"); - - verify(conn, times(1)).prepareStatement("select * from blah"); - verify(stmt, times(0)).setObject(any(Integer.class), any(Object.class)); - - executor.throwIfUnmappedParams(); - } - - @Test(expected=SQLException.class) - public void testMissingParamSql() throws SQLException { - createExecutor("select * from blah :first = first and :last=last"); - - verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); - - executor.bind("first", "first_name") - .bind(":last", "last_name") - .bind("phone", Integer.valueOf(12345)); // should throw - - verify(stmt, times(1)).setObject(1, "first_name"); - verify(stmt, times(1)).setObject(2, "last_name"); - verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); - } - - @Test(expected=SQLException.class) - public void testDoubleBind() throws SQLException { - createExecutor("select * from blah :first = first and :last=last"); - - verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); - - executor.bind("first", "first_name") - .bind(":last", "last_name") - .bind(":last", "last_name"); - - verify(stmt, times(1)).setObject(1, "first_name"); - verify(stmt, times(1)).setObject(2, "last_name"); - } - - @Test - public void testNullBind() throws SQLException { - createExecutor("select * from blah :first = first and :last=last"); - - verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); - - executor.bindNull("first") - .bindNull(":last", Types.NULL); - - verify(stmt, times(1)).setNull(1, Types.VARCHAR); - verify(stmt, times(1)).setNull(2, Types.NULL); - } -} +/* + * 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.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +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.SQLException; +import java.sql.Types; + +import org.apache.commons.dbutils2.AbstractExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class AbstractExecutorTest { + + @SuppressWarnings("rawtypes") // don't care about this in the unit test + private AbstractExecutor executor; + + @Mock private Connection conn; + @Mock private PreparedStatement stmt; + + @Before + public void setup() throws SQLException { + MockitoAnnotations.initMocks(this); + + when(conn.prepareStatement(any(String.class))).thenReturn(stmt); + } + + @SuppressWarnings("rawtypes") + public void createExecutor(String sql) throws SQLException { + executor = new AbstractExecutor(conn, sql) { }; + } + + @Test + public void testGoodSql() throws SQLException { + createExecutor("select * from blah :first = first and :last=last and phone=:phone"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last and phone=?"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind("phone", Integer.valueOf(12345)); + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); + + executor.throwIfUnmappedParams(); + } + + @SuppressWarnings("boxing") // test code + @Test + public void testNoParamsSql() throws SQLException { + createExecutor("select * from blah"); + + verify(conn, times(1)).prepareStatement("select * from blah"); + verify(stmt, times(0)).setObject(any(Integer.class), any(Object.class)); + + executor.throwIfUnmappedParams(); + } + + @Test(expected=SQLException.class) + public void testMissingParamSql() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind("phone", Integer.valueOf(12345)); // should throw + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345))); + } + + @Test(expected=SQLException.class) + public void testDoubleBind() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bind("first", "first_name") + .bind(":last", "last_name") + .bind(":last", "last_name"); + + verify(stmt, times(1)).setObject(1, "first_name"); + verify(stmt, times(1)).setObject(2, "last_name"); + } + + @Test + public void testNullBind() throws SQLException { + createExecutor("select * from blah :first = first and :last=last"); + + verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last"); + + executor.bindNull("first") + .bindNull(":last", Types.NULL); + + verify(stmt, times(1)).setNull(1, Types.VARCHAR); + verify(stmt, times(1)).setNull(2, Types.NULL); + } +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java b/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java index a049bc7..1ad3cd2 100644 --- a/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java +++ b/src/test/java/org/apache/commons/dbutils2/BatchExecutorTest.java @@ -1,69 +1,69 @@ -/* - * 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.*; -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.SQLException; - -import org.apache.commons.dbutils2.BatchExecutor; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - - -public class BatchExecutorTest { - - private BatchExecutor executor; - - @Mock private Connection conn; - @Mock private PreparedStatement stmt; - - @Before - public void setup() throws SQLException { - MockitoAnnotations.initMocks(this); - - when(conn.prepareStatement(any(String.class))).thenReturn(stmt); - when(stmt.executeBatch()).thenReturn(new int[] { 2, 3, 4 }); - } - - protected void createExecutor(String sql) throws Exception { - executor = new BatchExecutor(conn, sql, true); - } - - @Test - public void testGoodSQL() throws Exception { - createExecutor("insert into blah"); - - executor.addBatch(); - int[] ret = executor.execute(); - - assertEquals(3, ret.length); - assertEquals(2, ret[0]); - assertEquals(3, ret[1]); - assertEquals(4, ret[2]); - verify(conn, times(1)).close(); - verify(stmt, times(1)).close(); - } - -} +/* + * 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.*; +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.SQLException; + +import org.apache.commons.dbutils2.BatchExecutor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class BatchExecutorTest { + + private BatchExecutor executor; + + @Mock private Connection conn; + @Mock private PreparedStatement stmt; + + @Before + public void setup() throws SQLException { + MockitoAnnotations.initMocks(this); + + when(conn.prepareStatement(any(String.class))).thenReturn(stmt); + when(stmt.executeBatch()).thenReturn(new int[] { 2, 3, 4 }); + } + + protected void createExecutor(String sql) throws Exception { + executor = new BatchExecutor(conn, sql, true); + } + + @Test + public void testGoodSQL() throws Exception { + createExecutor("insert into blah"); + + executor.addBatch(); + int[] ret = executor.execute(); + + assertEquals(3, ret.length); + assertEquals(2, ret[0]); + assertEquals(3, ret[1]); + assertEquals(4, ret[2]); + verify(conn, times(1)).close(); + verify(stmt, times(1)).close(); + } + +} http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/d2ce0836/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java b/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java index c31bcdd..4b96e39 100644 --- a/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java +++ b/src/test/java/org/apache/commons/dbutils2/GenerousBeanProcessorTest.java @@ -1,116 +1,116 @@ -/* - * 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.Mockito.when; -import java.beans.PropertyDescriptor; -import java.sql.ResultSetMetaData; - -import org.apache.commons.dbutils2.GenerousBeanProcessor; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - - -@SuppressWarnings("boxing") // test code -public class GenerousBeanProcessorTest { - - GenerousBeanProcessor processor = new GenerousBeanProcessor(); - @Mock ResultSetMetaData metaData; - PropertyDescriptor[] propDescriptors; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - propDescriptors = new PropertyDescriptor[3]; - - propDescriptors[0] = new PropertyDescriptor("one", TestBean.class); - propDescriptors[1] = new PropertyDescriptor("two", TestBean.class); - propDescriptors[2] = new PropertyDescriptor("three", TestBean.class); - } - - @Test - public void testMapColumnsToPropertiesWithOutUnderscores() throws Exception { - when(metaData.getColumnCount()).thenReturn(3); - - when(metaData.getColumnLabel(1)).thenReturn("three"); - when(metaData.getColumnLabel(2)).thenReturn("one"); - when(metaData.getColumnLabel(3)).thenReturn("two"); - - int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors); - - assertNotNull(ret); - assertEquals(4, ret.length); - assertEquals(-1, ret[0]); - assertEquals(2, ret[1]); - assertEquals(0, ret[2]); - assertEquals(1, ret[3]); - } - - @Test - public void testMapColumnsToPropertiesWithUnderscores() throws Exception { - when(metaData.getColumnCount()).thenReturn(3); - - when(metaData.getColumnLabel(1)).thenReturn("t_h_r_e_e"); - when(metaData.getColumnLabel(2)).thenReturn("o_n_e"); - when(metaData.getColumnLabel(3)).thenReturn("t_w_o"); - - int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors); - - assertNotNull(ret); - assertEquals(4, ret.length); - assertEquals(-1, ret[0]); - assertEquals(2, ret[1]); - assertEquals(0, ret[2]); - assertEquals(1, ret[3]); - } - - static class TestBean { - private String one; - private int two; - private long three; - - public String getOne() { - return one; - } - - public void setOne(String one) { - this.one = one; - } - - public int getTwo() { - return two; - } - - public void setTwo(int two) { - this.two = two; - } - - public long getThree() { - return three; - } - - public void setThree(long three) { - this.three = three; - } - } - -} +/* + * 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.Mockito.when; +import java.beans.PropertyDescriptor; +import java.sql.ResultSetMetaData; + +import org.apache.commons.dbutils2.GenerousBeanProcessor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +@SuppressWarnings("boxing") // test code +public class GenerousBeanProcessorTest { + + GenerousBeanProcessor processor = new GenerousBeanProcessor(); + @Mock ResultSetMetaData metaData; + PropertyDescriptor[] propDescriptors; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + propDescriptors = new PropertyDescriptor[3]; + + propDescriptors[0] = new PropertyDescriptor("one", TestBean.class); + propDescriptors[1] = new PropertyDescriptor("two", TestBean.class); + propDescriptors[2] = new PropertyDescriptor("three", TestBean.class); + } + + @Test + public void testMapColumnsToPropertiesWithOutUnderscores() throws Exception { + when(metaData.getColumnCount()).thenReturn(3); + + when(metaData.getColumnLabel(1)).thenReturn("three"); + when(metaData.getColumnLabel(2)).thenReturn("one"); + when(metaData.getColumnLabel(3)).thenReturn("two"); + + int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors); + + assertNotNull(ret); + assertEquals(4, ret.length); + assertEquals(-1, ret[0]); + assertEquals(2, ret[1]); + assertEquals(0, ret[2]); + assertEquals(1, ret[3]); + } + + @Test + public void testMapColumnsToPropertiesWithUnderscores() throws Exception { + when(metaData.getColumnCount()).thenReturn(3); + + when(metaData.getColumnLabel(1)).thenReturn("t_h_r_e_e"); + when(metaData.getColumnLabel(2)).thenReturn("o_n_e"); + when(metaData.getColumnLabel(3)).thenReturn("t_w_o"); + + int[] ret = processor.mapColumnsToProperties(metaData, propDescriptors); + + assertNotNull(ret); + assertEquals(4, ret.length); + assertEquals(-1, ret[0]); + assertEquals(2, ret[1]); + assertEquals(0, ret[2]); + assertEquals(1, ret[3]); + } + + static class TestBean { + private String one; + private int two; + private long three; + + public String getOne() { + return one; + } + + public void setOne(String one) { + this.one = one; + } + + public int getTwo() { + return two; + } + + public void setTwo(int two) { + this.two = two; + } + + public long getThree() { + return three; + } + + public void setThree(long three) { + this.three = three; + } + } + +}
