Author: ppoddar
Date: Fri Apr 26 22:37:18 2013
New Revision: 1476427

URL: http://svn.apache.org/r1476427
Log:
improve StoredProcedure tests

Added:
    
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java
   (with props)
Modified:
    
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
    
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/AbstractProcedureList.java
    
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
    
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java

Modified: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java?rev=1476427&r1=1476426&r2=1476427&view=diff
==============================================================================
--- 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
 (original)
+++ 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/TestNativeQueryProcedures.java
 Fri Apr 26 22:37:18 2013
@@ -18,6 +18,7 @@
  */
 package org.apache.openjpa.persistence.jdbc.query;
 
+import java.util.Collection;
 import java.util.List;
 
 import javax.persistence.EntityManager;
@@ -30,6 +31,7 @@ import org.apache.openjpa.persistence.jd
 import org.apache.openjpa.persistence.jdbc.query.domain.Game;
 import org.apache.openjpa.persistence.jdbc.query.procedure.DerbyProcedureList;
 import 
org.apache.openjpa.persistence.jdbc.query.procedure.AbstractProcedureList;
+import org.apache.openjpa.persistence.jdbc.query.procedure.StoredProcedure;
 import org.apache.openjpa.persistence.test.AllowFailure;
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
@@ -56,43 +58,21 @@ public class TestNativeQueryProcedures e
             procedureList = new DerbyProcedureList();
         }
         
-        if (procedureList != null) {
-            EntityManager em = emf.createEntityManager();
-            List<String> createList = procedureList.getCreateProcedureList();
-            try {
-                for (String createProcedure : createList) {
-                    em.getTransaction().begin();
-                    Query query = em.createNativeQuery(createProcedure);
-                    query.executeUpdate();
-                    em.getTransaction().commit();
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-                em.getTransaction().commit();
-            }
-            em.clear();
-            em.close();
-        }
+//        if (procedureList != null) {
+//            Collection<StoredProcedure> createList = 
procedureList.getProcedureList();
+//            for (StoredProcedure proc : createList) {
+//                     createProcedure(proc);
+//            }
+//        }
     }
 
     public void tearDown() throws Exception {
-        if (procedureList != null) {
-            EntityManager em = emf.createEntityManager();
-            List<String> dropList = procedureList.getDropProcedureList();
-            try {
-                for (String dropProcedure : dropList) {
-                    em.getTransaction().begin();
-                    Query query = em.createNativeQuery(dropProcedure);
-                    query.executeUpdate();
-                    em.getTransaction().commit();
-                }
-            } catch (Exception e) {
-                e.printStackTrace();
-                em.getTransaction().commit();
-            }
-            em.clear();
-            em.close();
-        }
+//        if (procedureList != null) {
+//            Collection<StoredProcedure> dropList = 
procedureList.getProcedureList();
+//            for (StoredProcedure proc : dropList) {
+//                    dropProcedure(proc);
+//            }
+//        }
         super.tearDown();
     }
 
@@ -110,7 +90,7 @@ public class TestNativeQueryProcedures e
             em.persist(applicant2);
             em.getTransaction().commit();
 
-            String sql = procedureList.callAddXToCharlie();
+            String sql = 
procedureList.getProcedure("ADD_X_TO_CHARLIE").getCallSQL();
 
             // query.getSingleResult() and query.getResultList() both throw an
             // exception:
@@ -182,7 +162,7 @@ public class TestNativeQueryProcedures e
             em.persist(applicant2);
             em.getTransaction().commit();
 
-            String sql = procedureList.callAddSuffixToName();
+            String sql = 
procedureList.getProcedure("ADD_SUFFIX_TO_NAME").getCallSQL();
 
             // query.getSingleResult() and query.getResultList() both throw an
             // exception:
@@ -259,7 +239,7 @@ public class TestNativeQueryProcedures e
             em.persist(applicant1);
             em.getTransaction().commit();
 
