Update to put the generated id on newly inserted cached records

This commit is contained in:
2023-07-03 11:09:01 -05:00
parent 9e4743e8d8
commit 40d073bf54
3 changed files with 31 additions and 8 deletions

View File

@ -133,6 +133,23 @@ public class QueryActionCacheHelper
insertInput.setRecords(recordsToCache);
insertInput.setSkipUniqueKeyCheck(true);
InsertOutput insertOutput = new InsertAction().execute(insertInput);
//////////////////////////////////////////////////////////
// set the (generated) ids in the records being returne //
//////////////////////////////////////////////////////////
Map<List<Serializable>, QRecord> insertedRecordsByUniqueKey = new HashMap<>();
for(QRecord record : insertOutput.getRecords())
{
insertedRecordsByUniqueKey.put(getUniqueKeyValues(record), record);
}
for(QRecord record : recordsToReturn)
{
QRecord insertedRecord = insertedRecordsByUniqueKey.get(getUniqueKeyValues(record));
if(insertedRecord != null)
{
record.setValue(table.getPrimaryKeyField(), insertedRecord.getValue(table.getPrimaryKeyField()));
}
}
}
catch(Exception e)
{

View File

@ -349,32 +349,37 @@ public class MemoryRecordStore
QFieldMetaData primaryKeyField = table.getField(table.getPrimaryKeyField());
for(QRecord record : input.getRecords())
{
if(CollectionUtils.nullSafeHasContents(record.getErrors()))
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// make a copy of the record, to be inserted, and returned. this can avoid some cases where the in-memory store acts //
// differently from other backends, because of having the same record variable in the backend store and in the user-code. //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
QRecord recordToInsert = new QRecord(record);
if(CollectionUtils.nullSafeHasContents(recordToInsert.getErrors()))
{
outputRecords.add(record);
outputRecords.add(recordToInsert);
continue;
}
/////////////////////////////////////////////////
// set the next serial in the record if needed //
/////////////////////////////////////////////////
if(record.getValue(primaryKeyField.getName()) == null && primaryKeyField.getType().equals(QFieldType.INTEGER))
if(recordToInsert.getValue(primaryKeyField.getName()) == null && primaryKeyField.getType().equals(QFieldType.INTEGER))
{
record.setValue(primaryKeyField.getName(), nextSerial++);
recordToInsert.setValue(primaryKeyField.getName(), nextSerial++);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// make sure that if the user supplied a serial, greater than the one we had, that we skip ahead //
///////////////////////////////////////////////////////////////////////////////////////////////////
if(primaryKeyField.getType().equals(QFieldType.INTEGER) && record.getValueInteger(primaryKeyField.getName()) > nextSerial)
if(primaryKeyField.getType().equals(QFieldType.INTEGER) && recordToInsert.getValueInteger(primaryKeyField.getName()) > nextSerial)
{
nextSerial = record.getValueInteger(primaryKeyField.getName()) + 1;
nextSerial = recordToInsert.getValueInteger(primaryKeyField.getName()) + 1;
}
tableData.put(record.getValue(primaryKeyField.getName()), record);
tableData.put(recordToInsert.getValue(primaryKeyField.getName()), recordToInsert);
if(returnInsertedRecords)
{
outputRecords.add(record);
outputRecords.add(recordToInsert);
}
}

View File

@ -87,6 +87,7 @@ class QueryActionCacheHelperTest extends BaseTest
assertNotEquals(0, queryOutput.getRecords().size());
assertNotNull(queryOutput.getRecords().get(0).getValue("cachedDate"));
assertEquals(3, queryOutput.getRecords().get(0).getValue("noOfSides"));
assertNotNull(queryOutput.getRecords().get(0).getValue("id"));
}
////////////////////////////////////////////////////////////////////////////////////