~aleteoryx/muditaos

ref: df12fbaa95eb446fe3df67782f369457e13ef9dc muditaos/module-db/Common/Query.hpp -rw-r--r-- 3.3 KiB
df12fbaa — Maciej Gibowicz [BH-2099] Add synchronization of custom quotes with Center 8 months 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <functional>
#include <optional>
#include <endpoints/Context.hpp>

namespace db
{
    using sdesktop::endpoints::Context;
    using sdesktop::endpoints::PagedContext;

    class QueryResult; // Forward declaration
    using QueryCallbackFunction                  = std::function<bool(db::QueryResult *)>;
    using EndpointQueryCallbackFunction          = std::function<bool(db::QueryResult *, Context &)>;
    using EndpointQueryCallbackFunctionWithPages = std::function<bool(db::QueryResult *, PagedContext &)>;
    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, Context &_context);

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

      private:
        EndpointQueryCallbackFunction callback;
        Context context;
    };

    class EndpointListenerWithPages : public EndpointListener
    {
      public:
        EndpointListenerWithPages() = default;
        EndpointListenerWithPages(EndpointQueryCallbackFunctionWithPages &&_callback, const PagedContext &_context);

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

      private:
        EndpointQueryCallbackFunctionWithPages callback;
        PagedContext 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;
        void setRecordID(const uint32_t modifiedRecordID);
        [[nodiscard]] auto getRecordID() -> std::optional<uint32_t>;

        bool handle();

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

      protected:
        std::shared_ptr<Query> requestQuery;

      private:
        std::optional<uint32_t> recordID;
    };

} // namespace db