Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**********************************************************************
Copyright (c) 2008 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.datanucleus.store.db4o.query;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ObjectManager;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.query.compiler.QueryCompilation;
import org.datanucleus.query.evaluator.JPQLEvaluator;
import org.datanucleus.query.evaluator.JavaQueryEvaluator;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.db4o.DB4OStoreManager;
import org.datanucleus.store.db4o.DB4OUtils;
import org.datanucleus.store.query.AbstractJPQLQuery;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import com.db4o.ObjectContainer;
import com.db4o.query.Candidate;
import com.db4o.query.Evaluation;
import com.db4o.query.Query;
/**
* DB4O representation of a JPQL query for use by DataNucleus.
* The query can be specified via method calls, or via a single-string form.
*/
public class JPQLQuery extends AbstractJPQLQuery
{
protected static final Localiser LOCALISER_DB4O = Localiser.getInstance(
"org.datanucleus.store.db4o.Localisation", DB4OStoreManager.class.getClassLoader());
/**
* Constructs a new query instance that uses the given persistence manager.
* @param om the associated ObjectManager for this query.
*/
public JPQLQuery(ObjectManager om)
{
this(om, (JPQLQuery) null);
}
/**
* Constructs a new query instance having the same criteria as the given query.
* @param om The ObjectManager
* @param q The query from which to copy criteria.
*/
public JPQLQuery(ObjectManager om, JPQLQuery q)
{
super(om, q);
}
/**
* Constructor for a JPQL query where the query is specified using the "Single-String" format.
* @param om The persistence manager
* @param query The query string
*/
public JPQLQuery(ObjectManager om, String query)
{
super(om, query);
}
protected Object performExecute(Map parameters)
{
ClassLoaderResolver clr = om.getClassLoaderResolver();
if (candidateCollection != null && candidateCollection.isEmpty())
{
return Collections.EMPTY_LIST;
}
boolean inMemory = evaluateInMemory();
ManagedConnection mconn = om.getStoreManager().getConnection(om);
try
{
ObjectContainer cont = (ObjectContainer) mconn.getConnection();
// Execute the query
long startTime = 0;
if (NucleusLogger.QUERY.isDebugEnabled())
{
startTime = System.currentTimeMillis();
NucleusLogger.QUERY.debug(LOCALISER.msg("021046", "JPQL", getSingleStringQuery(), null));
}
List candidates = null;
boolean filterInMemory = false;
boolean orderingInMemory = false;
if (candidateCollection == null)
{
// Create the SODA query, optionally with the candidate and filter restrictions
Query query = createSODAQuery(cont, compilation, parameters, inMemory);
candidates = query.execute();
if (inMemory)
{
filterInMemory = true;
orderingInMemory = true;
}
}
else
{
candidates = (List)candidateCollection;
filterInMemory = true;
orderingInMemory = true;
}
// Apply any restrictions to the results (that we can't use in the input SODA query)
JavaQueryEvaluator resultMapper =
new JPQLEvaluator(this, candidates, compilation, parameters, clr);
Collection results = resultMapper.execute(filterInMemory, orderingInMemory, true, true, true);
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug(LOCALISER.msg("021074", "JPQL", "" + (System.currentTimeMillis() - startTime)));
}
// Assign StateManagers to any returned objects
Iterator iter = results.iterator();
while (iter.hasNext())
{
Object obj = iter.next();
AbstractClassMetaData cmd = om.getMetaDataManager().getMetaDataForClass(obj.getClass(), clr);
DB4OUtils.prepareDB4OObjectForUse(obj, om, cont, cmd, (DB4OStoreManager)om.getStoreManager());
}
if (type == BULK_DELETE)
{
iter = results.iterator();
while (iter.hasNext())
{
Object obj = iter.next();
om.deleteObject(obj);
}
return new Long(results.size());
}
else if (type == BULK_UPDATE)
{
throw new NucleusException("Bulk Update is not yet supported");
}
else
{
return results;
}
}
finally
{
mconn.release();
}
}
/**
* Method to create the SODA query object for the candidate class, and with the possible
* restrictions we can apply to the filter.
* @param cont ObjectContainer
* @param compilation The compilation
* @param parameters Any parameters
* @param inMemory whether to process everything in-memory
* @return The DB4O SODA Query
*/
private Query createSODAQuery(ObjectContainer cont, QueryCompilation compilation, Map parameters,
boolean inMemory)
{
Query query = cont.query();
query.constrain(candidateClass);
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug(LOCALISER_DB4O.msg("DB4O.SODA.Query",
"Query query = cont.query()"));
NucleusLogger.QUERY.debug(LOCALISER_DB4O.msg("DB4O.SODA.Query",
"query.constrain(" + candidateClass.getName() + ")"));
}
if (!subclasses)
{
query.constrain(new Evaluation()
{
public void evaluate(Candidate c)
{
c.include(c.getObject().getClass() == candidateClass);
}
});
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug(LOCALISER_DB4O.msg("DB4O.SODA.Query",
"query.constrain(new Evaluation(){ " +
"public void evaluate(Candidate c){ " +
"c.include(c.getObject().getClass() == " + candidateClass.getName() + ");}" +
"});"));
}
}
if (!inMemory)
{
// Constrain the query with filter and ordering constraints
new QueryToSODAMapper(query, compilation, parameters).compile();
}
return query;
}
}