Search in sources :

Example 36 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class DataServiceIT method testAggregateTwoDimensionalDistinct.

@WithMockUser(username = USERNAME_READ)
@Test(groups = "readtest")
public void testAggregateTwoDimensionalDistinct() {
    AggregateQuery aggregateQuery = new AggregateQueryImpl().query(new QueryImpl<>()).attrX(entityType.getAttribute(ATTR_BOOL)).attrY(entityType.getAttribute(ATTR_BOOL)).attrDistinct(entityType.getAttribute(ATTR_ENUM));
    AggregateResult result = dataService.aggregate(entityType.getId(), aggregateQuery);
    AggregateResult expectedResult = new AggregateResult(asList(asList(1L, 0L), asList(0L, 1L)), asList(0L, 1L), asList(0L, 1L));
    assertEquals(result, expectedResult);
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 37 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class DataServiceIT method testAggregateTwoDimensionalQuery.

@WithMockUser(username = USERNAME_READ)
@Test(groups = "readtest")
public void testAggregateTwoDimensionalQuery() {
    AggregateQuery aggregateQuery = new AggregateQueryImpl().query(new QueryImpl<>()).attrX(entityType.getAttribute(ATTR_BOOL)).attrY(entityType.getAttribute(ATTR_BOOL)).query(new QueryImpl<>().gt(ATTR_INT, 10));
    AggregateResult result = dataService.aggregate(entityType.getId(), aggregateQuery);
    AggregateResult expectedResult = new AggregateResult(asList(asList(1L, 0L), asList(0L, 1L)), asList(0L, 1L), asList(0L, 1L));
    assertEquals(result, expectedResult);
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 38 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class DataServiceIT method testAggregateTwoDimensionalQueryDistinct.

@WithMockUser(username = USERNAME_READ)
@Test(groups = "readtest")
public void testAggregateTwoDimensionalQueryDistinct() {
    AggregateQuery aggregateQuery = new AggregateQueryImpl().query(new QueryImpl<>()).attrX(entityType.getAttribute(ATTR_BOOL)).attrY(entityType.getAttribute(ATTR_ENUM)).attrDistinct(entityType.getAttribute(ATTR_ENUM)).query(new QueryImpl<>().gt(ATTR_INT, 1));
    AggregateResult result = dataService.aggregate(entityType.getId(), aggregateQuery);
    AggregateResult expectedResult = new AggregateResult(asList(asList(0L, 1L), asList(1L, 0L)), asList(0L, 1L), asList("option1", "option2"));
    assertEquals(result, expectedResult);
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) AggregateResult(org.molgenis.data.aggregation.AggregateResult) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 39 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class DataServiceIT method testFindOneQueryTypedStatic.

@WithMockUser(username = USERNAME_READ)
@Test(groups = "readtest")
public void testFindOneQueryTypedStatic() {
    Entity entity = staticEntities.get(0);
    TestEntityStatic testEntityStatic = dataService.findOne(entityTypeStatic.getId(), new QueryImpl<TestEntityStatic>().eq(ATTR_ID, entity.getIdValue()), TestEntityStatic.class);
    assertNotNull(testEntityStatic);
    assertEquals(testEntityStatic.getId(), entity.getIdValue());
}
Also used : AggregateQueryImpl(org.molgenis.data.support.AggregateQueryImpl) QueryImpl(org.molgenis.data.support.QueryImpl) TestEntityStatic(org.molgenis.data.staticentity.TestEntityStatic) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.testng.annotations.Test)

Example 40 with QueryImpl

use of org.molgenis.data.support.QueryImpl in project molgenis by molgenis.

the class PostgreSqlQueryGenerator method getSqlSelect.

static <E extends Entity> String getSqlSelect(EntityType entityType, Query<E> q, List<Object> parameters, boolean includeMrefs) {
    final StringBuilder select = new StringBuilder("SELECT ");
    if (isDistinctSelectRequired(entityType, q)) {
        select.append("DISTINCT ");
    }
    final StringBuilder group = new StringBuilder();
    final AtomicInteger count = new AtomicInteger();
    final Attribute idAttribute = entityType.getIdAttribute();
    getPersistedAttributes(entityType).forEach(attr -> {
        if (q.getFetch() == null || q.getFetch().hasField(attr.getName()) || (q.getSort() != null && q.getSort().hasField(attr.getName()))) {
            if (count.get() > 0) {
                select.append(", ");
            }
            if (isPersistedInOtherTable(attr)) {
                if (includeMrefs || (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy())) {
                    if (attr.getDataType() == ONE_TO_MANY && attr.isMappedBy()) {
                        Attribute refIdAttr = attr.getRefEntity().getIdAttribute();
                        String mrefSelect = "(SELECT array_agg(" + getColumnName(refIdAttr);
                        Sort orderBy = attr.getOrderBy();
                        if (orderBy == null) {
                            orderBy = new Sort(refIdAttr.getName());
                        }
                        mrefSelect += ' ' + getSqlSort(attr.getRefEntity(), new QueryImpl<>().sort(orderBy)) + ") FROM " + getTableName(attr.getRefEntity()) + " WHERE this." + getColumnName(idAttribute) + " = " + getTableName(attr.getRefEntity()) + '.' + getColumnName(attr.getMappedBy()) + ") AS " + getColumnName(attr);
                        select.append(mrefSelect);
                    } else {
                        // TODO retrieve mref values in separate queries to allow specifying limit and offset after nested MOLGENIS queries are implemented as sub-queries instead of query rules
                        String mrefSelect = MessageFormat.format("(SELECT array_agg(DISTINCT ARRAY[{0}.{1}::TEXT,{0}.{0}::TEXT]) " + "FROM {2} AS {0} WHERE this.{3} = {0}.{3}) AS {0}", getColumnName(attr), getJunctionTableOrderColumnName(), getJunctionTableName(entityType, attr), getColumnName(idAttribute));
                        select.append(mrefSelect);
                    }
                } else {
                    select.append("NULL AS ").append(getColumnName(attr));
                }
            } else {
                select.append("this.").append(getColumnName(attr));
                if (group.length() > 0) {
                    group.append(", this.").append(getColumnName(attr));
                } else {
                    group.append("this.").append(getColumnName(attr));
                }
            }
            count.incrementAndGet();
        }
    });
    // from
    StringBuilder result = new StringBuilder().append(select).append(getSqlFrom(entityType, q));
    // where
    String where = getSqlWhere(entityType, q, parameters, new AtomicInteger());
    if (where.length() > 0) {
        result.append(" WHERE ").append(where);
    }
    // order by
    result.append(' ').append(getSqlSort(entityType, q));
    // limit
    if (q.getPageSize() > 0) {
        result.append(" LIMIT ").append(q.getPageSize());
    }
    if (q.getOffset() > 0) {
        result.append(" OFFSET ").append(q.getOffset());
    }
    return result.toString().trim();
}
Also used : QueryImpl(org.molgenis.data.support.QueryImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

QueryImpl (org.molgenis.data.support.QueryImpl)98 Test (org.testng.annotations.Test)70 DynamicEntity (org.molgenis.data.support.DynamicEntity)37 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)36 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)36 EntityType (org.molgenis.data.meta.model.EntityType)28 Attribute (org.molgenis.data.meta.model.Attribute)25 Entity (org.molgenis.data.Entity)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)8 Stream (java.util.stream.Stream)7 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)7 AggregateQueryImpl (org.molgenis.data.support.AggregateQueryImpl)7 Objects.requireNonNull (java.util.Objects.requireNonNull)6 QueryRule (org.molgenis.data.QueryRule)6 AggregateQuery (org.molgenis.data.aggregation.AggregateQuery)6 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)6 BeforeMethod (org.testng.annotations.BeforeMethod)6 Instant (java.time.Instant)5 LocalDate (java.time.LocalDate)5 Operator (org.molgenis.data.QueryRule.Operator)5