Add aggregateAction; Add renderTemplateAction

This commit is contained in:
2022-11-18 16:30:48 -06:00
parent 1d1461deea
commit 105b2c92c9
26 changed files with 2030 additions and 82 deletions

View File

@ -22,6 +22,7 @@
package com.kingsrook.qqq.backend.module.rdbms;
import com.kingsrook.qqq.backend.core.actions.interfaces.AggregateInterface;
import com.kingsrook.qqq.backend.core.actions.interfaces.CountInterface;
import com.kingsrook.qqq.backend.core.actions.interfaces.DeleteInterface;
import com.kingsrook.qqq.backend.core.actions.interfaces.InsertInterface;
@ -30,6 +31,7 @@ import com.kingsrook.qqq.backend.core.actions.interfaces.UpdateInterface;
import com.kingsrook.qqq.backend.core.model.metadata.QBackendMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableBackendDetails;
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSAggregateAction;
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSCountAction;
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSDeleteAction;
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSInsertAction;
@ -87,7 +89,6 @@ public class RDBMSBackendModule implements QBackendModuleInterface
/*******************************************************************************
**
*******************************************************************************/
@ -130,4 +131,15 @@ public class RDBMSBackendModule implements QBackendModuleInterface
return (new RDBMSDeleteAction());
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public AggregateInterface getAggregateInterface()
{
return (new RDBMSAggregateAction());
}
}

View File

@ -24,6 +24,7 @@ package com.kingsrook.qqq.backend.module.rdbms.actions;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@ -33,7 +34,10 @@ import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.interfaces.QActionInterface;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.AbstractTableActionInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.QFilterOrderByAggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
@ -43,6 +47,7 @@ import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.QueryManager;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSBackendMetaData;
import com.kingsrook.qqq.backend.module.rdbms.model.metadata.RDBMSTableBackendDetails;
import org.apache.logging.log4j.LogManager;
@ -411,4 +416,80 @@ public abstract class AbstractRDBMSAction implements QActionInterface
return ("`" + id + "`");
}
/*******************************************************************************
**
*******************************************************************************/
protected Serializable getFieldValueFromResultSet(QFieldMetaData qFieldMetaData, ResultSet resultSet, int i) throws SQLException
{
switch(qFieldMetaData.getType())
{
case STRING:
case TEXT:
case HTML:
case PASSWORD:
{
return (QueryManager.getString(resultSet, i));
}
case INTEGER:
{
return (QueryManager.getInteger(resultSet, i));
}
case DECIMAL:
{
return (QueryManager.getBigDecimal(resultSet, i));
}
case DATE:
{
// todo - queryManager.getLocalDate?
return (QueryManager.getDate(resultSet, i));
}
case TIME:
{
return (QueryManager.getLocalTime(resultSet, i));
}
case DATE_TIME:
{
return (QueryManager.getInstant(resultSet, i));
}
case BOOLEAN:
{
return (QueryManager.getBoolean(resultSet, i));
}
default:
{
throw new IllegalStateException("Unexpected field type: " + qFieldMetaData.getType());
}
}
}
/*******************************************************************************
**
*******************************************************************************/
protected String makeOrderByClause(QTableMetaData table, List<QFilterOrderBy> orderBys)
{
List<String> clauses = new ArrayList<>();
for(QFilterOrderBy orderBy : orderBys)
{
String ascOrDesc = orderBy.getIsAscending() ? "ASC" : "DESC";
if(orderBy instanceof QFilterOrderByAggregate orderByAggregate)
{
Aggregate aggregate = orderByAggregate.getAggregate();
String clause = (aggregate.getOperator() + "(" + escapeIdentifier(getColumnName(table.getField(aggregate.getFieldName()))) + ")");
clauses.add(clause + " " + ascOrDesc);
}
else
{
QFieldMetaData field = table.getField(orderBy.getFieldName());
String column = escapeIdentifier(getColumnName(field));
clauses.add(column + " " + ascOrDesc);
}
}
return (String.join(", ", clauses));
}
}

View File

