| 1 |
/* |
| 2 |
RailControl - Model Railway Control Software |
| 3 |
|
| 4 |
Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 |
|
| 6 |
RailControl 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 |
| 8 |
Free Software Foundation; either version 3, or (at your option) any |
| 9 |
later version. |
| 10 |
|
| 11 |
RailControl is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
See the GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with RailControl; see the file LICENCE. If not see |
| 18 |
<http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include <map> |
| 25 |
|
| 26 |
#include "DataModel/DataModel.h" |
| 27 |
#include "DataTypes.h" |
| 28 |
#include "Hardware/HardwareParams.h" |
| 29 |
#include "Storage/Sqlite.h" |
| 30 |
#include "Storage/StorageInterface.h" |
| 31 |
#include "Storage/TransactionGuard.h" |
| 32 |
#include "Storage/StorageParams.h" |
| 33 |
|
| 34 |
namespace Storage |
| 35 |
{ |
| 36 |
class StorageHandler |
| 37 |
{ |
| 38 |
public: |
| 39 |
inline StorageHandler(Manager* manager, const StorageParams* params) |
| 40 |
: manager(manager), |
| 41 |
sqlite(params) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
inline ~StorageHandler() |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
inline void AllHardwareParams(std::map<ControlID,Hardware::HardwareParams*>& hardwareParams) |
| 50 |
{ |
| 51 |
sqlite.AllHardwareParams(hardwareParams); |
| 52 |
} |
| 53 |
|
| 54 |
inline void DeleteHardwareParams(const ControlID controlID) |
| 55 |
{ |
| 56 |
sqlite.DeleteHardwareParams(controlID); |
| 57 |
} |
| 58 |
|
| 59 |
void AllLocos(std::map<LocoID,DataModel::Loco*>& locos); |
| 60 |
void DeleteLoco(LocoID locoID); |
| 61 |
void AllMultipleUnits(std::map<LocoID,DataModel::MultipleUnit*>& multipleUnits); |
| 62 |
void DeleteMultipleUnit(MultipleUnitID multipleUnitID); |
| 63 |
void AllAccessories(std::map<AccessoryID,DataModel::Accessory*>& accessories); |
| 64 |
|
| 65 |
inline void DeleteAccessory(AccessoryID accessoryID) |
| 66 |
{ |
| 67 |
sqlite.DeleteObject(ObjectTypeAccessory, accessoryID); |
| 68 |
} |
| 69 |
|
| 70 |
void AllFeedbacks(std::map<FeedbackID,DataModel::Feedback*>& feedbacks); |
| 71 |
|
| 72 |
inline void DeleteFeedback(FeedbackID feedbackID) |
| 73 |
{ |
| 74 |
sqlite.DeleteObject(ObjectTypeFeedback, feedbackID); |
| 75 |
} |
| 76 |
|
| 77 |
void AllTracks(std::map<TrackID,DataModel::Track*>& tracks); |
| 78 |
void DeleteTrack(TrackID trackID); |
| 79 |
void AllSwitches(std::map<SwitchID,DataModel::Switch*>& switches); |
| 80 |
|
| 81 |
inline void DeleteSwitch(SwitchID switchID) |
| 82 |
{ |
| 83 |
sqlite.DeleteObject(ObjectTypeSwitch, switchID); |
| 84 |
} |
| 85 |
|
| 86 |
void AllRoutes(std::map<RouteID,DataModel::Route*>& routes); |
| 87 |
void DeleteRoute(RouteID routeID); |
| 88 |
void AllLayers(std::map<LayerID,DataModel::Layer*>& layers); |
| 89 |
|
| 90 |
inline void DeleteLayer(LayerID layerID) |
| 91 |
{ |
| 92 |
sqlite.DeleteObject(ObjectTypeLayer, layerID); |
| 93 |
} |
| 94 |
|
| 95 |
void AllSignals(std::map<SignalID,DataModel::Signal*>& signals); |
| 96 |
void DeleteSignal(SignalID signalID); |
| 97 |
void AllClusters(std::map<ClusterID,DataModel::Cluster*>& clusters); |
| 98 |
|
| 99 |
inline void DeleteCluster(ClusterID clusterID) |
| 100 |
{ |
| 101 |
sqlite.DeleteObject(ObjectTypeCluster, clusterID); |
| 102 |
} |
| 103 |
|
| 104 |
void AllTexts(std::map<TextID,DataModel::Text*>& texts); |
| 105 |
|
| 106 |
inline void DeleteText(TextID textID) |
| 107 |
{ |
| 108 |
sqlite.DeleteObject(ObjectTypeText, textID); |
| 109 |
} |
| 110 |
|
| 111 |
inline void Save(const Hardware::HardwareParams& hardwareParams) |
| 112 |
{ |
| 113 |
sqlite.SaveHardwareParams(hardwareParams); |
| 114 |
} |
| 115 |
|
| 116 |
void Save(const DataModel::Route& route); |
| 117 |
void Save(const DataModel::Loco& loco); |
| 118 |
void Save(const DataModel::MultipleUnit& multipleUnit); |
| 119 |
void Save(const DataModel::Cluster& cluster); |
| 120 |
void Save(const DataModel::Track& track); |
| 121 |
|
| 122 |
template<class T> void Save(const T& t) |
| 123 |
{ |
| 124 |
const std::string serialized = t.Serialize(); |
| 125 |
sqlite.SaveObject(t.GetObjectType(), t.GetID(), t.GetName(), serialized); |
| 126 |
} |
| 127 |
|
| 128 |
template <class T> static void Save(StorageHandler* storageHandler, const T* t) |
| 129 |
{ |
| 130 |
storageHandler->Save(*t); |
| 131 |
} |
| 132 |
|
| 133 |
inline void SaveSetting(const std::string& key, const std::string& value) |
| 134 |
{ |
| 135 |
sqlite.SaveSetting(key, value); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
inline std::string GetSetting(const std::string& key) |
| 140 |
{ |
| 141 |
return sqlite.GetSetting(key); |
| 142 |
} |
| 143 |
|
| 144 |
inline void StartTransaction() |
| 145 |
{ |
| 146 |
transactionMutex.lock(); |
| 147 |
sqlite.StartTransaction(); |
| 148 |
} |
| 149 |
|
| 150 |
inline void CommitTransaction() |
| 151 |
{ |
| 152 |
sqlite.CommitTransaction(); |
| 153 |
transactionMutex.unlock(); |
| 154 |
} |
| 155 |
|
| 156 |
private: |
| 157 |
void SaveRelations(const std::vector<DataModel::Relation*> relations); |
| 158 |
std::vector<DataModel::Relation*> RelationsFrom(const DataModel::Relation::RelationType type, const ObjectID objectID); |
| 159 |
|
| 160 |
Manager* manager; |
| 161 |
Storage::SQLite sqlite; |
| 162 |
std::mutex transactionMutex; |
| 163 |
}; |
| 164 |
} // namespace Storage |
| 165 |
|