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 Demo.java DemoHandler.java ProcedureUtilsTest.java
Date Tue, 11 Mar 2003 10:19:48 GMT
baliuka     2003/03/11 02:19:48

  Modified:    dbutils/src/java/org/apache/commons/dbutils
                        ProcedureUtils.java
               dbutils/src/test/org/apache/commons/dbutils Demo.java
                        DemoHandler.java ProcedureUtilsTest.java
  Log:
  Added support for dynamic SQL in ProcedureUtils
  
  Revision  Changes    Path
  1.6       +47 -29    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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ProcedureUtils.java	10 Mar 2003 19:35:42 -0000	1.5
  +++ ProcedureUtils.java	11 Mar 2003 10:19:48 -0000	1.6
  @@ -61,6 +61,7 @@
   import java.lang.reflect.*;
   import java.sql.*;
   import java.io.*;
  +import java.text.MessageFormat;
   
   import com.thoughtworks.qdox.JavaDocBuilder;
   import com.thoughtworks.qdox.model.*;
  @@ -84,6 +85,7 @@
           int indexMap[];
           ResultSetHandler handler;
           boolean update;
  +        boolean dynamic;
           //TODO:
           boolean cached;
           boolean flushOnExecute;
  @@ -95,34 +97,40 @@
           
       }
       
  -    static ProcedureDescriptor compile(Method proc, String sqlStr,
  -    ResultSetHandler handler,boolean update){
  +    static List parseSQL(String sqlStr,StringBuffer sb){
           
  -        java.util.ArrayList indexes = new java.util.ArrayList();
  +        List indexes = new java.util.ArrayList();
           char sql[] = sqlStr.toCharArray();
  -        StringBuffer sb = new StringBuffer();
           StringBuffer digit = new StringBuffer();
           
  -        boolean arg = false;
  -        boolean escape = false;
  +        final int ARG = 2;
  +        final int ESCAPE = 4;
  +        BitSet state = new BitSet();
           
           for (int i = 0; i < sql.length; i++ ){
               
               
               if(sql[i] == '$'){
  -                if(escape && !arg){
  -                    escape = false;
  -                    arg = false;
  +                if( state.get(ESCAPE) ){
  +                    sb.append('$');
  +                    state.clear();
                       continue;
                   }
  -                escape = true;
  -                arg = false;
  -                continue;
  +                if(state.get(ARG)){
  +                    indexes.add( new Integer(Integer.parseInt(digit.toString())) );
  +                    sb.append('?');
  +                    digit.delete(0, digit.length() );
  +                }
  +                state.clear();
  +                state.set(ESCAPE);
  +                continue ;
               }
               
  -            if (Character.isDigit(sql[i]) && escape){
  +            if ( Character.isDigit(sql[i]) &&
  +            (state.get(ESCAPE) || state.get(ARG))   ){
                   digit.append(sql[i]);
  -                arg = true;
  +                state.clear();
  +                state.set(ARG);
                   if( i != sql.length - 1 ){
                       continue;
                   }else {
  @@ -130,7 +138,7 @@
                   }
               }
               
  -            if(arg){
  +            if(state.get(ARG)){
                   indexes.add( new Integer(Integer.parseInt(digit.toString())) );
                   sb.append('?');
                   digit.delete(0, digit.length() );
  @@ -138,11 +146,18 @@
               if( i < sql.length ){
                   sb.append(sql[i]);
               }
  -            escape = false;
  -            arg    = false;
  -            
  +            state.clear();
           }
           
  +        return indexes;
  +    }
  +    
  +    static ProcedureDescriptor compile(Method proc, String sqlStr,
  +    ResultSetHandler handler, boolean update){
  +        
  +        StringBuffer sb = new StringBuffer();
  +        List indexes = parseSQL( sqlStr, sb );
  +        
           if( update &&  proc.getReturnType() != Void.TYPE && proc.getReturnType()
!= Integer.TYPE  ){
               
               throw new IllegalArgumentException( "update method " + proc +
  @@ -152,6 +167,11 @@
           }
           
           ProcedureDescriptor descriptor = new ProcedureDescriptor();
  +        Object textArgs[] = new Object[proc.getParameterTypes().length];
  +        Arrays.fill(textArgs,"");
  +        descriptor.dynamic = !sb.toString().equals( 
  +                     MessageFormat.format(sb.toString(),textArgs) );
  +        
           if(indexes.size() > 0){
               
               
  @@ -172,8 +192,8 @@
           
           return descriptor;
       }
  -   
  -     static Method findMethod(Class cls, JavaMethod jmethod){
  +    
  +    static Method findMethod(Class cls, JavaMethod jmethod){
           
           Method methods[] = cls.getDeclaredMethods();
           for(int i=0; i< methods.length; i++ ){
  @@ -202,7 +222,7 @@
           throw new IllegalStateException("metadata not found for " + jmethod);
       }
       
  -     static ResultSetHandler findHandler(Method method, JavaMethod jmethod){
  +    static ResultSetHandler findHandler(Method method, JavaMethod jmethod){
           
           
           ResultSetHandler handler = null;
  @@ -281,7 +301,7 @@
           throw new IllegalStateException( "Handler not found for " + method  );
       }
       
  -     static Map buildProcedures(Class cls){
  +    static Map buildProcedures(Class cls){
           
           String res = cls.getName().replace('.','/') + ".java";
           InputStream is = cls.getClassLoader().getResourceAsStream(res);
  @@ -321,7 +341,7 @@
           
       }
       //TODO: ConvertUtils
  -     static Object convert( Class to, Object from ){
  +    static Object convert( Class to, Object from ){
           
           return from;
       }
  @@ -363,7 +383,7 @@
               }
               return pargs;
           }
  -      
  +        
           public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
               
               try{
  @@ -383,14 +403,14 @@
                   
                   if(!descriptor.update){
                       return convert( method.getReturnType(), DbUtils.executeQuery(  connection,
  -                    descriptor.jdbcSQL,
  +                    descriptor.dynamic ? MessageFormat.format(descriptor.jdbcSQL,args)
: descriptor.jdbcSQL,
                       prepareArgs(descriptor,args),
                       descriptor.handler,
  -                     args
  +                    args
                       ));
                   }else{
                       int updateCount = DbUtils.executeUpdate(  connection,
  -                    descriptor.jdbcSQL,
  +                    descriptor.dynamic ? MessageFormat.format(descriptor.jdbcSQL,args)
: descriptor.jdbcSQL,
                       prepareArgs(descriptor,args)
                       );
                       
  @@ -418,8 +438,6 @@
                   
               }
           }
  -        
  -        
           
       }
       
  
  
  
  1.5       +11 -1     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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Demo.java	10 Mar 2003 19:35:42 -0000	1.4
  +++ Demo.java	11 Mar 2003 10:19:48 -0000	1.5
  @@ -37,11 +37,17 @@
       
       
       /**
  -     *@query SELECT ID, NAME FROM TBL 
  +     *@query SELECT * FROM TBL 
        *@handler DemoHandler
        */
       public int print(java.io.PrintStream out) throws java.io.IOException;
       
  +    /**
  +     *@query SELECT * FROM {1} WHERE {2} > $4
  +     *@handler DemoHandler
  +     */
  +    public int dynamicPrint(java.io.PrintStream out,
  +                            String table, String field, int value) ;
       
       
       
  @@ -58,4 +64,8 @@
        */
        
        public void undeclaredError();
  +     
  +     
  +     
  +     
   }
  
  
  
  1.4       +1 -0      jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/DemoHandler.java
  
  Index: DemoHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/DemoHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DemoHandler.java	10 Mar 2003 19:35:42 -0000	1.3
  +++ DemoHandler.java	11 Mar 2003 10:19:48 -0000	1.4
  @@ -30,6 +30,7 @@
             }
            
           }
  +         out.println();
           return new Integer(cnt);
       }
       
  
  
  
  1.5       +54 -1     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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ProcedureUtilsTest.java	10 Mar 2003 19:35:42 -0000	1.4
  +++ ProcedureUtilsTest.java	11 Mar 2003 10:19:48 -0000	1.5
  @@ -54,6 +54,56 @@
           demo.create();
       }
       
  +    public void testParseSQL() throws Exception {
  +        
  +     String in = "$1";
  +     String out = "?";
  +     StringBuffer buffer = new StringBuffer();
  +     List ind = ProcedureUtils.parseSQL(in, buffer);
  +     
  +     assertTrue(ind.contains(new Integer(1)));
  +     assertEquals( out, buffer.toString() );
  +     
  +     in = "$$1";
  +     out = "$1";
  +     buffer = new StringBuffer();
  +     ProcedureUtils.parseSQL(in, buffer);
  +     
  +     assertEquals( out, buffer.toString() );
  +     
  +     in = "$$$1";
  +     out = "$?";
  +     buffer = new StringBuffer();
  +     ProcedureUtils.parseSQL(in, buffer);
  +     
  +     assertEquals( out, buffer.toString() );
  +     
  +     
  +     in = "$2$1$3";
  +     out = "???";
  +     buffer = new StringBuffer();
  +     ind = ProcedureUtils.parseSQL(in, buffer);
  +     
  +     assertEquals( out, buffer.toString() );
  +     assertEquals( ind.get(0), new Integer(2) );
  +     assertEquals( ind.get(1), new Integer(1) );
  +     assertEquals( ind.get(2), new Integer(3) );     
  +     
  +     
  +     in = "A$2B$1C$3D$$0";
  +     out = "A?B?C?D$0";
  +     buffer = new StringBuffer();
  +     ind = ProcedureUtils.parseSQL(in, buffer);
  +     
  +     assertEquals( out, buffer.toString() );
  +     assertEquals( ind.get(0), new Integer(2) );
  +     assertEquals( ind.get(1), new Integer(1) );
  +     assertEquals( ind.get(2), new Integer(3) );     
  +     
  +     
  +        
  +    }
  +    
       public void testGetInstance() throws Exception {
           
          Demo demo = getDemo();
  @@ -65,9 +115,12 @@
              demo.add(i + 2,"test" + i);
          }
          demo.print(System.out);
  -     
  +       System.out.println("DYNAMIC SQL:");
  +       demo.dynamicPrint(System.out,"TBL","ID",8);
  +       
          demo.clear();
        
  +       
       }
       
        public void testSQLException() {
  
  
  

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