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/test/org/apache/commons/dbutils DemoBean.java DemoBeanHandler.java Demo.java ProcedureUtilsTest.java
Date Sun, 16 Mar 2003 13:28:54 GMT
baliuka     2003/03/16 05:28:54

  Modified:    dbutils/src/java/org/apache/commons/dbutils DbUtils.java
                        ProcedureUtils.java VectorHandler.java
               dbutils/src/test/org/apache/commons/dbutils Demo.java
                        ProcedureUtilsTest.java
  Added:       dbutils/src/java/org/apache/commons/dbutils MapHandler.java
               dbutils/src/test/org/apache/commons/dbutils DemoBean.java
                        DemoBeanHandler.java
  Log:
  added utilty methods to handle beans and maps
  
  Revision  Changes    Path
  1.18      +52 -0     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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- DbUtils.java	14 Mar 2003 17:27:32 -0000	1.17
  +++ DbUtils.java	16 Mar 2003 13:28:53 -0000	1.18
  @@ -287,7 +287,59 @@
               return false;
           }
       }
  +  
  +     /**
  +     * Create an Map from a ResultSet.
  +     * It is assumed that next() has already been called on the ResultSet.
  +     */
  +  
  +    public static java.util.Map resultSetToMap(ResultSet rs) throws SQLException {
  +         java.util.Map result = new java.util.Hashtable();
  +         ResultSetMetaData rsmd = rs.getMetaData();
  +         int cnt = rsmd.getColumnCount();
  +         for( int i = 1; i <= cnt; i++ ){
  +            result.put(rsmd.getColumnName(i),rs.getObject(i));  
  +         }
  +         return  result;         
  +    }
       
  +    /**
  +     * Create an Bean from a ResultSet.
  +     * It is assumed that next() has already been called on the ResultSet.
  +     */
  +  
  +    public static Object resultSetToBean(ResultSet rs, Object obj) throws SQLException
{
  +     
  +       try{     
  +           
  +        java.beans.BeanInfo bInfo = java.beans.Introspector.getBeanInfo(
  +                                                                  obj.getClass());
  +        java.beans.PropertyDescriptor pd[]  = bInfo.getPropertyDescriptors();
  +        
  +         ResultSetMetaData rsmd = rs.getMetaData();
  +         int cnt = rsmd.getColumnCount();
  +         LOOP:
  +         for( int i = 1; i <= cnt; i++ ){
  +           String name = rsmd.getColumnName(i);
  +           for( int j = 0; j < pd.length; j++ ){
  +                if(name.equals(pd[j].getName())){
  +                 try{   
  +                  pd[j].getWriteMethod().
  +                      invoke(obj, new Object[]{ rs.getObject(i) });
  +                 }catch(Exception e){
  +                   throw new DbException( "can not set " + name , e); 
  +                 }   
  +                      continue LOOP;
  +                }
  +           }
  +          throw new SQLException(name + " not found in " + obj.getClass().getName() );

  +         }
  +       }catch(java.beans.IntrospectionException ie){
  +         throw new DbException(ie); 
  +       } 
  +    
  +       return obj;
  +    }
       /**
        * Create an Object array from a ResultSet.
        * It is assumed that next() has already been called on the ResultSet.
  
  
  
  1.11      +13 -3     jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ProcedureUtils.java
  
  Index: ProcedureUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ProcedureUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ProcedureUtils.java	16 Mar 2003 09:29:32 -0000	1.10
  +++ ProcedureUtils.java	16 Mar 2003 13:28:53 -0000	1.11
  @@ -82,6 +82,8 @@
       private static final ResultSetHandler  SCALAR_HANDLER = new ScalarHandler();
       private static final ResultSetHandler VECTOR_HANDLER = new VectorHandler();
       private static final ResultSetHandler COLLECTION_HANDLER = new CollectionHandler();
  +    private static final ResultSetHandler MAP_HANDLER = new MapHandler();
  +    
       private static final String UPDATE_TAG = "update";
       private static final String QUERY_TAG = "query";
       private static final String CACHE_TAG = "useCache";
  @@ -205,9 +207,13 @@
     
       private static boolean isCollection(Class cls){
      
  -       return  cls.isAssignableFrom(List.class); 
  +       return  cls.isAssignableFrom(Collection.class); 
      } 
      
  +    private static boolean isMap(Class cls){
  +   
  +       return  cls.isAssignableFrom(Map.class); 
  +   }
       
      private static boolean isVector(Class cls){
      
  @@ -290,7 +296,11 @@
               return COLLECTION_HANDLER;
           }
           
  -     
  +        if( name == null && isMap( method.getReturnType() ) ){
  +            
  +            return MAP_HANDLER;
  +        }
  +       
           
           
           
  
  
  
  1.2       +4 -1      jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/VectorHandler.java
  
  Index: VectorHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/VectorHandler.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- VectorHandler.java	16 Mar 2003 09:29:32 -0000	1.1
  +++ VectorHandler.java	16 Mar 2003 13:28:53 -0000	1.2
  @@ -15,9 +15,12 @@
                  
                   if (rs.next()) {
                       Object result = DbUtils.resultSetToArray(rs);
  +                    if(rs.next()){
  +                       throw new SQLException("too many results");
  +                    }
                       return result;
                   }
  -               throw new SQLException();
  +                throw new SQLException("too many results");
               }
   
       
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/MapHandler.java
  
  Index: MapHandler.java
  ===================================================================
  package org.apache.commons.dbutils;
  
  import java.util.*;
  import java.sql.*;
  /**
   *
   * @author  baliuka
   */
  public class MapHandler implements ResultSetHandler {
      
      /** Creates a new instance of VectorHandler */
      public MapHandler() {
      }
      
      public Object handle(java.sql.ResultSet rs, Object[] params, Object userObject) throws
java.sql.SQLException{
                 
                  if (rs.next()) {
                      Map result = DbUtils.resultSetToMap(rs);
                      if(rs.next()){
                        throw new SQLException("too many results");
                      }
                      return result;
                  }
                  throw new SQLException("too many results");
              }
  
      
  }
  
  
  
  1.9       +14 -2     jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/Demo.java
  
  Index: Demo.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/Demo.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Demo.java	16 Mar 2003 09:29:32 -0000	1.8
  +++ Demo.java	16 Mar 2003 13:28:53 -0000	1.9
  @@ -1,6 +1,6 @@
   
   package org.apache.commons.dbutils;
  -
  +import java.util.Map;
   /**
    * this file is used for metadata and must be in classpath at runtime
    * @author  baliuka
  @@ -48,10 +48,22 @@
       public java.util.Date now();
       
       /**
  -     *@query SELECT * FROM TBL  
  +     *@query SELECT * FROM TBL  WHERE ID=$0 
        */
       public Object[] getRow( int id );
       
  +     /**
  +     *@query SELECT * FROM TBL WHERE ID=$0 
  +     */
  +     public Map getRowAsMap( int id );
  +   
  +   
  +     /**
  +     *@query SELECT  id, name FROM TBL WHERE ID=$1 
  +     *@handler DemoBeanHandler
  +     */
  +     public DemoBean getRowAsBean( DemoBean bean, int id );
  +   
       /**
        *@query SELECT * FROM {0}  
        */
  
  
  
  1.8       +5 -0      jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ProcedureUtilsTest.java
  
  Index: ProcedureUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ProcedureUtilsTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ProcedureUtilsTest.java	16 Mar 2003 09:29:32 -0000	1.7
  +++ ProcedureUtilsTest.java	16 Mar 2003 13:28:53 -0000	1.8
  @@ -123,7 +123,12 @@
          assertTrue( demo.now()  != null  );
          assertEquals(demo.concat("A","B"), "AB" );
          assertTrue(demo.getRow(max.intValue()).length == 2 );
  +       assertTrue(demo.getRowAsMap(max.intValue()).size() == 2 );
          assertTrue(demo.getAll("TBL").size() > 2 );
  +       DemoBean bean = new DemoBean();
  +       demo.getRowAsBean(bean, max.intValue());
  +       assertTrue( bean.getId() == max.intValue()  );
  +       assertTrue( bean.getName() != null );
          
          demo.clear();
               
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/DemoBean.java
  
  Index: DemoBean.java
  ===================================================================
  package org.apache.commons.dbutils;
  
  /**
   *
   * @author  baliuka
   */
  public class DemoBean {
      
      /** Holds value of property id. */
      private int id;
      
      /** Holds value of property name. */
      private String name;
      
      /** Creates a new instance of DemoBean */
      public DemoBean() {
      }
      
      /** Getter for property id.
       * @return Value of property id.
       *
       */
      public int getId() {
          return this.id;
      }
      
      /** Setter for property id.
       * @param id New value of property id.
       *
       */
      public void setId(int id) {
          this.id = id;
      }
      
      /** Getter for property name.
       * @return Value of property name.
       *
       */
      public String getName() {
          return this.name;
      }
      
      /** Setter for property name.
       * @param name New value of property name.
       *
       */
      public void setName(String name) {
          this.name = name;
      }
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/DemoBeanHandler.java
  
  Index: DemoBeanHandler.java
  ===================================================================
  package org.apache.commons.dbutils;
  
  /**
   *
   * @author  baliuka
   */
  public class DemoBeanHandler implements ResultSetHandler{
      
      /** Creates a new instance of DemoBeanHandler */
      public DemoBeanHandler() {
      }
      
      public Object handle(java.sql.ResultSet rs, Object[] params, Object userObject) throws
java.sql.SQLException {
          if(rs.next()){
               Object bean = ((Object [])userObject)[0];
               DbUtils.resultSetToBean(rs, bean);
               if(rs.next()){
                 throw new java.sql.SQLException();
               }
               return bean;
               
          }
          throw new java.sql.SQLException();
          
      }    
      
  }
  
  
  

---------------------------------------------------------------------
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