@ -0,0 +1,176 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.module.rdbms.actions;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.kingsrook.qqq.backend.core.actions.interfaces.AggregateInterface;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateResult;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.module.rdbms.jdbc.QueryManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/*******************************************************************************
**
*******************************************************************************/
public class RDBMSAggregateAction extends AbstractRDBMSAction implements AggregateInterface
{
private static final Logger LOG = LogManager.getLogger(RDBMSAggregateAction.class);
/*******************************************************************************
**
*******************************************************************************/
public AggregateOutput execute(AggregateInput aggregateInput) throws QException
{
try
{
QTableMetaData table = aggregateInput.getTable();
String tableName = getTableName(table);
List<String> selectClauses = buildSelectClauses(aggregateInput);
String sql = "SELECT " + StringUtils.join(", ", selectClauses)
+ " FROM " + escapeIdentifier(tableName);
QQueryFilter filter = aggregateInput.getFilter();
List<Serializable> params = new ArrayList<>();
if(filter != null && filter.hasAnyCriteria())
{
sql += " WHERE " + makeWhereClause(table, filter, params);
}
if(CollectionUtils.nullSafeHasContents(aggregateInput.getGroupByFieldNames()))
{
sql += " GROUP BY " + makeGroupByClause(aggregateInput);
}
if(filter != null && CollectionUtils.nullSafeHasContents(filter.getOrderBys()))
{
sql += " ORDER BY " + makeOrderByClause(table, filter.getOrderBys());
}
// todo sql customization - can edit sql and/or param list
LOG.debug(sql); // todo not commit - downgrade to trace
AggregateOutput rs = new AggregateOutput();
List<AggregateResult> results = new ArrayList<>();
rs.setResults(results);
try(Connection connection = getConnection(aggregateInput))
{
QueryManager.executeStatement(connection, sql, ((ResultSet resultSet) ->
{
while(resultSet.next())
{
AggregateResult result = new AggregateResult();
results.add(result);
int selectionIndex = 1;
for(String groupByFieldName : CollectionUtils.nonNullList(aggregateInput.getGroupByFieldNames()))
{
Serializable value = getFieldValueFromResultSet(table.getField(groupByFieldName), resultSet, selectionIndex++);
result.withGroupByValue(groupByFieldName, value);
}
for(Aggregate aggregate : aggregateInput.getAggregates())
{
QFieldMetaData field = table.getField(aggregate.getFieldName());
if(field.getType().equals(QFieldType.INTEGER) && aggregate.getOperator().equals(AggregateOperator.AVG))
{
field = new QFieldMetaData().withType(QFieldType.DECIMAL);
}
Serializable value = getFieldValueFromResultSet(field, resultSet, selectionIndex++);
result.withAggregateValue(aggregate, value);
}
}
}), params);
}
return rs;
}
catch(Exception e)
{
LOG.warn("Error executing aggregate", e);
throw new QException("Error executing aggregate", e);
}
}
/*******************************************************************************
**
*******************************************************************************/
private List<String> buildSelectClauses(AggregateInput aggregateInput)
{
QTableMetaData table = aggregateInput.getTable();
List<String> rs = new ArrayList<>();
for(String groupByFieldName : CollectionUtils.nonNullList(aggregateInput.getGroupByFieldNames()))
{
rs.add(escapeIdentifier(getColumnName(table.getField(groupByFieldName))));
}
for(Aggregate aggregate : aggregateInput.getAggregates())
{
rs.add(aggregate.getOperator() + "(" + escapeIdentifier(getColumnName(table.getField(aggregate.getFieldName()))) + ")");
}
return (rs);
}
/*******************************************************************************
**
*******************************************************************************/
private String makeGroupByClause(AggregateInput aggregateInput)
{
QTableMetaData table = aggregateInput.getTable();
List<String> columns = new ArrayList<>();
for(String groupByFieldName : aggregateInput.getGroupByFieldNames())
{
columns.add(escapeIdentifier(getColumnName(table.getField(groupByFieldName))));
}
return (StringUtils.join(",", columns));
}
}

View File

