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:
...
**********************************************************************/
package org.datanucleus.api.jdo;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.jdo.Extent;
import javax.jdo.FetchPlan;
import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.JDOFatalUserException;
import javax.jdo.JDOQueryInterruptedException;
import javax.jdo.JDOUnsupportedOptionException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.spi.JDOPermission;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.metadata.QueryMetaData;
import org.datanucleus.store.query.NoQueryResultsException;
import org.datanucleus.store.query.QueryInterruptedException;
import org.datanucleus.store.query.QueryTimeoutException;
import org.datanucleus.util.Localiser;
/**
* Wrapper for JDO Query class.
* Stores the PM the query is executed against, the internal query, and the query language.
* The language is stored since it is referenced by the JDO API and so we don't have to embody knowledge
* of which internal query type is for which language (could be moved to the internal query).
* @param Candidate class for this query
*/
public class JDOQuery implements Query
{
public static final String PROPERTY_CLOSEABLE_QUERY = "datanucleus.query.closeable";
private static final long serialVersionUID = -204134873012573162L;
public static final String JDOQL_QUERY_LANGUAGE = "javax.jdo.query.JDOQL";
public static final String JPQL_QUERY_LANGUAGE = "javax.jdo.query.JPQL";
public static final String SQL_QUERY_LANGUAGE = "javax.jdo.query.SQL";
/** PersistenceManager for the query. */
transient PersistenceManager pm;
/** Underlying query that will be executed. */
org.datanucleus.store.query.Query query;
private boolean closed = false;
/** Query language. */
String language;
/** JDO Fetch Plan. */
JDOFetchPlan fetchPlan = null;
/** Map of parameters keyed by their name. */
Map parameterValueByName = null;
/** Positional parameter values. */
Object[] parameterValues = null;
/**
* Constructor for a query used by JDO.
* @param pm PersistenceManager
* @param query Underlying query
* @param language Query language
*/
public JDOQuery(PersistenceManager pm, org.datanucleus.store.query.Query query, String language)
{
this.pm = pm;
this.query = query;
this.language = language;
}
public void close()
{
if (closed)
{
return;
}
closeAll();
Boolean closeableQuery = query.getExecutionContext().getBooleanProperty(PROPERTY_CLOSEABLE_QUERY);
if (closeableQuery == Boolean.TRUE)
{
// User has requested a closeable Query, so release connection to PM and underlying query etc
if (this.fetchPlan != null)
{
this.fetchPlan.clearGroups();
this.fetchPlan = null;
}
this.parameterValueByName = null;
this.parameterValues = null;
this.pm = null;
this.query = null;
this.closed = true;
}
}
/**
* Accessor for whether this Query is closed.
* @return Whether this Query is closed.
*/
public boolean isClosed()
{
return closed;
}
/**
* Close the query result.
* @param queryResult Query result
*/
public void close(Object queryResult)
{
assertIsOpen();
query.close(queryResult);
}
/**
* Close all query results for this query.
*/
public void closeAll()
{
assertIsOpen();
query.closeAll();
}
/**
* Compile the query.
*/
public void compile()
{
assertIsOpen();
try
{
query.compile();
}
catch (NucleusException jpe)
{
throw JDOAdapter.getJDOExceptionForNucleusException(jpe);
}
}
/**
* Declare any imports for the query.
* @param imports The imports
*/
public void declareImports(String imports)
{
assertIsOpen();
try
{
query.declareImports(imports);
}
catch (NucleusException jpe)
{
throw JDOAdapter.getJDOExceptionForNucleusException(jpe);
}
}
/**
* Declare any parameters for the query.
* @param parameters The parameters
*/
public void declareParameters(String parameters)
{
assertIsOpen();
try
{
query.declareExplicitParameters(parameters);
}
catch (NucleusException jpe)
{
throw JDOAdapter.getJDOExceptionForNucleusException(jpe);
}
}
/**
* Declare any variables for the query.
* @param variables The variables
*/
public void declareVariables(String variables)
{
assertIsOpen();
try
{
query.declareExplicitVariables(variables);
}
catch (NucleusException jpe)
{
throw JDOAdapter.getJDOExceptionForNucleusException(jpe);
}
}
public Query imports(String imports)
{
assertIsOpen();
declareImports(imports);
return this;
}
public Query parameters(String parameters)
{
assertIsOpen();
declareParameters(parameters);
return this;
}
public Query variables(String variables)
{
assertIsOpen();
declareVariables(variables);
return this;
}
public Query setParameters(Object... paramValues)
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = paramValues;
return this;
}
public Query setNamedParameters(Map paramMap)
{
assertIsOpen();
this.parameterValueByName = paramMap;
this.parameterValues = null;
return this;
}
/**
* Execute the query.
* @return The results
*/
public Object execute()
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = null;
return executeInternal();
}
/**
* Execute the query.
* @param p1 First param value
* @return The results
*/
public Object execute(Object p1)
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = new Object[]{p1};
return executeInternal();
}
/**
* Execute the query.
* @param p1 First param value
* @param p2 Second param value
* @return The results
*/
public Object execute(Object p1, Object p2)
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = new Object[]{p1, p2};
return executeInternal();
}
/**
* Execute the query.
* @param p1 First param value
* @param p2 Second param value
* @param p3 Third param value
* @return The results
*/
public Object execute(Object p1, Object p2, Object p3)
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = new Object[]{p1, p2, p3};
return executeInternal();
}
/**
* Execute the query.
* @param parameterValues Param values
* @return The results
*/
public Object executeWithArray(Object... parameterValues)
{
assertIsOpen();
this.parameterValueByName = null;
this.parameterValues = parameterValues;
return executeInternal();
}
/**
* Execute the query.
* @param parameters Param values
* @return The results
*/
public Object executeWithMap(Map parameters)
{
assertIsOpen();
this.parameterValueByName = parameters;
this.parameterValues = null;
return executeInternal();
}
/* (non-Javadoc)
* @see javax.jdo.Query#executeList()
*/
@Override
public List executeList()
{
assertIsOpen();
if (query.getResult() != null)
{
throw new JDOUserException("Cannot call executeXXX method when query has result set to " + query.getResult() + ". Use executeResultList() instead");
}
return (List) executeInternal();
}
/* (non-Javadoc)
* @see javax.jdo.Query#executeUnique()
*/
@Override
public T executeUnique()
{
assertIsOpen();
query.setUnique(true);
if (query.getResult() != null)
{
throw new JDOUserException("Cannot call executeXXX method when query has result set to " + query.getResult() + ". Use executeResultUnique() instead");
}
return (T) executeInternal();
}
/* (non-Javadoc)
* @see javax.jdo.Query#executeResultList(java.lang.Class)
*/
@Override
public List executeResultList(Class resultCls)
{
assertIsOpen();
if (resultCls == null)
{
throw new JDOUserException("Result Class must be specified");
}
this.query.setResultClass(resultCls);
return (List) executeInternal();
}
/* (non-Javadoc)
* @see javax.jdo.Query#executeResultUnique(java.lang.Class)
*/
@Override
public R executeResultUnique(Class resultCls)
{
assertIsOpen();
query.setUnique(true);
if (resultCls == null)
{
throw new JDOUserException("Result Class must be specified");
}
this.query.setResultClass(resultCls);
return (R) executeInternal();
}
/* (non-Javadoc)
* @see javax.jdo.Query#executeResultList()
*/
@Override
public List