cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-28 Thread dgraham
dgraham 2003/10/28 16:49:56

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Added a protected prepareStatement() method so
  subclasses can override PreparedStatement initialization.
  For example, they might want to call:
  
  conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)
  
  instead of the standard initialization.
  
  Revision  ChangesPath
  1.13  +25 -5 
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- QueryRunner.java  24 Oct 2003 04:25:25 -  1.12
  +++ QueryRunner.java  29 Oct 2003 00:49:56 -  1.13
  @@ -139,6 +139,26 @@
   }
   
   /**
  + * Factory method that creates and initializes a 
  + * codePreparedStatement/code object for the given SQL.  
  + * codeQueryRunner/code methods always call this method to prepare 
  + * statements for them.  Subclasses can override this method to provide 
  + * special PreparedStatement configuration if needed.  This implementation
  + * simply calls codeconn.prepareStatement(sql)/code.
  + *  
  + * @param conn The codeConnection/code used to create the 
  + * codePreparedStatement/code
  + * @param sql The SQL statement to prepare.
  + * @return An initialized codePreparedStatement/code.
  + * @throws SQLException
  + */
  +protected PreparedStatement prepareStatement(Connection conn, String sql)
  +throws SQLException {
  +
  +return conn.prepareStatement(sql);
  +}
  +
  +/**
* Execute an SQL SELECT query with a single replacement parameter.  The
* caller is responsible for connection cleanup.
* 
  @@ -182,7 +202,7 @@
   Object result = null;
   
   try {
  -stmt = conn.prepareStatement(sql);
  +stmt = this.prepareStatement(conn, sql);
   this.fillStatement(stmt, params);
   
   rs = this.wrap(stmt.executeQuery());
  @@ -367,7 +387,7 @@
   int rows = 0;
   
   try {
  -stmt = conn.prepareStatement(sql);
  +stmt = this.prepareStatement(conn, sql);
   this.fillStatement(stmt, params);
   
   rows = stmt.executeUpdate();
  
  
  

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-23 Thread dgraham
dgraham 2003/10/23 21:10:21

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Removed javadoc reference to ResultSetMetaDataHandler.
  
  Revision  ChangesPath
  1.11  +3 -4  
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- QueryRunner.java  23 Oct 2003 00:28:38 -  1.10
  +++ QueryRunner.java  24 Oct 2003 04:10:21 -  1.11
  @@ -75,7 +75,6 @@
* codeResultSet/codes.  This class is thread safe.
* 
* @see ResultSetHandler
  - * @see ResultSetMetaDataHandler
* 
* @author Henri Yandell
* @author Juozas Baliuka
  
  
  

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-22 Thread dgraham
dgraham 2003/10/22 16:25:02

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Removed references to ResultSetMetaDataHandler.
  
  Revision  ChangesPath
  1.8   +382 -520  
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- QueryRunner.java  22 Oct 2003 02:10:23 -  1.7
  +++ QueryRunner.java  22 Oct 2003 23:25:01 -  1.8
  @@ -66,9 +66,7 @@
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Types;
  -import java.util.ArrayList;
   import java.util.Arrays;
  -import java.util.List;
   
   import javax.sql.DataSource;
   
  @@ -86,520 +84,384 @@
*/
   public class QueryRunner {
   
  -/**
  - * The DataSource to retrieve connections from.
  - */
  -protected DataSource ds = null;
  -
  -/**
  - * Constructor for QueryRunner.
  - */
  -public QueryRunner() {
  -super();
  -}
  -
  -/**
  - * Constructor for QueryRunner.  Methods that do not take a 
  - * codeConnection/code parameter will retrieve connections from this
  - * codeDataSource/code.
  - * 
  - * @param ds The codeDataSource/code to retrieve connections from.
  - */
  -public QueryRunner(DataSource ds) {
  -super();
  -this.ds = ds;
  -}
  -
  -/**
  - * Execute any SQL statement and return all the results in an 
  - * codeObject[]/code.  Callers are responsible for connection cleanup.
  - * 
  - * @param conn The connection to execute the query with.
  - * @param sql The SQL statement.
  - * @param params SQL replacement parameters; codenull/code if no 
  - * parameters are required.
  - * @param rsh The handler to generate result objects with
  - * @param rsmdh Handles the meta data.
  - * @param userObject Object to pass to the handler.
  - * @return An array filled with result objects from the handler for each
  - * codeResultSet/code returned by the query; codenull/code if the
  - * statment was an UPDATE.
  - * @throws SQLException
  - */
  -public Object[] execute(
  -Connection conn,
  -String sql,
  -Object[] params,
  -ResultSetHandler rsh,
  -ResultSetMetaDataHandler rsmdh,
  -Object userObject)
  -throws SQLException {
  -
  -PreparedStatement stmt = null;
  -ResultSet rs = null;
  -
  -List results = new ArrayList();
  -
  -try {
  -stmt = conn.prepareStatement(sql);
  -this.fillStatement(stmt, params);
  -
  -if (!stmt.execute()) {
  -return null;
  -}
  -
  -do {
  -rs = stmt.getResultSet();
  -if (rs != null) {
  -rs = this.wrap(rs);
  -
  -if (rsmdh != null) {
  -rsmdh.handle(rs.getMetaData());
  -}
  -
  -results.add(rsh.handle(rs, params, userObject));
  -}
  -
  -} while (stmt.getMoreResults());
  -
  -} catch (SQLException e) {
  -rethrow(e, sql, params);
  -
  -} finally {
  -DbUtils.closeQuietly(rs);
  -DbUtils.closeQuietly(stmt);
  -}
  -
  -Object[] res = new Object[results.size()];
  -return results.toArray(res);
  -}
  -
  -/**
  - * Fill the codePreparedStatement/code replacement parameters with 
  - * the given objects.
  - * @param stmt
  - * @param params Query replacement parameters; codenull/code is a valid
  - * value to pass in.
  - * @throws SQLException
  - */
  -protected void fillStatement(PreparedStatement stmt, Object[] params)
  -throws SQLException {
  -
  -if (params == null) {
  -return;
  -}
  -
  -for (int i = 0; i  params.length; i++) {
  -if (params[i] != null) {
  -stmt.setObject(i + 1, params[i]);
  -} else {
  -stmt.setNull(i + 1, Types.OTHER);
  -}
  -}
  -}
  -
  -/**
  - * Returns the codeDataSource/code this runner is using.
  - */
  -public DataSource getDataSource() {
  -return this.ds;
  -}
  -
  -/**
  - * Execute an SQL SELECT query with a single replacement parameter.  The
  - * caller is responsible for connection cleanup.
  - * 
  - * @param conn The connection to execute the query in.
  - * @param sql The query to execute.
  - * @param param The replacement parameter.
  - * @param rsh The 

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-22 Thread dgraham
dgraham 2003/10/22 17:28:38

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  javadoc fixes, cast null parameters to make sure we call the
  correct method, always call DbUtils.closeQuietly()
  instead of DbUtils.close() in finally blocks so we don't
  lose a more interesting exception that occurred in the
  try block.
  
  Revision  ChangesPath
  1.10  +28 -19
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- QueryRunner.java  22 Oct 2003 23:33:25 -  1.9
  +++ QueryRunner.java  23 Oct 2003 00:28:38 -  1.10
  @@ -214,7 +214,7 @@
public Object query(Connection conn, String sql, ResultSetHandler rsh)
throws SQLException {
   
  - return this.query(conn, sql, null, rsh);
  + return this.query(conn, sql, (Object[]) null, rsh);
}
   
/**
  @@ -223,9 +223,10 @@
 * codeDataSource/code set in the constructor.
 * 
 * @param sql The SQL statement to execute.
  +  * @param param The replacement parameter.
 * @param rsh The handler used to create the result object from 
 * the codeResultSet/code.
  -  * @param param The replacement parameter.
  +  * 
 * @return An object generated by the handler.
 * @throws SQLException
 */
  @@ -241,10 +242,12 @@
 * codeDataSource/code set in the constructor.
 * 
 * @param sql The SQL statement to execute.
  -  * @param params Initialize the PreparedStatement's IN parameters with this
  -  * array.
  +  * @param params Initialize the PreparedStatement's IN parameters with 
  +  * this array.
  +  * 
 * @param rsh The handler used to create the result object from 
 * the codeResultSet/code.
  +  * 
 * @return An object generated by the handler.
 * @throws SQLException
 */
  @@ -259,7 +262,7 @@
result = this.query(conn, sql, params, rsh);
   
} finally {
  - DbUtils.close(conn);
  + DbUtils.closeQuietly(conn);
}
   
