MQTTSuite
Loading...
Searching...
No Matches
MariaDbStorage.h
Go to the documentation of this file.
1/*
2 * MQTTSuite - A lightweight MQTT Integration System
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2022, 2023, 2024, 2025, 2026
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 */
11
12#ifndef MQTTSTORE_LIB_MARIADBSTORAGE_H
13#define MQTTSTORE_LIB_MARIADBSTORAGE_H
14
15#include "StoragePlan.h"
16
17#include <database/mariadb/MariaDBClient.h>
18
19namespace mqtt::mqttstore::lib {
20 struct MqttMessage;
21}
22
23#ifndef DOXYGEN_SHOULD_SKIP_THIS
24
25#include <cstdint>
26#include <nlohmann/json_fwd.hpp>
27#include <optional>
28#include <string>
29
30#endif
31
32namespace mqtt::mqttstore::lib {
33
35 public:
37 std::string database;
38 std::string username;
39 std::string password;
40 std::string host;
41 std::uint16_t port = 3306;
42 std::string socket;
43 std::uint32_t flags = 0;
44 };
45
46 MariaDbStorage(const std::string& connectionName,
47 const ConnectionConfig& connectionConfig,
48 std::string rawTable,
49 bool autoCreateRawTable,
50 StoragePlan storagePlan);
51
52 void store(const MqttMessage& message);
53
54 private:
55 [[nodiscard]] static bool isSafeIdentifier(const std::string& identifier);
56 [[nodiscard]] static std::string quoteIdentifier(const std::string& identifier);
57 [[nodiscard]] static std::string sqlQuote(const std::string& value);
58 [[nodiscard]] static std::string sqlValue(const nlohmann::json& value);
59 [[nodiscard]] static std::optional<nlohmann::json> parsePayload(const std::string& payload);
60 [[nodiscard]] static bool hasBinaryContent(const std::string& payload);
61 [[nodiscard]] static std::string
62 buildRawInsertSql(const std::string& rawTable, const MqttMessage& message, const std::optional<nlohmann::json>& payloadJson);
63 [[nodiscard]] static std::string
64 buildProjectionInsertSql(const StoragePlan::Projection& projection, const MqttMessage& message, const nlohmann::json& payloadJson);
65 [[nodiscard]] static std::string
66 jsonValueForColumn(const StoragePlan::ColumnMapping& mapping, const MqttMessage& message, const nlohmann::json& payloadJson);
67 static void execLogFailure(const std::string& connectionName,
68 const std::string& operation,
69 const std::string& errorString,
70 unsigned int errorNumber);
71 void createRawTable();
72 void storeProjections(const MqttMessage& message, const std::optional<nlohmann::json>& payloadJson);
73
74 std::string connectionName;
75 database::mariadb::MariaDBClient mariaDB;
76 std::string rawTable;
78 };
79
80} // namespace mqtt::mqttstore::lib
81
82#endif // MQTTSTORE_LIB_MARIADBSTORAGE_H
static std::string quoteIdentifier(const std::string &identifier)
static std::string buildRawInsertSql(const std::string &rawTable, const MqttMessage &message, const std::optional< nlohmann::json > &payloadJson)
static std::string jsonValueForColumn(const StoragePlan::ColumnMapping &mapping, const MqttMessage &message, const nlohmann::json &payloadJson)
static std::string sqlValue(const nlohmann::json &value)
static bool hasBinaryContent(const std::string &payload)
MariaDbStorage(const std::string &connectionName, const ConnectionConfig &connectionConfig, std::string rawTable, bool autoCreateRawTable, StoragePlan storagePlan)
static bool isSafeIdentifier(const std::string &identifier)
void storeProjections(const MqttMessage &message, const std::optional< nlohmann::json > &payloadJson)
static std::string buildProjectionInsertSql(const StoragePlan::Projection &projection, const MqttMessage &message, const nlohmann::json &payloadJson)
database::mariadb::MariaDBClient mariaDB
static std::optional< nlohmann::json > parsePayload(const std::string &payload)
void store(const MqttMessage &message)
static void execLogFailure(const std::string &connectionName, const std::string &operation, const std::string &errorString, unsigned int errorNumber)
static std::string sqlQuote(const std::string &value)
void onConnected() final
Definition Mqtt.cpp:74
const std::string willTopic
Definition Mqtt.h:68
Mqtt(const std::string &connectionName, const std::string &clientId, std::uint8_t qoSDefault, std::uint16_t keepAlive, bool cleanSession, const std::string &willTopic, const std::string &willMessage, std::uint8_t willQoS, bool willRetain, const std::string &username, const std::string &password, const std::list< std::string > &subTopics, MariaDbStorage::ConnectionConfig connectionConfig, const std::string &rawTable, bool autoCreateRawTable, StoragePlan storagePlan, const std::string &sessionStoreFileName="")
Definition Mqtt.cpp:37
const std::uint8_t qoSDefault
Definition Mqtt.h:66
MariaDbStorage storage
Definition Mqtt.h:65
void onConnack(const iot::mqtt::packets::Connack &connack) final
Definition Mqtt.cpp:97
const bool cleanSession
Definition Mqtt.h:67
const bool willRetain
Definition Mqtt.h:71
const std::string willMessage
Definition Mqtt.h:69
iot::mqtt::client::Mqtt Super
Definition Mqtt.h:54
const std::uint8_t willQoS
Definition Mqtt.h:70
bool onSignal(int signum) final
Definition Mqtt.cpp:79
const std::list< std::string > subTopics
Definition Mqtt.h:74
void onPublish(const iot::mqtt::packets::Publish &publish) final
Definition Mqtt.cpp:136
const std::string username
Definition Mqtt.h:72
const std::string password
Definition Mqtt.h:73
static std::uint8_t getQos(const std::string &qoSString)
Definition Mqtt.cpp:87
void onSuback(const iot::mqtt::packets::Suback &suback) final
Definition Mqtt.cpp:150