commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bali...@apache.org
Subject cvs commit: jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils DbUtils.java
Date Sat, 22 Feb 2003 09:40:58 GMT
baliuka     2003/02/22 01:40:57

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
  Log:
  reduced code dublication
  
  Revision  Changes    Path
  1.9       +44 -21    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- DbUtils.java	22 Feb 2003 08:58:52 -0000	1.8
  +++ DbUtils.java	22 Feb 2003 09:40:57 -0000	1.9
  @@ -69,6 +69,20 @@
          public Object handle(ResultSet rs)throws SQLException;
          
       }
  +   public static abstract class ListAdapter extends ArrayList implements ResultSetHandler{
  +   
  +      final public Object handle(ResultSet rs)throws SQLException{
  +       
  +             while(rs.next()){
  +                add(fetch(rs));
  +             }
  +          return this;
  +      }
  +   
  +      public abstract Object fetch( ResultSet rs )throws SQLException;
  +      
  +   }
  +   
       
      public static Object executeQuery(Connection connection, String query, 
                                           Object[] vals, ResultSetHandler rsh
  @@ -108,7 +122,21 @@
               throw newsqle;
               
       }
  +   
  +   static void throwNoResults( String sql,Object[] vals )throws SQLException{
       
  +       rethrow( new SQLException("No results returned"), sql, vals );
  +       
  +    }
  +   
  +   static void throwMultipleResults( String sql,Object[] vals )throws SQLException{
  +    
  +       rethrow( new SQLException("No results returned"), sql, vals );
  +       
  +    }
  +  
  +   
  +   
       static void fillStatement(PreparedStatement stmt, Object[] vals) throws SQLException
{
           if (vals != null) {
               int size = vals.length;
  @@ -154,15 +182,14 @@
       {
         return (List)executeQuery(connection,query,vals,
         
  -      new ResultSetHandler(){
  -           public Object handle(ResultSet rs)throws SQLException{
  -            ArrayList list = new ArrayList();
  -            while (rs.next()) {
  -                list.add(resultSetToArray(rs));
  -            }
  -            return list;
  +      new ListAdapter(){
  +          
  +          public  Object fetch( ResultSet rs )throws SQLException{
  +            return resultSetToArray(rs);
  +          }
  +           
            }   
  -      }//ResultSetHandler
  +      
        ); 
        
       }
  @@ -191,7 +218,7 @@
        * Null values in the Object array will be passed to the driver.
        */
       public static int executeIntQuery(Connection connection, final String query, 
  -                                        Object[] vals
  +                                       final Object[] vals
                                          ) throws SQLException 
       {
         return ((Number) executeQuery(connection, query, vals,
  @@ -200,16 +227,16 @@
              public Object handle(ResultSet rs)throws SQLException{
               ResultSetMetaData rsmd = rs.getMetaData();
               if(rsmd.getColumnCount() > 1 ){
  -                throw new SQLException("multiple results returned by query " + query);
  +               throwMultipleResults(query, vals);
               }
               Number result = null;
               if (rs.next()) {
                 result = new Integer( rs.getInt(1) ); 
               }else{
  -              throw new SQLException("No results returned by query " + query);
  +              throwNoResults(query, vals);
               }
               if(rs.next()){
  -              throw new SQLException("multiple results returned by query " + query);
  +              throwMultipleResults(query, vals);
               }
               return result;
            }   
  @@ -233,21 +260,17 @@
         
         new ResultSetHandler(){
              public Object handle(ResultSet rs)throws SQLException{
  -            ResultSetMetaData rsmd = rs.getMetaData();
  -            int count = rsmd.getColumnCount();
  -            Object row[] = new Object[count];
               
               if (rs.next()) {
  -             for(int i = 0; i < count ; i++ ){   
  -              row[i] = rs.getObject(i + 1) ; 
  -             } 
  +               return resultSetToArray(rs); 
               }else{
  -              rethrow( new SQLException("No results returned"), query, vals );
  +              throwNoResults(query, vals);
               }
               if(rs.next()){
  -              rethrow( new SQLException("Multiple results returned"), query, vals );  

  +              throwMultipleResults(query, vals);
               }
  -            return row;
  +            //assert true
  +            return null;
            }   
          } 
          // ResultSetHandler      
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message