Search in sources :

Example 6 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class StoredProcedureQueryImpl method buildResultSetMappingQuery.

/**
 * Build a ResultSetMappingQuery from the sql result set mappings given
 *  a stored procedure call.
 *
 * This is called from a named stored procedure query that employs result
 * class name(s). The resultSetMappings are build from these class name(s)
 * and are not available from the session.
 */
public static DatabaseQuery buildResultSetMappingQuery(List<SQLResultSetMapping> resultSetMappings, StoredProcedureCall call, Map<String, Object> hints, ClassLoader classLoader, AbstractSession session) {
    // apply any query hints
    DatabaseQuery hintQuery = applyHints(hints, buildResultSetMappingQuery(resultSetMappings, call), classLoader, session);
    // apply any query arguments
    applyArguments(call, hintQuery);
    return hintQuery;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 7 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class StoredProcedureQueryImpl method close.

/**
 * Call this method to close any open connections to the database.
 */
@Override
public void close() {
    if (executeCall != null) {
        DatabaseQuery query = executeCall.getQuery();
        AbstractSession session = query.getSession();
        // Release the accessors acquired for the query.
        for (Accessor accessor : query.getAccessors()) {
            session.releaseReadConnection(accessor);
        }
        try {
            if (executeStatement != null) {
                DatabaseAccessor accessor = (DatabaseAccessor) query.getAccessor();
                accessor.releaseStatement(executeStatement, query.getSQLString(), executeCall, session);
            }
        } catch (SQLException exception) {
            // Catch the exception and log a message.
            session.log(SessionLog.WARNING, SessionLog.CONNECTION, "exception_caught_closing_statement", exception);
        }
    }
    executeCall = null;
    executeStatement = null;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) SQLException(java.sql.SQLException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 8 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class QueryImpl method setHintInternal.

/**
 * Set an implementation-specific hint. If the hint name is not recognized,
 * it is silently ignored.
 *
 * @throws IllegalArgumentException
 *             if the second argument is not valid for the implementation.
 */
protected void setHintInternal(String hintName, Object value) {
    cloneSharedQuery();
    ClassLoader loader = getEntityManager().getAbstractSession().getLoader();
    DatabaseQuery hintQuery = QueryHintsHandler.apply(hintName, value, getDatabaseQueryInternal(), loader, (AbstractSession) getActiveSession());
    if (hintQuery != null) {
        setDatabaseQuery(hintQuery);
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 9 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class QueryImpl method executeReadQuery.

/**
 * Execute a ReadQuery by assigning the stored parameter values and running
 * it in the database
 *
 * @return the results of the query execution
 */
protected Object executeReadQuery() {
    List<Object> parameterValues = processParameters();
    // TODO: the following performFlush() call is a temporary workaround for
    // bug 4752493:
    // CTS: INMEMORY QUERYING IN EJBQUERY BROKEN DUE TO CHANGE TO USE
    // REPORTQUERY.
    // Ideally we should only flush in case the selectionExpression can't be
    // conformed in memory.
    // There are two alternative ways to implement that:
    // 1. Try running the query with conformInUOW flag first - if it fails
    // with
    // QueryException.cannotConformExpression then flush and run the query
    // again -
    // now without conforming.
    // 2. Implement a new isComformable method on Expression which would
    // determine whether the expression
    // could be conformed in memory, flush only in case it returns false.
    // Note that doesConform method currently implemented on Expression
    // requires object(s) to be confirmed as parameter(s).
    // The new isComformable method should not take any objects as
    // parameters,
    // it should return false if there could be such an object that
    // passed to doesConform causes it to throw
    // QueryException.cannotConformExpression -
    // and true otherwise.
    boolean shouldResetConformResultsInUnitOfWork = false;
    DatabaseQuery query = getDatabaseQueryInternal();
    boolean isObjectLevelReadQuery = query.isObjectLevelReadQuery();
    if (isFlushModeAUTO() && (!isObjectLevelReadQuery || !((ObjectLevelReadQuery) query).isReadOnly())) {
        performPreQueryFlush();
        if (isObjectLevelReadQuery) {
            if (((ObjectLevelReadQuery) query).shouldConformResultsInUnitOfWork()) {
                cloneSharedQuery();
                query = getDatabaseQueryInternal();
                ((ObjectLevelReadQuery) query).setCacheUsage(ObjectLevelReadQuery.UseDescriptorSetting);
                shouldResetConformResultsInUnitOfWork = true;
            }
        }
    }
    // Set a pessimistic locking on the query if specified.
    if (this.lockMode != null && !this.lockMode.equals(LockModeType.NONE)) {
        // We need to throw TransactionRequiredException if there is no
        // active transaction
        this.entityManager.checkForTransaction(true);
        // The lock mode setters and getters validate the query type
        // so should be safe to make the casting.
        cloneSharedQuery();
        query = getDatabaseQueryInternal();
        // we were unable to set the lock mode.
        if (((ObjectLevelReadQuery) query).setLockModeType(lockMode.name(), (AbstractSession) getActiveSession())) {
            throw new PersistenceException(ExceptionLocalization.buildMessage("ejb30-wrong-lock_called_without_version_locking-index", null));
        }
    }
    Session session = getActiveSession();
    try {
        // in case it's a user-defined query
        if (query.isUserDefined()) {
            // and there is an active transaction
            if (this.entityManager.checkForTransaction(false) != null) {
                // verify whether uow has begun early transaction
                if (session.isUnitOfWork() && !((UnitOfWorkImpl) session).wasTransactionBegunPrematurely()) {
                    // uow begins early transaction in case it hasn't
                    // already begun.
                    // TODO: This is not good, it means that no SQL queries
                    // can ever use the cache,
                    // using isUserDefined to mean an SQL query is also
                    // wrong.
                    ((UnitOfWorkImpl) session).beginEarlyTransaction();
                }
            }
        }
        // Execute the query and return the result.
        return session.executeQuery(query, parameterValues);
    } catch (DatabaseException e) {
        throw getDetailedException(e);
    } catch (RuntimeException e) {
        setRollbackOnly();
        throw e;
    } finally {
        this.lockMode = null;
        if (shouldResetConformResultsInUnitOfWork) {
            ((ObjectLevelReadQuery) query).conformResultsInUnitOfWork();
        }
    }
}
Also used : ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceException(jakarta.persistence.PersistenceException) UnitOfWorkImpl(org.eclipse.persistence.internal.sessions.UnitOfWorkImpl) DatabaseException(org.eclipse.persistence.exceptions.DatabaseException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 10 with DatabaseQuery

use of org.eclipse.persistence.queries.DatabaseQuery in project eclipselink by eclipse-ee4j.

the class PersistenceContext method buildQuery.

/**
 * Builds the query.
 *
 * @param tenantId the tenant id
 * @param name the name
 * @param parameters the parameters
 * @param hints the hints
 * @return the query
 */
public Query buildQuery(Map<String, String> tenantId, String name, Map<?, ?> parameters, Map<String, ?> hints) {
    EntityManager em = getEmf().createEntityManager(tenantId);
    Query query = em.createNamedQuery(name);
    DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
    if (parameters != null) {
        Iterator<?> i = parameters.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
            String key = (String) entry.getKey();
            Class<?> parameterClass = null;
            int index = dbQuery.getArguments().indexOf(key);
            if (index >= 0) {
                parameterClass = dbQuery.getArgumentTypes().get(index);
            }
            Object parameter = entry.getValue();
            if (parameterClass != null) {
                parameter = ConversionManager.getDefaultManager().convertObject(parameter, parameterClass);
            }
            query.setParameter(key, parameter);
        }
    }
    if (hints != null) {
        for (Map.Entry<String, ?> entry : hints.entrySet()) {
            query.setHint(entry.getKey(), entry.getValue());
        }
    }
    return query;
}
Also used : Query(jakarta.persistence.Query) RestPageableQuery(org.eclipse.persistence.jpa.rs.annotations.RestPageableQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) EntityManager(jakarta.persistence.EntityManager) Map(java.util.Map) HashMap(java.util.HashMap)

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