SPRINT-17: updates to parent widget dropdown data, updated group bys to be objects allowing group by with custom formats

This commit is contained in:
Tim Chamberlain
2022-12-07 15:31:48 -06:00
parent 241741e2e5
commit 9b34ee7fe7
14 changed files with 766 additions and 67 deletions

View File

@ -37,7 +37,9 @@ 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.GroupBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.QFilterOrderByAggregate;
import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.QFilterOrderByGroupBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.JoinsContext;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
@ -123,7 +125,7 @@ public abstract class AbstractRDBMSAction implements QActionInterface
*******************************************************************************/
protected Serializable scrubValue(QFieldMetaData field, Serializable value, boolean isInsert)
{
if("".equals(value))
if("" .equals(value))
{
QFieldType type = field.getType();
if(type.equals(QFieldType.INTEGER) || type.equals(QFieldType.DECIMAL) || type.equals(QFieldType.DATE) || type.equals(QFieldType.DATE_TIME) || type.equals(QFieldType.BOOLEAN))
@ -513,9 +515,9 @@ public abstract class AbstractRDBMSAction implements QActionInterface
/*******************************************************************************
**
*******************************************************************************/
protected Serializable getFieldValueFromResultSet(QFieldMetaData qFieldMetaData, ResultSet resultSet, int i) throws SQLException
protected Serializable getFieldValueFromResultSet(QFieldType type, ResultSet resultSet, int i) throws SQLException
{
switch(qFieldMetaData.getType())
switch(type)
{
case STRING:
case TEXT:
@ -551,10 +553,19 @@ public abstract class AbstractRDBMSAction implements QActionInterface
}
default:
{
throw new IllegalStateException("Unexpected field type: " + qFieldMetaData.getType());
throw new IllegalStateException("Unexpected field type: " + type);
}
}
}
/*******************************************************************************
**
*******************************************************************************/
protected Serializable getFieldValueFromResultSet(QFieldMetaData qFieldMetaData, ResultSet resultSet, int i) throws SQLException
{
return (getFieldValueFromResultSet(qFieldMetaData.getType(), resultSet, i));
}
@ -575,6 +586,10 @@ public abstract class AbstractRDBMSAction implements QActionInterface
String clause = (aggregate.getOperator() + "(" + escapeIdentifier(getColumnName(table.getField(aggregate.getFieldName()))) + ")");
clauses.add(clause + " " + ascOrDesc);
}
else if(orderBy instanceof QFilterOrderByGroupBy orderByGroupBy)
{
clauses.add(getSingleGroupByClause(orderByGroupBy.getGroupBy(), joinsContext) + " " + ascOrDesc);
}
else
{
JoinsContext.FieldAndTableNameOrAlias otherFieldAndTableNameOrAlias = joinsContext.getFieldAndTableNameOrAlias(orderBy.getFieldName());
@ -586,4 +601,23 @@ public abstract class AbstractRDBMSAction implements QActionInterface
}
return (String.join(", ", clauses));
}
/*******************************************************************************
**
*******************************************************************************/
protected String getSingleGroupByClause(GroupBy groupBy, JoinsContext joinsContext)
{
JoinsContext.FieldAndTableNameOrAlias fieldAndTableNameOrAlias = joinsContext.getFieldAndTableNameOrAlias(groupBy.getFieldName());
String fullFieldName = escapeIdentifier(fieldAndTableNameOrAlias.tableNameOrAlias()) + "." + escapeIdentifier(getColumnName(fieldAndTableNameOrAlias.field()));
if(groupBy.getFormatString() == null)
{
return (fullFieldName);
}
else
{
return (String.format(groupBy.getFormatString(), fullFieldName));
}
}
}

View File

