CE-1405 fix bug with fieldNamesToInclude for tables w/ no selected fields

This commit is contained in:
2024-08-20 09:38:40 -05:00
parent fea757c46d
commit 59a70a4cb7
2 changed files with 33 additions and 2 deletions

View File

@ -51,6 +51,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryJoin;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.data.SparseQRecord;
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;
@ -177,7 +178,7 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
{
setQueryStatFirstResultTime();
QRecord record = new QRecord();
QRecord record = queryInput.getFieldNamesToInclude() == null ? new QRecord() : new SparseQRecord(queryInput.getFieldNamesToInclude());
record.setTableName(table.getName());
LinkedHashMap<String, Serializable> values = new LinkedHashMap<>();
record.setValues(values);
@ -330,6 +331,8 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
StringBuilder selectClause = new StringBuilder((requiresDistinct) ? "SELECT DISTINCT " : "SELECT ").append(columns);
List<QFieldMetaData> selectionFieldList = new ArrayList<>(fieldList);
boolean needCommaBeforeJoinFields = !columns.isEmpty();
///////////////////////////////////
// add any 'selected' queryJoins //
///////////////////////////////////
@ -350,6 +353,10 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
List<QFieldMetaData> joinFieldList = joinTable.getFields().values()
.stream().filter(field -> fieldNamesToInclude == null || fieldNamesToInclude.contains(tableNameOrAlias + "." + field.getName()))
.toList();
if(joinFieldList.isEmpty())
{
continue;
}
/////////////////////////////////////////////////////
// map to columns, wrapping heavy fields as needed //
@ -363,7 +370,13 @@ public class RDBMSQueryAction extends AbstractRDBMSAction implements QueryInterf
// append to output objects. //
// note that fields are cloned, since we are changing their names to have table/alias prefix. //
////////////////////////////////////////////////////////////////////////////////////////////////
selectClause.append(", ").append(joinColumns);
if(needCommaBeforeJoinFields)
{
selectClause.append(", ");
}
selectClause.append(joinColumns);
needCommaBeforeJoinFields = true;
selectionFieldList.addAll(joinFieldList.stream().map(field -> field.clone().withName(tableNameOrAlias + "." + field.getName())).toList());
}
}

View File

@ -1077,6 +1077,24 @@ public class RDBMSQueryActionJoinsTest extends RDBMSActionTest
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().containsKey("firstName"));
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().containsKey(TestUtils.TABLE_NAME_PERSONAL_ID_CARD + ".idNumber"));
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().size() > 2);
////////////////////////////////////////////////////////////////////////////
// regression from original build - where only join fields made sql error //
////////////////////////////////////////////////////////////////////////////
queryInput.withFieldNamesToInclude(Set.of(TestUtils.TABLE_NAME_PERSONAL_ID_CARD + ".idNumber"));
queryOutput = new QueryAction().execute(queryInput);
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().containsKey(TestUtils.TABLE_NAME_PERSONAL_ID_CARD + ".idNumber"));
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().size() == 1);
//////////////////////////////////////////////////////////////////////////////////////////
// similar regression to above, if one of the join tables didn't have anything selected //
//////////////////////////////////////////////////////////////////////////////////////////
queryInput.withQueryJoin(new QueryJoin(TestUtils.TABLE_NAME_PERSONAL_ID_CARD).withAlias("id2").withSelect(true));
queryInput.withFieldNamesToInclude(Set.of("firstName", "id2.idNumber"));
queryOutput = new QueryAction().execute(queryInput);
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().containsKey("firstName"));
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().containsKey("id2.idNumber"));
assertThat(queryOutput.getRecords()).allMatch(r -> r.getValues().size() == 2);
}
}