Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.h @ 2760:ab35523fced1 Orthanc-0.8.5
close old branch
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 17 Jul 2018 09:41:40 +0200 |
parents | 1169528a9a5f |
children | efece308018e |
rev | line source |
---|---|
183 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
183 | 4 * Belgium |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #pragma once | |
34 | |
35 #include "../Core/SQLite/Connection.h" | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
36 #include "../Core/SQLite/Transaction.h" |
183 | 37 #include "../Core/DicomFormat/DicomInstanceHasher.h" |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
38 #include "../Core/FileStorage/FileInfo.h" |
183 | 39 #include "IServerIndexListener.h" |
40 | |
41 #include <list> | |
42 #include <boost/date_time/posix_time/posix_time.hpp> | |
43 | |
44 namespace Orthanc | |
45 { | |
46 namespace Internals | |
47 { | |
48 class SignalRemainingAncestor; | |
49 } | |
50 | |
51 /** | |
52 * This class manages an instance of the Orthanc SQLite database. It | |
53 * translates low-level requests into SQL statements. Mutual | |
54 * exclusion MUST be implemented at a higher level. | |
55 **/ | |
56 class DatabaseWrapper | |
57 { | |
58 private: | |
59 IServerIndexListener& listener_; | |
60 SQLite::Connection db_; | |
61 Internals::SignalRemainingAncestor* signalRemainingAncestor_; | |
62 | |
63 void Open(); | |
64 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
65 void GetChangesInternal(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
66 SQLite::Statement& s, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
67 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
68 unsigned int maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
69 |
742 | 70 void GetExportedResourcesInternal(Json::Value& target, |
71 SQLite::Statement& s, | |
72 int64_t since, | |
73 unsigned int maxResults); | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
74 |
183 | 75 public: |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
76 void SetGlobalProperty(GlobalProperty property, |
183 | 77 const std::string& value); |
78 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
79 bool LookupGlobalProperty(std::string& target, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
80 GlobalProperty property); |
183 | 81 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
82 std::string GetGlobalProperty(GlobalProperty property, |
183 | 83 const std::string& defaultValue = ""); |
84 | |
85 int64_t CreateResource(const std::string& publicId, | |
86 ResourceType type); | |
87 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
88 bool LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
89 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
90 ResourceType& type); |
183 | 91 |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
92 bool LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
93 int64_t resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
94 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
95 std::string GetPublicId(int64_t resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
96 |
304 | 97 ResourceType GetResourceType(int64_t resourceId); |
98 | |
183 | 99 void AttachChild(int64_t parent, |
100 int64_t child); | |
101 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
102 void GetChildren(Json::Value& childrenPublicIds, |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
103 int64_t id); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
104 |
183 | 105 void DeleteResource(int64_t id); |
106 | |
107 void SetMetadata(int64_t id, | |
108 MetadataType type, | |
109 const std::string& value); | |
110 | |
438 | 111 void DeleteMetadata(int64_t id, |
112 MetadataType type); | |
113 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
114 bool LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
115 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
116 MetadataType type); |
183 | 117 |
739 | 118 void ListAvailableMetadata(std::list<MetadataType>& target, |
436
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
119 int64_t id); |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
120 |
183 | 121 std::string GetMetadata(int64_t id, |
122 MetadataType type, | |
123 const std::string& defaultValue = ""); | |
124 | |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
125 bool GetMetadataAsInteger(int& result, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
126 int64_t id, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
127 MetadataType type); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
128 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
129 void AddAttachment(int64_t id, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
130 const FileInfo& attachment); |
183 | 131 |
699
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
132 void DeleteAttachment(int64_t id, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
133 FileContentType attachment); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
134 |
440
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
135 void ListAvailableAttachments(std::list<FileContentType>& result, |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
136 int64_t id); |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
137 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
138 bool LookupAttachment(FileInfo& attachment, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
139 int64_t id, |
233 | 140 FileContentType contentType); |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
141 |
183 | 142 void SetMainDicomTags(int64_t id, |
143 const DicomMap& tags); | |
144 | |
145 void GetMainDicomTags(DicomMap& map, | |
146 int64_t id); | |
147 | |
148 bool GetParentPublicId(std::string& result, | |
149 int64_t id); | |
150 | |
151 void GetChildrenPublicId(std::list<std::string>& result, | |
152 int64_t id); | |
153 | |
199 | 154 void GetChildrenInternalId(std::list<int64_t>& result, |
155 int64_t id); | |
156 | |
1198 | 157 void LogChange(int64_t internalId, |
158 ChangeType changeType, | |
183 | 159 ResourceType resourceType, |
1198 | 160 const std::string& publicId) |
161 { | |
162 ServerIndexChange change(changeType, resourceType, publicId); | |
163 LogChange(internalId, change); | |
164 } | |
165 | |
166 void LogChange(int64_t internalId, | |
167 const ServerIndexChange& change); | |
183 | 168 |
204 | 169 void GetChanges(Json::Value& target, |
170 int64_t since, | |
171 unsigned int maxResults); | |
172 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
173 void GetLastChange(Json::Value& target); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
174 |
231 | 175 void LogExportedResource(ResourceType resourceType, |
176 const std::string& publicId, | |
177 const std::string& remoteModality, | |
178 const std::string& patientId, | |
179 const std::string& studyInstanceUid, | |
180 const std::string& seriesInstanceUid, | |
181 const std::string& sopInstanceUid, | |
182 const boost::posix_time::ptime& date = | |
183 boost::posix_time::second_clock::local_time()); | |
183 | 184 |
231 | 185 void GetExportedResources(Json::Value& target, |
186 int64_t since, | |
187 unsigned int maxResults); | |
188 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
189 void GetLastExportedResource(Json::Value& target); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
190 |
238 | 191 // For unit testing only! |
183 | 192 int64_t GetTableRecordCount(const std::string& table); |
193 | |
194 uint64_t GetTotalCompressedSize(); | |
195 | |
196 uint64_t GetTotalUncompressedSize(); | |
197 | |
238 | 198 uint64_t GetResourceCount(ResourceType resourceType); |
199 | |
190 | 200 void GetAllPublicIds(Json::Value& target, |
201 ResourceType resourceType); | |
202 | |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
203 bool SelectPatientToRecycle(int64_t& internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
204 |
268
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
205 bool SelectPatientToRecycle(int64_t& internalId, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
206 int64_t patientIdToAvoid); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
207 |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
208 bool IsProtectedPatient(int64_t internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
209 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
210 void SetProtectedPatient(int64_t internalId, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
211 bool isProtected); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
212 |
183 | 213 DatabaseWrapper(const std::string& path, |
214 IServerIndexListener& listener); | |
215 | |
216 DatabaseWrapper(IServerIndexListener& listener); | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
217 |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
218 SQLite::Transaction* StartTransaction() |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
219 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
220 return new SQLite::Transaction(db_); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
221 } |
202 | 222 |
223 const char* GetErrorMessage() const | |
224 { | |
225 return db_.GetErrorMessage(); | |
226 } | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
227 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
228 void FlushToDisk() |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
229 { |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
230 db_.FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
231 } |
310 | 232 |
233 uint64_t IncrementGlobalSequence(GlobalProperty property); | |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
234 |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
235 void ClearTable(const std::string& tableName); |
513 | 236 |
237 bool IsExistingResource(int64_t internalId); | |
521 | 238 |
1162 | 239 void LookupIdentifier(std::list<int64_t>& result, |
240 const DicomTag& tag, | |
241 const std::string& value); | |
521 | 242 |
1162 | 243 void LookupIdentifier(std::list<int64_t>& result, |
244 const std::string& value); | |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
742
diff
changeset
|
245 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
742
diff
changeset
|
246 void GetAllMetadata(std::map<MetadataType, std::string>& result, |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
742
diff
changeset
|
247 int64_t id); |
183 | 248 }; |
249 } |