@ -34,6 +34,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateIn
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.GroupBy;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.JoinsContext;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
@ -78,7 +79,7 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
sql += " WHERE " + makeWhereClause(aggregateInput.getInstance(), table, joinsContext, filter, params);
}
if(CollectionUtils.nullSafeHasContents(aggregateInput.getGroupByFieldNames()))
if(CollectionUtils.nullSafeHasContents(aggregateInput.getGroupBys()))
{
sql += " GROUP BY " + makeGroupByClause(aggregateInput, joinsContext);
}
@ -105,11 +106,10 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
results.add(result);
int selectionIndex = 1;
for(String groupByFieldName : CollectionUtils.nonNullList(aggregateInput.getGroupByFieldNames()))
for(GroupBy groupBy : CollectionUtils.nonNullList(aggregateInput.getGroupBys()))
{
JoinsContext.FieldAndTableNameOrAlias fieldAndTableNameOrAlias = joinsContext.getFieldAndTableNameOrAlias(groupByFieldName);
Serializable value = getFieldValueFromResultSet(fieldAndTableNameOrAlias.field(), resultSet, selectionIndex++);
result.withGroupByValue(groupByFieldName, value);
Serializable value = getFieldValueFromResultSet(groupBy.getType(), resultSet, selectionIndex++);
result.withGroupByValue(groupBy, value);
}
for(Aggregate aggregate : aggregateInput.getAggregates())
@ -148,10 +148,9 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
{
List<String> rs = new ArrayList<>();
for(String groupByFieldName : CollectionUtils.nonNullList(aggregateInput.getGroupByFieldNames()))
for(GroupBy groupBy : CollectionUtils.nonNullList(aggregateInput.getGroupBys()))
{
JoinsContext.FieldAndTableNameOrAlias fieldAndTableNameOrAlias = joinsContext.getFieldAndTableNameOrAlias(groupByFieldName);
rs.add(escapeIdentifier(fieldAndTableNameOrAlias.tableNameOrAlias()) + "." + escapeIdentifier(getColumnName(fieldAndTableNameOrAlias.field())));
rs.add(getSingleGroupByClause(groupBy, joinsContext));
}
for(Aggregate aggregate : aggregateInput.getAggregates())
@ -170,10 +169,9 @@ public class RDBMSAggregateAction extends AbstractRDBMSAction implements Aggrega
private String makeGroupByClause(AggregateInput aggregateInput, JoinsContext joinsContext)
{
List<String> columns = new ArrayList<>();
for(String groupByFieldName : aggregateInput.getGroupByFieldNames())
for(GroupBy groupBy : CollectionUtils.nonNullList(aggregateInput.getGroupBys()))
{
JoinsContext.FieldAndTableNameOrAlias fieldAndTableNameOrAlias = joinsContext.getFieldAndTableNameOrAlias(groupByFieldName);
columns.add(escapeIdentifier(fieldAndTableNameOrAlias.tableNameOrAlias()) + "." + escapeIdentifier(getColumnName(fieldAndTableNameOrAlias.field())));
columns.add(getSingleGroupByClause(groupBy, joinsContext));
}
return (StringUtils.join(",", columns));

View File

@ -32,6 +32,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.aggregate.AggregateIn
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.GroupBy;
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;
@ -40,6 +41,7 @@ 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.QueryJoin;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.session.QSession;
import com.kingsrook.qqq.backend.module.rdbms.TestUtils;
import org.junit.jupiter.api.Assertions;
@ -145,7 +147,7 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, "lastName", null));
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderBy("lastName")));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
@ -182,8 +184,8 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
aggregateInput.withAggregate(countOfId);
aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.withGroupByFieldName("firstName");
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, "lastName", null));
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, "firstName", null));
aggregateInput.setFilter(new QQueryFilter()
.withOrderBy(new QFilterOrderBy("lastName"))
@ -238,7 +240,7 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
aggregateInput.withAggregate(countOfId);
// note - don't query this value - just order by it!! aggregateInput.withAggregate(sumOfDaysWorked);
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, "lastName", null));
aggregateInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderByAggregate(sumOfDaysWorked, false)));
@ -290,7 +292,7 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
/////////////////////////////////////////////////////////////////////////////////////////
// but re-run w/ a group-by -- then, if no rows are found, there are 0 result objects. //
/////////////////////////////////////////////////////////////////////////////////////////
aggregateInput.withGroupByFieldName("lastName");
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, "lastName", null));
aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
assertTrue(aggregateOutput.getResults().isEmpty());
}
@ -328,7 +330,7 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
aggregateInput.setSession(new QSession());
aggregateInput.setTableName(TestUtils.TABLE_NAME_ORDER);
aggregateInput.withAggregate(sumOfQuantity);
aggregateInput.withGroupByFieldName(TestUtils.TABLE_NAME_ORDER_LINE + ".sku");
aggregateInput.withGroupBy(new GroupBy(QFieldType.STRING, TestUtils.TABLE_NAME_ORDER_LINE + ".sku", null));
aggregateInput.withQueryJoin(new QueryJoin(TestUtils.TABLE_NAME_ORDER, TestUtils.TABLE_NAME_ORDER_LINE));
AggregateOutput aggregateOutput = new RDBMSAggregateAction().execute(aggregateInput);
@ -392,4 +394,4 @@ public class RDBMSAggregateActionTest extends RDBMSActionTest
return aggregateInput;
}
}
}