Search in sources :

Example 1 with FalsePredicate

use of com.yahoo.elide.core.filter.predicates.FalsePredicate 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 2 with FalsePredicate

use of com.yahoo.elide.core.filter.predicates.FalsePredicate in project elide by yahoo.

the class InMemoryFilterExecutorTest method inAndNotInPredicateTest.

@Test
public void inAndNotInPredicateTest() throws Exception {
    author = new Author();
    author.setId(1L);
    author.setName("AuthorForTest");
    // Test exact match
    expression = new InPredicate(authorIdElement, 1L);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotInPredicate(authorIdElement, 1L);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // Test exact match insensitive
    expression = new InInsensitivePredicate(authorNameElement, author.getName().toUpperCase(Locale.ENGLISH));
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotInInsensitivePredicate(authorNameElement, author.getName().toUpperCase(Locale.ENGLISH));
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // Test contains works
    expression = new InPredicate(authorIdElement, 1, 2);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotInPredicate(authorIdElement, 1, 2);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // Test type
    expression = new InPredicate(authorIdElement, "1");
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new NotInPredicate(authorIdElement, "1");
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // Test not in
    expression = new InPredicate(authorIdElement, 3L);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new NotInPredicate(authorIdElement, 3L);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    // Test empty
    expression = new InPredicate(authorIdElement, Collections.emptyList());
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new NotInPredicate(authorIdElement, Collections.emptyList());
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    // Test TRUE/FALSE
    expression = new TruePredicate(authorIdElement);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
    expression = new FalsePredicate(authorIdElement);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    // Test null
    author.setId(null);
    expression = new InPredicate(authorIdElement, 1);
    fn = expression.accept(visitor);
    assertFalse(fn.test(author));
    expression = new NotInPredicate(authorIdElement, 1);
    fn = expression.accept(visitor);
    assertTrue(fn.test(author));
}
Also used : InInsensitivePredicate(com.yahoo.elide.core.filter.predicates.InInsensitivePredicate) NotInInsensitivePredicate(com.yahoo.elide.core.filter.predicates.NotInInsensitivePredicate) TruePredicate(com.yahoo.elide.core.filter.predicates.TruePredicate) Author(example.Author) NotInPredicate(com.yahoo.elide.core.filter.predicates.NotInPredicate) InPredicate(com.yahoo.elide.core.filter.predicates.InPredicate) NotInPredicate(com.yahoo.elide.core.filter.predicates.NotInPredicate) NotInInsensitivePredicate(com.yahoo.elide.core.filter.predicates.NotInInsensitivePredicate) FalsePredicate(com.yahoo.elide.core.filter.predicates.FalsePredicate) Test(org.junit.jupiter.api.Test)

Aggregations

FalsePredicate (com.yahoo.elide.core.filter.predicates.FalsePredicate)2 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)2 Path (com.yahoo.elide.core.Path)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)1 InInsensitivePredicate (com.yahoo.elide.core.filter.predicates.InInsensitivePredicate)1 NotInInsensitivePredicate (com.yahoo.elide.core.filter.predicates.NotInInsensitivePredicate)1 NotInPredicate (com.yahoo.elide.core.filter.predicates.NotInPredicate)1 TruePredicate (com.yahoo.elide.core.filter.predicates.TruePredicate)1 Query (com.yahoo.elide.datastores.jpql.porting.Query)1 RootCollectionFetchQueryBuilder (com.yahoo.elide.datastores.jpql.query.RootCollectionFetchQueryBuilder)1 Author (example.Author)1 Test (org.junit.jupiter.api.Test)1