~aleteoryx/muditaos

ref: 6533aba24ef55cde97a89ef8040b9da21425d571 muditaos/module-db/Interface/SettingsRecord_v2.hpp -rw-r--r-- 2.1 KiB
6533aba2 — KacperLewandowski [EGD-4073] Alarms database rework (#1006) 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
// 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 "Databases/SettingsDB.hpp"
#include "Record.hpp"

#include <memory>
#include <sstream>
#include <string>

// fw declarations
namespace db::query::settings
{
    class SettingsQuery;
    class SettingsResult;
    class AddOrIgnoreQuery;
    class AddOrIgnoreResult;
    class UpdateQuery;
    class UpdateResult;
} // namespace db::query::settings

class SettingsRecord_v2 : public Record
{
  public:
    SettingsRecord_v2()          = default;
    virtual ~SettingsRecord_v2() = default;
    SettingsRecord_v2(const SettingsTableRow_v2 &tableRow);

    friend std::ostream &operator<<(std::ostream &out, const SettingsRecord_v2 &rec);

    [[nodiscard]] auto getPath() const noexcept -> std::string
    {
        return path;
    }

    template <typename T>[[nodiscard]] auto getValue(const T &defaultValue = {}) const -> T
    {
        if (value.empty()) {
            return defaultValue;
        }
        T ret;
        std::istringstream(value) >> ret;
        return ret;
    }

  private:
    std::string path{};
    std::string value{};
};

enum class SettingsRecordField_v2
{
    GroupID,
    Path,
    Value
};

class SettingsRecordInterface_v2 : public RecordInterface<SettingsRecord_v2, SettingsRecordField_v2>
{
  public:
    SettingsRecordInterface_v2(SettingsDB *settingsDB);
    virtual ~SettingsRecordInterface_v2() override = default;

    SettingsRecord_v2 GetByPath(const std::string &path);
    SettingsRecord_v2 GetByID(uint32_t id) override final;
    bool Add(const SettingsRecord_v2 &entry) override final;
    uint32_t GetCount() override final;
    bool RemoveByID(uint32_t id) override final;
    bool Update(const SettingsRecord_v2 &recUpdated) override final;
    bool Update(const std::string &path, const std::string &value) noexcept;
    std::unique_ptr<db::QueryResult> runQuery(std::shared_ptr<db::Query> query) override;

  private:
    SettingsDB *settingsDB = nullptr;

    std::unique_ptr<db::query::settings::SettingsResult> runQueryImpl(const db::query::settings::SettingsQuery *query);
};