-            String sql = procedureList.callGetAllApplicants();
+            String sql = 
procedureList.getProcedure("GET_ALL_APPLICANTS").getCallSQL();
 
             try {
                 em.getTransaction().begin();
@@ -333,7 +313,7 @@ public class TestNativeQueryProcedures e
             em.persist(applicant4);
             em.getTransaction().commit();
 
-            String sql = procedureList.callGetTwoApplicants();
+            String sql = 
procedureList.getProcedure("GET_TWO_APPLICANTS").getCallSQL();
 
             try {
                 em.getTransaction().begin();
@@ -393,4 +373,26 @@ public class TestNativeQueryProcedures e
             em = emf.createEntityManager();
         }
     }
+    
+    protected void createProcedure(StoredProcedure sp) {
+               EntityManager em = emf.createEntityManager();
+               em.getTransaction().begin();
+       try {
+               em.createNativeQuery(sp.getCreateSQL()).executeUpdate();
+               em.getTransaction().commit();
+       } catch (Exception ex) {
+               System.err.println(ex.toString());
+       }
+    }
+    
+    protected void dropProcedure(StoredProcedure sp) {
+               EntityManager em = emf.createEntityManager();
+               em.getTransaction().begin();
+       try {
+               em.createNativeQuery(sp.getDropSQL()).executeUpdate();
+               em.getTransaction().commit();
+       } catch (Exception ex) {
+               System.err.println(ex.toString());
+       }
+    }
 }

Modified: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/AbstractProcedureList.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/AbstractProcedureList.java?rev=1476427&r1=1476426&r2=1476427&view=diff
==============================================================================
--- 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/AbstractProcedureList.java
 (original)
+++ 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/AbstractProcedureList.java
 Fri Apr 26 22:37:18 2013
