mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
CE-1180 add option to setPrimaryKeyInInsertedRecords (on the INPUT records)
This commit is contained in:
@ -157,6 +157,17 @@ public class ReplaceAction extends AbstractQActionFunction<ReplaceInput, Replace
|
|||||||
output.setDeleteOutput(deleteOutput);
|
output.setDeleteOutput(deleteOutput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(input.getSetPrimaryKeyInInsertedRecords())
|
||||||
|
{
|
||||||
|
for(int i = 0; i < insertList.size(); i++)
|
||||||
|
{
|
||||||
|
if(i < insertOutput.getRecords().size())
|
||||||
|
{
|
||||||
|
insertList.get(i).setValue(primaryKeyField, insertOutput.getRecords().get(i).getValue(primaryKeyField));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(weOwnTheTransaction)
|
if(weOwnTheTransaction)
|
||||||
{
|
{
|
||||||
transaction.commit();
|
transaction.commit();
|
||||||
|
@ -41,6 +41,7 @@ public class ReplaceInput extends AbstractTableActionInput
|
|||||||
private QQueryFilter filter;
|
private QQueryFilter filter;
|
||||||
private boolean performDeletes = true;
|
private boolean performDeletes = true;
|
||||||
private boolean allowNullKeyValuesToEqual = false;
|
private boolean allowNullKeyValuesToEqual = false;
|
||||||
|
private boolean setPrimaryKeyInInsertedRecords = false;
|
||||||
|
|
||||||
private boolean omitDmlAudit = false;
|
private boolean omitDmlAudit = false;
|
||||||
|
|
||||||
@ -271,4 +272,35 @@ public class ReplaceInput extends AbstractTableActionInput
|
|||||||
return (this);
|
return (this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Getter for setPrimaryKeyInInsertedRecords
|
||||||
|
*******************************************************************************/
|
||||||
|
public boolean getSetPrimaryKeyInInsertedRecords()
|
||||||
|
{
|
||||||
|
return (this.setPrimaryKeyInInsertedRecords);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Setter for setPrimaryKeyInInsertedRecords
|
||||||
|
*******************************************************************************/
|
||||||
|
public void setSetPrimaryKeyInInsertedRecords(boolean setPrimaryKeyInInsertedRecords)
|
||||||
|
{
|
||||||
|
this.setPrimaryKeyInInsertedRecords = setPrimaryKeyInInsertedRecords;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
** Fluent setter for setPrimaryKeyInInsertedRecords
|
||||||
|
*******************************************************************************/
|
||||||
|
public ReplaceInput withSetPrimaryKeyInInsertedRecords(boolean setPrimaryKeyInInsertedRecords)
|
||||||
|
{
|
||||||
|
this.setPrimaryKeyInInsertedRecords = setPrimaryKeyInInsertedRecords;
|
||||||
|
return (this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ package com.kingsrook.qqq.backend.core.actions.tables;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import com.kingsrook.qqq.backend.core.BaseTest;
|
import com.kingsrook.qqq.backend.core.BaseTest;
|
||||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||||
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
|
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
|
||||||
@ -38,8 +39,11 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
|||||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
|
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
|
||||||
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
import com.kingsrook.qqq.backend.core.utils.TestUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
@ -81,12 +85,27 @@ class ReplaceActionTest extends BaseTest
|
|||||||
replaceInput.setOmitDmlAudit(true);
|
replaceInput.setOmitDmlAudit(true);
|
||||||
replaceInput.setRecords(newPeople);
|
replaceInput.setRecords(newPeople);
|
||||||
replaceInput.setFilter(null);
|
replaceInput.setFilter(null);
|
||||||
|
replaceInput.setSetPrimaryKeyInInsertedRecords(false);
|
||||||
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
|
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
|
||||||
|
|
||||||
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
|
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
|
||||||
assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size());
|
assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size());
|
||||||
assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount());
|
assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount());
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// due to false for SetPrimaryKeyInInsertedRecords, make sure primary keys aren't on the records that got inserted //
|
||||||
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Optional<QRecord> ned = newPeople.stream().filter(r -> r.getValueString("firstName").equals("Ned")).findFirst();
|
||||||
|
assertThat(ned).isPresent();
|
||||||
|
assertNull(ned.get().getValue("id"), "the record that got inserted should not have its primary key set");
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// but note, homer (who was updated) would have had its primary key set too, as part of the internal processing that does the update. //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Optional<QRecord> homer = newPeople.stream().filter(r -> r.getValueString("firstName").equals("Homer")).findFirst();
|
||||||
|
assertThat(homer).isPresent();
|
||||||
|
assertNotNull(homer.get().getValue("id"), "the record that got updated should have its primary key set");
|
||||||
|
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
// assert homer was updated //
|
// assert homer was updated //
|
||||||
//////////////////////////////
|
//////////////////////////////
|
||||||
@ -136,12 +155,18 @@ class ReplaceActionTest extends BaseTest
|
|||||||
replaceInput.setOmitDmlAudit(true);
|
replaceInput.setOmitDmlAudit(true);
|
||||||
replaceInput.setRecords(newPeople);
|
replaceInput.setRecords(newPeople);
|
||||||
replaceInput.setFilter(null);
|
replaceInput.setFilter(null);
|
||||||
|
replaceInput.setSetPrimaryKeyInInsertedRecords(true);
|
||||||
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
|
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
|
||||||
|
|
||||||
assertEquals(2, replaceOutput.getInsertOutput().getRecords().size());
|
assertEquals(2, replaceOutput.getInsertOutput().getRecords().size());
|
||||||
assertEquals(0, replaceOutput.getUpdateOutput().getRecords().size());
|
assertEquals(0, replaceOutput.getUpdateOutput().getRecords().size());
|
||||||
assertEquals(2, replaceOutput.getDeleteOutput().getDeletedRecordCount());
|
assertEquals(2, replaceOutput.getDeleteOutput().getDeletedRecordCount());
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// due to true for SetPrimaryKeyInInsertedRecords, make sure primary keys ARE on all the records that got inserted //
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
assertTrue(newPeople.stream().allMatch(r -> r.getValue("id") != null), "All inserted records should have their primary key");
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// assert homer & marge were deleted //
|
// assert homer & marge were deleted //
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
|
Reference in New Issue
Block a user