comparison OrthancServer/Database/SQLiteDatabaseWrapper.h @ 3160:fc9a4a2dad63

merge
author Alain Mazy <alain@mazy.be>
date Thu, 24 Jan 2019 10:55:19 +0100
parents c0d7aee8c3f8
children 4bbadcd03966
comparison
equal deleted inserted replaced
3159:4cfed5c2eacd 3160:fc9a4a2dad63
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #pragma once
35
36 #include "IDatabaseWrapper.h"
37
38 #include "../../Core/SQLite/Connection.h"
39 #include "Compatibility/ICreateInstance.h"
40 #include "Compatibility/IGetChildrenMetadata.h"
41 #include "Compatibility/ISetResourcesContent.h"
42
43 namespace Orthanc
44 {
45 namespace Internals
46 {
47 class SignalRemainingAncestor;
48 }
49
50 /**
51 * This class manages an instance of the Orthanc SQLite database. It
52 * translates low-level requests into SQL statements. Mutual
53 * exclusion MUST be implemented at a higher level.
54 **/
55 class SQLiteDatabaseWrapper :
56 public IDatabaseWrapper,
57 public Compatibility::ICreateInstance,
58 public Compatibility::IGetChildrenMetadata,
59 public Compatibility::ISetResourcesContent
60 {
61 private:
62 class Transaction;
63 class LookupFormatter;
64
65 IDatabaseListener* listener_;
66 SQLite::Connection db_;
67 Internals::SignalRemainingAncestor* signalRemainingAncestor_;
68 unsigned int version_;
69
70 void GetChangesInternal(std::list<ServerIndexChange>& target,
71 bool& done,
72 SQLite::Statement& s,
73 uint32_t maxResults);
74
75 void GetExportedResourcesInternal(std::list<ExportedResource>& target,
76 bool& done,
77 SQLite::Statement& s,
78 uint32_t maxResults);
79
80 void ClearTable(const std::string& tableName);
81
82 // Unused => could be removed
83 int GetGlobalIntegerProperty(GlobalProperty property,
84 int defaultValue);
85
86 public:
87 SQLiteDatabaseWrapper(const std::string& path);
88
89 SQLiteDatabaseWrapper();
90
91 virtual void Open()
92 ORTHANC_OVERRIDE;
93
94 virtual void Close()
95 ORTHANC_OVERRIDE
96 {
97 db_.Close();
98 }
99
100 virtual void SetListener(IDatabaseListener& listener)
101 ORTHANC_OVERRIDE;
102
103 virtual bool LookupParent(int64_t& parentId,
104 int64_t resourceId)
105 ORTHANC_OVERRIDE;
106
107 virtual std::string GetPublicId(int64_t resourceId)
108 ORTHANC_OVERRIDE;
109
110 virtual ResourceType GetResourceType(int64_t resourceId)
111 ORTHANC_OVERRIDE;
112
113 virtual void DeleteResource(int64_t id)
114 ORTHANC_OVERRIDE;
115
116 virtual void GetChanges(std::list<ServerIndexChange>& target /*out*/,
117 bool& done /*out*/,
118 int64_t since,
119 uint32_t maxResults)
120 ORTHANC_OVERRIDE;
121
122 virtual void GetLastChange(std::list<ServerIndexChange>& target /*out*/)
123 ORTHANC_OVERRIDE;
124
125 virtual IDatabaseWrapper::ITransaction* StartTransaction()
126 ORTHANC_OVERRIDE;
127
128 virtual void FlushToDisk()
129 ORTHANC_OVERRIDE
130 {
131 db_.FlushToDisk();
132 }
133
134 virtual bool HasFlushToDisk() const
135 ORTHANC_OVERRIDE
136 {
137 return true;
138 }
139
140 virtual void ClearChanges()
141 ORTHANC_OVERRIDE
142 {
143 ClearTable("Changes");
144 }
145
146 virtual void ClearExportedResources()
147 ORTHANC_OVERRIDE
148 {
149 ClearTable("ExportedResources");
150 }
151
152 virtual void GetAllMetadata(std::map<MetadataType, std::string>& target,
153 int64_t id)
154 ORTHANC_OVERRIDE;
155
156 virtual unsigned int GetDatabaseVersion()
157 ORTHANC_OVERRIDE
158 {
159 return version_;
160 }
161
162 virtual void Upgrade(unsigned int targetVersion,
163 IStorageArea& storageArea)
164 ORTHANC_OVERRIDE;
165
166
167 /**
168 * The methods declared below are for unit testing only!
169 **/
170
171 const char* GetErrorMessage() const
172 {
173 return db_.GetErrorMessage();
174 }
175
176 void GetChildren(std::list<std::string>& childrenPublicIds,
177 int64_t id);
178
179 int64_t GetTableRecordCount(const std::string& table);
180
181 bool GetParentPublicId(std::string& target,
182 int64_t id);
183
184
185
186 /**
187 * Until Orthanc 1.4.0, the methods below were part of the
188 * "DatabaseWrapperBase" class, that is now placed in the
189 * graveyard.
190 **/
191
192 virtual void SetGlobalProperty(GlobalProperty property,
193 const std::string& value)
194 ORTHANC_OVERRIDE;
195
196 virtual bool LookupGlobalProperty(std::string& target,
197 GlobalProperty property)
198 ORTHANC_OVERRIDE;
199
200 virtual int64_t CreateResource(const std::string& publicId,
201 ResourceType type)
202 ORTHANC_OVERRIDE;
203
204 virtual bool LookupResource(int64_t& id,
205 ResourceType& type,
206 const std::string& publicId)
207 ORTHANC_OVERRIDE;
208
209 virtual void AttachChild(int64_t parent,
210 int64_t child)
211 ORTHANC_OVERRIDE;
212
213 virtual void SetMetadata(int64_t id,
214 MetadataType type,
215 const std::string& value)
216 ORTHANC_OVERRIDE;
217
218 virtual void DeleteMetadata(int64_t id,
219 MetadataType type)
220 ORTHANC_OVERRIDE;
221
222 virtual bool LookupMetadata(std::string& target,
223 int64_t id,
224 MetadataType type)
225 ORTHANC_OVERRIDE;
226
227 virtual void ListAvailableMetadata(std::list<MetadataType>& target,
228 int64_t id)
229 ORTHANC_OVERRIDE;
230
231 virtual void AddAttachment(int64_t id,
232 const FileInfo& attachment)
233 ORTHANC_OVERRIDE;
234
235 virtual void DeleteAttachment(int64_t id,
236 FileContentType attachment)
237 ORTHANC_OVERRIDE;
238
239 virtual void ListAvailableAttachments(std::list<FileContentType>& target,
240 int64_t id)
241 ORTHANC_OVERRIDE;
242
243 virtual bool LookupAttachment(FileInfo& attachment,
244 int64_t id,
245 FileContentType contentType)
246 ORTHANC_OVERRIDE;
247
248 virtual void ClearMainDicomTags(int64_t id)
249 ORTHANC_OVERRIDE;
250
251 virtual void SetMainDicomTag(int64_t id,
252 const DicomTag& tag,
253 const std::string& value)
254 ORTHANC_OVERRIDE;
255
256 virtual void SetIdentifierTag(int64_t id,
257 const DicomTag& tag,
258 const std::string& value)
259 ORTHANC_OVERRIDE;
260
261 virtual void GetMainDicomTags(DicomMap& map,
262 int64_t id)
263 ORTHANC_OVERRIDE;
264
265 virtual void GetChildrenPublicId(std::list<std::string>& target,
266 int64_t id)
267 ORTHANC_OVERRIDE;
268
269 virtual void GetChildrenInternalId(std::list<int64_t>& target,
270 int64_t id)
271 ORTHANC_OVERRIDE;
272
273 virtual void LogChange(int64_t internalId,
274 const ServerIndexChange& change)
275 ORTHANC_OVERRIDE;
276
277 virtual void LogExportedResource(const ExportedResource& resource)
278 ORTHANC_OVERRIDE;
279
280 virtual void GetExportedResources(std::list<ExportedResource>& target /*out*/,
281 bool& done /*out*/,
282 int64_t since,
283 uint32_t maxResults)
284 ORTHANC_OVERRIDE;
285
286 virtual void GetLastExportedResource(std::list<ExportedResource>& target /*out*/)
287 ORTHANC_OVERRIDE;
288
289 virtual uint64_t GetTotalCompressedSize()
290 ORTHANC_OVERRIDE;
291
292 virtual uint64_t GetTotalUncompressedSize()
293 ORTHANC_OVERRIDE;
294
295 virtual uint64_t GetResourceCount(ResourceType resourceType)
296 ORTHANC_OVERRIDE;
297
298 virtual void GetAllPublicIds(std::list<std::string>& target,
299 ResourceType resourceType)
300 ORTHANC_OVERRIDE;
301
302 virtual void GetAllPublicIds(std::list<std::string>& target,
303 ResourceType resourceType,
304 size_t since,
305 size_t limit)
306 ORTHANC_OVERRIDE;
307
308 virtual bool SelectPatientToRecycle(int64_t& internalId)
309 ORTHANC_OVERRIDE;
310
311 virtual bool SelectPatientToRecycle(int64_t& internalId,
312 int64_t patientIdToAvoid)
313 ORTHANC_OVERRIDE;
314
315 virtual bool IsProtectedPatient(int64_t internalId)
316 ORTHANC_OVERRIDE;
317
318 virtual void SetProtectedPatient(int64_t internalId,
319 bool isProtected)
320 ORTHANC_OVERRIDE;
321
322 virtual bool IsExistingResource(int64_t internalId)
323 ORTHANC_OVERRIDE;
324
325 virtual bool IsDiskSizeAbove(uint64_t threshold)
326 ORTHANC_OVERRIDE;
327
328 virtual void ApplyLookupResources(std::list<std::string>& resourcesId,
329 std::list<std::string>* instancesId,
330 const std::vector<DatabaseConstraint>& lookup,
331 ResourceType queryLevel,
332 size_t limit)
333 ORTHANC_OVERRIDE;
334
335 virtual bool CreateInstance(CreateInstanceResult& result,
336 int64_t& instanceId,
337 const std::string& patient,
338 const std::string& study,
339 const std::string& series,
340 const std::string& instance)
341 ORTHANC_OVERRIDE
342 {
343 return ICreateInstance::Apply
344 (*this, result, instanceId, patient, study, series, instance);
345 }
346
347 virtual void SetResourcesContent(const Orthanc::ResourcesContent& content)
348 ORTHANC_OVERRIDE
349 {
350 ISetResourcesContent::Apply(*this, content);
351 }
352
353 virtual void GetChildrenMetadata(std::list<std::string>& target,
354 int64_t resourceId,
355 MetadataType metadata)
356 ORTHANC_OVERRIDE
357 {
358 IGetChildrenMetadata::Apply(*this, target, resourceId, metadata);
359 }
360
361 virtual int64_t GetLastChangeIndex() ORTHANC_OVERRIDE;
362
363 virtual void TagMostRecentPatient(int64_t patient) ORTHANC_OVERRIDE;
364 };
365 }