@@ -18,33 +18,32 @@
  */
 package org.apache.openjpa.persistence.jdbc.query.procedure;
 
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
 /*
  * holds the stored procedures that will be used by test cases
  */
 public abstract class AbstractProcedureList implements ProcedureList {
-
-    public static void addXToCharlie() throws Exception {
-        Exception e =
-            new Exception("Method not implemented by inheriting class");
-        throw e;
+       private Map<String, StoredProcedure> _procs = new HashMap<String, 
StoredProcedure>();
+       
+    public final Collection<StoredProcedure> getProcedureList () {
+       return _procs.values();
     }
-
-    public static void addSuffixToName(String name, String suffix)
-        throws Exception {
-        Exception e =
-            new Exception("Method not implemented by inheriting class");
-        throw e;
+    
+    public StoredProcedure getProcedure(String name) {
+       if (!_procs.containsKey(name))
+               throw new RuntimeException("Procdure [" + name + "] is not 
recognized");
+       return _procs.get(name);
     }
-
-    public static void getAllApplicants() throws Exception {
-        Exception e =
-            new Exception("Method not implemented by inheriting class");
-        throw e;
+    
+    protected final void put(StoredProcedure p) {
+       _procs.put(p.getName(), p);
     }
-
-    public static void getTwoApplicants() throws Exception {
-        Exception e =
-            new Exception("Method not implemented by inheriting class");
-        throw e;
+    
+    public final boolean contains(String name) {
+       return _procs.containsKey(name);
     }
+
 }

Modified: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java?rev=1476427&r1=1476426&r2=1476427&view=diff
==============================================================================
--- 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
 (original)
+++ 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/DerbyProcedureList.java
 Fri Apr 26 22:37:18 2013
@@ -22,90 +22,54 @@ import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
-import java.util.ArrayList;
-import java.util.List;
 
 /*
  * holds the stored procedures that will be used by test cases
  */
 public class DerbyProcedureList extends AbstractProcedureList {
-
-    public List<String> getCreateProcedureList () {
-        ArrayList<String> retList = new ArrayList<String>();
-
-        retList.add("create procedure ADD_X_TO_CHARLIE () "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA "
-            + "EXTERNAL NAME '" + DerbyProcedureList.class.getName()
-            + ".addXToCharlie'");
-        retList.add("create procedure ADD_SUFFIX_TO_NAME (NAME VARCHAR(128), "
-            + "SUFFIX VARCHAR(128)) "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA MODIFIES SQL DATA "
-            + "EXTERNAL NAME '" + DerbyProcedureList.class.getName()
-            + ".addSuffixToName'");
-        retList.add("create procedure GET_ALL_APPLICANTS () "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA READS SQL DATA DYNAMIC "
-            + "RESULT SETS 1 " + "EXTERNAL NAME '"
-            + DerbyProcedureList.class.getName() + ".getAllApplicants'");
-        retList.add("create procedure GET_TWO_APPLICANTS (NAME VARCHAR(128), "
-            + "SUFFIX VARCHAR(128)) "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA READS SQL DATA DYNAMIC "
-            + "RESULT SETS 1 " + "EXTERNAL NAME '"
-            + DerbyProcedureList.class.getName() + ".getTwoApplicants'");
-        retList.add("create procedure GET_ALL_APPLICANTS_AND_GAMES () "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA READS SQL DATA DYNAMIC "
-            + "RESULT SETS 2 " + "EXTERNAL NAME '"
-            + DerbyProcedureList.class.getName()
-            + ".getAllApplicantsAndGames'");
-        retList.add("create procedure GET_TWO_APPLICANTS_AND_GAMES "
-            + "(NAME VARCHAR(128), SUFFIX VARCHAR(128)) "
-            + "PARAMETER STYLE JAVA LANGUAGE JAVA READS SQL DATA DYNAMIC "
-            + "RESULT SETS 2 " + "EXTERNAL NAME '"
-            + DerbyProcedureList.class.getName()
-            + ".getTwoApplicantsAndGames'");
-
-        return retList;
-    }
-
-    public List<String> getDropProcedureList () {
-        ArrayList<String> retList = new ArrayList<String>();
-
-        retList.add ("drop procedure ADD_X_TO_CHARLIE");
-        retList.add ("drop procedure ADD_SUFFIX_TO_NAME");
-        retList.add ("drop procedure GET_ALL_APPLICANTS");
-        retList.add ("drop procedure GET_TWO_APPLICANTS");
-        retList.add ("drop procedure GET_ALL_APPLICANTS_AND_GAMES");
-        retList.add ("drop procedure GET_TWO_APPLICANTS_AND_GAMES");
-
-        return retList;
+    
+    public DerbyProcedureList() {
+       put(new StoredProcedure("ADD_X_TO_CHARLIE")
+                       .styleSQL("JAVA")
+               .updateSQL(this.getClass(), "addXToCharlie", (Class<?>[])null));
+        put(new StoredProcedure("ADD_SUFFIX_TO_NAME")
+                .addParameter("NAME VARCHAR(128)")
+                .addParameter("SUFFIX VARCHAR(128)")
+                       .styleSQL("JAVA")
+                               .updateSQL(this.getClass(), "addSuffixToName", 
String.class, String.class));
+        put(new StoredProcedure("GET_ALL_APPLICANTS")
+                       .styleSQL("JAVA")
+                .readSQL(1, this.getClass(), "getAllApplicants", 
ResultSet[].class));
+        put(new StoredProcedure("GET_TWO_APPLICANTS")
+                       .addParameter("NAME VARCHAR(128)")
+                .addParameter("SUFFIX VARCHAR(128)")
+                       .styleSQL("JAVA")
+                .readSQL(1, this.getClass(), "getTwoApplicants", String.class, 
String.class, ResultSet[].class));
+        put(new StoredProcedure("GET_ALL_APPLICANTS_AND_GAMES")
+                      .styleSQL("JAVA")
+               .readSQL(2, this.getClass(), "getAllApplicantsAndGames", 
ResultSet[].class, ResultSet[].class));
+        put(new StoredProcedure("GET_TWO_APPLICANTS_AND_GAMES")
+                       .addParameter("NAME VARCHAR(128)")
+                       .addParameter("SUFFIX VARCHAR(128)")
+                       .styleSQL("JAVA")
+                       .readSQL(2, this.getClass(), 
"getTwoApplicantsAndGames", String.class,String.class,
+                                       ResultSet[].class, ResultSet[].class));
     }
 
-    public String callAddXToCharlie () {
-        return "{ call ADD_X_TO_CHARLIE () }";
-    }
 
     public static void addXToCharlie() throws Exception {
-        Connection conn =
-            DriverManager.getConnection("jdbc:default:connection");
-        PreparedStatement ps1 =
-            conn
-                .prepareStatement("update APPLICANT set name = 'Charliex' " 
+        Connection conn =  
DriverManager.getConnection("jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("update APPLICANT set 
name = 'Charliex' " 
                     + "where name = 'Charlie'");
         ps1.executeUpdate();
 
         conn.close();
     }
 
-    public String callAddSuffixToName () {
-        return "{ call ADD_SUFFIX_TO_NAME (?, ?) }";
-    }
-
     public static void addSuffixToName(String name, String suffix)
         throws Exception {
-        Connection conn =
-            DriverManager.getConnection("jdbc:default:connection");
-        PreparedStatement ps1 =
-            conn.prepareStatement("update APPLICANT set name = ? "
-                + "where name = ?");
+        Connection conn = 
DriverManager.getConnection("jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("update APPLICANT set 
name = ? where name = ?");
         ps1.setString(1, name + suffix);
         ps1.setString(2, name);
         ps1.executeUpdate();
@@ -113,31 +77,17 @@ public class DerbyProcedureList extends 
         conn.close();
     }
 
-    public String callGetAllApplicants () {
-        return "{ call GET_ALL_APPLICANTS () }";
-    }
-
     public static void getAllApplicants(ResultSet[] rs1) throws Exception {
-        Connection conn =
-            DriverManager.getConnection("jdbc:default:connection");
-        PreparedStatement ps1 =
-            conn.prepareStatement("select * from APPLICANT");
+        Connection conn = 
DriverManager.getConnection("jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("select * from 
APPLICANT");
         rs1[0] = ps1.executeQuery();
 
         conn.close();
     }
 
-    public String callGetTwoApplicants () {
-        return "{ call GET_TWO_APPLICANTS (?, ?) }";
-    }
-
-    public static void getTwoApplicants(String name1, String name2,
-        ResultSet[] rs1) throws Exception {
-        Connection conn =
-            DriverManager.getConnection("jdbc:default:connection");
-        PreparedStatement ps1 =
-            conn.prepareStatement("select * from APPLICANT where name = ? "
-                + "or name = ?");
+    public static void getTwoApplicants(String name1, String name2, 
ResultSet[] rs1) throws Exception {
+        Connection conn = 
DriverManager.getConnection("jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("select * from APPLICANT 
where name = ? or name = ?");
         ps1.setString(1, name1);
         ps1.setString(2, name2);
         rs1[0] = ps1.executeQuery();
@@ -145,43 +95,32 @@ public class DerbyProcedureList extends 
         conn.close();
     }
 
-    public String callGetAllApplicantsAndGames () {
-        return "{ call GET_ALL_APPLICANTS_AND_GAMES () }";
+    public static void getAllApplicantsAndGames(ResultSet[] rs1, ResultSet[] 
rs2)
+        throws Exception {
+        Connection conn = 
DriverManager.getConnection("jdbc:default:connection");
+        PreparedStatement ps1 = conn.prepareStatement("select * from 
APPLICANT");
+        rs1[0] = ps1.executeQuery();
+
+        PreparedStatement ps2 = conn.prepareStatement("select * from GAME");
+        rs2[0] = ps2.executeQuery();
+
+        conn.close();
     }
 
-    public static void getAllApplicantsAndGames(ResultSet[] rs1, 
-        ResultSet[] rs2)
-        throws Exception {
+    public static void getTwoApplicantsAndGames(String name1, String name2,
+        ResultSet[] rs1, ResultSet[] rs2) throws Exception {
         Connection conn =
             DriverManager.getConnection("jdbc:default:connection");
         PreparedStatement ps1 =
-            conn.prepareStatement("select * from APPLICANT");
+            conn.prepareStatement("select * from APPLICANT where name = ?");
+        ps1.setString(1, name1);
         rs1[0] = ps1.executeQuery();
 
-        PreparedStatement ps2 = conn.prepareStatement("select * from GAME");
+        PreparedStatement ps2 =
+            conn.prepareStatement("select * from GAME where name = ?");
+        ps2.setString(2, name2);
         rs2[0] = ps2.executeQuery();
 
         conn.close();
     }
-
-//    public String callGetTwoApplicantsAndGames () {
-//        return "{ call GET_TWO_APPLICANTS_AND_GAMES (?, ?) }";
-//    }
-//
-//    public static void getTwoApplicantsAndGames(String name1, String name2,
-//        ResultSet[] rs1, ResultSet[] rs2) throws Exception {
-//        Connection conn =
-//            DriverManager.getConnection("jdbc:default:connection");
-//        PreparedStatement ps1 =
-//            conn.prepareStatement("select * from APPLICANT where name = ?");
-//        ps1.setString(1, name1);
-//        rs1[0] = ps1.executeQuery();
-//
-//        PreparedStatement ps2 =
-//            conn.prepareStatement("select * from GAME where name = ?");
-//        ps2.setString(2, name2);
-//        rs2[0] = ps2.executeQuery();
-//
-//        conn.close();
-//    }
 }

Modified: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java?rev=1476427&r1=1476426&r2=1476427&view=diff
==============================================================================
--- 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
 (original)
+++ 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/ProcedureList.java
 Fri Apr 26 22:37:18 2013
@@ -18,18 +18,32 @@
  */
 package org.apache.openjpa.persistence.jdbc.query.procedure;
 
-import java.util.List;
+import java.util.Collection;
 
+/**
+ * Provides a collection of stored procedure definitions
+ * to be tested.
+ * 
+ * @author ppoddar
+ *
+ */
 public interface ProcedureList {
-    public List<String> getCreateProcedureList();
-
-    public List<String> getDropProcedureList();
-
-    public String callAddXToCharlie();
-
-    public String callAddSuffixToName();
-
-    public String callGetAllApplicants();
+       /**
+        * Gets the collection of procedures to be tested.
+        */
+    public Collection<StoredProcedure> getProcedureList();
+
+    /**
+     * Gets the procedure of the given name.
+     * @throws RuntimeException if the procdure of the given name does not 
exist.
+     */
+    public StoredProcedure getProcedure(String name);
+    
+    /**
+     * Affirms if a procedure of the given name exists.
+     * @param name
+     * @return
+     */
+    public boolean contains(String name);
 
-    public String callGetTwoApplicants();
 }

Added: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java
URL: 
http://svn.apache.org/viewvc/openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java?rev=1476427&view=auto
==============================================================================
--- 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java
 (added)
+++ 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java
 Fri Apr 26 22:37:18 2013
@@ -0,0 +1,168 @@
+/*
+ * 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.openjpa.persistence.jdbc.query.procedure;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.openjpa.enhance.Reflection;
+
+
+/**
+ * A structure to hold the metadata about a stored proedure.
+ * Can generate the SQL to create, drop or call the procedure.
+ * 
+ * @author ppoddar
+ *
+ */
+public class StoredProcedure {
+       private StringBuilder _sql;
+       private String _name;
+       private List<String> _params = new ArrayList<String>();
+       private static enum Action {CREATE, DROP, CALL};
+       
+       /**
+        * Create a procedure of the given name.
+        */
+       public StoredProcedure(String name) {
+               _name = name;
+       }
+       
+       /**
+        * Gets the name of this procedure.
+        */
+       public String getName() {
+               return _name;
+       }
+       
+       /**
+        * Adds the given string as a parameter declaration.
+        */
+       public StoredProcedure addParameter(String p) {
+               _params.add(p);
+               return this;
+       }
+       
+       /**
+        * Gets the SQL for creating this procedure.
+        */
+       public String getCreateSQL() {
+               return getSQL(Action.CREATE);
+       }
+       
+       /**
+        * Gets the SQL for dropping this procedure.
+        */
+       public String getDropSQL() {
+               return getSQL(Action.DROP);
+       }
+       
+       /**
+        * Gets the SQL for calling this procedure.
+        */
+       public String getCallSQL() {
+               return getSQL(Action.CALL);
+       }
+       
+       /**
+        * Sets the SQL for this procedure.
+        */
+       public StoredProcedure setSQL(String sql) {
+               if (_sql == null) _sql = new StringBuilder();
+               else _sql.append(" ");
+               _sql.append(sql);
+               return this;
+       }
+       
+       /**
+        * Adds a read SQL statement via an external method.
+        * @param i number of result sets to be read
+        * @param cls the owning classof the method
+        * @param method name of the static method
+        * @param paramTypes argument type of the method
+        * @return
+        */
+       public StoredProcedure readSQL(int i, Class<?> cls, String method, 
Class<?>... paramTypes) {
+               assertStaticMethod(cls, method, paramTypes);
+               String fragment = "READS SQL DATA DYNAMIC RESULT SETS " + i + " 
EXTERNAL NAME'" + cls + '.' + method + "'";
+               return setSQL(fragment);
+       }
+       public StoredProcedure styleSQL(String lang) {
+               setSQL("PARAMETER STYLE " + lang + " LANGUAGE " + lang);
+               return this;
+       }
+       /**
+        * Adds a update SQL statement via an external method.
+        * @param cls the owning classof the method
+        * @param method name of the static method
+        * @param paramTypes argument type of the method
+        * @return
+        */
+       public StoredProcedure updateSQL(Class<?> cls, String method, 
Class<?>... paramTypes) {
+               assertStaticMethod(cls, method, paramTypes);
+               String fragment = "MODIFIES SQL DATA EXTERNAL NAME '" + cls + 
'.' + method + "'";
+               return setSQL(fragment);
+       }
+       /**
+        * Gets the SQL statement for the procedure for the given action.
+        */
+       private String getSQL(Action action) {
+               StringBuilder buf = new StringBuilder();
+               buf.append(action.toString().toLowerCase());
+               buf.append(action != Action.CALL ? " procedure " : " ");
+               buf.append(_name); buf.append(" ");
+               if (action == Action.CREATE) {
+                       buf.append(_params.isEmpty() ? "() " : "(");
+                       for (Iterator<String> p = _params.iterator(); 
p.hasNext();) {
+                               buf.append(p.next());
+                               buf.append(p.hasNext() ? "," : ") ");
+                       }
+                       
+                       buf.append(_sql);
+               } else if (action == Action.CALL) {
+                          buf.append("(");
+                          for (Iterator<String> p = _params.iterator(); 
p.hasNext();) {
+                                   p.next();
+                                       buf.append("?");
+                                       if (p.hasNext()) buf.append(",");
+                               }
+                          buf.append(")");
+               }
+               return buf.toString().trim();
+       }
+       
+       private void assertStaticMethod(Class<?> cls, String method, 
Class<?>...paramTypes) {
+               try {
+                       Method m = cls.getMethod(method, paramTypes);
+                       if (m == null || !Modifier.isStatic(m.getModifiers())) {
+                               throw new RuntimeException("No static method " 
+ method + " with arguments " 
+                                               + Arrays.toString(paramTypes) + 
" in " + cls);
+                       }
+               } catch (Exception ex) {
+                       throw new RuntimeException("No static method " + method 
+ " with arguments " 
+                                       + Arrays.toString(paramTypes) + " in " 
+ cls, ex);
+               }
+       }
+       
+       
+}
\ No newline at end of file

Propchange: 
openjpa/sandboxes/21/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/query/procedure/StoredProcedure.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to