comparison OrthancServer/Sources/Database/SQLiteDatabaseWrapper.h @ 4591:ff8170d17d90 db-changes

moving all accesses to databases from IDatabaseWrapper to ITransaction
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 15 Mar 2021 15:30:42 +0100
parents bec74e29f86b
children d494b4f1103e
comparison
equal deleted inserted replaced
4590:4a0bf1019335 4591:ff8170d17d90
34 #pragma once 34 #pragma once
35 35
36 #include "IDatabaseWrapper.h" 36 #include "IDatabaseWrapper.h"
37 37
38 #include "../../../OrthancFramework/Sources/SQLite/Connection.h" 38 #include "../../../OrthancFramework/Sources/SQLite/Connection.h"
39 #include "Compatibility/ICreateInstance.h"
40 #include "Compatibility/IGetChildrenMetadata.h"
41 #include "Compatibility/ILookupResourceAndParent.h"
42 #include "Compatibility/ISetResourcesContent.h"
43 39
44 namespace Orthanc 40 namespace Orthanc
45 { 41 {
46 /** 42 /**
47 * This class manages an instance of the Orthanc SQLite database. It 43 * This class manages an instance of the Orthanc SQLite database. It
48 * translates low-level requests into SQL statements. Mutual 44 * translates low-level requests into SQL statements. Mutual
49 * exclusion MUST be implemented at a higher level. 45 * exclusion MUST be implemented at a higher level.
50 **/ 46 **/
51 class SQLiteDatabaseWrapper : 47 class SQLiteDatabaseWrapper : public IDatabaseWrapper
52 public IDatabaseWrapper,
53 public Compatibility::ICreateInstance,
54 public Compatibility::IGetChildrenMetadata,
55 public Compatibility::ILookupResourceAndParent,
56 public Compatibility::ISetResourcesContent
57 { 48 {
58 private: 49 private:
50 class TransactionBase;
59 class SignalFileDeleted; 51 class SignalFileDeleted;
60 class SignalResourceDeleted; 52 class SignalResourceDeleted;
61 class SignalRemainingAncestor; 53 class SignalRemainingAncestor;
62 class ReadOnlyTransaction; 54 class ReadOnlyTransaction;
63 class ReadWriteTransaction; 55 class ReadWriteTransaction;
64 class LookupFormatter; 56 class LookupFormatter;
65 57
66 IDatabaseListener* listener_;
67 SQLite::Connection db_; 58 SQLite::Connection db_;
59 TransactionBase* activeTransaction_;
68 SignalRemainingAncestor* signalRemainingAncestor_; 60 SignalRemainingAncestor* signalRemainingAncestor_;
69 unsigned int version_; 61 unsigned int version_;
70 62
71 void GetChangesInternal(std::list<ServerIndexChange>& target, 63 void GetChangesInternal(std::list<ServerIndexChange>& target,
72 bool& done, 64 bool& done,
76 void GetExportedResourcesInternal(std::list<ExportedResource>& target, 68 void GetExportedResourcesInternal(std::list<ExportedResource>& target,
77 bool& done, 69 bool& done,
78 SQLite::Statement& s, 70 SQLite::Statement& s,
79 uint32_t maxResults); 71 uint32_t maxResults);
80 72
81 void ClearTable(const std::string& tableName);
82
83 // Unused => could be removed
84 int GetGlobalIntegerProperty(GlobalProperty property,
85 int defaultValue);
86
87 public: 73 public:
88 SQLiteDatabaseWrapper(const std::string& path); 74 SQLiteDatabaseWrapper(const std::string& path);
89 75
90 SQLiteDatabaseWrapper(); 76 SQLiteDatabaseWrapper();
91 77
92 virtual void Open() 78 virtual ~SQLiteDatabaseWrapper();
93 ORTHANC_OVERRIDE;
94 79
95 virtual void Close() 80 virtual void Open() ORTHANC_OVERRIDE;
96 ORTHANC_OVERRIDE 81
82 virtual void Close() ORTHANC_OVERRIDE
97 { 83 {
98 db_.Close(); 84 db_.Close();
99 } 85 }
100
101 virtual bool LookupParent(int64_t& parentId,
102 int64_t resourceId)
103 ORTHANC_OVERRIDE;
104
105 virtual std::string GetPublicId(int64_t resourceId)
106 ORTHANC_OVERRIDE;
107
108 virtual ResourceType GetResourceType(int64_t resourceId)
109 ORTHANC_OVERRIDE;
110
111 virtual void DeleteResource(int64_t id)
112 ORTHANC_OVERRIDE;
113
114 virtual void GetChanges(std::list<ServerIndexChange>& target /*out*/,
115 bool& done /*out*/,
116 int64_t since,
117 uint32_t maxResults)
118 ORTHANC_OVERRIDE;
119
120 virtual void GetLastChange(std::list<ServerIndexChange>& target /*out*/)
121 ORTHANC_OVERRIDE;
122 86
123 virtual IDatabaseWrapper::ITransaction* StartTransaction(TransactionType type, 87 virtual IDatabaseWrapper::ITransaction* StartTransaction(TransactionType type,
124 IDatabaseListener& listener) 88 IDatabaseListener& listener)
125 ORTHANC_OVERRIDE; 89 ORTHANC_OVERRIDE;
126 90
127 virtual void FlushToDisk() 91 virtual void FlushToDisk() ORTHANC_OVERRIDE
128 ORTHANC_OVERRIDE
129 { 92 {
130 db_.FlushToDisk(); 93 db_.FlushToDisk();
131 } 94 }
132 95
133 virtual bool HasFlushToDisk() const 96 virtual bool HasFlushToDisk() const ORTHANC_OVERRIDE
134 ORTHANC_OVERRIDE
135 { 97 {
136 return true; 98 return true;
137 } 99 }
138 100
139 virtual void ClearChanges() 101 virtual unsigned int GetDatabaseVersion() ORTHANC_OVERRIDE
140 ORTHANC_OVERRIDE
141 {
142 ClearTable("Changes");
143 }
144
145 virtual void ClearExportedResources()
146 ORTHANC_OVERRIDE
147 {
148 ClearTable("ExportedResources");
149 }
150
151 virtual void GetAllMetadata(std::map<MetadataType, std::string>& target,
152 int64_t id)
153 ORTHANC_OVERRIDE;
154
155 virtual unsigned int GetDatabaseVersion()
156 ORTHANC_OVERRIDE
157 { 102 {
158 return version_; 103 return version_;
159 } 104 }
160 105
161 virtual void Upgrade(unsigned int targetVersion, 106 virtual void Upgrade(unsigned int targetVersion,
162 IStorageArea& storageArea) 107 IStorageArea& storageArea) ORTHANC_OVERRIDE;
163 ORTHANC_OVERRIDE;
164
165
166 /**
167 * The methods declared below are for unit testing only!
168 **/
169
170 const char* GetErrorMessage() const
171 {
172 return db_.GetErrorMessage();
173 }
174
175 void GetChildren(std::list<std::string>& childrenPublicIds,
176 int64_t id);
177
178 int64_t GetTableRecordCount(const std::string& table);
179
180 bool GetParentPublicId(std::string& target,
181 int64_t id);
182 108
183 109
184 110
185 /** 111 /**
186 * Until Orthanc 1.4.0, the methods below were part of the 112 * The "StartTransaction()" method is guaranteed to return a class
187 * "DatabaseWrapperBase" class, that is now placed in the 113 * derived from "UnitTestsTransaction". The methods of
188 * graveyard. 114 * "UnitTestsTransaction" give access to additional information
115 * about the underlying SQLite database to be used in unit tests.
189 **/ 116 **/
117 class UnitTestsTransaction : public ITransaction
118 {
119 protected:
120 SQLite::Connection& db_;
121
122 public:
123 UnitTestsTransaction(SQLite::Connection& db) :
124 db_(db)
125 {
126 }
127
128 void GetChildren(std::list<std::string>& childrenPublicIds,
129 int64_t id);
190 130
191 virtual void SetGlobalProperty(GlobalProperty property, 131 int64_t GetTableRecordCount(const std::string& table);
192 const std::string& value) 132
193 ORTHANC_OVERRIDE; 133 bool GetParentPublicId(std::string& target,
134 int64_t id);
194 135
195 virtual bool LookupGlobalProperty(std::string& target, 136 int64_t CreateResource(const std::string& publicId,
196 GlobalProperty property) 137 ResourceType type);
197 ORTHANC_OVERRIDE;
198 138
199 virtual int64_t CreateResource(const std::string& publicId, 139 void AttachChild(int64_t parent,
200 ResourceType type) 140 int64_t child);
201 ORTHANC_OVERRIDE;
202 141
203 virtual bool LookupResource(int64_t& id, 142 void SetIdentifierTag(int64_t id,
204 ResourceType& type, 143 const DicomTag& tag,
205 const std::string& publicId) 144 const std::string& value);
206 ORTHANC_OVERRIDE;
207 145
208 virtual void AttachChild(int64_t parent, 146 void SetMainDicomTag(int64_t id,
209 int64_t child) 147 const DicomTag& tag,
210 ORTHANC_OVERRIDE; 148 const std::string& value);
211 149 };
212 virtual void SetMetadata(int64_t id,
213 MetadataType type,
214 const std::string& value)
215 ORTHANC_OVERRIDE;
216
217 virtual void DeleteMetadata(int64_t id,
218 MetadataType type)
219 ORTHANC_OVERRIDE;
220
221 virtual bool LookupMetadata(std::string& target,
222 int64_t id,
223 MetadataType type)
224 ORTHANC_OVERRIDE;
225
226 virtual void AddAttachment(int64_t id,
227 const FileInfo& attachment)
228 ORTHANC_OVERRIDE;
229
230 virtual void DeleteAttachment(int64_t id,
231 FileContentType attachment)
232 ORTHANC_OVERRIDE;
233
234 virtual void ListAvailableAttachments(std::set<FileContentType>& target,
235 int64_t id)
236 ORTHANC_OVERRIDE;
237
238 virtual bool LookupAttachment(FileInfo& attachment,
239 int64_t id,
240 FileContentType contentType)
241 ORTHANC_OVERRIDE;
242
243 virtual void ClearMainDicomTags(int64_t id)
244 ORTHANC_OVERRIDE;
245
246 virtual void SetMainDicomTag(int64_t id,
247 const DicomTag& tag,
248 const std::string& value)
249 ORTHANC_OVERRIDE;
250
251 virtual void SetIdentifierTag(int64_t id,
252 const DicomTag& tag,
253 const std::string& value)
254 ORTHANC_OVERRIDE;
255
256 virtual void GetMainDicomTags(DicomMap& map,
257 int64_t id)
258 ORTHANC_OVERRIDE;
259
260 virtual void GetChildrenPublicId(std::list<std::string>& target,
261 int64_t id)
262 ORTHANC_OVERRIDE;
263
264 virtual void GetChildrenInternalId(std::list<int64_t>& target,
265 int64_t id)
266 ORTHANC_OVERRIDE;
267
268 virtual void LogChange(int64_t internalId,
269 const ServerIndexChange& change)
270 ORTHANC_OVERRIDE;
271
272 virtual void LogExportedResource(const ExportedResource& resource)
273 ORTHANC_OVERRIDE;
274
275 virtual void GetExportedResources(std::list<ExportedResource>& target /*out*/,
276 bool& done /*out*/,
277 int64_t since,
278 uint32_t maxResults)
279 ORTHANC_OVERRIDE;
280
281 virtual void GetLastExportedResource(std::list<ExportedResource>& target /*out*/)
282 ORTHANC_OVERRIDE;
283
284 virtual uint64_t GetTotalCompressedSize()
285 ORTHANC_OVERRIDE;
286
287 virtual uint64_t GetTotalUncompressedSize()
288 ORTHANC_OVERRIDE;
289
290 virtual uint64_t GetResourceCount(ResourceType resourceType)
291 ORTHANC_OVERRIDE;
292
293 virtual void GetAllPublicIds(std::list<std::string>& target,
294 ResourceType resourceType)
295 ORTHANC_OVERRIDE;
296
297 virtual void GetAllPublicIds(std::list<std::string>& target,
298 ResourceType resourceType,
299 size_t since,
300 size_t limit)
301 ORTHANC_OVERRIDE;
302
303 virtual bool SelectPatientToRecycle(int64_t& internalId)
304 ORTHANC_OVERRIDE;
305
306 virtual bool SelectPatientToRecycle(int64_t& internalId,
307 int64_t patientIdToAvoid)
308 ORTHANC_OVERRIDE;
309
310 virtual bool IsProtectedPatient(int64_t internalId)
311 ORTHANC_OVERRIDE;
312
313 virtual void SetProtectedPatient(int64_t internalId,
314 bool isProtected)
315 ORTHANC_OVERRIDE;
316
317 virtual bool IsExistingResource(int64_t internalId)
318 ORTHANC_OVERRIDE;
319
320 virtual bool IsDiskSizeAbove(uint64_t threshold)
321 ORTHANC_OVERRIDE;
322
323 virtual void ApplyLookupResources(std::list<std::string>& resourcesId,
324 std::list<std::string>* instancesId,
325 const std::vector<DatabaseConstraint>& lookup,
326 ResourceType queryLevel,
327 size_t limit)
328 ORTHANC_OVERRIDE;
329
330 virtual bool CreateInstance(CreateInstanceResult& result,
331 int64_t& instanceId,
332 const std::string& patient,
333 const std::string& study,
334 const std::string& series,
335 const std::string& instance)
336 ORTHANC_OVERRIDE
337 {
338 return ICreateInstance::Apply
339 (*this, result, instanceId, patient, study, series, instance);
340 }
341
342 virtual void SetResourcesContent(const Orthanc::ResourcesContent& content)
343 ORTHANC_OVERRIDE
344 {
345 ISetResourcesContent::Apply(*this, content);
346 }
347
348 virtual void GetChildrenMetadata(std::list<std::string>& target,
349 int64_t resourceId,
350 MetadataType metadata)
351 ORTHANC_OVERRIDE
352 {
353 IGetChildrenMetadata::Apply(*this, target, resourceId, metadata);
354 }
355
356 virtual int64_t GetLastChangeIndex() ORTHANC_OVERRIDE;
357
358 virtual void TagMostRecentPatient(int64_t patient) ORTHANC_OVERRIDE;
359
360 virtual bool LookupResourceAndParent(int64_t& id,
361 ResourceType& type,
362 std::string& parentPublicId,
363 const std::string& publicId)
364 ORTHANC_OVERRIDE
365 {
366 return ILookupResourceAndParent::Apply(*this, id, type, parentPublicId, publicId);
367 }
368 }; 150 };
369 } 151 }