// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "SettingsRecord_v2.hpp" #include "module-db/queries/settings/QuerySettingsGet_v2.hpp" #include "module-db/queries/settings/QuerySettingsUpdate_v2.hpp" #include "module-db/queries/settings/QuerySettingsAddOrIgnore_v2.hpp" #include #include SettingsRecord_v2::SettingsRecord_v2(const SettingsTableRow_v2 &tableRow) : Record{tableRow.ID}, path{tableRow.path}, value{tableRow.value} {} std::ostream &operator<<(std::ostream &out, const SettingsRecord_v2 &rec) { out << "settings:" << rec.ID << "|" << (rec.path) << "|" << rec.value << "|"; return out; } SettingsRecordInterface_v2::SettingsRecordInterface_v2(SettingsDB *settingsDB) : settingsDB{settingsDB} {} SettingsRecord_v2 SettingsRecordInterface_v2::GetByPath(const std::string &path) { return settingsDB->settings_v2.getByPath(path); } SettingsRecord_v2 SettingsRecordInterface_v2::GetByID(uint32_t id) { return settingsDB->settings_v2.getById(id); } uint32_t SettingsRecordInterface_v2::GetCount() { return settingsDB->settings_v2.count(); } bool SettingsRecordInterface_v2::Add(const SettingsRecord_v2 &entry) { return settingsDB->settings_v2.add({{.ID = 0}, .path = entry.getPath(), .value = entry.getValue()}); } bool SettingsRecordInterface_v2::RemoveByID(uint32_t id) { auto entry = settingsDB->settings_v2.getById(id); if (!entry.isValid()) { return false; } return settingsDB->settings_v2.removeById(id); } bool SettingsRecordInterface_v2::Update(const SettingsRecord_v2 &recUpdated) { auto recCurrent = GetByID(recUpdated.ID); if (!recCurrent.isValid()) { return false; } return settingsDB->settings_v2.update(SettingsTableRow_v2{ {.ID = recCurrent.ID}, .path = recUpdated.getPath(), .value = recUpdated.getValue()}); } bool SettingsRecordInterface_v2::Update(const std::string &path, const std::string &value) noexcept { auto recCurrent = GetByPath(path); if (!recCurrent.isValid()) { return false; } return settingsDB->settings_v2.update( SettingsTableRow_v2{{.ID = recCurrent.ID}, .path = recCurrent.getPath(), .value = value}); } std::unique_ptr SettingsRecordInterface_v2::runQuery(std::shared_ptr query) { if (auto settingsQuery = dynamic_cast(query.get())) { auto value = GetByPath(settingsQuery->getPath()); return std::make_unique(value); } if (auto updateQuery = dynamic_cast(query.get())) { auto updatedRec = updateQuery->getRecord(); auto value = Update(updatedRec.getPath(), updatedRec.getValue()); return std::make_unique(value); } if (auto addOrIgnoreQuery = dynamic_cast(query.get())) { auto record = addOrIgnoreQuery->getRecord(); auto value = Add(record); return std::make_unique(value); } return nullptr; } std::unique_ptr SettingsRecordInterface_v2::runQueryImpl( const db::query::settings::SettingsQuery *query) { auto value = GetByPath(query->getPath()); return std::make_unique(value); }