use of com.yahoo.elide.core.Path in project elide by yahoo.
the class SubCollectionFetchQueryBuilderTest method testSubCollectionFetchWithJoinFilter.
@Test
public void testSubCollectionFetchWithJoinFilter() {
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);
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 (:books_publisher_name_XXX) AND example_Author__fetch=:example_Author__fetch ";
String actual = query.getQueryText();
actual = actual.replaceFirst(":publisher_name_\\w+_\\w+", ":books_publisher_name_XXX");
assertEquals(expected, actual);
}
use of com.yahoo.elide.core.Path in project elide by yahoo.
the class FilterTranslatorTest method testHQLQueryVisitor.
@Test
public void testHQLQueryVisitor() throws Exception {
List<Path.PathElement> p0Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"));
FilterPredicate p0 = new NotEmptyPredicate(new Path(p0Path));
List<Path.PathElement> p1Path = Arrays.asList(new Path.PathElement(Book.class, Author.class, "authors"), new Path.PathElement(Author.class, String.class, "name"));
FilterPredicate p1 = new InPredicate(new Path(p1Path), "foo", "bar");
List<Path.PathElement> p2Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "name"));
FilterPredicate p2 = new InPredicate(new Path(p2Path), "blah");
List<Path.PathElement> p3Path = Arrays.asList(new Path.PathElement(Book.class, String.class, "genre"));
FilterPredicate p3 = new InPredicate(new Path(p3Path), "scifi");
OrFilterExpression or = new OrFilterExpression(p2, p3);
AndFilterExpression and1 = new AndFilterExpression(p0, p1);
AndFilterExpression and2 = new AndFilterExpression(or, and1);
NotFilterExpression not = new NotFilterExpression(and2);
FilterTranslator filterOp = new FilterTranslator(dictionary);
String query = filterOp.apply(not, false);
query = query.trim().replaceAll(" +", " ");
String p1Params = p1.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String p2Params = p2.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String p3Params = p3.getParameters().stream().map(FilterPredicate.FilterParameter::getPlaceholder).collect(Collectors.joining(", "));
String expected = "NOT (((name IN (" + p2Params + ") OR genre IN (" + p3Params + ")) " + "AND (authors IS NOT EMPTY AND authors.name IN (" + p1Params + "))))";
assertEquals(expected, query);
}
use of com.yahoo.elide.core.Path in project elide by yahoo.
the class SearchDataTransaction method canSort.
/**
* Returns whether or not Lucene can be used to sort the query.
* @param sorting The elide sorting clause
* @param entityClass The entity being sorted.
* @return true if Lucene can sort. False otherwise.
*/
private boolean canSort(Sorting sorting, Type<?> entityClass) {
for (Map.Entry<Path, Sorting.SortOrder> entry : sorting.getSortingPaths().entrySet()) {
Path path = entry.getKey();
if (path.getPathElements().size() != 1) {
return false;
}
Path.PathElement last = path.lastElement().get();
String fieldName = last.getFieldName();
if (dictionary.getAttributeOrRelationAnnotation(entityClass, SortableField.class, fieldName) == null) {
return false;
}
}
return true;
}
use of com.yahoo.elide.core.Path in project elide by yahoo.
the class QueryTranslator method extractOrderBy.
/**
* Given a list of columns to sort on, constructs an ORDER BY clause in SQL.
* @param sortClauses The list of sort columns and their sort order (ascending or descending).
* @return A SQL expression
*/
private String extractOrderBy(Map<Path, Sorting.SortOrder> sortClauses, Query query) {
if (sortClauses.isEmpty()) {
return "";
}
return " ORDER BY " + sortClauses.entrySet().stream().map((entry) -> {
Path path = entry.getKey();
Sorting.SortOrder order = entry.getValue();
Path.PathElement last = path.lastElement().get();
SQLColumnProjection projection = fieldToColumnProjection(query, last.getAlias());
String orderByClause = (query.getColumnProjections().contains(projection) && dialect.useAliasForOrderByClause()) ? applyQuotes(projection.getSafeAlias()) : projection.toSQL(query, metaDataStore);
return orderByClause + (order.equals(Sorting.SortOrder.desc) ? " DESC" : " ASC");
}).collect(Collectors.joining(","));
}
use of com.yahoo.elide.core.Path in project elide by yahoo.
the class HasMemberJPQLGenerator method generate.
@Override
public String generate(FilterPredicate predicate, Function<Path, String> aliasGenerator) {
Preconditions.checkArgument(predicate.getParameters().size() == 1);
String notMember = negated ? "NOT " : "";
if (!FilterPredicate.toManyInPath(dictionary, predicate.getPath())) {
return String.format("%s %sMEMBER OF %s", predicate.getParameters().get(0).getPlaceholder(), notMember, aliasGenerator.apply(predicate.getPath()));
}
Path path = predicate.getPath();
Preconditions.checkArgument(path.lastElement().isPresent());
Preconditions.checkArgument(!(path.lastElement().get().getType() instanceof Collection));
// _INNER_example_Book_authors.name = :authors_name_7c02839c_0)
return String.format("%sEXISTS (SELECT 1 FROM %s WHERE %s = %s AND %s = %s)", notMember, getFromClause(path), getInnerQueryIdField(path), getOuterQueryIdField(path, aliasGenerator), getInnerFilterFieldReference(path), predicate.getParameters().get(0).getPlaceholder());
}
Aggregations