Search in sources :

Example 56 with EntityDictionary

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;
}
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 57 with EntityDictionary

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)));
}
Also used : Entity(javax.persistence.Entity) EntityManager(javax.persistence.EntityManager) Test(org.junit.jupiter.api.Test) Include(com.yahoo.elide.annotation.Include) Metamodel(javax.persistence.metamodel.Metamodel) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 58 with EntityDictionary

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);
    }
}
Also used : EntityType(javax.persistence.metamodel.EntityType) Entity(javax.persistence.Entity) EntityManager(javax.persistence.EntityManager) Test(org.junit.jupiter.api.Test) Include(com.yahoo.elide.annotation.Include) Metamodel(javax.persistence.metamodel.Metamodel) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Test(org.junit.jupiter.api.Test)

Example 59 with EntityDictionary

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());
}
Also used : NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) PlayerStatsWithRequiredFilter(example.PlayerStatsWithRequiredFilter) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 60 with EntityDictionary

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());
}
Also used : NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) PlayerStatsWithRequiredFilter(example.PlayerStatsWithRequiredFilter) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)87 Test (org.junit.jupiter.api.Test)31 RequestScope (com.yahoo.elide.core.RequestScope)27 Include (com.yahoo.elide.annotation.Include)17 Entity (javax.persistence.Entity)17 HashSet (java.util.HashSet)16 Type (com.yahoo.elide.core.type.Type)13 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)12 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)10 Map (java.util.Map)10 BeforeAll (org.junit.jupiter.api.BeforeAll)10 PersistentResource (com.yahoo.elide.core.PersistentResource)9 Set (java.util.Set)9 ReadPermission (com.yahoo.elide.annotation.ReadPermission)8 ClassType (com.yahoo.elide.core.type.ClassType)8 List (java.util.List)8 ElideSettingsBuilder (com.yahoo.elide.ElideSettingsBuilder)7 DataStore (com.yahoo.elide.core.datastore.DataStore)7 InvalidObjectIdentifierException (com.yahoo.elide.core.exceptions.InvalidObjectIdentifierException)7 Check (com.yahoo.elide.core.security.checks.Check)7