Search in sources :

Example 26 with DatabaseQuery

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

the class AbstractSession method getQuery.

/**
 * INTERNAL:
 * Return the query from the session pre-defined queries with the given name and argument types.
 * This allows for common queries to be pre-defined, reused and executed by name.
 * This method should be used if the Session has multiple queries with the same name but
 * different arguments.
 *
 * @param shouldSearchParent indicates whether parent should be searched if query not found.
 * @see #getQuery(String, List)
 */
public DatabaseQuery getQuery(String name, Vector arguments, boolean shouldSearchParent) {
    List<DatabaseQuery> queries = getQueries().get(name);
    if ((queries != null) && !queries.isEmpty()) {
        // Short circuit the simple, most common case of only one query.
        if (queries.size() == 1) {
            return queries.get(0);
        }
        // CR#3754; Predrag; mar 19/2002;
        // We allow multiple named queries with the same name but
        // different argument set; we can have only one query with
        // no arguments; Vector queries is not sorted;
        // When asked for the query with no parameters the
        // old version did return the first query - wrong:
        // return (DatabaseQuery) queries.firstElement();
        int argumentTypesSize = 0;
        if (arguments != null) {
            argumentTypesSize = arguments.size();
        }
        Vector argumentTypes = new Vector(argumentTypesSize);
        for (int i = 0; i < argumentTypesSize; i++) {
            argumentTypes.addElement(arguments.elementAt(i).getClass());
        }
        for (DatabaseQuery query : queries) {
            if (Helper.areTypesAssignable(argumentTypes, query.getArgumentTypes())) {
                return query;
            }
        }
    }
    if (shouldSearchParent) {
        AbstractSession parent = getParent();
        if (parent != null) {
            return parent.getQuery(name, arguments, true);
        }
    }
    return null;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Vector(java.util.Vector) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 27 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)
 */
public Object executeQuery(String queryName, Class<?> domainClass, Vector argumentValues) throws DatabaseException {
    ClassDescriptor descriptor = getDescriptor(domainClass);
    if (descriptor == null) {
        throw QueryException.descriptorIsMissingForNamedQuery(domainClass, queryName);
    }
    DatabaseQuery query = descriptor.getQueryManager().getQuery(queryName, argumentValues);
    if (query == null) {
        throw QueryException.queryNotDefined(queryName, domainClass);
    }
    return executeQuery(query, argumentValues);
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 28 with DatabaseQuery

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

the class AbstractSession method addQuery.

/**
 * INTERNAL:
 * Add the query to the session queries.
 */
protected synchronized void addQuery(DatabaseQuery query, boolean nameMustBeUnique) {
    List<DatabaseQuery> queriesByName = getQueries().get(query.getName());
    if (queriesByName == null) {
        // lazily create Vector in Hashtable.
        queriesByName = new ArrayList<>();
        getQueries().put(query.getName(), queriesByName);
    }
    if (nameMustBeUnique) {
        // JPA addNamedQuery
        if (queriesByName.size() <= 1) {
            queriesByName.clear();
        } else {
            throw new IllegalStateException(ExceptionLocalization.buildMessage("argument_keyed_named_query_with_JPA", new Object[] { query.getName() }));
        }
    } else {
        // Check that we do not already have a query that matched it
        for (Iterator<DatabaseQuery> enumtr = queriesByName.iterator(); enumtr.hasNext(); ) {
            DatabaseQuery existingQuery = enumtr.next();
            if (Helper.areTypesAssignable(query.getArgumentTypes(), existingQuery.getArgumentTypes())) {
                throw ValidationException.existingQueryTypeConflict(query, existingQuery);
            }
        }
    }
    queriesByName.add(query);
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery)

Example 29 with DatabaseQuery

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

the class AbstractSession method postAcquireConnection.

/**
 * INTERNAL:
 * This method rises appropriate for the session event(s)
 * right after connection is acquired.
 */
public void postAcquireConnection(Accessor accessor) {
    if (getProject().hasVPDIdentifier(this)) {
        if (getPlatform().supportsVPD()) {
            DatabaseQuery query = getPlatform().getVPDSetIdentifierQuery(getProject().getVPDIdentifier());
            List argValues = new ArrayList();
            query.addArgument(getProject().getVPDIdentifier());
            argValues.add(getProperty(getProject().getVPDIdentifier()));
            executeQuery(query, argValues);
        } else {
            throw ValidationException.vpdNotSupported(getPlatform().getClass().getName());
        }
    }
    if (this.eventManager != null) {
        this.eventManager.postAcquireConnection(accessor);
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) ExposedNodeLinkedList(org.eclipse.persistence.internal.helper.linkedlist.ExposedNodeLinkedList)

Example 30 with DatabaseQuery

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

the class TransporterHelper method read.

public static org.eclipse.persistence.internal.sessions.remote.Transporter read(org.omg.CORBA.portable.InputStream istream) {
    org.eclipse.persistence.internal.sessions.remote.Transporter value = new org.eclipse.persistence.internal.sessions.remote.Transporter();
    int length = istream.read_long();
    byte[] bytes = new byte[length];
    istream.read_octet_array(bytes, 0, length);
    java.io.ByteArrayInputStream byteIn = new java.io.ByteArrayInputStream(bytes);
    Object anObject = null;
    Map descriptors = null;
    DatabaseQuery query = null;
    if (bytes.length == 0) {
        return null;
    }
    try {
        java.io.ObjectInputStream objectIn = new java.io.ObjectInputStream(byteIn);
        anObject = objectIn.readObject();
        descriptors = (Map) objectIn.readObject();
        query = (DatabaseQuery) objectIn.readObject();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    value.object = anObject;
    value.wasOperationSuccessful = istream.read_boolean();
    value.setQuery(query);
    value.setObjectDescriptors(descriptors);
    return value;
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Map(java.util.Map)

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