You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/05/23 14:04:01 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/jdo QueryImpl.java PersistenceManagerImpl.java

tomdz       2004/05/23 05:04:01

  Modified:    src/java/org/apache/ojb/jdo QueryImpl.java
                        PersistenceManagerImpl.java
  Log:
  Integrated jdoql parsing into the query impl
  Removed dependency of the query impl to OTMConnection for now (the query can get it from the persistence manager impl if needed)
  
  Revision  Changes    Path
  1.10      +324 -149  db-ojb/src/java/org/apache/ojb/jdo/QueryImpl.java
  
  Index: QueryImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/QueryImpl.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- QueryImpl.java	22 May 2004 11:28:47 -0000	1.9
  +++ QueryImpl.java	23 May 2004 12:04:01 -0000	1.10
  @@ -14,18 +14,18 @@
    * See the License for the specific language governing permissions and
    * limitations under the License.
    */
  -import org.apache.commons.collections.CollectionUtils;
  -import org.apache.ojb.broker.query.Criteria;
  -import org.apache.ojb.broker.query.QueryByCriteria;
  -import org.apache.ojb.broker.query.QueryFactory;
  -import org.apache.ojb.otm.OTMConnection;
   
   import javax.jdo.Extent;
  +import javax.jdo.JDOUserException;
   import javax.jdo.PersistenceManager;
   import javax.jdo.Query;
  -import java.util.Collection;
  -import java.util.HashSet;
  -import java.util.Map;
  +
  +import org.apache.ojb.jdo.jdoql.Expression;
  +import org.apache.ojb.jdo.jdoql.LocalVariable;
  +import org.apache.ojb.jdo.jdoql.QueryParsingHelper;
  +import org.apache.ojb.jdo.jdoql.QueryTreeResolver;
  +
  +import java.util.*;
   
   /**
    * Not Really Functional Yet:
  @@ -41,172 +41,347 @@
    *
    * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
    * @author <a href="mailto:brianm@apache.org">Brian McCallister</a>
  - * @version $Id$
  + * @author <a href="mailto:tomdz@apache.org">Thomas Dudziak</a>
    */
   
   public class QueryImpl implements Query
   {
  -    private PersistenceManager m_pm;
  -    private OTMConnection m_conn;
  -    private Criteria m_criteria;
  -    private Class m_searchClass;
  -    private Collection m_candidates;
  -    private String m_filter;
  -    private String m_imports;
  -    private String m_parameters;
  -    private String m_ordering;
  -    private String m_variables;
  -    private boolean m_ignoreCache;
  -    private boolean m_compiled = false;
  -
  +    /** The persistence manager this query is bound to */
  +    private PersistenceManagerImpl _persistenceManager;
  +    /** The searched class */
  +    private Class _searchedClass;
  +    /** Candidate instances */
  +    private Collection _candidateInstances;
  +    /** The original filter string */
  +    private String _filterString;
  +    /** The parsed filter expression */
  +    private Expression _filterExpression;
  +    /** The original imports string */
  +    private String _importString;
  +    /** The imports */
  +    private Collection _imports;
  +    /** The original parameter string */
  +    private String _parameterString;
  +    /** The parameters */
  +    private Map _parameters;
  +    /** The original variables string */
  +    private String _variableString;
  +    /** The variables */
  +    private Map _variables;
  +    /** The original ordering string */
  +    private String _orderingString;
  +    /** The orderings */
  +    private Collection _orderings;
  +    /** Whether to ignore the cache while processing this query */
  +    private boolean _ignoreCache;
  +    /** Whether this query must be resolved and compiled first */
  +    private boolean _needsCompilation = true;
   
  -    QueryImpl(PersistenceManager pm, OTMConnection conn)
  +    /**
  +     * Creates a new query that uses the given persistence manager.
  +     * 
  +     * @param pm The persistence manager to use
  +     */
  +    public QueryImpl(PersistenceManagerImpl pm)
       {
  -        this.m_pm = pm;
  -        this.m_conn = conn;
  -        this.m_candidates = null;
  +        _persistenceManager = pm;
  +        _candidateInstances = null;
       }
   
       /**
  -     * @return uncompiled clone of this query
  +     * Returns the persistence manager that this query uses.
  +     * 
  +     * @return The persistence manager
        */
  -    QueryImpl ojbClone()
  +    public PersistenceManager getPersistenceManager()
       {
  -        QueryImpl query = new QueryImpl(m_pm, m_conn);
  -        query.m_candidates = m_candidates;
  -        query.m_searchClass = m_searchClass;
  -        query.m_filter = m_filter;
  -        query.m_imports = m_imports;
  -        query.m_parameters = m_parameters;
  -        query.m_ordering = m_ordering;
  -        query.m_variables = m_variables;
  -        return query;
  +        return _persistenceManager;
       }
   
  -	public void setClass(Class aClass)
  -	{
  -        this.m_searchClass = aClass;
  -        this.m_compiled = false;
  -	}
  +    /**
  +     * Sets the class whose objects this query searches for.
  +     * 
  +     * @param searchedClass The class of the searched objects
  +     */
  +    public void setClass(Class searchedClass)
  +    {
  +        _searchedClass    = searchedClass;
  +        _needsCompilation = true;
  +    }
   
       /**
  +     * Returns the class of the searched objects.
  +     * 
  +     * @return The class of the searched objects
  +     */
  +    public Class getSearchedClass()
  +    {
  +        return _searchedClass;
  +    }
  +    
  +    /**
        * Simply ovewrites the setClass(..) value
        */
  -	public void setCandidates(Extent extent)
  -	{
  -        this.m_searchClass = ((ExtentImpl)extent).ojbGetClass();
  -        this.m_compiled = false;
  -	}
  -
  -	public void setCandidates(Collection collection)
  -	{
  -        if (m_candidates == null) m_candidates = new HashSet();
  -        this.m_candidates.addAll(collection);
  -        this.m_compiled = false;
  -	}
  -
  -	public void setFilter(String s)
  -	{
  -        this.m_filter = s;
  -        this.m_compiled = false;
  -	}
  -
  -	public void declareImports(String s)
  -	{
  -        this.m_imports = s;
  -        this.m_compiled = false;
  -	}
  -
  -	public void declareParameters(String s)
  -	{
  -        this.m_parameters = s;
  -        this.m_compiled = false;
  -	}
  -
  -	public void declareVariables(String s)
  -	{
  -        this.m_variables = s;
  -        this.m_compiled = false;
  -	}
  -
  -	public void setOrdering(String s)
  -	{
  -        this.m_ordering = s;
  -        this.m_compiled = false;
  -	}
  -
  -    /**
  -     * @todo presently ignored
  -     */
  -	public void setIgnoreCache(boolean b)
  -	{
  -        m_ignoreCache = b;
  -        this.m_compiled = false;
  -	}
  -
  -	public boolean getIgnoreCache()
  -	{
  -		return m_ignoreCache;
  -	}
  -
  -    /**
  -     * @todo verify correctness of all the Strings is required here
  -     */
  -	public void compile()
  -	{
  -        m_criteria = new Criteria();
  -	}
  -
  -	public Object execute()
  -	{
  -        if (!m_compiled) this.compile();
  -        QueryByCriteria query = QueryFactory.newQuery(m_searchClass, this.m_criteria);
  -		Collection results =  m_conn.getCollectionByQuery(query);
  -        if (m_candidates != null) return CollectionUtils.intersection(results, this.m_candidates);
  -        else return results;
  -	}
  +    public void setCandidates(Extent extent)
  +    {
  +        _searchedClass    = ((ExtentImpl)extent).ojbGetClass();
  +        _needsCompilation = true;
  +    }
  +
  +    public void setCandidates(Collection candidates)
  +    {
  +        _candidateInstances = candidates;
  +        _needsCompilation   = true;
  +    }
  +
  +    /**
  +     * Sets the filter of this query.
  +     * 
  +     * @param filter The filter expression
  +     */
  +    public void setFilter(String filter) throws JDOUserException
  +    {
  +        _filterString     = filter;
  +        _filterExpression = new QueryParsingHelper().parseFilter(filter);
  +        _needsCompilation = true;
  +    }
  +
  +    /**
  +     * Returns the filter expression.
  +     * 
  +     * @return The filter expression
  +     */
  +    public Expression getFilterExpression()
  +    {
  +        return _filterExpression;
  +    }
  +
  +    /**
  +     * Specifies the classes/packages imported by this query for use in the filter
  +     * and ordering expressions.
  +     * 
  +     * @param imports The import declarations
  +     */
  +    public void declareImports(String imports) throws JDOUserException
  +    {
  +        _importString     = imports;
  +        _imports          = new QueryParsingHelper().parseImports(imports);
  +        _needsCompilation = true;
  +    }
  +
  +    /**
  +     * Returns the imports of this query.
  +     * 
  +     * @return The imports, a collection of {@link org.apache.ojb.jdo.jdoql.Import} objects
  +     */
  +    public Collection getImports()
  +    {
  +        return _imports;
  +    }
  +
  +    /**
  +     * Sets the parameters of this query.
  +     * 
  +     * @param params The parameter declarations
  +     */
  +    public void declareParameters(String params) throws JDOUserException
  +    {
  +        _parameterString  = params;
  +        _parameters       = new QueryParsingHelper().parseParameters(params);
  +        _needsCompilation = true;
  +    }
  +
  +    /**
  +     * Returns the parameters of this query.
  +     * 
  +     * @return The parameters, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
  +     *         indexed by their names
  +     */
  +    public Map getParameters()
  +    {
  +        return _parameters;
  +    }
  +
  +    /**
  +     * Returns the parameter of the given name if it exists.
  +     * 
  +     * @param name The parameter name
  +     * @return The parameter
  +     */
  +    public LocalVariable getParameter(String name)
  +    {
  +        return (LocalVariable)_variables.get(name);
  +    }
  +
  +    /**
  +     * Declares the variables used in the filter expression of this query.
  +     * 
  +     * @param variables The variable declarations
  +     */
  +    public void declareVariables(String variables) throws JDOUserException
  +    {
  +        _variableString   = variables;
  +        _variables        = new QueryParsingHelper().parseVariables(variables);
  +        _needsCompilation = true;
  +    }
  +
  +    /**
  +     * Returns the variables of this query.
  +     * 
  +     * @return The variables, a map of {@link org.apache.ojb.jdo.jdoql.LocalVariable} objects
  +     *         indexed by their names
  +     */
  +    public Map getVariables()
  +    {
  +        return _variables;
  +    }
  +
  +    /**
  +     * Returns the variable of the given name if it exists.
  +     * 
  +     * @param name The variable name
  +     * @return The variable
  +     */
  +    public LocalVariable getVariable(String name)
  +    {
  +        return (LocalVariable)_variables.get(name);
  +    }
  +    
  +    /**
  +     * Defines the ordering of this query.
  +     * 
  +     * @param orderings The ordering specifications
  +     */
  +    public void setOrdering(String orderings) throws JDOUserException
  +    {
  +        _orderingString   = orderings;
  +        _orderings        = new QueryParsingHelper().parseOrderings(orderings);
  +        _needsCompilation = true;
  +    }
  +
  +    /**
  +     * Returns the orderings of this query.
  +     * 
  +     * @return The orderings, a collection of {@link org.apache.ojb.jdo.jdoql.Ordering} objects
  +     */
  +    public Collection getOrderings()
  +    {
  +        return _orderings;
  +    }
  +
  +    /**
  +     * Specifies whether the query should ignore any objects in the cache.
  +     * 
  +     * @param shouldIgnoreCache Whether to ignore the cache
  +     */
  +    public void setIgnoreCache(boolean shouldIgnoreCache)
  +    {
  +        _ignoreCache = shouldIgnoreCache;
  +    }
  +
  +    /**
  +     * Determines whether this query ignores objects in the cache.
  +     * 
  +     * @return <code>true</code> if this query ignores cached objects
  +     */
  +    public boolean getIgnoreCache()
  +    {
  +        return _ignoreCache;
  +    }
  +
  +    /**
  +     * Compiles the query. In effect this resolves the various expressions and
  +     * declarations against each other, checks that they are valid, and enhances
  +     * the filter and ordering expressions with executable ojb queries.
  +     */
  +    public void compile()
  +    {
  +        if (_needsCompilation)
  +        {
  +            // first we resolve this query 
  +            new QueryTreeResolver().resolveAndCheck(this);
  +            // TODO: next the filter and ordering expressions are enhanced with
  +            //       actual database queries, e.g. like this:
  +            // new QueryCompiler().compile(this);
  +            //       which adds ojb queries to the filter expressions
  +            //       (including the ordering) 
  +            throw new UnsupportedOperationException("Not yet implemented");
  +
  +            // _needsCompilation = false;
  +        }
  +    }
  +
  +    /**
  +     * Performs this query.
  +     * 
  +     * @return The query result
  +     */
  +    public Object execute()
  +    {
  +        if (_needsCompilation)
  +        {
  +            compile();
  +        }
  +        throw new UnsupportedOperationException("Not yet implemented");
  +    }
   
       /**
        * @todo implement
        */
  -	public Object execute(Object o)
  -	{
  +    public Object execute(Object o)
  +    {
           throw new UnsupportedOperationException("Not yet implemented!");
  -  	}
  +    }
   
  -	public Object execute(Object o, Object o1)
  -	{
  -		throw new UnsupportedOperationException("Not yet implemented!");
  -	}
  -
  -	public Object execute(Object o, Object o1, Object o2)
  -	{
  -		throw new UnsupportedOperationException("Not yet implemented!");
  -	}
  -
  -	public Object executeWithMap(Map map)
  -	{
  -		throw new UnsupportedOperationException("Not yet implemented!");
  -	}
  -
  -	public Object executeWithArray(Object[] objects)
  -	{
  -		throw new UnsupportedOperationException("Not yet implemented!");
  -	}
  -
  -	public PersistenceManager getPersistenceManager()
  -	{
  -		return m_pm;
  -	}
  +    public Object execute(Object o, Object o1)
  +    {
  +        throw new UnsupportedOperationException("Not yet implemented!");
  +    }
  +
  +    public Object execute(Object o, Object o1, Object o2)
  +    {
  +        throw new UnsupportedOperationException("Not yet implemented!");
  +    }
  +
  +    public Object executeWithMap(Map map)
  +    {
  +        throw new UnsupportedOperationException("Not yet implemented!");
  +    }
  +
  +    public Object executeWithArray(Object[] objects)
  +    {
  +        throw new UnsupportedOperationException("Not yet implemented!");
  +    }
   
       /**
        * @param o is a Collection returned from execute()
        */
  -	public void close(Object o)
  -	{
  -	}
  -
  -	public void closeAll()
  -	{
  -	}
  +    public void close(Object o)
  +    {
  +    }
  +
  +    public void closeAll()
  +    {
  +    }
  +
  +    /**
  +     * Creates an uncompiled deep clone of this query.
  +     * 
  +     * @return The clone
  +     */
  +    QueryImpl ojbClone()
  +    {
  +        QueryImpl query = new QueryImpl(_persistenceManager);
  +
  +        query.setClass(_searchedClass);
  +        query.setCandidates(_candidateInstances);
  +        query.declareImports(_importString);
  +        query.declareParameters(_parameterString);
  +        query.declareVariables(_variableString);
  +        query.setFilter(_filterString);
  +        query.setOrdering(_orderingString);
  +
  +        return query;
  +    }
  +
   }
  
  
  
  1.13      +7 -7      db-ojb/src/java/org/apache/ojb/jdo/PersistenceManagerImpl.java
  
  Index: PersistenceManagerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/jdo/PersistenceManagerImpl.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- PersistenceManagerImpl.java	3 May 2004 00:12:02 -0000	1.12
  +++ PersistenceManagerImpl.java	23 May 2004 12:04:01 -0000	1.13
  @@ -289,7 +289,7 @@
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery()"));
           }
   
  -        return new QueryImpl(this, m_conn);
  +        return new QueryImpl(this);
       }
   
       public Query newQuery(Object o)
  @@ -324,7 +324,7 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class)"));
           }
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setClass(aClass);
           return query;
       }
  @@ -336,7 +336,7 @@
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Extent)"));
           }
   
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setCandidates(extent);
           return query;
       }
  @@ -347,7 +347,7 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, Collection)"));
           }
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setCandidates(collection);
           query.setClass(aClass);
           return query;
  @@ -359,7 +359,7 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, String)"));
           }
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setClass(aClass);
           query.setFilter(s);
           return query;
  @@ -371,7 +371,7 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Class, Collection, String)"));
           }
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setCandidates(collection);
           query.setClass(aClass);
           query.setFilter(s);
  @@ -384,7 +384,7 @@
           {
               throw new JDOFatalUserException(generateIsClosedErrorMessage("newQuery(Extent, String)"));
           }
  -        Query query = new QueryImpl(this, m_conn);
  +        Query query = new QueryImpl(this);
           query.setCandidates(extent);
           query.setFilter(s);
           return query;
  
  
  

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