comments inline

  ----- Original Message ----- 
  From: Craig L Russell 
  To: [email protected] 
  Sent: Tuesday, August 15, 2006 11:21 AM
  Subject: Query interface for Datamapper


  Hi,


  Here's what I propose as a starting point for the query interface between 
Datamapper Managers and Datamapper Persistence. 


  There are enough differences between JDO, JPA, and Hibernate that I found it 
really awkward to define a set of methods on PersistenceStrategy that covered 
the functionality. In particular, there are two methods in JPA that both 
execute the query and determine the result shape, and parameters are passed one 
by one. In JDO, there is an API that determines the result shape and one method 
to execute the query, passing parameter values. It's trivial to encapsulate 
these differences in a Query instance. 


  I've included the Query API below for discussion, along with calling sequence 
from Mapper.


  Craig


  public class DatamapperPersistenceStrategy {
  ...
       /**
       * Create query.
       * @param clazz the class of instances to find
       * @param queryName the name of the query
       * @throws org.apache.roller.RollerException on any error
       */


      public DatamapperQuery newQuery(Class clazz, String queryName)
              throws RollerException;
  }
This confused me.  Shouldn't this be an interface?  Wouldn't each persistence 
strategy be a factory implementing this interface ?



  public class DatamapperUserManagerImpl {
  ...
      public WebsiteData getWebsiteByHandle(String handle, Boolean enabled) 
              throws RollerException {
          // XXX cache websites by handle?
          return (WebsiteData)strategy.newQuery(WebsiteData.class,
                  "getByHandle&&Enabled")
              .execute(new Object[]{handle, enabled});
      }

OK.  I got the pattern here.  And here you do exactly what I thought you would, 
using the strategy as the factory implementation.

The earlier idea was to use just say "strategy.getWebsiteByHandle(handle, 
enabled)", defining each such method on a PersistenceStrategy interface and 
implementing them in each strategy.  This would probably turn out to make the 
strategy class huge and unwieldy, but there might be a typesafe alternative



                


  public interface DatamapperQuery {


      /** Execute the query with no parameters.
       * @return the results of the query
       */
      Object execute();


      /** Execute the query with one parameter.
       * @param param the parameter
       * @return the results of the query
       */
      Object execute(Object param);


      /** Execute the query with parameters.
       * @param params the parameters
       * @return the results of the query
       */
      Object execute(Object[] params);


      /** Remove instances selected by the query with no parameters.
       * @return the results of the query
       */
      void removeAll();


      /** Remove instances selected by the query with one parameter.
       * @param param the parameter
       * @return the results of the query
       */
      void removeAll(Object param);


      /** Remove instances selected by the query with parameters.
       * @param params the parameters
       * @return the results of the query
       */
      void removeAll(Object[] params);


      /** Set the result to be a single instance (not a List).
       * @result the instance on which this method is called
       */
      DatamapperQuery setUnique();


      /** Set the types of the parameters. This is only needed if the 
       * parameter types are temporal types, e.g. Date, Time, Calendar.
       * @param the types of the parameters in corresponding positions.
       * @result the instance on which this method is called
       */
      DatamapperQuery setTypes(Object[] types);


      /** Set the range of results for this query.
       * @fromIncl the beginning row number
       * @toExcl the ending row number
       * @return the instance on which this method is called
       */
      DatamapperQuery setRange(long fromIncl, long toExcl);
  }





  Craig Russell
  [EMAIL PROTECTED] http://db.apache.org/jdo



Reply via email to