~aleteoryx/muditaos

ref: 92a8a78ed46d6383ca23aad83ffdb35f9b61d0a4 muditaos/module-db/Common/Query.hpp -rw-r--r-- 2.6 KiB
92a8a78e — Marcin Smoczyński Merge branch 'master' into stable 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <memory>
#include <string>
#include <functional>
#include <log/log.hpp>
#include <module-services/service-desktop/endpoints/Context.hpp>

namespace db
{
    class QueryResult; // Forward declaration
    using QueryCallbackFunction         = std::function<bool(db::QueryResult *)>;
    using EndpointQueryCallbackFunction = std::function<bool(db::QueryResult *, parserFSM::Context &)>;

    class QueryListener
    {
      public:
        virtual ~QueryListener() = default;

        virtual bool handleQueryResponse(QueryResult *response) = 0;
    };

    class QueryCallback : public db::QueryListener
    {
      public:
        static std::unique_ptr<QueryCallback> fromFunction(QueryCallbackFunction &&callbackFunction);

        explicit QueryCallback(QueryCallbackFunction &&_callback);

        bool handleQueryResponse(QueryResult *response) override;

      private:
        QueryCallbackFunction callback;
    };

    class EndpointListener : public db::QueryListener
    {
      public:
        EndpointListener() = default;
        EndpointListener(EndpointQueryCallbackFunction &&_callback, parserFSM::Context &_context);

        bool handleQueryResponse(db::QueryResult *result) override;

      private:
        EndpointQueryCallbackFunction callback;
        parserFSM::Context context;
    };

    /// virtual query input interface
    class Query
    {
      public:
        enum class Type
        {
            Create,
            Read,
            Update,
            Delete
        };

        explicit Query(Type type);
        virtual ~Query() = default;

        QueryListener *getQueryListener() const noexcept;
        void setQueryListener(std::unique_ptr<QueryListener> &&listener) noexcept;

        [[nodiscard]] virtual auto debugInfo() const -> std::string = 0;

        const Type type;

      private:
        std::unique_ptr<QueryListener> queryListener;
    };

    /// virtual query output (result) interface
    class QueryResult
    {
      public:
        explicit QueryResult(std::shared_ptr<Query> requestQuery = nullptr);
        virtual ~QueryResult() = default;

        void setRequestQuery(const std::shared_ptr<Query> &requestQueryToSet);
        std::shared_ptr<Query> getRequestQuery() const noexcept;

        bool handle();

        [[nodiscard]] bool hasListener() const noexcept;
        [[nodiscard]] virtual auto debugInfo() const -> std::string = 0;

      protected:
        std::shared_ptr<Query> requestQuery;
    };

} // namespace db