Search in sources :

Example 6 with Query

use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.

the class AbstractHQLQueryBuilderTest method testSettingQueryParams.

@Test
public void testSettingQueryParams() {
    Path.PathElement idPath = new Path.PathElement(Book.class, Chapter.class, "id");
    Query query = mock(Query.class);
    FilterPredicate predicate = new InPredicate(idPath, ABC, DEF);
    supplyFilterQueryParameters(query, Arrays.asList(predicate));
    verify(query, times(2)).setParameter(anyString(), any());
    query = mock(Query.class);
    predicate = new InfixPredicate(idPath, ABC);
    supplyFilterQueryParameters(query, Arrays.asList(predicate));
    verify(query, times(1)).setParameter(anyString(), any());
}
Also used : Path(com.yahoo.elide.core.Path) InfixPredicate(com.yahoo.elide.core.filter.predicates.InfixPredicate) Query(com.yahoo.elide.datastores.jpql.porting.Query) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Test(org.junit.jupiter.api.Test)

Example 7 with Query

use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.

the class AbstractHQLQueryBuilderTest method testSettingQueryPagination.

@Test
public void testSettingQueryPagination() {
    Query query = mock(Query.class);
    PaginationImpl paginationMock = mock(PaginationImpl.class);
    when(paginationMock.getLimit()).thenReturn(10);
    when(paginationMock.getOffset()).thenReturn(50);
    when(this.entityProjection.getPagination()).thenReturn(paginationMock);
    addPaginationToQuery(query);
    verify(query, times(1)).setMaxResults(10);
    verify(query, times(1)).setFirstResult(50);
}
Also used : PaginationImpl(com.yahoo.elide.core.pagination.PaginationImpl) Query(com.yahoo.elide.datastores.jpql.porting.Query) Test(org.junit.jupiter.api.Test)

Example 8 with Query

use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.

the class JPQLTransaction method getToManyRelation.

@Override
public <T, R> DataStoreIterable<R> getToManyRelation(DataStoreTransaction relationTx, T entity, Relationship relation, RequestScope scope) {
    FilterExpression filterExpression = relation.getProjection().getFilterExpression();
    Sorting sorting = relation.getProjection().getSorting();
    Pagination pagination = relation.getProjection().getPagination();
    EntityDictionary dictionary = scope.getDictionary();
    Iterable val = (Iterable) com.yahoo.elide.core.PersistentResource.getValue(entity, relation.getName(), scope);
    // If the query is safe for N+1 and the value is an ORM managed, persistent collection, run a JPQL query...
    if (doInDatabase(entity) && val instanceof Collection && isPersistentCollection().test((Collection<?>) val)) {
        /*
             * If there is no filtering or sorting required in the data store, and the pagination is default,
             * return the proxy and let Hibernate manage the SQL generation.
             */
        if (filterExpression == null && sorting == null && (pagination == null || (pagination.isDefaultInstance()))) {
            return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
        }
        RelationshipImpl relationship = new RelationshipImpl(dictionary.lookupEntityClass(EntityDictionary.getType(entity)), entity, relation);
        if (pagination != null && pagination.returnPageTotals()) {
            pagination.setPageTotals(getTotalRecords(relationship, scope.getDictionary()));
        }
        final Query query = new SubCollectionFetchQueryBuilder(relationship, dictionary, sessionWrapper).build();
        if (query != null) {
            return new DataStoreIterableBuilder(addSingleElement(query.list())).build();
        }
    }
    return new DataStoreIterableBuilder<R>(addSingleElement(val)).allInMemory().build();
}
Also used : Pagination(com.yahoo.elide.core.request.Pagination) DataStoreIterable(com.yahoo.elide.core.datastore.DataStoreIterable) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Query(com.yahoo.elide.datastores.jpql.porting.Query) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) Collection(java.util.Collection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Sorting(com.yahoo.elide.core.request.Sorting)

Example 9 with Query

use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.

the class JPQLTransaction method loadObject.

/**
 * load a single record with id and filter.
 *
 * @param projection The projection to query
 * @param id id of the query object
 * @param scope Request scope associated with specific request
 */
@Override
public <T> T loadObject(EntityProjection projection, Serializable id, RequestScope scope) {
    Type<?> entityClass = projection.getType();
    FilterExpression filterExpression = projection.getFilterExpression();
    EntityDictionary dictionary = scope.getDictionary();
    Type<?> idType = dictionary.getIdType(entityClass);
    String idField = dictionary.getIdFieldName(entityClass);
    // Construct a predicate that selects an individual element of the relationship's parent (Author.id = 3).
    FilterPredicate idExpression;
    Path.PathElement idPath = new Path.PathElement(entityClass, idType, idField);
    if (id != null) {
        idExpression = new InPredicate(idPath, id);
    } else {
        idExpression = new FalsePredicate(idPath);
    }
    FilterExpression joinedExpression = (filterExpression != null) ? new AndFilterExpression(filterExpression, idExpression) : idExpression;
    projection = projection.copyOf().filterExpression(joinedExpression).build();
    Query query = new RootCollectionFetchQueryBuilder(projection, dictionary, sessionWrapper).build();
    T loaded = new TimedFunction<T>(() -> query.uniqueResult(), "Query Hash: " + query.hashCode()).get();
    if (loaded != null) {
        singleElementLoads.add(loaded);
    }
    return loaded;
}
Also used : Path(com.yahoo.elide.core.Path) Query(com.yahoo.elide.datastores.jpql.porting.Query) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) RootCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.RootCollectionFetchQueryBuilder) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FalsePredicate(com.yahoo.elide.core.filter.predicates.FalsePredicate)

Example 10 with Query

use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.

the class EntityManagerWrapper method createQuery.

@Override
public Query createQuery(String queryText) {
    Query query = new QueryWrapper(entityManager.createQuery(queryText));
    logger.log(String.format("Query Hash: %d\tHQL Query: %s", query.hashCode(), queryText));
    return query;
}
Also used : Query(com.yahoo.elide.datastores.jpql.porting.Query)

Aggregations

Query (com.yahoo.elide.datastores.jpql.porting.Query)10 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)6 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)6 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)4 FilterTranslator (com.yahoo.elide.datastores.jpql.filter.FilterTranslator)4 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)3 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)3 Collection (java.util.Collection)3 Path (com.yahoo.elide.core.Path)2 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)2 Pagination (com.yahoo.elide.core.request.Pagination)2 RootCollectionFetchQueryBuilder (com.yahoo.elide.datastores.jpql.query.RootCollectionFetchQueryBuilder)2 Test (org.junit.jupiter.api.Test)2 PathElement (com.yahoo.elide.core.Path.PathElement)1 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)1 InvalidValueException (com.yahoo.elide.core.exceptions.InvalidValueException)1 ExpressionScopingVisitor (com.yahoo.elide.core.filter.expression.ExpressionScopingVisitor)1 FalsePredicate (com.yahoo.elide.core.filter.predicates.FalsePredicate)1 InfixPredicate (com.yahoo.elide.core.filter.predicates.InfixPredicate)1