Skip to content
Snippets Groups Projects
Commit 15087d94 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Support for COUNT queries.

parent 70aa4c4e
No related branches found
No related tags found
1 merge request!2ENH: Retrieving single entities works on the mex side.
Pipeline #11982 passed with warnings
Pipeline: caosdb-octaveinttest

#12039

    ......@@ -123,6 +123,7 @@ classdef Caosdb < handle
    % Execute a query
    %
    % entities = Caosdb.query("FIND Record Foo WITH bar=baz")
    % entities = Caosdb.query("COUNT Record Foo WITH bar=baz")
    %
    % Parameters
    % ----------
    ......@@ -133,7 +134,7 @@ classdef Caosdb < handle
    % Returns
    % -------
    % entities : cell array
    % The retrieved entities.
    % The retrieved entities. If the query was a COUNT query, the result is an int64 instead.
    function entities = query(obj, query_str)
    % Ensure that QUERY is a string.
    assert(ischar(query_str), "maox:InvalidArgument", "QUERY must be a string, was:\n%s", ...
    ......@@ -142,8 +143,11 @@ classdef Caosdb < handle
    % disp("query:");
    % disp(query_str);
    try
    collection = maox_query(obj.connection, query_str);
    [collection, count_results] = maox_query(obj.connection, query_str);
    entities = maox_convert_collection(collection);
    if (count_results >= 0)
    entities = count_results;
    end
    catch
    % disp("error handling in Caosdb.m");
    % disp(lasterror());
    ......
    ......@@ -19,14 +19,16 @@ using std::string;
    *
    * @details This function returns the entities as a cell array.
    *
    * @param connection_name A string with the connection name. May be omitted or
    * an empty string.
    * @param connection_name A string with the connection name. May be omitted or an empty string.
    *
    * @param query The query string.
    * @param query The query string.
    *
    * @return collection A struct with the entities.
    * @return resultSet A struct with the entities.
    *
    * @return countResult The result of a COUNT query, is -1 if zero or more than one COUNT queries
    * were given.
    */
    void mexFunction(int /*nlhs*/, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    string conn_name;
    string query;
    ......@@ -41,6 +43,14 @@ void mexFunction(int /*nlhs*/, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    mexErrMsgIdAndTxt("maox:TooManyArguments",
    "Need 2 arguments: connection_name and a query string.");
    }
    if (nlhs < 2) {
    mexErrMsgIdAndTxt("maox:InsufficientReturnArguments",
    "Need 2 return arguments: resultSet and queryCount.");
    }
    if (nlhs > 2) {
    mexErrMsgIdAndTxt("maox:TooManyReturnArguments",
    "Need 2 return arguments: resultSet and queryCount.");
    }
    if (!mxIsChar(prhs[1])) {
    mexErrMsgIdAndTxt("maox:InvalidArgument", "The second argument must be a string.");
    }
    ......@@ -65,10 +75,11 @@ void mexFunction(int /*nlhs*/, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    auto t_stat = transaction->WaitForIt();
    maoxdb::throwOctExceptionIfError(transaction.get());
    // Status must be OK or GENERIC_TRANSACTION_ERROR now.
    const auto &results = transaction->GetResultSet();
    // std::cout << "size: " << results.Size() << std::endl;
    const auto &results = transaction->GetResultSet();
    auto *mxResults = maoxdb::mxFromResultSet(results);
    auto *mxCountResults = maoxdb::mxScalarINT64(transaction->GetCountResult());
    plhs[0] = mxDuplicateArray(mxResults);
    plhs[1] = mxCountResults;
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment