lacteApp
C++17 service for Lacte hardware
Loading...
Searching...
No Matches
AppDb.hpp
Go to the documentation of this file.
1#pragma once
2#include <sqlite.hpp>
3
11struct Stat {
12 std::string m_id; // первичный ключ
13 std::string m_date;
14 int m_status{};
15 std::string m_n_RFID;
17};
18
20 std::string m_id; // первичный ключ
21 std::string m_date;
22 std::string m_field;
23 std::string m_value;
25};
26
27struct Errors {
28 std::string m_id; // первичный ключ
29 std::string m_date;
30 int m_error{}; // underlying(BoardError)
31 bool m_value{}; // хранится как INTEGER 0/1
33};
34
35// Статическое словарное отображение: область ("error"/"status"), числовой
36// код, описание и версия набора данных. Намеренно БЕЗ первичного ключа -
37// много строк законно делят один и тот же m_area (разные m_code). Маркер
38// "NO KEY" ниже - это то, что gen_db_meta.py (см. struct_re) реально
39// парсит, чтобы эмитить K_NO_PRIMARY_KEY = true в сгенерированный блок;
40// без него первое поле (m_area) молча становится PRIMARY KEY - ровно то,
41// что уронило пересборку схемы на прод-плате
42// ("UNIQUE constraint failed: dictionary_backup74.area").
43// NO KEY
44struct Dictionary {
45 std::string m_area; // например, "error" или "status"
46 int m_code{}; // числовой код (например, значение enum)
47 std::string m_description; // текст, понятный человеку
48 std::string m_version; // версия набора данных, для обновления при запуске
49 auto operator==(const Dictionary& other) const -> bool {
50 return m_area == other.m_area && m_code == other.m_code &&
51 m_version == other.m_version && m_description == other.m_description;
52 }
53
54 auto operator!=(const Dictionary& other) const -> bool {
55 return !(*this == other);
56 }
57};
58} // namespace insitech::database::models
59
60// ===== GENERATED CODE (DO NOT EDIT) =====
62
63struct StatMeta {
65 static constexpr auto K_TABLE_NAME = "stat";
66 static constexpr auto fields() {
67 return std::make_tuple(&Object::m_id, &Object::m_date, &Object::m_status,
69 }
70 static constexpr std::array<const char*, 5> K_FIELD_NAMES{
71 "id", "date", "status", "n_RFID", "mqtt_status"};
72};
73
76 static constexpr auto K_TABLE_NAME = "service_info";
77 static constexpr auto fields() {
78 return std::make_tuple(&Object::m_id, &Object::m_date, &Object::m_field,
80 }
81 static constexpr std::array<const char*, 5> K_FIELD_NAMES{
82 "id", "date", "field", "value", "mqtt_status"};
83};
84
85struct ErrorsMeta {
87 static constexpr auto K_TABLE_NAME = "errors";
88 static constexpr auto fields() {
89 return std::make_tuple(&Object::m_id, &Object::m_date, &Object::m_error,
91 }
92 static constexpr std::array<const char*, 5> K_FIELD_NAMES{
93 "id", "date", "error", "value", "mqtt_status"};
94};
95
98 static constexpr auto K_TABLE_NAME = "dictionary";
99 static constexpr bool K_NO_PRIMARY_KEY = true;
100 static constexpr auto fields() {
101 return std::make_tuple(&Object::m_area, &Object::m_code,
103 }
104 static constexpr std::array<const char*, 4> K_FIELD_NAMES{
105 "area", "code", "description", "version"};
106};
107
108} // namespace insitech::database::meta
109
110// ===== DATABASE WRAPPER =====
111
112class AppDb : public insitech::database::ThreadSafeDB<
113 insitech::database::meta::StatMeta,
114 insitech::database::meta::ServiceInfoMeta,
115 insitech::database::meta::ErrorsMeta,
116 insitech::database::meta::DictionaryMeta> {
117 public:
118 using Base = insitech::database::ThreadSafeDB<
123 explicit AppDb(const std::string& db_path) : Base(db_path) {}
124
132
134 this->write(input);
135 }
136
138 this->write(input);
139 }
140
142 this->write(input);
143 }
144
146 this->write(input);
147 }
148};
149
150// ===== END GENERATED CODE =====
void add_stat(const insitech::database::models::Stat &input)
Definition AppDb.hpp:133
insitech::database::ThreadSafeDB< insitech::database::meta::StatMeta, insitech::database::meta::ServiceInfoMeta, insitech::database::meta::ErrorsMeta, insitech::database::meta::DictionaryMeta > Base
Definition AppDb.hpp:118
void add_dictionary(const insitech::database::models::Dictionary &input)
Definition AppDb.hpp:145
void add_errors(const insitech::database::models::Errors &input)
Definition AppDb.hpp:141
AppDb(const std::string &db_path)
Definition AppDb.hpp:123
void reset_data_keep_schema() const
Definition AppDb.hpp:125
void add_service_info(const insitech::database::models::ServiceInfo &input)
Definition AppDb.hpp:137
static constexpr auto K_TABLE_NAME
Definition AppDb.hpp:98
static constexpr std::array< const char *, 4 > K_FIELD_NAMES
Definition AppDb.hpp:104
static constexpr auto fields()
Definition AppDb.hpp:100
static constexpr bool K_NO_PRIMARY_KEY
Definition AppDb.hpp:99
static constexpr auto K_TABLE_NAME
Definition AppDb.hpp:87
static constexpr auto fields()
Definition AppDb.hpp:88
static constexpr std::array< const char *, 5 > K_FIELD_NAMES
Definition AppDb.hpp:92
static constexpr auto fields()
Definition AppDb.hpp:77
static constexpr std::array< const char *, 5 > K_FIELD_NAMES
Definition AppDb.hpp:81
static constexpr auto K_TABLE_NAME
Definition AppDb.hpp:76
static constexpr auto fields()
Definition AppDb.hpp:66
static constexpr std::array< const char *, 5 > K_FIELD_NAMES
Definition AppDb.hpp:70
static constexpr auto K_TABLE_NAME
Definition AppDb.hpp:65
auto operator!=(const Dictionary &other) const -> bool
Definition AppDb.hpp:54
auto operator==(const Dictionary &other) const -> bool
Definition AppDb.hpp:49