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