mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-19 05:30:43 +00:00
Hotfix - sqs batch mode, and rdbms delete-by-filter
This commit is contained in:
@ -188,7 +188,7 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
|
||||
String sql = "DELETE FROM "
|
||||
+ escapeIdentifier(tableName)
|
||||
+ " WHERE "
|
||||
+ primaryKeyName + " = ?";
|
||||
+ escapeIdentifier(primaryKeyName) + " = ?";
|
||||
|
||||
try
|
||||
{
|
||||
@ -263,7 +263,7 @@ public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInte
|
||||
|
||||
// todo sql customization - can edit sql and/or param list?
|
||||
String sql = "DELETE FROM "
|
||||
+ tableName
|
||||
+ escapeIdentifier(tableName) + " AS " + escapeIdentifier(table.getName())
|
||||
+ " WHERE "
|
||||
+ whereClause;
|
||||
|
||||
|
@ -26,6 +26,9 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
|
||||
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.QQueryFilter;
|
||||
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;
|
||||
@ -164,9 +167,9 @@ public class RDBMSDeleteActionTest extends RDBMSActionTest
|
||||
|
||||
DeleteOutput deleteResult = new RDBMSDeleteAction().execute(deleteInput);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// assert that 6 queries ran - the initial delete (which failed), then 6 more //
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// assert that 6 queries ran - the initial delete (which failed), then 5 more deletes //
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
QueryManager.setCollectStatistics(false);
|
||||
Map<String, Integer> queryStats = QueryManager.getStatistics();
|
||||
assertEquals(6, queryStats.get(QueryManager.STAT_QUERIES_RAN), "Number of queries ran");
|
||||
@ -191,6 +194,104 @@ public class RDBMSDeleteActionTest extends RDBMSActionTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testDeleteByFilterThatJustWorks() throws Exception
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// load the parent-child tables, with foreign keys and instance //
|
||||
//////////////////////////////////////////////////////////////////
|
||||
TestUtils.primeTestDatabase("prime-test-database-parent-child-tables.sql");
|
||||
DeleteInput deleteInput = initChildTableInstanceAndDeleteRequest();
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// try to delete the records without a foreign key that'll block them //
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
deleteInput.setQueryFilter(new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.IN, List.of(2, 4, 5))));
|
||||
|
||||
QueryManager.setCollectStatistics(true);
|
||||
QueryManager.resetStatistics();
|
||||
|
||||
DeleteOutput deleteResult = new RDBMSDeleteAction().execute(deleteInput);
|
||||
|
||||
//////////////////////////////////
|
||||
// assert that just 1 query ran //
|
||||
//////////////////////////////////
|
||||
QueryManager.setCollectStatistics(false);
|
||||
Map<String, Integer> queryStats = QueryManager.getStatistics();
|
||||
assertEquals(1, queryStats.get(QueryManager.STAT_QUERIES_RAN), "Number of queries ran");
|
||||
assertEquals(3, deleteResult.getDeletedRecordCount(), "Should get back that 3 were deleted");
|
||||
|
||||
runTestSql("SELECT id FROM child_table", (rs -> {
|
||||
int rowsFound = 0;
|
||||
while(rs.next())
|
||||
{
|
||||
rowsFound++;
|
||||
///////////////////////////////////////////
|
||||
// child_table rows 1 & 3 should survive //
|
||||
///////////////////////////////////////////
|
||||
assertTrue(rs.getInt(1) == 1 || rs.getInt(1) == 3);
|
||||
}
|
||||
assertEquals(2, rowsFound);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void testDeleteByFilterWhereForeignKeyBlocksSome() throws Exception
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// load the parent-child tables, with foreign keys and instance //
|
||||
//////////////////////////////////////////////////////////////////
|
||||
TestUtils.primeTestDatabase("prime-test-database-parent-child-tables.sql");
|
||||
DeleteInput deleteInput = initChildTableInstanceAndDeleteRequest();
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// try to delete all of the child records - 2 should fail, because they are referenced by parent_table.child_id //
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
deleteInput.setQueryFilter(new QQueryFilter(new QFilterCriteria("id", QCriteriaOperator.IN, List.of(1, 2, 3, 4, 5))));
|
||||
|
||||
QueryManager.setCollectStatistics(true);
|
||||
QueryManager.resetStatistics();
|
||||
|
||||
DeleteOutput deleteResult = new RDBMSDeleteAction().execute(deleteInput);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// assert that 8 queries ran - the initial delete (which failed), then 1 to look up the ids //
|
||||
// from that query, another to try to delete all those ids (also fails), and finally 5 deletes by id //
|
||||
// todo - maybe we shouldn't do that 2nd "try to delete 'em all by id"... why would it ever work, //
|
||||
// but the original filter query didn't (other than malformed SQL)? //
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
QueryManager.setCollectStatistics(false);
|
||||
Map<String, Integer> queryStats = QueryManager.getStatistics();
|
||||
assertEquals(8, queryStats.get(QueryManager.STAT_QUERIES_RAN), "Number of queries ran");
|
||||
|
||||
assertEquals(2, deleteResult.getRecordsWithErrors().size(), "Should get back the 2 records with errors");
|
||||
assertTrue(deleteResult.getRecordsWithErrors().stream().noneMatch(r -> r.getErrors().isEmpty()), "All we got back should have errors");
|
||||
assertEquals(3, deleteResult.getDeletedRecordCount(), "Should get back that 3 were deleted");
|
||||
|
||||
runTestSql("SELECT id FROM child_table", (rs -> {
|
||||
int rowsFound = 0;
|
||||
while(rs.next())
|
||||
{
|
||||
rowsFound++;
|
||||
///////////////////////////////////////////
|
||||
// child_table rows 1 & 3 should survive //
|
||||
///////////////////////////////////////////
|
||||
assertTrue(rs.getInt(1) == 1 || rs.getInt(1) == 3);
|
||||
}
|
||||
assertEquals(2, rowsFound);
|
||||
}));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -19,7 +19,9 @@
|
||||
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS parent_table;
|
||||
DROP TABLE IF EXISTS child_table;
|
||||
|
||||
CREATE TABLE child_table
|
||||
(
|
||||
id INT AUTO_INCREMENT primary key,
|
||||
@ -32,7 +34,6 @@ INSERT INTO child_table (id, name) VALUES (3, 'Johnny');
|
||||
INSERT INTO child_table (id, name) VALUES (4, 'Gracie');
|
||||
INSERT INTO child_table (id, name) VALUES (5, 'Suzie');
|
||||
|
||||
DROP TABLE IF EXISTS parent_table;
|
||||
CREATE TABLE parent_table
|
||||
(
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
|
Reference in New Issue
Block a user