use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class ProjectXMLQueryManagerQueryOrderTest method verify.
@Override
protected void verify() {
if (original.size() != current.size()) {
throw new TestErrorException("The number of queries read was not equal to the number originally.");
}
Iterator orig = original.iterator();
Iterator curr = current.iterator();
while (orig.hasNext()) {
DatabaseQuery origQuery = (DatabaseQuery) orig.next();
int argumentTypesSize = 0;
if (origQuery.getArguments() != null) {
argumentTypesSize = origQuery.getArguments().size();
}
Vector argumentTypes = new Vector();
for (int i = 0; i < argumentTypesSize; i++) {
argumentTypes.addElement(origQuery.getArgumentTypeNames().get(i));
}
DatabaseQuery currentQuery = (DatabaseQuery) curr.next();
if ((origQuery.getName() != currentQuery.getName()) && ((origQuery.getName() == null) || !origQuery.getName().equals(currentQuery.getName())))
if (!argumentTypes.equals(currentQuery.getArgumentTypeNames())) {
throw new TestErrorException("A query in the descriptor query manager does not match the original based on arguments");
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class DatabaseAccessor method closeStatement.
/**
* INTERNAL:
* Closes a PreparedStatement (which is supposed to close it's current resultSet).
* Factored out to simplify coding and handle exceptions.
*/
public void closeStatement(Statement statement, AbstractSession session, DatabaseCall call) throws SQLException {
if (statement == null) {
decrementCallCount();
return;
}
DatabaseQuery query = ((call == null) ? null : call.getQuery());
try {
session.startOperationProfile(SessionProfiler.StatementExecute, query, SessionProfiler.ALL);
statement.close();
} finally {
session.endOperationProfile(SessionProfiler.StatementExecute, query, SessionProfiler.ALL);
decrementCallCount();
// If this is the cached dynamic statement, release it.
if (statement == this.dynamicStatement) {
this.dynamicStatement = null;
// The dynamic statement is cached and only closed on disconnect.
setIsDynamicStatementInUse(false);
}
}
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class StoredProcedureQueryHandler method initializeDatabaseQuery.
@Override
public void initializeDatabaseQuery(XRServiceAdapter xrService, QueryOperation queryOperation) {
DatabaseQuery databaseQueryToInitialize;
if (queryOperation.hasResponse()) {
QName type = queryOperation.getResult().getType();
if (queryOperation.isCollection()) {
if (queryOperation.isSimpleXMLFormat()) {
databaseQueryToInitialize = new DataReadQuery();
} else {
if (!xrService.descriptorsByQName.containsKey(type)) {
// data-read query
databaseQueryToInitialize = new DataReadQuery();
} else {
// check if descriptor is aggregate
Class<?> typeClass = xrService.getTypeClass(type);
if (xrService.getORSession().getDescriptor(typeClass).isAggregateDescriptor()) {
databaseQueryToInitialize = new DataReadQuery();
} else {
// read-all query for the class mapped to the type
databaseQueryToInitialize = new ReadAllQuery(typeClass);
}
}
}
} else {
if (getOutArguments().size() == 0 && getInOutArguments().size() == 0) {
if (isStoredFunctionQueryHandler()) {
if (!xrService.descriptorsByQName.containsKey(type)) {
databaseQueryToInitialize = new ValueReadQuery();
} else {
// read object query for the class mapped to the type
databaseQueryToInitialize = new ReadObjectQuery(xrService.getTypeClass(type));
}
} else {
// special case - no out args for SP: the return
// will be a single int
// rowcount
databaseQueryToInitialize = new DataModifyQuery();
}
} else {
if (!xrService.descriptorsByQName.containsKey(type)) {
if (type.equals(SXF_QNAME)) {
databaseQueryToInitialize = new DataReadQuery();
} else {
databaseQueryToInitialize = new ValueReadQuery();
}
} else {
// read object query for the class mapped to the type
databaseQueryToInitialize = new ReadObjectQuery(xrService.getTypeClass(type));
}
}
}
} else {
databaseQueryToInitialize = new ValueReadQuery();
}
databaseQueryToInitialize.bindAllParameters();
setDatabaseQuery(databaseQueryToInitialize);
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class DeleteOperation method invoke.
/**
* Execute <code>DELETE</code> operation on the database
* @param xrService parent <code>XRService</code> that owns this <code>Operation</code>
* @param invocation contains runtime argument values to be bound to the list of
* {@link Parameter}'s.
* @return result - can be <code>null</code> if the underlying <code>DELETE</code> operation on the
* database does not return a value
*
* @see Operation
*/
@SuppressWarnings("rawtypes")
@Override
public Object invoke(XRServiceAdapter xrService, Invocation invocation) {
DatabaseQuery query = classDescriptor.getQueryManager().getQuery(getFindByPKQuery());
// a named query created via ORM metadata processing needs initialization
if (query instanceof JPAQuery) {
query = ((JPAQuery) query).processSQLQuery(xrService.getORSession().getActiveSession());
}
UnitOfWork uow = xrService.getORSession().acquireUnitOfWork();
Object toBeDeleted;
// a query created via ORM metadata processing does not have parameters set, however, the operation should
if (query.getArguments().size() == 0) {
int idx = 0;
for (Parameter param : getParameters()) {
// for custom SQL query (as configured via ORM metadata processing) we add args by position
query.addArgument(Integer.toString(++idx), Util.SCHEMA_2_CLASS.get(param.getType()));
query.addArgumentValue(invocation.getParameter(param.getName()));
}
toBeDeleted = uow.executeQuery(query);
} else {
// set query args or execute args for the non-JPAQuery case,
// i.e. stored proc/funcs get populated from ORM metadata
// whereas named queries (SQL strings) do not...
List<String> queryArguments = query.getArguments();
int queryArgumentsSize = queryArguments.size();
Vector<Object> executeArguments = new NonSynchronizedVector<>();
for (int i = 0; i < queryArgumentsSize; i++) {
String argName = queryArguments.get(i);
executeArguments.add(invocation.getParameter(argName));
}
toBeDeleted = uow.executeQuery(query, executeArguments);
}
// JPAQuery will return a single result in a Vector
if (!isCollection() && toBeDeleted instanceof Vector) {
if (((Vector) toBeDeleted).isEmpty()) {
toBeDeleted = null;
} else {
toBeDeleted = ((Vector) toBeDeleted).firstElement();
}
}
if (toBeDeleted != null) {
uow.deleteObject(toBeDeleted);
uow.commit();
}
return null;
}
use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.
the class AbstractSession method executeQuery.
/**
* PUBLIC:
* Execute the pre-defined query by name and return the result.
* Queries can be pre-defined and named to allow for their reuse.
* The class is the descriptor in which the query was pre-defined.
*
* @see DescriptorQueryManager#addQuery(String, DatabaseQuery)
*/
@Override
public Object executeQuery(String queryName, Class<?> domainClass) throws DatabaseException {
ClassDescriptor descriptor = getDescriptor(domainClass);
if (descriptor == null) {
throw QueryException.descriptorIsMissingForNamedQuery(domainClass, queryName);
}
DatabaseQuery query = descriptor.getQueryManager().getQuery(queryName);
if (query == null) {
throw QueryException.queryNotDefined(queryName, domainClass);
}
return executeQuery(query);
}
Aggregations