Search in sources :

Example 66 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class AbstractHQLQueryBuilder method extractJoinClause.

/**
 * Extracts a join clause from a path (if it exists).
 * @param path The path to examine
 * @param skipFetches Don't fetch join
 * @return A HQL string representing the join
 */
private String extractJoinClause(Path path, boolean skipFetches) {
    StringBuilder joinClause = new StringBuilder();
    String previousAlias = null;
    for (Path.PathElement pathElement : path.getPathElements()) {
        String fieldName = pathElement.getFieldName();
        Type<?> typeClass = dictionary.lookupEntityClass(pathElement.getType());
        String typeAlias = getTypeAlias(typeClass);
        // Nothing left to join.
        if (!dictionary.isRelation(pathElement.getType(), fieldName)) {
            return joinClause.toString();
        }
        String alias = previousAlias == null ? appendAlias(typeAlias, fieldName) : appendAlias(previousAlias, fieldName);
        String joinKey;
        // This is the first path element
        if (previousAlias == null) {
            joinKey = typeAlias + PERIOD + fieldName;
        } else {
            joinKey = previousAlias + PERIOD + fieldName;
        }
        String fetch = "";
        RelationshipType type = dictionary.getRelationshipType(pathElement.getType(), fieldName);
        // This is a to-One relationship belonging to the collection being retrieved.
        if (!skipFetches && entityProjection.getIncludedRelationsName().contains(fieldName) && type.isToOne() && !type.isComputed() && previousAlias == null) {
            fetch = "FETCH ";
        }
        String joinFragment = LEFT + JOIN + fetch + joinKey + SPACE + alias + SPACE;
        if (!alreadyJoined.contains(joinKey)) {
            joinClause.append(joinFragment);
            alreadyJoined.add(joinKey);
        }
        previousAlias = alias;
    }
    return joinClause.toString();
}
Also used : Path(com.yahoo.elide.core.Path) RelationshipType(com.yahoo.elide.core.dictionary.RelationshipType)

Example 67 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class AbstractHQLQueryBuilderTest method testFilterJoinClause.

@Test
public void testFilterJoinClause() {
    List<Path.PathElement> chapterTitlePath = Arrays.asList(new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Chapter.class, "chapters"), new Path.PathElement(Chapter.class, String.class, TITLE));
    FilterPredicate titlePredicate = new InPredicate(new Path(chapterTitlePath), ABC, DEF);
    FilterPredicate titlePredicateDuplicate = new InPredicate(new Path(chapterTitlePath), ABC, DEF);
    List<Path.PathElement> publisherNamePath = Arrays.asList(new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, NAME));
    FilterPredicate publisherNamePredicate = new InPredicate(new Path(publisherNamePath), "Pub1");
    OrFilterExpression orExpression = new OrFilterExpression(titlePredicate, publisherNamePredicate);
    AndFilterExpression andExpression = new AndFilterExpression(orExpression, titlePredicateDuplicate);
    String actual = getJoinClauseFromFilters(andExpression);
    String expected = " LEFT JOIN example_Author.books example_Author_books  " + "LEFT JOIN example_Author_books.chapters example_Author_books_chapters   " + "LEFT JOIN example_Author_books.publisher example_Author_books_publisher  ";
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Chapter(example.Chapter) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Test(org.junit.jupiter.api.Test)

Example 68 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class RootCollectionPageTotalsQueryBuilderTest method testRootFetchWithJoinFilter.

