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) 2007 Erik Bengtson 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:
2008 Andy Jefferson - compilation process
...
**********************************************************************/
package org.datanucleus.store.db4o.query;
import java.util.ArrayList;
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.metadata.AbstractClassMetaData;
import org.datanucleus.query.compiler.QueryCompilation;
import org.datanucleus.query.evaluator.JDOQLEvaluator;
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.AbstractJDOQLQuery;
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 JDOQL query for use by DataNucleus.
* The query can be specified via method calls, or via a single-string form.
*/
public class JDOQLQuery extends AbstractJDOQLQuery
{
/**
* Constructs a new query instance that uses the given persistence manager.
* @param om the associated ObjectManager for this query.
*/
public JDOQLQuery(ObjectManager om)
{
this(om, (JDOQLQuery) 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 JDOQLQuery(ObjectManager om, JDOQLQuery q)
{
super(om, q);
}
/**
* Constructor for a JDOQL query where the query is specified using the "Single-String" format.
* @param om The persistence manager
* @param query The query string
*/
public JDOQLQuery(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);
ObjectContainer cont = (ObjectContainer) mconn.getConnection();
try
{
// Execute the query
long startTime = System.currentTimeMillis();
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug(LOCALISER.msg("021046", "JDOQL", 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 = new ArrayList(candidateCollection);
filterInMemory = true;
orderingInMemory = true;
}
// Apply any restrictions to the results (that we can't use in the input SODA query)
JavaQueryEvaluator resultMapper =
new JDOQLEvaluator(this, candidates, compilation, parameters, clr);
Collection results = resultMapper.execute(filterInMemory, orderingInMemory, true, true, true);
if (NucleusLogger.QUERY.isDebugEnabled())
{
NucleusLogger.QUERY.debug(LOCALISER.msg("021074", "JDOQL", "" + (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());
}
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 (!subclasses)
{
query.constrain(new Evaluation()
{
public void evaluate(Candidate c)
{
c.include(c.getObject().getClass() == candidateClass);
}
});
}
if (!inMemory)
{
// Constrain the query with filter and ordering constraints
new QueryToSODAMapper(query, compilation, parameters).compile();
}
return query;
}
}