Search in sources :

Example 11 with RelationshipImpl

use of com.yahoo.elide.datastores.jpql.query.RelationshipImpl in project elide by yahoo.

the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotalsWithJoinFilter.

@Test
public void testSubCollectionPageTotalsWithJoinFilter() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    List<Path.PathElement> publisherNamePath = Arrays.asList(new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, "name"));
    FilterPredicate publisherNamePredicate = new InPredicate(new Path(publisherNamePath), "Pub1");
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).filterExpression(publisherNamePredicate).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionPageTotalsQueryBuilder builder = new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT COUNT(DISTINCT example_Author_books) " + "FROM example.Author AS example_Author " + "LEFT JOIN example_Author.books example_Author_books " + "LEFT JOIN example_Author_books.publisher example_Author_books_publisher " + "WHERE (example_Author_books_publisher.name IN (:books_publisher_name_XXX) " + "AND example_Author.id IN (:id_XXX))";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":books_publisher_name_\\w+", ":books_publisher_name_XXX");
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) SubCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder) Test(org.junit.jupiter.api.Test)

Example 12 with RelationshipImpl

use of com.yahoo.elide.datastores.jpql.query.RelationshipImpl 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 13 with RelationshipImpl

use of com.yahoo.elide.datastores.jpql.query.RelationshipImpl in project elide by yahoo.

the class SubCollectionPageTotalsQueryBuilderTest method testSubCollectionPageTotals.

@Test
public void testSubCollectionPageTotals() {
    Author author = new Author();
    author.setId(1L);
    Book book = new Book();
    book.setId(2);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).build();
    Relationship relationshipProjection = Relationship.builder().projection(entityProjection).name(BOOKS).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionPageTotalsQueryBuilder builder = new SubCollectionPageTotalsQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":id_\\w+", ":id_XXX");
    String expected = "SELECT COUNT(DISTINCT example_Author_books) " + "FROM example.Author AS example_Author " + "JOIN example_Author.books example_Author_books " + "WHERE example_Author.id IN (:id_XXX)";
    assertEquals(expected, actual);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SubCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

RelationshipImpl (com.yahoo.elide.datastores.jpql.query.RelationshipImpl)13 EntityProjection (com.yahoo.elide.core.request.EntityProjection)12 Relationship (com.yahoo.elide.core.request.Relationship)12 Book (example.Book)12 Test (org.junit.jupiter.api.Test)12 Author (example.Author)11 SubCollectionFetchQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder)9 SortingImpl (com.yahoo.elide.core.sort.SortingImpl)5 HashMap (java.util.HashMap)5 SubCollectionPageTotalsQueryBuilder (com.yahoo.elide.datastores.jpql.query.SubCollectionPageTotalsQueryBuilder)4 Publisher (example.Publisher)4 Path (com.yahoo.elide.core.Path)3 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)3 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)3 PaginationImpl (com.yahoo.elide.core.pagination.PaginationImpl)2 Pagination (com.yahoo.elide.core.request.Pagination)2 Sorting (com.yahoo.elide.core.request.Sorting)2 DataStoreIterable (com.yahoo.elide.core.datastore.DataStoreIterable)1 DataStoreIterableBuilder (com.yahoo.elide.core.datastore.DataStoreIterableBuilder)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1