@Test
public void testRootFetchWithJoinFilter() {
    List<Path.PathElement> chapterTitlePath = Arrays.asList(new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Chapter.class, "chapters"), new Path.PathElement(Chapter.class, String.class, TITLE));
    FilterPredicate titlePredicate = new InPredicate(new Path(chapterTitlePath), "ABC", "DEF");
    List<Path.PathElement> publisherNamePath = Arrays.asList(new Path.PathElement(Author.class, Book.class, BOOKS), new Path.PathElement(Book.class, Publisher.class, PUBLISHER), new Path.PathElement(Publisher.class, String.class, "name"));
    FilterPredicate publisherNamePredicate = new InPredicate(new Path(publisherNamePath), "Pub1");
    OrFilterExpression expression = new OrFilterExpression(titlePredicate, publisherNamePredicate);
    EntityProjection entityProjection = EntityProjection.builder().type(Author.class).filterExpression(expression).build();
    RootCollectionPageTotalsQueryBuilder builder = new RootCollectionPageTotalsQueryBuilder(entityProjection, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT COUNT(DISTINCT example_Author) FROM example.Author AS example_Author " + "LEFT JOIN example_Author.books example_Author_books " + "LEFT JOIN example_Author_books.chapters example_Author_books_chapters " + "LEFT JOIN example_Author_books.publisher example_Author_books_publisher " + "WHERE (example_Author_books_chapters.title IN " + "(:books_chapters_title_XXX, :books_chapters_title_XXX) " + "OR example_Author_books_publisher.name IN (:books_publisher_name_XXX))";
    String actual = query.getQueryText();
    actual = actual.trim().replaceAll(" +", " ");
    actual = actual.replaceFirst(":books_chapters_title_\\w\\w\\w\\w+", ":books_chapters_title_XXX");
    actual = actual.replaceFirst(":books_chapters_title_\\w\\w\\w\\w+", ":books_chapters_title_XXX");
    actual = actual.replaceFirst(":books_publisher_name_\\w\\w\\w\\w+", ":books_publisher_name_XXX");
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) RootCollectionPageTotalsQueryBuilder(com.yahoo.elide.datastores.jpql.query.RootCollectionPageTotalsQueryBuilder) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Chapter(example.Chapter) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) Book(example.Book) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 69 with Path

use of com.yahoo.elide.core.Path in project elide by yahoo.

the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithSortingAndFilters.

@Test
public void testSubCollectionFetchWithSortingAndFilters() {
    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);
    Map<String, Sorting.SortOrder> sorting = new HashMap<>();
    sorting.put(TITLE, Sorting.SortOrder.asc);
    EntityProjection entityProjection = EntityProjection.builder().type(Book.class).filterExpression(publisherNamePredicate).sorting(new SortingImpl(sorting, Book.class, dictionary)).build();
    Relationship relationshipProjection = Relationship.builder().name(BOOKS).projection(entityProjection).build();
    RelationshipImpl relationship = new RelationshipImpl(ClassType.of(Author.class), author, relationshipProjection);
    SubCollectionFetchQueryBuilder builder = new SubCollectionFetchQueryBuilder(relationship, dictionary, new TestSessionWrapper());
    TestQueryWrapper query = (TestQueryWrapper) builder.build();
    String expected = "SELECT example_Book FROM example.Author example_Author__fetch " + "JOIN example_Author__fetch.books example_Book " + "LEFT JOIN example_Book.publisher example_Book_publisher " + "WHERE example_Book_publisher.name IN (:publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch order by example_Book.title asc";
    String actual = query.getQueryText();
    actual = actual.replaceFirst(":publisher_name_\\w+", ":publisher_name_XXX");
    actual = actual.trim().replaceAll(" +", " ");
    assertEquals(expected, actual);
}
Also used : Path(com.yahoo.elide.core.Path) EntityProjection(com.yahoo.elide.core.request.EntityProjection) SubCollectionFetchQueryBuilder(com.yahoo.elide.datastores.jpql.query.SubCollectionFetchQueryBuilder) HashMap(java.util.HashMap) Publisher(example.Publisher) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) RelationshipImpl(com.yahoo.elide.datastores.jpql.query.RelationshipImpl) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) Book(example.Book) Relationship(com.yahoo.elide.core.request.Relationship) Author(example.Author) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Test(org.junit.jupiter.api.Test)

Example 70 with Path

use of com.yahoo.elide.core.Path 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)

Aggregations

Path (com.yahoo.elide.core.Path)88 Test (org.junit.jupiter.api.Test)67 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)51 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)41 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)35 Query (com.yahoo.elide.datastores.aggregation.query.Query)35 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)34 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)33 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)31 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)29 Book (example.Book)29 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)22 Date (java.util.Date)19 Argument (com.yahoo.elide.core.request.Argument)14 EntityProjection (com.yahoo.elide.core.request.EntityProjection)14 GameRevenue (example.GameRevenue)14 HashSet (java.util.HashSet)14 Author (example.Author)12 Publisher (example.Publisher)9 Sorting (com.yahoo.elide.core.request.Sorting)8