Search in sources :

Example 71 with DatabaseQuery

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

the class QueryImpl method processParameters.

/**
 * Internal method to add the parameters values to the query prior to
 * execution. Returns a list of parameter values in the order the parameters
 * are defined for the databaseQuery.
 */
protected List<Object> processParameters() {
    DatabaseQuery query = getDatabaseQueryInternal();
    List<String> arguments = query.getArguments();
    if (arguments.isEmpty()) {
        // This occurs for native queries, as the query does not know of its arguments.
        // This may have issues, it is better if the query set its arguments
        // when parsing the SQL.
        arguments = new ArrayList<>(this.parameterValues.keySet());
        query.setArguments(arguments);
    }
    // now create parameterValues in the same order as the argument list
    int size = arguments.size();
    List<Object> parameterValues = new ArrayList<Object>(size);
    for (int index = 0; index < size; index++) {
        String name = arguments.get(index);
        Object parameter = this.parameterValues.get(name);
        if ((parameter != null) || this.parameterValues.containsKey(name)) {
            parameterValues.add(parameter);
        } else if (query.hasNullableArguments() && query.getNullableArguments().contains(new DatabaseField(name))) {
            parameterValues.add(null);
        } else {
            // Error: missing actual parameter value
            throw new IllegalStateException(ExceptionLocalization.buildMessage("missing_parameter_value", new Object[] { name }));
        }
    }
    return parameterValues;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ArrayList(java.util.ArrayList)

Example 72 with DatabaseQuery

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

the class QueryImpl method propagateResultProperties.

/**
 * Configure the firstResult, maxRows and lock mode in the EclipseLink
 * ReadQuery.
 */
protected void propagateResultProperties() {
    DatabaseQuery databaseQuery = getDatabaseQueryInternal();
    if (databaseQuery.isReadQuery()) {
        ReadQuery readQuery = (ReadQuery) databaseQuery;
        if (maxResults >= 0) {
            cloneSharedQuery();
            readQuery = (ReadQuery) getDatabaseQueryInternal();
            int maxRows = maxResults + ((firstResultIndex >= 0) ? firstResultIndex : 0);
            readQuery.setMaxRows(maxRows);
        }
        if (firstResultIndex > UNDEFINED) {
            cloneSharedQuery();
            readQuery = (ReadQuery) getDatabaseQueryInternal();
            readQuery.setFirstResult(firstResultIndex);
        }
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ObjectLevelReadQuery(org.eclipse.persistence.queries.ObjectLevelReadQuery) DataReadQuery(org.eclipse.persistence.queries.DataReadQuery) ReadQuery(org.eclipse.persistence.queries.ReadQuery)

Example 73 with DatabaseQuery

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

the class QueryImpl method getResultList.

/**
 * Execute the query and return the query results as a List.
 *
 * @return a list of the results
 */
public List getResultList() {
    // bug51411440: need to throw IllegalStateException if query
    // executed on closed em
    this.entityManager.verifyOpenWithSetRollbackOnly();
    try {
        setAsSQLReadQuery();
        propagateResultProperties();
        // bug:4297903, check container policy class and throw exception if
        // its not the right type
        DatabaseQuery query = getDatabaseQueryInternal();
        if (query.isReadAllQuery()) {
            Class<?> containerClass = ((ReadAllQuery) query).getContainerPolicy().getContainerClass();
            if (!Helper.classImplementsInterface(containerClass, ClassConstants.List_Class)) {
                throw QueryException.invalidContainerClass(containerClass, ClassConstants.List_Class);
            }
        } else if (query.isReadObjectQuery()) {
            List<Object> resultList = new ArrayList<>();
            Object result = executeReadQuery();
            if (result != null) {
                resultList.add(result);
            }
            return resultList;
        } else if (!query.isReadQuery()) {
            throw new IllegalStateException(ExceptionLocalization.buildMessage("incorrect_query_for_get_result_list"));
        }
        return (List) executeReadQuery();
    } catch (LockTimeoutException exception) {
        throw exception;
    } catch (PersistenceException exception) {
        setRollbackOnly();
        throw exception;
    } catch (IllegalStateException exception) {
        setRollbackOnly();
        throw exception;
    } catch (RuntimeException exception) {
        setRollbackOnly();
        throw new PersistenceException(exception);
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceException(jakarta.persistence.PersistenceException) ArrayList(java.util.ArrayList) List(java.util.List) LockTimeoutException(jakarta.persistence.LockTimeoutException)

Example 74 with DatabaseQuery

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

the class QueryImpl method cloneSharedQuery.

/**
 * If the query was from the jpql parse cache it must be cloned before being
 * modified.
 */
protected void cloneSharedQuery() {
    DatabaseQuery query = getDatabaseQueryInternal();
    if (this.isShared) {
        // Clone to allow setting of hints or other properties without
        // corrupting original query.
        query = (DatabaseQuery) databaseQuery.clone();
        setDatabaseQuery(query);
        this.isShared = false;
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 75 with DatabaseQuery

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

the class QueryImpl method setParameterInternal.

/**
 * Bind an argument to a named or indexed parameter.
 *
 * @param name
 *            the parameter name
 * @param value
 *            to bind
 * @param isIndex
 *            defines if index or named
 */
protected void setParameterInternal(String name, Object value, boolean isIndex) {
    DatabaseQuery query = getDatabaseQueryInternal();
    if (query.getQueryMechanism().isJPQLCallQueryMechanism()) {
        // only non native queries
        int index = query.getArguments().indexOf(name);
        if (index == -1) {
            if (isIndex) {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-index", new Object[] { name, query.getEJBQLString() }));
            } else {
                throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-name", new Object[] { name, query.getEJBQLString() }));
            }
        }
        Class<?> type = query.getArgumentTypes().get(index);
        if (!isValidActualParameter(value, type)) {
            throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-incorrect-parameter-type", new Object[] { name, value.getClass(), query.getArgumentTypes().get(index), query.getEJBQLString() }));
        }
    } else {
        // native queries start a 1 not 0.
        if (isIndex && name.equals("0")) {
            throw new IllegalArgumentException(ExceptionLocalization.buildMessage("ejb30-wrong-argument-index", new Object[] { name, query.getSQLString() }));
        }
    }
    this.parameterValues.put(name, value);
}
Also used : 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