Search in sources :

Example 76 with DatabaseQuery

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

the class QueryImpl method getInternalParameters.

/**
 * Return the internal map of parameters.
 */
protected Map<String, Parameter<?>> getInternalParameters() {
    if (this.parameters == null) {
        this.parameters = new HashMap<String, Parameter<?>>();
        // Retrieve named
        DatabaseQuery query = getDatabaseQueryInternal();
        // query
        int count = 0;
        if (query.getArguments() != null && !query.getArguments().isEmpty()) {
            boolean checkParameterType = query.getArgumentParameterTypes().size() == query.getArguments().size();
            for (String argName : query.getArguments()) {
                Parameter<?> param = null;
                ParameterType type = null;
                if (checkParameterType) {
                    type = query.getArgumentParameterTypes().get(count);
                }
                if (type == ParameterType.POSITIONAL) {
                    Integer position = Integer.parseInt(argName);
                    param = new ParameterExpressionImpl(null, query.getArgumentTypes().get(count), position);
                } else {
                    param = new ParameterExpressionImpl(null, query.getArgumentTypes().get(count), argName);
                }
                this.parameters.put(argName, param);
                ++count;
            }
        }
    }
    return this.parameters;
}
Also used : ParameterType(org.eclipse.persistence.queries.DatabaseQuery.ParameterType) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ParameterExpressionImpl(org.eclipse.persistence.internal.jpa.querydef.ParameterExpressionImpl) Parameter(jakarta.persistence.Parameter)

Example 77 with DatabaseQuery

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

the class AbstractPersistenceUnitResource method getQuery.

private Query getQuery(DatabaseQuery query, PersistenceContext context) {
    String method = query.isReadQuery() ? "get" : "post";
    String jpql = query.getJPQLString() == null ? "" : query.getJPQLString();
    StringBuilder parameterString = new StringBuilder();
    Iterator<String> argumentsIterator = query.getArguments().iterator();
    while (argumentsIterator.hasNext()) {
        String argument = argumentsIterator.next();
        parameterString.append(";");
        parameterString.append(argument).append("={").append(argument).append("}");
    }
    String version = context.getVersion();
    Query returnQuery = null;
    if (version != null) {
        returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + version + "/" + context.getName() + "/query/" + query.getName() + parameterString));
    } else {
        returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + context.getName() + "/query/" + query.getName() + parameterString));
    }
    if (query.isReportQuery()) {
        query.checkPrepare((AbstractSession) context.getServerSession(), new DatabaseRecord());
        for (ReportItem item : ((ReportQuery) query).getItems()) {
            if (item.getMapping() != null) {
                if (item.getAttributeExpression() != null && item.getAttributeExpression().isMapEntryExpression()) {
                    if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
                        returnQuery.getReturnTypes().add(Map.Entry.class.getSimpleName());
                    } else {
                        returnQuery.getReturnTypes().add(((Class<?>) item.getMapping().getContainerPolicy().getKeyType()).getSimpleName());
                    }
                } else {
                    returnQuery.getReturnTypes().add(item.getMapping().getAttributeClassification().getSimpleName());
                }
            } else if (item.getResultType() != null) {
                returnQuery.getReturnTypes().add(item.getResultType().getSimpleName());
            } else if (item.getDescriptor() != null) {
                returnQuery.getReturnTypes().add(item.getDescriptor().getJavaClass().getSimpleName());
            } else if (item.getAttributeExpression() != null && item.getAttributeExpression().isConstantExpression()) {
                returnQuery.getReturnTypes().add(((ConstantExpression) item.getAttributeExpression()).getValue().getClass().getSimpleName());
            } else {
                // Use Object.class by default.
                returnQuery.getReturnTypes().add(ClassConstants.OBJECT.getSimpleName());
            }
        }
    } else {
        returnQuery.getReturnTypes().add(query.getReferenceClassName() == null ? "" : query.getReferenceClass().getSimpleName());
    }
    return returnQuery;
}
Also used : MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) LinkTemplate(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 78 with DatabaseQuery

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

the class AbstractQueryResource method namedQueryInternal.

/**
 * Executes given named query.
 *
 * @param version the version
 * @param persistenceUnit the persistence unit
 * @param queryName named query to execute
 * @param headers the http headers
 * @param uriInfo the uri info
 * @return the response
 */
protected Response namedQueryInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "namedQueryInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        final Query query = context.buildQuery(getMatrixParameters(uriInfo, persistenceUnit), queryName, getMatrixParameters(uriInfo, queryName), getQueryParameters(uriInfo));
        final DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
        final FeatureSet featureSet = context.getSupportedFeatureSet();
        final Response response;
        if (featureSet.isSupported(Feature.PAGING)) {
            response = processPageableQuery(context, queryName, dbQuery, query, headers, uriInfo);
        } else {
            response = namedQueryResponse(context, queryName, dbQuery, query, headers, uriInfo, featureSet.getResponseBuilder(Feature.NO_PAGING));
        }
        return response;
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ReportQuery(org.eclipse.persistence.queries.ReportQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) FeatureSet(org.eclipse.persistence.jpa.rs.features.FeatureSet) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 79 with DatabaseQuery

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

the class AbstractQueryResource method buildQueryOptionsResponse.

protected Response buildQueryOptionsResponse(String version, String persistenceUnit, String queryName, HttpHeaders httpHeaders, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "buildQueryOptionsResponse", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
    // We need to make sure that query with given name exists
    final DatabaseQuery query = context.getServerSession().getQuery(queryName);
    if (query == null) {
        JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_query", new Object[] { queryName, persistenceUnit });
        throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
    }
    final String linkValue = "<" + HrefHelper.buildQueryMetadataHref(context, queryName) + ">; rel=describedby";
    httpHeaders.getRequestHeaders().putSingle("Link", linkValue);
    return Response.ok().header("Link", linkValue).build();
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext)

Example 80 with DatabaseQuery

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

the class MetadataResource method buildMetadataCatalog.

private MetadataCatalog buildMetadataCatalog(PersistenceContext context) {
    final MetadataCatalog result = new MetadataCatalog();
    // Entities
    final Map<Class<?>, ClassDescriptor> descriptors = context.getServerSession().getDescriptors();
    for (ClassDescriptor descriptor : descriptors.values()) {
        // Skip embeddables
        if (!descriptor.isAggregateDescriptor()) {
            result.addResource(buildEntityMetadata(context, descriptor));
        }
    }
    // Queries
    final Map<String, List<DatabaseQuery>> allQueries = context.getServerSession().getQueries();
    for (List<DatabaseQuery> databaseQueries : allQueries.values()) {
        if (databaseQueries != null) {
            for (DatabaseQuery query : databaseQueries) {
                if (query.getReferenceClassName() != null) {
                    result.addResource(buildQueryMetadata(context, query));
                }
            }
        }
    }
    final String href = HrefHelper.buildMetadataCatalogHref(context);
    final List<LinkV2> links = (new ItemLinksBuilder()).addCanonical(href).getList();
    result.setLinks(links);
    return result;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) List(java.util.List) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2) MetadataCatalog(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.MetadataCatalog)

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