return result;
  @@ -273,20 +276,24 @@
 * @param sql The SQL statement to execute.
 * @param rsh The handler used to create the result object from 
 * the codeResultSet/code.
  +  * 
 * @return An object generated by the handler.
 * @throws SQLException
 */
public Object query(String sql, ResultSetHandler rsh) throws SQLException {
  - return this.query(sql, null, rsh);
  + return this.query(sql, (Object[]) null, rsh);
}
   
/**
 * 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. 
  +  * 
 * @param sql The query that was executing when the exception happened.
 * @param params The query replacement paramaters; codenull/code is a 
 * valid value to pass in.
  +  * 
 * @throws SQLException
 */
protected void rethrow(SQLException cause, String sql, Object[] params)
  @@ -309,6 +316,7 @@
 * database connections from.  This should be called after creating a
 * runner with the default constructor if you intend to use the
 * execute methods without passing in a codeConnection/code.
  +  * 
 * @param dataSource The DataSource to use.
 */
public void setDataSource(DataSource dataSource) {
  @@ -325,7 +333,7 @@
 * @throws SQLException
 */
public int update(Connection conn, String sql) throws SQLException {
  - return this.update(conn, sql, null);
  + return this.update(conn, sql, (Object[]) null);
}
   
/**
  @@ -346,6 +354,7 @@
   
/**
 * Execute an SQL INSERT, UPDATE, or DELETE query.
  +  * 
 * @param conn The connection to use to run the query.
 * @param sql The SQL to execute.
 * @param params The query replacement parameters.
  @@ -355,12 +364,13 @@
public int update(Connection conn, String sql, Object[] params)
throws SQLException {
   
  - PreparedStatement stmt = conn.prepareStatement(sql);
  - this.fillStatement(stmt, params);
  -
  + PreparedStatement stmt = null;
int rows = 0;
   
try {
  + stmt = conn.prepareStatement(sql);
  + this.fillStatement(stmt, params);

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-20 Thread dgraham
dgraham 2003/10/20 17:12:58

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Added javadoc and more versions of query() and update().  There are
  now methods that take 0, 1, or an array of replacement parameters.
  
  Revision  ChangesPath
  1.6   +203 -42   
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- QueryRunner.java  20 Oct 2003 02:10:21 -  1.5
  +++ QueryRunner.java  21 Oct 2003 00:12:58 -  1.6
  @@ -66,7 +66,9 @@
   import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Types;
  +import java.util.ArrayList;
   import java.util.Arrays;
  +import java.util.List;
   
   import javax.sql.DataSource;
   
  @@ -97,18 +99,36 @@
   }
   
   /**
  - * Constructor for QueryRunner.
  - * @param ds The codeDataSource/code to retrieve connections
  - * from.
  + * Constructor for QueryRunner.  Methods that do not take a 
  + * codeConnection/code parameter will retrieve connections from this
  + * codeDataSource/code.
  + * 
  + * @param ds The codeDataSource/code to retrieve connections from.
*/
   public QueryRunner(DataSource ds) {
   super();
   this.ds = ds;
   }
   
  -public boolean execute(
  +/**
  + * Execute any SQL statement and return all the results in an 
  + * codeObject[]/code.  Callers are responsible for connection cleanup.
  + * 
  + * @param conn The connection to execute the query with.
  + * @param sql The SQL statement.
  + * @param params SQL replacement parameters; codenull/code if no 
  + * parameters are required.
  + * @param rsh The handler to generate result objects with
  + * @param rsmdh Handles the meta data.
  + * @param userObject Object to pass to the handler.
  + * @return An array filled with result objects from the handler for each
  + * codeResultSet/code returned by the query; codenull/code if the
  + * statment was an UPDATE.
  + * @throws SQLException
  + */
  +public Object[] execute(
   Connection conn,
  -String query,
  +String sql,
   Object[] params,
   ResultSetHandler rsh,
   ResultSetMetaDataHandler rsmdh,
  @@ -118,37 +138,40 @@
   PreparedStatement stmt = null;
   ResultSet rs = null;
   
  +List results = new ArrayList();
  +
   try {
  -stmt = conn.prepareStatement(query);
  -fillStatement(stmt, params);
  +stmt = conn.prepareStatement(sql);
  +this.fillStatement(stmt, params);
   
   if (!stmt.execute()) {
  -return false;
  +return null;
   }
   
   do {
   rs = stmt.getResultSet();
   if (rs != null) {
   rs = this.wrap(rs);
  -
  +
   if (rsmdh != null) {
   rsmdh.handle(rs.getMetaData());
   }
   
  -rsh.handle(rs, params, userObject);
  +results.add(rsh.handle(rs, params, userObject));
   }
   
   } while (stmt.getMoreResults());
   
   } catch (SQLException e) {
  -rethrow(e, query, params);
  +rethrow(e, sql, params);
   
   } finally {
   DbUtils.closeQuietly(rs);
   DbUtils.closeQuietly(stmt);
   }
   
  -return true;
  +Object[] res = new Object[results.size()];
  +return results.toArray(res);
   }
   
   /**
  @@ -183,35 +206,86 @@
   }
   
   /**
  - * Creates a PreparedStatement using the String and Object array,
  - * executes this using the Connection, and returns the results
  - * inside an Iterator.
  - * Null values in the Object array will be passed to the driver.
  + * Execute an SQL SELECT query with a single replacement parameter.  The
  + * caller is responsible for connection cleanup.
  + * 
  + * @param conn The connection to execute the query in.
  + * @param sql The query to execute.
  + * @param param The replacement parameter.
  + * @param rsh The handler that converts the results into an object.
  + * @return The object returned by the handler.
  + * @throws SQLException
*/
   public Object query(
   Connection conn,
  -String query,
  +String sql,
  +Object param,
  +ResultSetHandler rsh)
  +throws SQLException {
  +
  +return this.query(conn, sql, new 

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java DbUtils.java

2003-10-19 Thread dgraham
dgraham 2003/10/19 12:21:30

  Modified:dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Added:   dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Moved execute methods from DbUtils to new QueryRunner class as 
  non-static methods to allow subclass customization and to simplify 
  DbUtils API.  
  
  Added rollback() method to DbUtils.
  
  Added initial support for DataSources in QueryRunner.
  
  Revision  ChangesPath
  1.39  +73 -283   
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java
  
  Index: DbUtils.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbUtils.java,v
  retrieving revision 1.38
  retrieving revision 1.39
  diff -u -r1.38 -r1.39
  --- DbUtils.java  19 Oct 2003 18:30:24 -  1.38
  +++ DbUtils.java  19 Oct 2003 19:21:30 -  1.39
  @@ -63,14 +63,9 @@
   
   import java.io.PrintWriter;
   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.Arrays;
  -import java.util.Iterator;
  -import java.util.List;
   
   /**
* A collection of JDBC helper methods.
  @@ -81,252 +76,6 @@
* @author David Graham
*/
   public final class DbUtils {
  -
  -public static boolean execute(
  -Connection conn,
  -String query,
  -Object[] vals,
  -ResultSetHandler rsh,
  -ResultSetMetaDataHandler rsmdh,
  -Object userObject)
  -throws SQLException {
  -
  -PreparedStatement stmt = null;
  -ResultSet rs = null;
  -
  -try {
  -stmt = conn.prepareStatement(query);
  -fillStatement(stmt, vals);
  -
  -if (!stmt.execute()) {
  -return false;
  -}
  -
  -do {
  -rs = stmt.getResultSet();
  -if (rs != null) {
  -if (rsmdh != null) {
  -rsmdh.handle(rs.getMetaData());
  -}
  -
  -rsh.handle(rs, vals, userObject);
  -}
  -
  -} while (stmt.getMoreResults());
  -
  -} catch (SQLException e) {
  -rethrow(e, query, vals);
  -
  -} finally {
  -closeQuietly(rs);
  -closeQuietly(stmt);
  -}
  -
  -return true;
  -}
  -
  -public static Object executeQuery(
  -Connection conn,
  -String query,
  -Object[] vals,
  -ResultSetHandler rsh,
  -ResultSetMetaDataHandler rsmdh)
  -throws SQLException {
  -
  -return executeQuery(conn, query, vals, rsh, rsmdh, null);
  -}
  -
  -public static Object executeQuery(
  -Connection conn,
  -String query,
  -Object[] vals,
  -ResultSetHandler rsh,
  -ResultSetMetaDataHandler rsmdh,
  -Object userObject)
  -throws SQLException {
  -
  -PreparedStatement stmt = null;
  -ResultSet rs = null;
  -Object result = null;
  -
  -try {
  -stmt = conn.prepareStatement(query);
  -fillStatement(stmt, vals);
  -
  -rs = stmt.executeQuery();
  -
  -if (rsmdh != null) {
  -rsmdh.handle(rs.getMetaData());
  -}
  -
  -result = rsh.handle(rs, vals, userObject);
  -
  -} catch (SQLException sqle) {
  -rethrow(sqle, query, vals);
  -} finally {
  -closeQuietly(rs);
  -closeQuietly(stmt);
  -}
  -
  -return result;
  -}
  -
  -/**
  - * 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. 
  - * @param sql The query that was executing when the exception happened.
  - * @param vals The query replacement paramaters.
  - * @throws SQLException
  - */
  -private static void rethrow(SQLException cause, String sql, Object[] vals)
  -throws SQLException {
  -
  -StringBuffer msg = new StringBuffer(cause.getMessage() +  in query  + 
sql);
  -if (vals != null) {
  -msg.append(Arrays.asList(vals).toString());
  -}
  -
  -SQLException newsqle = new SQLException(msg.toString());
  -newsqle.setNextException(cause);
  -
  -throw newsqle;
  -}
  -
  -/**
  - * Fill the codePreparedStatement/code replacement parameters with 
  - * the given objects.
  - * @param stmt
  - * @param params
  - * @throws SQLException
  - */
  -private static void 

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-19 Thread dgraham
dgraham 2003/10/19 12:22:33

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Fixed javadoc.
  
  Revision  ChangesPath
  1.2   +5 -7  
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- QueryRunner.java  19 Oct 2003 19:21:30 -  1.1
  +++ QueryRunner.java  19 Oct 2003 19:22:33 -  1.2
  @@ -347,11 +347,9 @@
* statement is executed in it's own transaction that will be committed or 
* rolled back depending on any SQLExceptions thrown.
* @param sql The SQL statement to execute.
  - * @param preparer Initializes the PreparedStatement's IN (ie. '?') 
  + * @param params Initializes the PreparedStatement's IN (ie. '?') 
* parameters.
  - * @param prepareObject An object to pass to the preparer to setup the 
  - * PreparedStatement.
  - * @throws MapperException
  + * @throws SQLException
* @return The number of rows updated.
*/
   public int executeUpdate(String sql, Object[] params) throws SQLException {
  
  
  

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-19 Thread dgraham
dgraham 2003/10/19 12:38:51

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Added more execute methods.
  
  Revision  ChangesPath
  1.3   +105 -65   
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- QueryRunner.java  19 Oct 2003 19:22:33 -  1.2
  +++ QueryRunner.java  19 Oct 2003 19:38:51 -  1.3
  @@ -72,7 +72,7 @@
   
   /**
* Executes SQL queries with pluggable strategies for handling 
  - * codeResultSet/codes.
  + * codeResultSet/codes.  This class is thread safe.
* 
* @see ResultSetHandler
* @see ResultSetMetaDataHandler
  @@ -201,8 +201,9 @@
   
   result = rsh.handle(rs, params, userObject);
   
  -} catch (SQLException sqle) {
  -this.rethrow(sqle, query, params);
  +} catch (SQLException e) {
  +this.rethrow(e, query, params);
  +
   } finally {
   DbUtils.closeQuietly(rs);
   DbUtils.closeQuietly(stmt);
  @@ -222,6 +223,59 @@
   }
   
   /**
  + * Executes the given SELECT SQL query and returns a result object.
  + * @param sql The SQL statement to execute.
  + * @param params Initialize the PreparedStatement's IN parameters with this
  + * array.
  + * @param rsh The handler used to create the result object from 
  + * the codeResultSet/code.
  + * @return An object generated by the handler.
  + * @throws SQLException
  + */
  +public Object executeQuery(String sql, Object[] params, ResultSetHandler rsh)
  +throws SQLException {
  +
  +Connection conn = this.ds.getConnection();
  +
  +Object result = null;
  +
  +try {
  +result = this.executeQuery(conn, sql, params, rsh, null, null);
  +
  +} finally {
  +DbUtils.close(conn);
  +}
  +
  +return result;
  +}
  +
  +/**
  + * Executes the given SELECT SQL without any replacement parameters.
  + * @param sql The SQL statement to execute.
  + * @param rsh The handler used to create the result object from 
  + * the codeResultSet/code.
  + * @return An object generated by the handler.
  + * @throws SQLException
  + */
  +public Object executeQuery(String sql, ResultSetHandler rsh)
  +throws SQLException {
  +
  +return this.executeQuery(sql, null, rsh);
  +}
  +
  +/**
  + * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
  + * parameters.
  + * @param conn The connection to use to run the query.
  + * @param query The SQL to execute.
  + * @return The number of rows updated.
  + * @throws SQLException
  + */
  +public int executeUpdate(Connection conn, String query) throws SQLException {
  +return this.executeUpdate(conn, query, null);
  +}
  +
  +/**
* Execute an SQL INSERT, UPDATE, or DELETE query.
* @param conn The connection to use to run the query.
* @param query The SQL to execute.
  @@ -251,6 +305,51 @@
   }
   
   /**
  + * Executes the given INSERT, UPDATE, or DELETE SQL statement without
  + * any replacement parameters.  The statement is executed in it's own 
  + * transaction that will be committed or rolled back depending on any 
  + * SQLExceptions thrown.
  + * @param sql The SQL statement to execute.
  + * @throws SQLException
  + * @return The number of rows updated.
  + */
  +public int executeUpdate(String sql) throws SQLException {
  +return this.executeUpdate(sql, null);
  +}
  +
  +/**
  + * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The 
  + * statement is executed in it's own transaction that will be committed or 
  + * rolled back depending on any SQLExceptions thrown.
  + * @param sql The SQL statement to execute.
  + * @param params Initializes the PreparedStatement's IN (ie. '?') 
  + * parameters.
  + * @throws SQLException
  + * @return The number of rows updated.
  + */
  +public int executeUpdate(String sql, Object[] params) throws SQLException {
  +
  +Connection conn = this.ds.getConnection();
  +
  +int rows = 0;
  +
  +try {
  +conn.setAutoCommit(false); // single transaction.
  +rows = this.executeUpdate(conn, sql, params);
  +conn.commit();
  +
  +} catch (SQLException e) {
  +DbUtils.rollback(conn);
  +throw e;
  +
  +} finally {
  +DbUtils.close(conn);
  +}
  +
  +return rows;
  +}
  +
  +/**
   

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-19 Thread dgraham
dgraham 2003/10/19 12:47:51

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Renamed executeUpdate() methods to update() and executeQuery()
  to query().
  
  Revision  ChangesPath
  1.4   +21 -21
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- QueryRunner.java  19 Oct 2003 19:38:51 -  1.3
  +++ QueryRunner.java  19 Oct 2003 19:47:51 -  1.4
  @@ -155,17 +155,17 @@
* inside an Iterator.
* Null values in the Object array will be passed to the driver.
*/
  -public Object executeQuery(
  +public Object query(
   Connection conn,
   String query,
   Object[] params,
   ResultSetHandler rsh)
   throws SQLException {
   
  -return this.executeQuery(conn, query, params, rsh, null, null);
  +return this.query(conn, query, params, rsh, null, null);
   }
   
  -public Object executeQuery(
  +public Object query(
   Connection conn,
   String query,
   Object[] params,
  @@ -173,10 +173,10 @@
   ResultSetMetaDataHandler rsmdh)
   throws SQLException {
   
  -return this.executeQuery(conn, query, params, rsh, rsmdh, null);
  +return this.query(conn, query, params, rsh, rsmdh, null);
   }
   
  -public Object executeQuery(
  +public Object query(
   Connection conn,
   String query,
   Object[] params,
  @@ -212,14 +212,14 @@
   return result;
   }
   
  -public Object executeQuery(
  +public Object query(
   Connection conn,
   String query,
   Object[] params,
   ResultSetMetaDataHandler rsmdh)
   throws SQLException {
   
  -return this.executeQuery(conn, query, params, null, rsmdh, null);
  +return this.query(conn, query, params, null, rsmdh, null);
   }
   
   /**
  @@ -232,7 +232,7 @@
* @return An object generated by the handler.
* @throws SQLException
*/
  -public Object executeQuery(String sql, Object[] params, ResultSetHandler rsh)
  +public Object query(String sql, Object[] params, ResultSetHandler rsh)
   throws SQLException {
   
   Connection conn = this.ds.getConnection();
  @@ -240,7 +240,7 @@
   Object result = null;
   
   try {
  -result = this.executeQuery(conn, sql, params, rsh, null, null);
  +result = this.query(conn, sql, params, rsh, null, null);
   
   } finally {
   DbUtils.close(conn);
  @@ -257,10 +257,10 @@
* @return An object generated by the handler.
* @throws SQLException
*/
  -public Object executeQuery(String sql, ResultSetHandler rsh)
  +public Object query(String sql, ResultSetHandler rsh)
   throws SQLException {
   
  -return this.executeQuery(sql, null, rsh);
  +return this.query(sql, null, rsh);
   }
   
   /**
  @@ -271,8 +271,8 @@
* @return The number of rows updated.
* @throws SQLException
*/
  -public int executeUpdate(Connection conn, String query) throws SQLException {
  -return this.executeUpdate(conn, query, null);
  +public int update(Connection conn, String query) throws SQLException {
  +return this.update(conn, query, null);
   }
   
   /**
  @@ -283,7 +283,7 @@
* @return The number of rows updated.
* @throws SQLException
*/
  -public int executeUpdate(Connection conn, String query, Object[] params)
  +public int update(Connection conn, String query, Object[] params)
   throws SQLException {
   
   PreparedStatement stmt = conn.prepareStatement(query);
  @@ -313,8 +313,8 @@
* @throws SQLException
* @return The number of rows updated.
*/
  -public int executeUpdate(String sql) throws SQLException {
  -return this.executeUpdate(sql, null);
  +public int update(String sql) throws SQLException {
  +return this.update(sql, null);
   }
   
   /**
  @@ -327,7 +327,7 @@
* @throws SQLException
* @return The number of rows updated.
*/
  -public int executeUpdate(String sql, Object[] params) throws SQLException {
  +public int update(String sql, Object[] params) throws SQLException {
   
   Connection conn = this.ds.getConnection();
   
  @@ -335,7 +335,7 @@
   
   try {
   conn.setAutoCommit(false); // single transaction.
  -rows = this.executeUpdate(conn, sql, params);
  +rows = this.update(conn, sql, params);
 

cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils QueryRunner.java

2003-10-19 Thread dgraham
dgraham 2003/10/19 19:10:21

  Modified:dbutils/src/java/org/apache/commons/dbutils QueryRunner.java
  Log:
  Added wrap(ResultSet) method that allows subclasses to decorate 
  a ResultSet before processing it.  Clients can override this method to
  wrap a StringTrimmedResultSet or SqlNullCheckedResultSet around
  the results.
  
  Revision  ChangesPath
  1.5   +84 -70
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java
  
  Index: QueryRunner.java
  ===
  RCS file: 
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/QueryRunner.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- QueryRunner.java  19 Oct 2003 19:47:51 -  1.4
  +++ QueryRunner.java  20 Oct 2003 02:10:21 -  1.5
  @@ -109,7 +109,7 @@
   public boolean execute(
   Connection conn,
   String query,
  -Object[] vals,
  +Object[] params,
   ResultSetHandler rsh,
   ResultSetMetaDataHandler rsmdh,
   Object userObject)
  @@ -120,7 +120,7 @@
   
   try {
   stmt = conn.prepareStatement(query);
  -fillStatement(stmt, vals);
  +fillStatement(stmt, params);
   
   if (!stmt.execute()) {
   return false;
  @@ -129,17 +129,19 @@
   do {
   rs = stmt.getResultSet();
   if (rs != null) {
  +rs = this.wrap(rs);
  +
   if (rsmdh != null) {
   rsmdh.handle(rs.getMetaData());
   }
   
  -rsh.handle(rs, vals, userObject);
  +rsh.handle(rs, params, userObject);
   }
   
   } while (stmt.getMoreResults());
   
   } catch (SQLException e) {
  -rethrow(e, query, vals);
  +rethrow(e, query, params);
   
   } finally {
   DbUtils.closeQuietly(rs);
  @@ -150,6 +152,37 @@
   }
   
   /**
  + * Fill the codePreparedStatement/code replacement parameters with 
  + * the given objects.
  + * @param stmt
  + * @param params Query replacement parameters; codenull/code is a valid
  + * value to pass in.
  + * @throws SQLException
  + */
  +protected void fillStatement(PreparedStatement stmt, Object[] params)
  +throws SQLException {
  +
  +if (params == null) {
  +return;
  +}
  +
  +for (int i = 0; i  params.length; i++) {
  +if (params[i] != null) {
  +stmt.setObject(i + 1, params[i]);
  +} else {
  +stmt.setNull(i + 1, Types.OTHER);
  +}
  +}
  +}
  +
  +/**
  + * Returns the codeDataSource/code this runner is using.
  + */
  +public DataSource getDataSource() {
  +return this.ds;
  +}
  +
  +/**
* Creates a PreparedStatement using the String and Object array,
* executes this using the Connection, and returns the results
* inside an Iterator.
  @@ -193,7 +226,7 @@
   stmt = conn.prepareStatement(query);
   this.fillStatement(stmt, params);
   
  -rs = stmt.executeQuery();
  +rs = this.wrap(stmt.executeQuery());
   
   if (rsmdh != null) {
   rsmdh.handle(rs.getMetaData());
  @@ -264,6 +297,40 @@
   }
   
   /**
  + * 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. 
  + * @param sql The query that was executing when the exception happened.
  + * @param params The query replacement paramaters; codenull/code is a 
  + * valid value to pass in.
  + * @throws SQLException
  + */
  +protected void rethrow(SQLException cause, String sql, Object[] params)
  +throws SQLException {
  +
  +StringBuffer msg = new StringBuffer(cause.getMessage() +  in query  + 
sql);
  +if (params != null) {
  +msg.append(Arrays.asList(params).toString());
  +}
  +
  +SQLException newsqle = new SQLException(msg.toString());
  +newsqle.setNextException(cause);
  +
  +throw newsqle;
  +}
  +
  +/**
  + * Sets the codeDataSource/code this runner will use to get
  + * database connections from.  This should be called after creating a
  + * runner with the default constructor if you intend to use the
  + * execute methods without passing in a codeConnection/code.
  + * @param dataSource The DataSource to use.
  + */
  +public void setDataSource(DataSource dataSource) {
  +this.ds = dataSource;
  +}
  +
  +/**
* Execute an SQL INSERT,