Package org.jpox.store.query

Source Code of org.jpox.store.query.AbstractJPQLQuery

/**********************************************************************
Copyright (c) 2007 Andy Jefferson and others. All rights reserved.
Licensed 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.

Contributors:
    ...
**********************************************************************/
package org.jpox.store.query;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.jpox.ObjectManager;
import org.jpox.exceptions.JPOXUserException;
import org.jpox.metadata.AbstractClassMetaData;

/**
* Abstract representation of a JPQL query used by JPOX.
* The query can be specified via method calls, or via a single-string form.
*
* @see Query
*/
public abstract class AbstractJPQLQuery extends AbstractJavaQuery
{
    /**
     * Constructor.
     * @param om ObjectManager
     */
    public AbstractJPQLQuery(ObjectManager om)
    {
        super(om);
    }

    /**
     * Method to take the defined parameters for the query and form a single string.
     * This is used to print out the query for logging.
     * @return The single string
     */
    public String getSingleStringQuery()
    {
        if (singleString != null)
        {
            return singleString;
        }

        StringBuffer str = new StringBuffer();
        if (type == BULK_UPDATE)
        {
            str.append("UPDATE " + update + " ");
        }
        else if (type == BULK_DELETE)
        {
            str.append("DELETE ");
        }
        else
        {
            str.append("SELECT ");
        }

        if (result != null)
        {
            str.append(result + " ");
        }
        if (from != null)
        {
            str.append("FROM " + from + " ");
        }
        if (filter != null)
        {
            str.append("WHERE " + filter + " ");
        }
        if (grouping != null)
        {
            str.append("GROUP BY " + grouping + " ");
        }
        if (having != null)
        {
            str.append("HAVING " + having + " ");
        }
        if (ordering != null)
        {
            str.append("ORDER BY " + ordering + " ");
        }

        singleString = str.toString().trim();
        return singleString;
    }

    /**
     * Execute the query to delete persistent objects.
     * @param parameters the Map containing all of the parameters.
     * @return the number of deleted objects.
     */
    protected long performDeletePersistentAll(Map parameters)
    {
        // Save the required value for unique (find the instances without it)
        boolean requiresUnique = unique;
        if (unique)
        {
            unique = false;
        }
   
        Collection results = (Collection)performExecute(parameters);
        int number = 0;
        if (results == null)
        {
            number = 0;
        }
        else
        {
            // TODO : To make this most efficient we shouldn't instantiate things into memory
            // but instead check if the object to be deleted implements DeleteCallback, or there are
            // any lifecycle listeners for this object type, or if there are any dependent fields
            // and only then do we instantiate it.
            number = results.size();
            if (requiresUnique && number > 1)
            {
                throw new JPOXUserException(LOCALISER.msg("021032"));
            }
   
            // Instances to be deleted are flushed first (JDO2 [14.8-4])
            Iterator resultsIter = results.iterator();
            while (resultsIter.hasNext())
            {
                om.findStateManager(resultsIter.next()).flush();
            }
   
            // Perform the deletion
            om.deleteObjects(results);
   
            // TODO Do we need to remove these from the PM.queryRun store and this.queryResults ?
            ((QueryResult)results).close();
        }
        return number;
    }

    /**
     * Utility to resolve the declaration to a particular class.
     * Takes the passed in name, together with the defined import declarations and returns the
     * class represented by the declaration.
     * @param classDecl The declaration
     * @return The class it resolves to (if any)
     * @throws JPOXUserException Thrown if the class cannot be resolved.
     */
    public Class resolveClassDeclaration(String classDecl)
    {
        // Try to find an entity name before relaying to the superclass method
        AbstractClassMetaData acmd = this.getStoreManager().getOMFContext().getMetaDataManager().getMetaDataForEntityName(classDecl);
        if (acmd != null)
        {
            classDecl = acmd.getFullClassName();
        }

        return super.resolveClassDeclaration(classDecl);
    }
}
TOP

Related Classes of org.jpox.store.query.AbstractJPQLQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.