Search in sources :

Example 21 with DatabaseQuery

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");
            }
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) TestErrorException(org.eclipse.persistence.testing.framework.TestErrorException) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 22 with DatabaseQuery

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);
        }
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 23 with DatabaseQuery

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);
}
Also used : ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) QName(javax.xml.namespace.QName) ValueReadQuery(org.eclipse.persistence.queries.ValueReadQuery) ReadAllQuery(org.eclipse.persistence.queries.ReadAllQuery) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) DataModifyQuery(org.eclipse.persistence.queries.DataModifyQuery)

Example 24 with DatabaseQuery

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;
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) JPAQuery(org.eclipse.persistence.internal.jpa.JPAQuery) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) NonSynchronizedVector(org.eclipse.persistence.internal.helper.NonSynchronizedVector) Vector(java.util.Vector)

Example 25 with DatabaseQuery

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);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Aggregations

DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)86 ArrayList (java.util.ArrayList)18 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)15 List (java.util.List)14 Vector (java.util.Vector)12 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)12 ObjectLevelReadQuery (org.eclipse.persistence.queries.ObjectLevelReadQuery)12 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)8 EntityManager (jakarta.persistence.EntityManager)6 HashMap (java.util.HashMap)6 QName (javax.xml.namespace.QName)6 EJBQueryImpl (org.eclipse.persistence.internal.jpa.EJBQueryImpl)6 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)6 DataReadQuery (org.eclipse.persistence.queries.DataReadQuery)6 Session (org.eclipse.persistence.sessions.Session)6 Test (org.junit.Test)6 NonSynchronizedVector (org.eclipse.persistence.internal.helper.NonSynchronizedVector)5 ReadQuery (org.eclipse.persistence.queries.ReadQuery)5 ReportQuery (org.eclipse.persistence.queries.ReportQuery)5 PersistenceException (jakarta.persistence.PersistenceException)4