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