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);
}
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();
}
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);
}
Aggregations