@ -34,7 +34,6 @@ import java.util.List;
import java.util.stream.Collectors;
import com.kingsrook.qqq.backend.core.actions.interfaces.QueryInterface;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryOutput;
@ -129,7 +128,7 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
for(int i = 1; i <= metaData.getColumnCount(); i++)
{
QFieldMetaData qFieldMetaData = fieldList.get(i - 1);
Serializable value = getValue(qFieldMetaData, resultSet, i);
Serializable value = getFieldValueFromResultSet(qFieldMetaData, resultSet, i);
values.put(qFieldMetaData.getName(), value);
}
@ -187,71 +186,4 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
return (statement);
}
/*******************************************************************************
**
*******************************************************************************/
private Serializable getValue(QFieldMetaData qFieldMetaData, ResultSet resultSet, int i) throws SQLException
{
switch(qFieldMetaData.getType())
{
case STRING:
case TEXT:
case HTML:
case PASSWORD:
{
return (QueryManager.getString(resultSet, i));
}
case INTEGER:
{
return (QueryManager.getInteger(resultSet, i));
}
case DECIMAL:
{
return (QueryManager.getBigDecimal(resultSet, i));
}
case DATE:
{
// todo - queryManager.getLocalDate?
return (QueryManager.getDate(resultSet, i));
}
case TIME:
{
return (QueryManager.getLocalTime(resultSet, i));
}
case DATE_TIME:
{
return (QueryManager.getInstant(resultSet, i));
}
case BOOLEAN:
{
return (QueryManager.getBoolean(resultSet, i));
}
default:
{
throw new IllegalStateException("Unexpected field type: " + qFieldMetaData.getType());
}
}
}
/*******************************************************************************
**
*******************************************************************************/
private String makeOrderByClause(QTableMetaData table, List<QFilterOrderBy> orderBys)
{
List<String> clauses = new ArrayList<>();
for(QFilterOrderBy orderBy : orderBys)
{
QFieldMetaData field = table.getField(orderBy.getFieldName());
String column = getColumnName(field);
clauses.add(column + " " + (orderBy.getIsAscending() ? "ASC" : "DESC"));
}
return (String.join(", ", clauses));
}
}

View File

@ -26,9 +26,9 @@ import java.io.InputStream;
import java.sql.Connection;
import java.util.List;
import com.kingsrook.qqq.backend.core.model.metadata.QAuthenticationType;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.modules.authentication.metadata.QAuthenticationMetaData;
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSActionTest;
@ -131,7 +131,10 @@ public class TestUtils
.withField(new QFieldMetaData("firstName", QFieldType.STRING).withBackendName("first_name"))
.withField(new QFieldMetaData("lastName", QFieldType.STRING).withBackendName("last_name"))
.withField(new QFieldMetaData("birthDate", QFieldType.DATE).withBackendName("birth_date"))
.withField(new QFieldMetaData("email", QFieldType.STRING))
.withField(new QFieldMetaData("email", QFieldType.STRING).withBackendName("email"))
.withField(new QFieldMetaData("isEmployed", QFieldType.BOOLEAN).withBackendName("is_employed"))
.withField(new QFieldMetaData("annualSalary", QFieldType.DECIMAL).withBackendName("annual_salary"))
.withField(new QFieldMetaData("daysWorked", QFieldType.INTEGER).withBackendName("days_worked"))
.withBackendDetails(new RDBMSTableBackendDetails()
.withTableName("person"));
}

View File

