mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
Implemented delete
This commit is contained in:
@ -1,9 +1,11 @@
|
|||||||
package com.kingsrook.qqq.backend.module.rdbms;
|
package com.kingsrook.qqq.backend.module.rdbms;
|
||||||
|
|
||||||
|
|
||||||
|
import com.kingsrook.qqq.backend.core.modules.interfaces.DeleteInterface;
|
||||||
import com.kingsrook.qqq.backend.core.modules.interfaces.InsertInterface;
|
import com.kingsrook.qqq.backend.core.modules.interfaces.InsertInterface;
|
||||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QModuleInterface;
|
import com.kingsrook.qqq.backend.core.modules.interfaces.QModuleInterface;
|
||||||
import com.kingsrook.qqq.backend.core.modules.interfaces.QueryInterface;
|
import com.kingsrook.qqq.backend.core.modules.interfaces.QueryInterface;
|
||||||
|
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSDeleteAction;
|
||||||
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSInsertAction;
|
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSInsertAction;
|
||||||
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSQueryAction;
|
import com.kingsrook.qqq.backend.module.rdbms.actions.RDBMSQueryAction;
|
||||||
|
|
||||||
@ -32,4 +34,15 @@ public class RDBSMModule implements QModuleInterface
|
|||||||
{
|
{
|
||||||
return (new RDBMSInsertAction());
|
return (new RDBMSInsertAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Override
|
||||||
|
public DeleteInterface getDeleteInterface()
|
||||||
|
{
|
||||||
|
return (new RDBMSDeleteAction());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,72 @@
|
|||||||
|
package com.kingsrook.qqq.backend.module.rdbms.actions;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.data.QRecordWithStatus;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.metadata.QTableMetaData;
|
||||||
|
import com.kingsrook.qqq.backend.core.modules.interfaces.DeleteInterface;
|
||||||
|
import com.kingsrook.qqq.backend.module.rdbms.RDBSMBackendMetaData;
|
||||||
|
import com.kingsrook.qqq.backend.module.rdbms.jdbc.ConnectionManager;
|
||||||
|
import com.kingsrook.qqq.backend.module.rdbms.jdbc.QueryManager;
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public class RDBMSDeleteAction extends AbstractRDBMSAction implements DeleteInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public DeleteResult execute(DeleteRequest deleteRequest) throws QException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
DeleteResult rs = new DeleteResult();
|
||||||
|
QTableMetaData table = deleteRequest.getTable();
|
||||||
|
|
||||||
|
String tableName = table.getName();
|
||||||
|
String primaryKeyName = getColumnName(table.getField(table.getPrimaryKeyField()));
|
||||||
|
String sql = "DELETE FROM "
|
||||||
|
+ tableName
|
||||||
|
+ " WHERE "
|
||||||
|
+ primaryKeyName
|
||||||
|
+ " IN ("
|
||||||
|
+ deleteRequest.getPrimaryKeys().stream().map(x -> "?").collect(Collectors.joining(","))
|
||||||
|
+ ")";
|
||||||
|
List<Serializable> params = deleteRequest.getPrimaryKeys();
|
||||||
|
|
||||||
|
// todo sql customization - can edit sql and/or param list
|
||||||
|
|
||||||
|
ConnectionManager connectionManager = new ConnectionManager();
|
||||||
|
Connection connection = connectionManager.getConnection(new RDBSMBackendMetaData(deleteRequest.getBackend()));
|
||||||
|
|
||||||
|
QueryManager.executeUpdateForRowCount(connection, sql, params);
|
||||||
|
List<QRecordWithStatus> recordsWithStatus = new ArrayList<>();
|
||||||
|
rs.setRecords(recordsWithStatus);
|
||||||
|
for(Serializable primaryKey : deleteRequest.getPrimaryKeys())
|
||||||
|
{
|
||||||
|
QRecord qRecord = new QRecord().withTableName(deleteRequest.getTableName()).withPrimaryKey(primaryKey);
|
||||||
|
// todo uh, identify any errors?
|
||||||
|
QRecordWithStatus recordWithStatus = new QRecordWithStatus(qRecord);
|
||||||
|
recordsWithStatus.add(recordWithStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs;
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
throw new QException("Error executing delete: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -90,4 +90,16 @@ public class RDBMSActionTest
|
|||||||
QueryManager.executeUpdate(connection, sql);
|
QueryManager.executeUpdate(connection, sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
protected void runTestSql(String sql, QueryManager.ResultSetProcessor resultSetProcessor) throws Exception
|
||||||
|
{
|
||||||
|
ConnectionManager connectionManager = new ConnectionManager();
|
||||||
|
Connection connection = connectionManager.getConnection(new RDBSMBackendMetaData(defineBackend()));
|
||||||
|
QueryManager.executeStatement(connection, sql, resultSetProcessor);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
package com.kingsrook.qqq.backend.module.rdbms.actions;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.DeleteRequest;
|
||||||
|
import com.kingsrook.qqq.backend.core.model.actions.DeleteResult;
|
||||||
|
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
public class RDBMSDeleteActionTest extends RDBMSActionTest
|
||||||
|
{
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@BeforeEach
|
||||||
|
public void beforeEach() throws Exception
|
||||||
|
{
|
||||||
|
super.primeTestDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
public void testDeleteAll() throws Exception
|
||||||
|
{
|
||||||
|
DeleteRequest deleteRequest = initDeleteRequest();
|
||||||
|
deleteRequest.setPrimaryKeys(List.of(1, 2, 3, 4, 5));
|
||||||
|
DeleteResult deleteResult = new RDBMSDeleteAction().execute(deleteRequest);
|
||||||
|
assertEquals(5, deleteResult.getRecords().size(), "Unfiltered delete should return all rows");
|
||||||
|
assertTrue(deleteResult.getRecords().stream().noneMatch(qrs -> CollectionUtils.nullSafeHasContents(qrs.getErrors())), "There should be no errors");
|
||||||
|
runTestSql("SELECT id FROM person", (rs -> assertFalse(rs.next())));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
public void testDeleteOne() throws Exception
|
||||||
|
{
|
||||||
|
DeleteRequest deleteRequest = initDeleteRequest();
|
||||||
|
deleteRequest.setPrimaryKeys(List.of(1));
|
||||||
|
DeleteResult deleteResult = new RDBMSDeleteAction().execute(deleteRequest);
|
||||||
|
assertEquals(1, deleteResult.getRecords().size(), "Should delete one row");
|
||||||
|
assertTrue(deleteResult.getRecords().stream().noneMatch(qrs -> CollectionUtils.nullSafeHasContents(qrs.getErrors())), "There should be no errors");
|
||||||
|
runTestSql("SELECT id FROM person WHERE id = 1", (rs -> assertFalse(rs.next())));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
@Test
|
||||||
|
public void testDeleteSome() throws Exception
|
||||||
|
{
|
||||||
|
DeleteRequest deleteRequest = initDeleteRequest();
|
||||||
|
deleteRequest.setPrimaryKeys(List.of(1, 3, 5));
|
||||||
|
DeleteResult deleteResult = new RDBMSDeleteAction().execute(deleteRequest);
|
||||||
|
assertEquals(3, deleteResult.getRecords().size(), "Should delete one row");
|
||||||
|
assertTrue(deleteResult.getRecords().stream().noneMatch(qrs -> CollectionUtils.nullSafeHasContents(qrs.getErrors())), "There should be no errors");
|
||||||
|
runTestSql("SELECT id FROM person", (rs -> {
|
||||||
|
int rowsFound = 0;
|
||||||
|
while(rs.next())
|
||||||
|
{
|
||||||
|
rowsFound++;
|
||||||
|
assertTrue(rs.getInt(1) == 2 || rs.getInt(1) == 4);
|
||||||
|
}
|
||||||
|
assertEquals(2, rowsFound);
|
||||||
|
}));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*******************************************************************************
|
||||||
|
**
|
||||||
|
*******************************************************************************/
|
||||||
|
private DeleteRequest initDeleteRequest()
|
||||||
|
{
|
||||||
|
DeleteRequest deleteRequest = new DeleteRequest();
|
||||||
|
deleteRequest.setInstance(defineInstance());
|
||||||
|
deleteRequest.setTableName(defineTablePerson().getName());
|
||||||
|
return deleteRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user