mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 13:10:44 +00:00
QQQ-21: added 'count' action
This commit is contained in:
4
pom.xml
4
pom.xml
@ -53,12 +53,12 @@
|
||||
<dependency>
|
||||
<groupId>com.kingsrook.qqq</groupId>
|
||||
<artifactId>qqq-backend-core</artifactId>
|
||||
<version>0.1.0-20220708.155822-4</version>
|
||||
<version>0.1.0-20220708.195335-5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.kingsrook.qqq</groupId>
|
||||
<artifactId>qqq-backend-module-rdbms</artifactId>
|
||||
<version>0.0.0</version>
|
||||
<version>0.1.0-20220708.202041-3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
@ -90,6 +90,7 @@ public class QCommandBuilder
|
||||
// add table-specific sub-commands for the table //
|
||||
///////////////////////////////////////////////////
|
||||
tableCommand.addSubcommand("meta-data", defineMetaDataCommand(table));
|
||||
tableCommand.addSubcommand("count", defineQueryCommand(table));
|
||||
tableCommand.addSubcommand("query", defineQueryCommand(table));
|
||||
tableCommand.addSubcommand("insert", defineInsertCommand(table));
|
||||
tableCommand.addSubcommand("update", defineUpdateCommand(table));
|
||||
|
@ -32,6 +32,7 @@ import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.actions.CountAction;
|
||||
import com.kingsrook.qqq.backend.core.actions.DeleteAction;
|
||||
import com.kingsrook.qqq.backend.core.actions.InsertAction;
|
||||
import com.kingsrook.qqq.backend.core.actions.MetaDataAction;
|
||||
@ -45,6 +46,8 @@ import com.kingsrook.qqq.backend.core.adapters.JsonToQRecordAdapter;
|
||||
import com.kingsrook.qqq.backend.core.adapters.QInstanceAdapter;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QModuleDispatchException;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.count.CountRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.count.CountResult;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.delete.DeleteRequest;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.delete.DeleteResult;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.insert.InsertRequest;
|
||||
@ -289,6 +292,10 @@ public class QPicoCliImplementation
|
||||
{
|
||||
return runTableMetaData(commandLine, tableName, subParseResult);
|
||||
}
|
||||
case "count":
|
||||
{
|
||||
return runTableCount(commandLine, tableName, subParseResult);
|
||||
}
|
||||
case "query":
|
||||
{
|
||||
return runTableQuery(commandLine, tableName, subParseResult);
|
||||
@ -418,6 +425,24 @@ public class QPicoCliImplementation
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private int runTableCount(CommandLine commandLine, String tableName, ParseResult subParseResult) throws QException
|
||||
{
|
||||
CountRequest countRequest = new CountRequest(qInstance);
|
||||
countRequest.setSession(session);
|
||||
countRequest.setTableName(tableName);
|
||||
countRequest.setFilter(generateQueryFilter(subParseResult));
|
||||
|
||||
CountAction countAction = new CountAction();
|
||||
CountResult countResult = countAction.execute(countRequest);
|
||||
commandLine.getOut().println(JsonUtils.toPrettyJson(countResult));
|
||||
return commandLine.getCommandSpec().exitCodeOnSuccess();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@ -428,9 +453,22 @@ public class QPicoCliImplementation
|
||||
queryRequest.setTableName(tableName);
|
||||
queryRequest.setSkip(subParseResult.matchedOptionValue("skip", null));
|
||||
queryRequest.setLimit(subParseResult.matchedOptionValue("limit", DEFAULT_LIMIT));
|
||||
queryRequest.setFilter(generateQueryFilter(subParseResult));
|
||||
|
||||
QueryAction queryAction = new QueryAction();
|
||||
QueryResult queryResult = queryAction.execute(queryRequest);
|
||||
commandLine.getOut().println(JsonUtils.toPrettyJson(queryResult));
|
||||
return commandLine.getCommandSpec().exitCodeOnSuccess();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private QQueryFilter generateQueryFilter(ParseResult subParseResult)
|
||||
{
|
||||
QQueryFilter filter = new QQueryFilter();
|
||||
queryRequest.setFilter(filter);
|
||||
|
||||
String[] criteria = subParseResult.matchedOptionValue("criteria", new String[] {});
|
||||
for(String criterion : criteria)
|
||||
@ -444,10 +482,7 @@ public class QPicoCliImplementation
|
||||
filter.addCriteria(qQueryCriteria);
|
||||
}
|
||||
|
||||
QueryAction queryAction = new QueryAction();
|
||||
QueryResult queryResult = queryAction.execute(queryRequest);
|
||||
commandLine.getOut().println(JsonUtils.toPrettyJson(queryResult));
|
||||
return commandLine.getCommandSpec().exitCodeOnSuccess();
|
||||
return filter;
|
||||
}
|
||||
|
||||
|
||||
|
@ -214,6 +214,28 @@ class QPicoCliImplementationTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** test running a count on a table
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
public void test_tableCount()
|
||||
{
|
||||
TestOutput testOutput = testCli("person", "count", "--criteria", "id NOT_EQUALS 3");
|
||||
JSONObject countResult = JsonUtils.toJSONObject(testOutput.getOutput());
|
||||
assertNotNull(countResult);
|
||||
int count = countResult.getInt("count");
|
||||
assertEquals(4, count);
|
||||
|
||||
testOutput = testCli("person", "count", "--criteria", "id EQUALS 3");
|
||||
countResult = JsonUtils.toJSONObject(testOutput.getOutput());
|
||||
assertNotNull(countResult);
|
||||
count = countResult.getInt("count");
|
||||
assertEquals(1, count);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** test running a query on a table
|
||||
**
|
||||
|
Reference in New Issue
Block a user