@ -0,0 +1,327 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2022. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.module.rdbms.actions;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import com.kingsrook.qqq.backend.core.actions.tables.InsertAction;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.Aggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateResult;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.QFilterOrderByAggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.module.rdbms.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/*******************************************************************************
**
*******************************************************************************/
public class RDBMSAggregateActionTest extends RDBMSActionTest
{
/*******************************************************************************
**
*******************************************************************************/
@BeforeEach
public void beforeEach() throws Exception
{
super.primeTestDatabase();
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testUnfilteredNoGroupBy() throws QException
{
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
Aggregate sumOfId = new Aggregate("id", AggregateOperator.SUM);
Aggregate averageOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.AVG);
Aggregate maxAnnualSalary = new Aggregate("annualSalary", AggregateOperator.MAX);
Aggregate minFirstName = new Aggregate("firstName", AggregateOperator.MIN);
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfId);
aggregateInput.withAggregate(averageOfDaysWorked);
aggregateInput.withAggregate(maxAnnualSalary);
aggregateInput.withAggregate(minFirstName);
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
Assertions.assertEquals(5, aggregateResult.getAggregateValue(countOfId));
Assertions.assertEquals(15, aggregateResult.getAggregateValue(sumOfId));
Assertions.assertEquals(new BigDecimal("96.4"), aggregateResult.getAggregateValue(averageOfDaysWorked));
Assertions.assertEquals(new BigDecimal("1000000.00"), aggregateResult.getAggregateValue(maxAnnualSalary));
Assertions.assertEquals("Darin", aggregateResult.getAggregateValue(minFirstName));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testFilteredNoGroupBy() throws QException
{
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
Aggregate sumOfId = new Aggregate("id", AggregateOperator.SUM);
Aggregate averageOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.AVG);
Aggregate maxAnnualSalary = new Aggregate("annualSalary", AggregateOperator.MAX);
Aggregate minFirstName = new Aggregate("firstName", AggregateOperator.MIN);
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfId);
aggregateInput.withAggregate(averageOfDaysWorked);
aggregateInput.withAggregate(maxAnnualSalary);
aggregateInput.withAggregate(minFirstName);
aggregateInput.setFilter(new QQueryFilter().withCriteria(new QFilterCriteria("firstName", QCriteriaOperator.IN, List.of("Tim", "James"))));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
Assertions.assertEquals(5, aggregateResult.getAggregateValue(sumOfId));
Assertions.assertEquals(new BigDecimal("62.0"), aggregateResult.getAggregateValue(averageOfDaysWorked));
Assertions.assertEquals(new BigDecimal("26000.00"), aggregateResult.getAggregateValue(maxAnnualSalary));
Assertions.assertEquals("James", aggregateResult.getAggregateValue(minFirstName));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testUnfilteredWithGroupBy() throws QException
{
////////////////////////////////////////////////////
// insert a few extra rows from the core data set //
////////////////////////////////////////////////////
insertExtraPersonRecords();
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderBy("lastName")));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
{
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
Assertions.assertEquals(17, aggregateResult.getAggregateValue(sumOfDaysWorked));
}
{
AggregateResult aggregateResult = aggregateOutput.getResults().get(1);
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(4, aggregateResult.getAggregateValue(countOfId));
Assertions.assertEquals(11364, aggregateResult.getAggregateValue(sumOfDaysWorked));
}
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testUnfilteredWithMultiGroupBy() throws QException
{
////////////////////////////////////////////////////
// insert a few extra rows from the core data set //
////////////////////////////////////////////////////
insertExtraPersonRecords();
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.withGroupByFieldName("firstName");
aggregateInput.setFilter(new QQueryFilter()
.withOrderBy(new QFilterOrderBy("lastName"))
.withOrderBy(new QFilterOrderBy("firstName")));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
Iterator<AggregateResult> iterator = aggregateOutput.getResults().iterator();
AggregateResult aggregateResult;
aggregateResult = iterator.next();
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals("Donny", aggregateResult.getGroupByValue("firstName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals("Tim", aggregateResult.getGroupByValue("firstName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals("Aaron", aggregateResult.getGroupByValue("firstName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals("Darin", aggregateResult.getGroupByValue("firstName"));
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals("Trevor", aggregateResult.getGroupByValue("firstName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testOrderByAggregate() throws QException
{
////////////////////////////////////////////////////
// insert a few extra rows from the core data set //
////////////////////////////////////////////////////
insertExtraPersonRecords();
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
Aggregate sumOfDaysWorked = new Aggregate("daysWorked", AggregateOperator.SUM);
aggregateInput.withAggregate(countOfId);
// note - don't query this value - just order by it!! aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderByAggregate(sumOfDaysWorked, false)));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
Iterator<AggregateResult> iterator = aggregateOutput.getResults().iterator();
AggregateResult aggregateResult;
aggregateResult = iterator.next();
Assertions.assertEquals("Kelkhoff", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(4, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Richardson", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Maes", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Samples", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(1, aggregateResult.getAggregateValue(countOfId));
aggregateResult = iterator.next();
Assertions.assertEquals("Chamberlain", aggregateResult.getGroupByValue("lastName"));
Assertions.assertEquals(2, aggregateResult.getAggregateValue(countOfId));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
public void testNoRowsFound() throws QException
{
AggregateInput aggregateInput = initAggregateRequest();
Aggregate countOfId = new Aggregate("id", AggregateOperator.COUNT);
aggregateInput.withAggregate(countOfId);
aggregateInput.withFilter(new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.EQUALS, -9)));
////////////////////////////////////////////////////////////
// when there's no group-by, we get a row, but w/ 0 count //
////////////////////////////////////////////////////////////
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
AggregateResult aggregateResult = aggregateOutput.getResults().get(0);
Assertions.assertEquals(0, aggregateResult.getAggregateValue(countOfId));
/////////////////////////////////////////////////////////////////////////////////////////
// but re-run w/ a group-by -- then, if no rows are found, there are 0 result objects. //
/////////////////////////////////////////////////////////////////////////////////////////
aggregateInput.withGroupByFieldName("lastName");
aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
assertTrue(aggregateOutput.getResults().isEmpty());
}
/*******************************************************************************
**
*******************************************************************************/
private static void insertExtraPersonRecords() throws QException
{
InsertInput insertInput = new InsertInput(TestUtils.defineInstance());
insertInput.setSession(new QSession());
insertInput.setTableName(TestUtils.defineTablePerson().getName());
insertInput.setRecords(List.of(
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Trevor").withValue("email", "tk@kr.com").withValue("daysWorked", 1024),
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Darin").withValue("email", "dk2@kr.com").withValue("daysWorked", 314),
new QRecord().withValue("lastName", "Kelkhoff").withValue("firstName", "Aaron").withValue("email", "ak@kr.com").withValue("daysWorked", 9999),
new QRecord().withValue("lastName", "Chamberlain").withValue("firstName", "Donny").withValue("email", "dc@kr.com").withValue("daysWorked", 17)
));
new InsertAction().execute(insertInput);
}
/*******************************************************************************
**
*******************************************************************************/
private AggregateInput initAggregateRequest()
{
AggregateInput aggregateInput = new AggregateInput();
aggregateInput.setInstance(TestUtils.defineInstance());
aggregateInput.setTableName(TestUtils.defineTablePerson().getName());
return aggregateInput;
}
}

View File

@ -29,14 +29,17 @@ CREATE TABLE person
first_name VARCHAR(80) NOT NULL,
last_name VARCHAR(80) NOT NULL,
birth_date DATE,
email VARCHAR(250) NOT NULL
email VARCHAR(250) NOT NULL,
is_employed BOOLEAN,
annual_salary DECIMAL(12,2),
days_worked INTEGER
);
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (1, 'Darin', 'Kelkhoff', '1980-05-31', 'darin.kelkhoff@gmail.com');
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (2, 'James', 'Maes', '1980-05-15', 'jmaes@mmltholdings.com');
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (3, 'Tim', 'Chamberlain', '1976-05-28', 'tchamberlain@mmltholdings.com');
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (4, 'Tyler', 'Samples', NULL, 'tsamples@mmltholdings.com');
INSERT INTO person (id, first_name, last_name, birth_date, email) VALUES (5, 'Garret', 'Richardson', '1981-01-01', 'grichardson@mmltholdings.com');
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (1, 'Darin', 'Kelkhoff', '1980-05-31', 'darin.kelkhoff@gmail.com', 1, 25000, 27);
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (2, 'James', 'Maes', '1980-05-15', 'jmaes@mmltholdings.com', 1, 26000, 124);
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (3, 'Tim', 'Chamberlain', '1976-05-28', 'tchamberlain@mmltholdings.com', 0, null, 0);
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (4, 'Tyler', 'Samples', NULL, 'tsamples@mmltholdings.com', 1, 30000, 99);
INSERT INTO person (id, first_name, last_name, birth_date, email, is_employed, annual_salary, days_worked) VALUES (5, 'Garret', 'Richardson', '1981-01-01', 'grichardson@mmltholdings.com', 1, 1000000, 232);
DROP TABLE IF EXISTS carrier;
CREATE TABLE carrier