~aleteoryx/muditaos

ref: sign_test muditaos/module-db/queries/RecordQuery.hpp -rw-r--r-- 4.8 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <Common/Query.hpp>
#include <module-apps/application-phonebook/data/ContactsMap.hpp>

#include <string>
#include <utility>
#include <vector>

namespace db::query
{
    /**
     * @brief An universal query class to read database records with offset and limit.
     */
    class RecordQuery : public db::Query
    {
      private:
        std::size_t limit  = 0;
        std::size_t offset = 0;

      public:
        /**
         * @brief Default constructor. Limit and offset are zeroed indicating that
         * all data should be read.
         */
        RecordQuery() noexcept;

        /**
         * @brief Construct a new RecordQuery object with limit and offset values.
         *
         * @param limit maximum number of records to read
         * @param offset start offset to read records from
         */
        RecordQuery(std::size_t limit, std::size_t offset) noexcept;

        /**
         * @brief Gets both limit and offset values
         *
         * @return bound pair of limit and offset for more convenient reading.
         */
        [[nodiscard]] std::pair<std::size_t, std::size_t> getLimitOffset() const noexcept;

        /**
         * @brief Gets limit of a query
         *
         * @return limit
         */
        [[nodiscard]] std::size_t getLimit() const noexcept;

        /**
         * @brief Gets offset of a query
         *
         * @return offset
         */
        [[nodiscard]] std::size_t getOffset() const noexcept;

        /**
         * @brief debug info
         *
         * @return class name
         */
        [[nodiscard]] auto debugInfo() const -> std::string override;
    };

    /**
     * @brief A response to the RecordQuery request.
     *
     * @tparam T type of records which were requested.
     */
    template <typename T>
    class RecordQueryResult : public db::QueryResult
    {
      private:
        std::vector<T> records;

      public:
        /**
         * @brief Default constructor. Constructs an empty response (with no records)
         */
        RecordQueryResult() noexcept = default;

        /**
         * @brief Constructs response with a vector of records to be sent
         *
         * @param result vector of records (std::vector<T>)
         */
        RecordQueryResult(std::vector<T> result) noexcept : records(std::move(result))
        {}

        /**
         * @brief Gets records from query
         *
         * @return received records
         */
        [[nodiscard]] const std::vector<T> &getRecords() const noexcept
        {
            return records;
        }

        /**
         * @brief debug info
         *
         * @return class name
         */
        [[nodiscard]] auto debugInfo() const -> std::string override
        {
            return "RecordQueryResult";
        }
    };

    class LetterMapResult : public db::QueryResult
    {
      private:
        ContactsMapData letterMap;

      public:
        LetterMapResult() noexcept = default;
        LetterMapResult(ContactsMapData result) noexcept : letterMap(std::move(result))
        {}

        [[nodiscard]] const ContactsMapData &getLetterMap() const noexcept
        {
            return letterMap;
        }

        [[nodiscard]] auto debugInfo() const -> std::string override
        {
            return "RecordQueryResult";
        }
    };

    /**
     * @brief An universal query to ask for a count of records in a table
     *
     */
    class RecordsSizeQuery : public db::Query
    {
      public:
        /**
         * @brief Default constructor
         *
         */
        RecordsSizeQuery() noexcept;

        /**
         * @brief debug info
         *
         * @return class name
         */
        [[nodiscard]] auto debugInfo() const -> std::string override;
    };

    /**
     * @brief A response to the db::query::RecordsSizeQuery records count query
     *
     */
    class RecordsSizeQueryResult : public db::QueryResult
    {
      private:
        std::size_t size = 0;

      public:
        /**
         * @brief Default constructor. Constructs an empty response (record count 0)
         */
        RecordsSizeQueryResult() noexcept = default;

        /**
         * @brief Constructs a reponse to the db::query::RecordsSizeQuery with a value
         *
         * @param count of records
         */
        RecordsSizeQueryResult(std::size_t size) noexcept;

        /**
         * @brief Gets number of records
         *
         * @return number of records
         */
        [[nodiscard]] std::size_t getSize() const noexcept;

        /**
         * @brief debug info
         *
         * @return class name
         */
        [[nodiscard]] auto debugInfo() const -> std::string override;
    };

}; // namespace db::query