Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.h @ 1245:aea9277dee75
catch more exceptions (patch from Valdas Rapsevicius)
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 08 Dec 2014 12:42:50 +0100 |
parents | a0e420c5f2b8 |
children | 54bf0f0245f4 |
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 | |
1242 | 44 #include "ExportedResource.h" |
45 | |
183 | 46 namespace Orthanc |
47 { | |
48 namespace Internals | |
49 { | |
50 class SignalRemainingAncestor; | |
51 } | |
52 | |
53 /** | |
54 * This class manages an instance of the Orthanc SQLite database. It | |
55 * translates low-level requests into SQL statements. Mutual | |
56 * exclusion MUST be implemented at a higher level. | |
57 **/ | |
58 class DatabaseWrapper | |
59 { | |
60 private: | |
61 IServerIndexListener& listener_; | |
62 SQLite::Connection db_; | |
63 Internals::SignalRemainingAncestor* signalRemainingAncestor_; | |
64 | |
65 void Open(); | |
66 | |
1240 | 67 void GetChangesInternal(std::list<ServerIndexChange>& target, |
68 bool& done, | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
69 SQLite::Statement& s, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
70 unsigned int maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
71 |
1244 | 72 void GetExportedResourcesInternal(std::list<ExportedResource>& target, |
73 bool& done, | |
742 | 74 SQLite::Statement& s, |
75 unsigned int maxResults); | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
76 |
183 | 77 public: |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
78 void SetGlobalProperty(GlobalProperty property, |
183 | 79 const std::string& value); |
80 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
81 bool LookupGlobalProperty(std::string& target, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
82 GlobalProperty property); |
183 | 83 |
84 int64_t CreateResource(const std::string& publicId, | |
85 ResourceType type); | |
86 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
87 bool LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
88 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
89 ResourceType& type); |
183 | 90 |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
91 bool LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
92 int64_t resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
93 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
94 std::string GetPublicId(int64_t resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
95 |
304 | 96 ResourceType GetResourceType(int64_t resourceId); |
97 | |
183 | 98 void AttachChild(int64_t parent, |
99 int64_t child); | |
100 | |
101 void DeleteResource(int64_t id); | |
102 | |
103 void SetMetadata(int64_t id, | |
104 MetadataType type, | |
105 const std::string& value); | |
106 | |
438 | 107 void DeleteMetadata(int64_t id, |
108 MetadataType type); | |
109 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
110 bool LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
111 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
112 MetadataType type); |
183 | 113 |
739 | 114 void ListAvailableMetadata(std::list<MetadataType>& target, |
436
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
115 int64_t id); |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
116 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
117 void AddAttachment(int64_t id, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
118 const FileInfo& attachment); |
183 | 119 |
699
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
120 void DeleteAttachment(int64_t id, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
121 FileContentType attachment); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
122 |
440
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
123 void ListAvailableAttachments(std::list<FileContentType>& result, |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
124 int64_t id); |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
125 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
126 bool LookupAttachment(FileInfo& attachment, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
127 int64_t id, |
233 | 128 FileContentType contentType); |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
129 |
183 | 130 void SetMainDicomTags(int64_t id, |
131 const DicomMap& tags); | |
132 | |
133 void GetMainDicomTags(DicomMap& map, | |
134 int64_t id); | |
135 | |
136 void GetChildrenPublicId(std::list<std::string>& result, | |
137 int64_t id); | |
138 | |
199 | 139 void GetChildrenInternalId(std::list<int64_t>& result, |
140 int64_t id); | |
141 | |
1198 | 142 void LogChange(int64_t internalId, |
143 const ServerIndexChange& change); | |
183 | 144 |
1240 | 145 void GetChanges(std::list<ServerIndexChange>& target /* out */, |
146 bool& done /* out */, | |
204 | 147 int64_t since, |
148 unsigned int maxResults); | |
149 | |
1240 | 150 void GetLastChange(std::list<ServerIndexChange>& target /* out */); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
151 |
231 | 152 void LogExportedResource(ResourceType resourceType, |
153 const std::string& publicId, | |
154 const std::string& remoteModality, | |
155 const std::string& patientId, | |
156 const std::string& studyInstanceUid, | |
157 const std::string& seriesInstanceUid, | |
158 const std::string& sopInstanceUid, | |
1236 | 159 const boost::posix_time::ptime& date); |
183 | 160 |
1244 | 161 void GetExportedResources(std::list<ExportedResource>& target /* out */, |
162 bool& done /* out */, | |
231 | 163 int64_t since, |
164 unsigned int maxResults); | |
165 | |
1244 | 166 void GetLastExportedResource(std::list<ExportedResource>& target /* out */); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
167 |
183 | 168 uint64_t GetTotalCompressedSize(); |
169 | |
170 uint64_t GetTotalUncompressedSize(); | |
171 | |
238 | 172 uint64_t GetResourceCount(ResourceType resourceType); |
173 | |
1241 | 174 void GetAllPublicIds(std::list<std::string>& target, |
190 | 175 ResourceType resourceType); |
176 | |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
177 bool SelectPatientToRecycle(int64_t& internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
178 |
268
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
179 bool SelectPatientToRecycle(int64_t& internalId, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
180 int64_t patientIdToAvoid); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
181 |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
182 bool IsProtectedPatient(int64_t internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
183 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
184 void SetProtectedPatient(int64_t internalId, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
185 bool isProtected); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
238
diff
changeset
|
186 |
183 | 187 DatabaseWrapper(const std::string& path, |
188 IServerIndexListener& listener); | |
189 | |
190 DatabaseWrapper(IServerIndexListener& listener); | |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
191 |
1235 | 192 SQLite::ITransaction* StartTransaction() |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
193 { |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
194 return new SQLite::Transaction(db_); |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
195 } |
202 | 196 |
197 const char* GetErrorMessage() const | |
198 { | |
199 return db_.GetErrorMessage(); | |
200 } | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
201 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
202 void FlushToDisk() |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
203 { |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
204 db_.FlushToDisk(); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
205 } |
310 | 206 |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
207 void ClearTable(const std::string& tableName); |
513 | 208 |
209 bool IsExistingResource(int64_t internalId); | |
521 | 210 |
1162 | 211 void LookupIdentifier(std::list<int64_t>& result, |
212 const DicomTag& tag, | |
213 const std::string& value); | |
521 | 214 |
1162 | 215 void LookupIdentifier(std::list<int64_t>& result, |
216 const std::string& value); | |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
742
diff
changeset
|
217 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
742
diff
changeset
|
218 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
|
219 int64_t id); |
1241 | 220 |
221 | |
222 | |
223 | |
224 /** | |
225 * The methods declared below are for unit testing only! | |
226 **/ | |
227 | |
228 void GetChildren(std::list<std::string>& childrenPublicIds, | |
229 int64_t id); | |
230 | |
231 int64_t GetTableRecordCount(const std::string& table); | |
232 | |
233 bool GetParentPublicId(std::string& result, | |
234 int64_t id); | |
235 | |
183 | 236 }; |
237 } |