use of com.yahoo.elide.core.dictionary.EntityDictionary 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.dictionary.EntityDictionary in project elide by yahoo.
the class JpaDataStoreTest method verifyManualEntityBinding.
@Test
public void verifyManualEntityBinding() {
@Include(rootLevel = false)
@Entity
class Test {
@Id
private long id;
private String name;
}
Metamodel mockModel = mock(Metamodel.class);
when(mockModel.getEntities()).thenReturn(Sets.newHashSet());
EntityManager managerMock = mock(EntityManager.class);
when(managerMock.getMetamodel()).thenReturn(mockModel);
JpaDataStore store = new JpaDataStore(() -> managerMock, unused -> null, ClassType.of(Test.class));
EntityDictionary dictionary = EntityDictionary.builder().build();
store.populateEntityDictionary(dictionary);
assertNotNull(dictionary.lookupBoundClass(ClassType.of(Test.class)));
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class JpaDataStoreTest method verifyJPQLGeneratorRegistration.
@Test
public void verifyJPQLGeneratorRegistration() {
@Include(rootLevel = false)
@Entity
class Test {
@Id
private long id;
@JPQLFilterFragment(operator = Operator.IN, generator = TestGenerator.class)
private String name;
}
EntityType mockType = mock(EntityType.class);
when(mockType.getJavaType()).thenReturn(Test.class);
Metamodel mockModel = mock(Metamodel.class);
when(mockModel.getEntities()).thenReturn(Sets.newHashSet(mockType));
EntityManager managerMock = mock(EntityManager.class);
when(managerMock.getMetamodel()).thenReturn(mockModel);
JpaDataStore store = new JpaDataStore(() -> managerMock, unused -> null);
EntityDictionary dictionary = EntityDictionary.builder().build();
try {
store.populateEntityDictionary(dictionary);
assertNotNull(FilterTranslator.lookupJPQLGenerator(Operator.IN, ClassType.of(Test.class), "name"));
} finally {
FilterTranslator.registerJPQLGenerator(Operator.IN, ClassType.of(Test.class), "name", null);
}
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class AggregationDataStoreTransactionTest method testRequiredColumnFilterArguments.
@Test
public void testRequiredColumnFilterArguments() throws Exception {
Type<PlayerStatsWithRequiredFilter> tableType = ClassType.of(PlayerStatsWithRequiredFilter.class);
EntityDictionary dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(PlayerStatsWithRequiredFilter.class);
SQLTable table = new SQLTable(new Namespace(DEFAULT_NAMESPACE), tableType, dictionary);
RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
FilterExpression where = filterDialect.parse(tableType, new HashSet<>(), "recordedDate>2019-07-12T00:00Z", NO_VERSION);
Query query = Query.builder().column(SQLMetricProjection.builder().name("highScore").alias("highScore").build()).whereFilter(where).source(table).build();
AggregationDataStoreTransaction tx = new AggregationDataStoreTransaction(queryEngine, cache, queryLogger);
Query modifiedQuery = tx.addColumnFilterArguments(table, query, dictionary);
Map<String, Argument> columnArguments = modifiedQuery.getColumnProjection("highScore").getArguments();
assertTrue(columnArguments.containsKey("recordedDate"));
assertEquals(1, columnArguments.size());
}
use of com.yahoo.elide.core.dictionary.EntityDictionary in project elide by yahoo.
the class AggregationDataStoreTransactionTest method testRequiredTableFilterArguments.
@Test
public void testRequiredTableFilterArguments() throws Exception {
Type<PlayerStatsWithRequiredFilter> tableType = ClassType.of(PlayerStatsWithRequiredFilter.class);
EntityDictionary dictionary = EntityDictionary.builder().build();
dictionary.bindEntity(PlayerStatsWithRequiredFilter.class);
SQLTable table = new SQLTable(new Namespace(DEFAULT_NAMESPACE), tableType, dictionary);
RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
FilterExpression where = filterDialect.parse(tableType, new HashSet<>(), "recordedDate>=2019-07-12T00:00Z;recordedDate<2030-07-12T00:00Z", NO_VERSION);
Query query = Query.builder().source(table).whereFilter(where).build();
AggregationDataStoreTransaction tx = new AggregationDataStoreTransaction(queryEngine, cache, queryLogger);
Query modifiedQuery = tx.addTableFilterArguments(table, query, dictionary);
Map<String, Argument> tableArguments = modifiedQuery.getArguments();
assertTrue(tableArguments.containsKey("start"));
assertTrue(tableArguments.containsKey("end"));
assertEquals(2, tableArguments.size());
}
Aggregations