use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.
the class AbstractHQLQueryBuilderTest method testSettingQueryParams.
@Test
public void testSettingQueryParams() {
Path.PathElement idPath = new Path.PathElement(Book.class, Chapter.class, "id");
Query query = mock(Query.class);
FilterPredicate predicate = new InPredicate(idPath, ABC, DEF);
supplyFilterQueryParameters(query, Arrays.asList(predicate));
verify(query, times(2)).setParameter(anyString(), any());
query = mock(Query.class);
predicate = new InfixPredicate(idPath, ABC);
supplyFilterQueryParameters(query, Arrays.asList(predicate));
verify(query, times(1)).setParameter(anyString(), any());
}
use of com.yahoo.elide.datastores.jpql.porting.Query in project elide by yahoo.
the class AbstractHQLQueryBuilderTest method testSettingQueryPagination.
@Test
public void testSettingQueryPagination() {
Query query = mock(Query.class);
PaginationImpl paginationMock = mock(PaginationImpl.class);
when(paginationMock.getLimit()).thenReturn(10);
when(paginationMock.getOffset()).thenReturn(50);
when(this.entityProjection.getPagination()).thenReturn(paginationMock);
addPaginationToQuery(query);
verify(query, times(1)).setMaxResults(10);
verify(query, times(1)).setFirstResult(50);
}
use of com.yahoo.elide.datastores.jpql.porting.Query 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.porting.Query 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.datastores.jpql.porting.Query in project elide by yahoo.
the class EntityManagerWrapper method createQuery.
@Override
public Query createQuery(String queryText) {
Query query = new QueryWrapper(entityManager.createQuery(queryText));
logger.log(String.format("Query Hash: %d\tHQL Query: %s", query.hashCode(), queryText));
return query;
}
Aggregations