Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 1238:6c07108ff1e2
cleaning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Dec 2014 16:14:57 +0100 |
parents | 0f3716b88af7 |
children | 92c6b3b57699 |
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 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
33 #include "PrecompiledHeadersServer.h" |
183 | 34 #include "DatabaseWrapper.h" |
35 | |
36 #include "../Core/DicomFormat/DicomArray.h" | |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
37 #include "../Core/Uuid.h" |
183 | 38 #include "EmbeddedResources.h" |
39 | |
40 #include <glog/logging.h> | |
41 #include <stdio.h> | |
42 | |
43 namespace Orthanc | |
44 { | |
45 | |
46 namespace Internals | |
47 { | |
48 class SignalFileDeleted : public SQLite::IScalarFunction | |
49 { | |
50 private: | |
51 IServerIndexListener& listener_; | |
52 | |
53 public: | |
54 SignalFileDeleted(IServerIndexListener& listener) : | |
55 listener_(listener) | |
56 { | |
57 } | |
58 | |
59 virtual const char* GetName() const | |
60 { | |
61 return "SignalFileDeleted"; | |
62 } | |
63 | |
64 virtual unsigned int GetCardinality() const | |
65 { | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
66 return 7; |
183 | 67 } |
68 | |
69 virtual void Compute(SQLite::FunctionContext& context) | |
70 { | |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
71 std::string uncompressedMD5, compressedMD5; |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
72 |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
73 if (!context.IsNullValue(5)) |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
74 { |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
75 uncompressedMD5 = context.GetStringValue(5); |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
76 } |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
77 |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
78 if (!context.IsNullValue(6)) |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
79 { |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
80 compressedMD5 = context.GetStringValue(6); |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
81 } |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
82 |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
83 FileInfo info(context.GetStringValue(0), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
84 static_cast<FileContentType>(context.GetIntValue(1)), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
85 static_cast<uint64_t>(context.GetInt64Value(2)), |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
86 uncompressedMD5, |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
87 static_cast<CompressionType>(context.GetIntValue(3)), |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
88 static_cast<uint64_t>(context.GetInt64Value(4)), |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
89 compressedMD5); |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
90 |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
91 listener_.SignalFileDeleted(info); |
183 | 92 } |
93 }; | |
94 | |
1158 | 95 class SignalResourceDeleted : public SQLite::IScalarFunction |
96 { | |
97 private: | |
98 IServerIndexListener& listener_; | |
99 | |
100 public: | |
101 SignalResourceDeleted(IServerIndexListener& listener) : | |
102 listener_(listener) | |
103 { | |
104 } | |
105 | |
106 virtual const char* GetName() const | |
107 { | |
108 return "SignalResourceDeleted"; | |
109 } | |
110 | |
111 virtual unsigned int GetCardinality() const | |
112 { | |
113 return 2; | |
114 } | |
115 | |
116 virtual void Compute(SQLite::FunctionContext& context) | |
117 { | |
118 ResourceType type = static_cast<ResourceType>(context.GetIntValue(1)); | |
1198 | 119 ServerIndexChange change(ChangeType_Deleted, type, context.GetStringValue(0)); |
120 listener_.SignalChange(change); | |
1158 | 121 } |
122 }; | |
123 | |
183 | 124 class SignalRemainingAncestor : public SQLite::IScalarFunction |
125 { | |
126 private: | |
127 bool hasRemainingAncestor_; | |
128 std::string remainingPublicId_; | |
129 ResourceType remainingType_; | |
130 | |
131 public: | |
660 | 132 SignalRemainingAncestor() : |
133 hasRemainingAncestor_(false) | |
134 { | |
135 } | |
136 | |
183 | 137 void Reset() |
138 { | |
139 hasRemainingAncestor_ = false; | |
140 } | |
141 | |
142 virtual const char* GetName() const | |
143 { | |
144 return "SignalRemainingAncestor"; | |
145 } | |
146 | |
147 virtual unsigned int GetCardinality() const | |
148 { | |
149 return 2; | |
150 } | |
151 | |
152 virtual void Compute(SQLite::FunctionContext& context) | |
153 { | |
154 VLOG(1) << "There exists a remaining ancestor with public ID \"" | |
155 << context.GetStringValue(0) | |
156 << "\" of type " | |
157 << context.GetIntValue(1); | |
158 | |
159 if (!hasRemainingAncestor_ || | |
160 remainingType_ >= context.GetIntValue(1)) | |
161 { | |
162 hasRemainingAncestor_ = true; | |
163 remainingPublicId_ = context.GetStringValue(0); | |
164 remainingType_ = static_cast<ResourceType>(context.GetIntValue(1)); | |
165 } | |
166 } | |
167 | |
168 bool HasRemainingAncestor() const | |
169 { | |
170 return hasRemainingAncestor_; | |
171 } | |
172 | |
173 const std::string& GetRemainingAncestorId() const | |
174 { | |
175 assert(hasRemainingAncestor_); | |
176 return remainingPublicId_; | |
177 } | |
178 | |
179 ResourceType GetRemainingAncestorType() const | |
180 { | |
181 assert(hasRemainingAncestor_); | |
182 return remainingType_; | |
183 } | |
184 }; | |
185 } | |
186 | |
187 | |
188 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
189 void DatabaseWrapper::SetGlobalProperty(GlobalProperty property, |
183 | 190 const std::string& value) |
191 { | |
192 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO GlobalProperties VALUES(?, ?)"); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
193 s.BindInt(0, property); |
183 | 194 s.BindString(1, value); |
195 s.Run(); | |
196 } | |
197 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
198 bool DatabaseWrapper::LookupGlobalProperty(std::string& target, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
199 GlobalProperty property) |
183 | 200 { |
201 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
202 "SELECT value FROM GlobalProperties WHERE property=?"); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
203 s.BindInt(0, property); |
183 | 204 |
205 if (!s.Step()) | |
206 { | |
207 return false; | |
208 } | |
209 else | |
210 { | |
211 target = s.ColumnString(0); | |
212 return true; | |
213 } | |
214 } | |
215 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
216 std::string DatabaseWrapper::GetGlobalProperty(GlobalProperty property, |
183 | 217 const std::string& defaultValue) |
218 { | |
219 std::string s; | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
220 if (LookupGlobalProperty(s, property)) |
183 | 221 { |
222 return s; | |
223 } | |
224 else | |
225 { | |
226 return defaultValue; | |
227 } | |
228 } | |
229 | |
230 int64_t DatabaseWrapper::CreateResource(const std::string& publicId, | |
231 ResourceType type) | |
232 { | |
233 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
234 s.BindInt(0, type); | |
235 s.BindString(1, publicId); | |
236 s.Run(); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
237 int64_t id = db_.GetLastInsertRowId(); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
238 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
239 ChangeType changeType; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
240 switch (type) |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
241 { |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
242 case ResourceType_Patient: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
243 changeType = ChangeType_NewPatient; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
244 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
245 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
246 case ResourceType_Study: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
247 changeType = ChangeType_NewStudy; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
248 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
249 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
250 case ResourceType_Series: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
251 changeType = ChangeType_NewSeries; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
252 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
253 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
254 case ResourceType_Instance: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
255 changeType = ChangeType_NewInstance; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
256 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
257 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
258 default: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
259 throw OrthancException(ErrorCode_InternalError); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
260 } |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
261 |
1237 | 262 ServerIndexChange change(changeType, type, publicId); |
263 LogChange(id, change); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
264 return id; |
183 | 265 } |
266 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
267 bool DatabaseWrapper::LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
268 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
269 ResourceType& type) |
183 | 270 { |
271 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
272 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
273 s.BindString(0, publicId); | |
274 | |
275 if (!s.Step()) | |
276 { | |
277 return false; | |
278 } | |
279 else | |
280 { | |
281 id = s.ColumnInt(0); | |
282 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
283 | |
284 // Check whether there is a single resource with this public id | |
285 assert(!s.Step()); | |
286 | |
287 return true; | |
288 } | |
289 } | |
290 | |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
291 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
292 int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
293 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
294 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
295 "SELECT parentId FROM Resources WHERE internalId=?"); |
585 | 296 s.BindInt64(0, resourceId); |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
297 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
298 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
299 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
300 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
301 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
302 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
303 if (s.ColumnIsNull(0)) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
304 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
305 return false; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
306 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
307 else |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
308 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
309 parentId = s.ColumnInt(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
310 return true; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
311 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
312 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
313 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
314 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
315 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
316 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
317 "SELECT publicId FROM Resources WHERE internalId=?"); |
585 | 318 s.BindInt64(0, resourceId); |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
319 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
320 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
321 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
322 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
323 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
324 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
325 return s.ColumnString(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
326 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
327 |
304 | 328 |
329 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) | |
330 { | |
331 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
332 "SELECT resourceType FROM Resources WHERE internalId=?"); | |
585 | 333 s.BindInt64(0, resourceId); |
304 | 334 |
335 if (!s.Step()) | |
336 { | |
337 throw OrthancException(ErrorCode_UnknownResource); | |
338 } | |
339 | |
340 return static_cast<ResourceType>(s.ColumnInt(0)); | |
341 } | |
342 | |
343 | |
183 | 344 void DatabaseWrapper::AttachChild(int64_t parent, |
345 int64_t child) | |
346 { | |
347 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
585 | 348 s.BindInt64(0, parent); |
349 s.BindInt64(1, child); | |
183 | 350 s.Run(); |
351 } | |
352 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
353 void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds, |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
354 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
355 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
356 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); |
585 | 357 s.BindInt64(0, id); |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
358 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
359 childrenPublicIds = Json::arrayValue; |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
360 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
361 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
362 childrenPublicIds.append(s.ColumnString(0)); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
363 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
364 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
365 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
366 |
183 | 367 void DatabaseWrapper::DeleteResource(int64_t id) |
368 { | |
369 signalRemainingAncestor_->Reset(); | |
370 | |
371 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
585 | 372 s.BindInt64(0, id); |
183 | 373 s.Run(); |
374 | |
375 if (signalRemainingAncestor_->HasRemainingAncestor()) | |
376 { | |
377 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), | |
378 signalRemainingAncestor_->GetRemainingAncestorId()); | |
379 } | |
380 } | |
381 | |
382 void DatabaseWrapper::SetMetadata(int64_t id, | |
383 MetadataType type, | |
384 const std::string& value) | |
385 { | |
386 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
585 | 387 s.BindInt64(0, id); |
183 | 388 s.BindInt(1, type); |
389 s.BindString(2, value); | |
390 s.Run(); | |
391 } | |
392 | |
438 | 393 void DatabaseWrapper::DeleteMetadata(int64_t id, |
394 MetadataType type) | |
395 { | |
396 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Metadata WHERE id=? and type=?"); | |
585 | 397 s.BindInt64(0, id); |
438 | 398 s.BindInt(1, type); |
399 s.Run(); | |
400 } | |
401 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
402 bool DatabaseWrapper::LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
403 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
404 MetadataType type) |
183 | 405 { |
406 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
407 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
585 | 408 s.BindInt64(0, id); |
183 | 409 s.BindInt(1, type); |
410 | |
411 if (!s.Step()) | |
412 { | |
413 return false; | |
414 } | |
415 else | |
416 { | |
417 target = s.ColumnString(0); | |
418 return true; | |
419 } | |
420 } | |
421 | |
739 | 422 void DatabaseWrapper::ListAvailableMetadata(std::list<MetadataType>& target, |
436
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
423 int64_t id) |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
424 { |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
425 target.clear(); |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
426 |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
427 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type FROM Metadata WHERE id=?"); |
585 | 428 s.BindInt64(0, id); |
436
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
429 |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
430 while (s.Step()) |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
431 { |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
432 target.push_back(static_cast<MetadataType>(s.ColumnInt(0))); |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
433 } |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
434 } |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
435 |
d51186bf7602
read access to metadata
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
434
diff
changeset
|
436 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
437 void DatabaseWrapper::AddAttachment(int64_t id, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
438 const FileInfo& attachment) |
183 | 439 { |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
440 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); |
585 | 441 s.BindInt64(0, id); |
233 | 442 s.BindInt(1, attachment.GetContentType()); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
443 s.BindString(2, attachment.GetUuid()); |
585 | 444 s.BindInt64(3, attachment.GetCompressedSize()); |
445 s.BindInt64(4, attachment.GetUncompressedSize()); | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
446 s.BindInt(5, attachment.GetCompressionType()); |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
447 s.BindString(6, attachment.GetUncompressedMD5()); |
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
448 s.BindString(7, attachment.GetCompressedMD5()); |
183 | 449 s.Run(); |
450 } | |
451 | |
699
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
452 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
453 void DatabaseWrapper::DeleteAttachment(int64_t id, |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
454 FileContentType attachment) |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
455 { |
701 | 456 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM AttachedFiles WHERE id=? AND fileType=?"); |
699
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
457 s.BindInt64(0, id); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
458 s.BindInt(1, attachment); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
459 s.Run(); |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
460 } |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
461 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
462 |
2929e17f8447
add attachments to resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
694
diff
changeset
|
463 |
440
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
464 void DatabaseWrapper::ListAvailableAttachments(std::list<FileContentType>& result, |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
465 int64_t id) |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
466 { |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
467 result.clear(); |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
468 |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
469 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
470 "SELECT fileType FROM AttachedFiles WHERE id=?"); |
585 | 471 s.BindInt64(0, id); |
440
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
472 |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
473 while (s.Step()) |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
474 { |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
475 result.push_back(static_cast<FileContentType>(s.ColumnInt(0))); |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
476 } |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
477 } |
23e5b35e3c5c
statistics for patient/studies/series/instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
438
diff
changeset
|
478 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
479 bool DatabaseWrapper::LookupAttachment(FileInfo& attachment, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
480 int64_t id, |
233 | 481 FileContentType contentType) |
183 | 482 { |
483 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
484 "SELECT uuid, uncompressedSize, compressionType, compressedSize, uncompressedMD5, compressedMD5 FROM AttachedFiles WHERE id=? AND fileType=?"); |
585 | 485 s.BindInt64(0, id); |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
486 s.BindInt(1, contentType); |
183 | 487 |
488 if (!s.Step()) | |
489 { | |
490 return false; | |
491 } | |
492 else | |
493 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
494 attachment = FileInfo(s.ColumnString(0), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
495 contentType, |
740 | 496 s.ColumnInt64(1), |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
497 s.ColumnString(4), |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
498 static_cast<CompressionType>(s.ColumnInt(2)), |
740 | 499 s.ColumnInt64(3), |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
500 s.ColumnString(5)); |
183 | 501 return true; |
502 } | |
503 } | |
504 | |
1162 | 505 |
506 static void SetMainDicomTagsInternal(SQLite::Statement& s, | |
507 int64_t id, | |
508 const DicomElement& element) | |
509 { | |
510 s.BindInt64(0, id); | |
511 s.BindInt(1, element.GetTag().GetGroup()); | |
512 s.BindInt(2, element.GetTag().GetElement()); | |
513 s.BindString(3, element.GetValue().AsString()); | |
514 s.Run(); | |
515 } | |
516 | |
517 | |
183 | 518 void DatabaseWrapper::SetMainDicomTags(int64_t id, |
519 const DicomMap& tags) | |
520 { | |
521 DicomArray flattened(tags); | |
522 for (size_t i = 0; i < flattened.GetSize(); i++) | |
523 { | |
1162 | 524 if (flattened.GetElement(i).GetTag().IsIdentifier()) |
525 { | |
1173 | 526 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO DicomIdentifiers VALUES(?, ?, ?, ?)"); |
1162 | 527 SetMainDicomTagsInternal(s, id, flattened.GetElement(i)); |
528 } | |
529 else | |
530 { | |
1173 | 531 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); |
1162 | 532 SetMainDicomTagsInternal(s, id, flattened.GetElement(i)); |
533 } | |
183 | 534 } |
535 } | |
536 | |
537 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
538 int64_t id) | |
539 { | |
540 map.Clear(); | |
541 | |
542 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
585 | 543 s.BindInt64(0, id); |
183 | 544 while (s.Step()) |
545 { | |
546 map.SetValue(s.ColumnInt(1), | |
547 s.ColumnInt(2), | |
548 s.ColumnString(3)); | |
549 } | |
1162 | 550 |
551 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT * FROM DicomIdentifiers WHERE id=?"); | |
552 s2.BindInt64(0, id); | |
553 while (s2.Step()) | |
554 { | |
555 map.SetValue(s2.ColumnInt(1), | |
556 s2.ColumnInt(2), | |
557 s2.ColumnString(3)); | |
558 } | |
183 | 559 } |
560 | |
561 | |
562 bool DatabaseWrapper::GetParentPublicId(std::string& result, | |
563 int64_t id) | |
564 { | |
565 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
566 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
585 | 567 s.BindInt64(0, id); |
183 | 568 |
569 if (s.Step()) | |
570 { | |
571 result = s.ColumnString(0); | |
572 return true; | |
573 } | |
574 else | |
575 { | |
576 return false; | |
577 } | |
578 } | |
579 | |
580 | |
581 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, | |
582 int64_t id) | |
583 { | |
584 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
585 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
585 | 586 s.BindInt64(0, id); |
183 | 587 |
588 result.clear(); | |
589 | |
590 while (s.Step()) | |
591 { | |
592 result.push_back(s.ColumnString(0)); | |
593 } | |
594 } | |
595 | |
596 | |
199 | 597 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result, |
598 int64_t id) | |
599 { | |
600 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " | |
601 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
585 | 602 s.BindInt64(0, id); |
199 | 603 |
604 result.clear(); | |
605 | |
606 while (s.Step()) | |
607 { | |
741 | 608 result.push_back(s.ColumnInt64(0)); |
199 | 609 } |
610 } | |
611 | |
612 | |
1198 | 613 void DatabaseWrapper::LogChange(int64_t internalId, |
614 const ServerIndexChange& change) | |
183 | 615 { |
1198 | 616 if (change.GetChangeType() <= ChangeType_INTERNAL_LastLogged) |
1177
5b2d8c280ac2
Plugins can monitor changes through callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1173
diff
changeset
|
617 { |
1189
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
618 const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); |
1177
5b2d8c280ac2
Plugins can monitor changes through callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1173
diff
changeset
|
619 |
1189
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
620 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); |
1198 | 621 s.BindInt(0, change.GetChangeType()); |
1189
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
622 s.BindInt64(1, internalId); |
1198 | 623 s.BindInt(2, change.GetResourceType()); |
1189
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
624 s.BindString(3, boost::posix_time::to_iso_string(now)); |
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
625 s.Run(); |
6b9b02a16e99
NewChildInstance change type
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1177
diff
changeset
|
626 } |
1177
5b2d8c280ac2
Plugins can monitor changes through callbacks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1173
diff
changeset
|
627 |
1198 | 628 listener_.SignalChange(change); |
183 | 629 } |
630 | |
631 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
632 void DatabaseWrapper::GetChangesInternal(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
633 SQLite::Statement& s, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
634 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
635 unsigned int maxResults) |
204 | 636 { |
637 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
638 int64_t last = since; |
204 | 639 |
640 while (changes.size() < maxResults && s.Step()) | |
641 { | |
742 | 642 int64_t seq = s.ColumnInt64(0); |
204 | 643 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); |
742 | 644 int64_t internalId = s.ColumnInt64(2); |
204 | 645 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); |
646 const std::string& date = s.ColumnString(4); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
647 std::string publicId = GetPublicId(internalId); |
204 | 648 |
649 Json::Value item = Json::objectValue; | |
650 item["Seq"] = static_cast<int>(seq); | |
434
ccf3a0a43dac
EnumerationDictionary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
651 item["ChangeType"] = EnumerationToString(changeType); |
ccf3a0a43dac
EnumerationDictionary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
652 item["ResourceType"] = EnumerationToString(resourceType); |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
653 item["ID"] = publicId; |
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
654 item["Path"] = GetBasePath(resourceType, publicId); |
204 | 655 item["Date"] = date; |
656 last = seq; | |
657 | |
658 changes.append(item); | |
659 } | |
660 | |
661 target = Json::objectValue; | |
662 target["Changes"] = changes; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
663 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
664 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
665 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
666 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
667 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
668 void DatabaseWrapper::GetChanges(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
669 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
670 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
671 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
672 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); |
585 | 673 s.BindInt64(0, since); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
674 s.BindInt(1, maxResults + 1); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
675 GetChangesInternal(target, s, since, maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
676 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
677 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
678 void DatabaseWrapper::GetLastChange(Json::Value& target) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
679 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
680 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
681 GetChangesInternal(target, s, 0, 1); |
204 | 682 } |
683 | |
684 | |
231 | 685 void DatabaseWrapper::LogExportedResource(ResourceType resourceType, |
686 const std::string& publicId, | |
687 const std::string& remoteModality, | |
688 const std::string& patientId, | |
689 const std::string& studyInstanceUid, | |
690 const std::string& seriesInstanceUid, | |
691 const std::string& sopInstanceUid, | |
183 | 692 const boost::posix_time::ptime& date) |
693 { | |
231 | 694 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
695 "INSERT INTO ExportedResources VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
696 | |
697 s.BindInt(0, resourceType); | |
698 s.BindString(1, publicId); | |
699 s.BindString(2, remoteModality); | |
700 s.BindString(3, patientId); | |
701 s.BindString(4, studyInstanceUid); | |
702 s.BindString(5, seriesInstanceUid); | |
703 s.BindString(6, sopInstanceUid); | |
704 s.BindString(7, boost::posix_time::to_iso_string(date)); | |
705 | |
183 | 706 s.Run(); |
707 } | |
231 | 708 |
709 | |
742 | 710 void DatabaseWrapper::GetExportedResourcesInternal(Json::Value& target, |
711 SQLite::Statement& s, | |
712 int64_t since, | |
713 unsigned int maxResults) | |
231 | 714 { |
715 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
716 int64_t last = since; |
231 | 717 |
718 while (changes.size() < maxResults && s.Step()) | |
719 { | |
743 | 720 int64_t seq = s.ColumnInt64(0); |
231 | 721 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(1)); |
722 std::string publicId = s.ColumnString(2); | |
723 | |
724 Json::Value item = Json::objectValue; | |
725 item["Seq"] = static_cast<int>(seq); | |
434
ccf3a0a43dac
EnumerationDictionary
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
413
diff
changeset
|
726 item["ResourceType"] = EnumerationToString(resourceType); |
231 | 727 item["ID"] = publicId; |
728 item["Path"] = GetBasePath(resourceType, publicId); | |
729 item["RemoteModality"] = s.ColumnString(3); | |
730 item["Date"] = s.ColumnString(8); | |
731 | |
732 // WARNING: Do not add "break" below and do not reorder the case items! | |
733 switch (resourceType) | |
734 { | |
735 case ResourceType_Instance: | |
736 item["SopInstanceUid"] = s.ColumnString(7); | |
737 | |
738 case ResourceType_Series: | |
739 item["SeriesInstanceUid"] = s.ColumnString(6); | |
740 | |
741 case ResourceType_Study: | |
742 item["StudyInstanceUid"] = s.ColumnString(5); | |
743 | |
744 case ResourceType_Patient: | |
745 item["PatientId"] = s.ColumnString(4); | |
746 break; | |
747 | |
748 default: | |
749 throw OrthancException(ErrorCode_InternalError); | |
750 } | |
751 | |
752 last = seq; | |
753 | |
754 changes.append(item); | |
755 } | |
756 | |
757 target = Json::objectValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
758 target["Exports"] = changes; |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
759 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
760 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
761 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
762 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
763 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
764 void DatabaseWrapper::GetExportedResources(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
765 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
766 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
767 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
768 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
769 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?"); |
585 | 770 s.BindInt64(0, since); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
771 s.BindInt(1, maxResults + 1); |
742 | 772 GetExportedResourcesInternal(target, s, since, maxResults); |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
773 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
774 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
775 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
776 void DatabaseWrapper::GetLastExportedResource(Json::Value& target) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
777 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
778 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
779 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); |
742 | 780 GetExportedResourcesInternal(target, s, 0, 1); |
231 | 781 } |
782 | |
783 | |
183 | 784 |
785 | |
786 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
787 { | |
788 char buf[128]; | |
789 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
790 SQLite::Statement s(db_, buf); | |
791 | |
218 | 792 if (!s.Step()) |
793 { | |
794 throw OrthancException(ErrorCode_InternalError); | |
795 } | |
796 | |
183 | 797 int64_t c = s.ColumnInt(0); |
798 assert(!s.Step()); | |
799 | |
800 return c; | |
801 } | |
802 | |
803 | |
804 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
805 { | |
806 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
807 s.Run(); | |
808 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
809 } | |
810 | |
811 | |
812 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
813 { | |
814 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
815 s.Run(); | |
816 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
817 } | |
818 | |
190 | 819 void DatabaseWrapper::GetAllPublicIds(Json::Value& target, |
820 ResourceType resourceType) | |
821 { | |
822 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
823 s.BindInt(0, resourceType); | |
824 | |
825 target = Json::arrayValue; | |
826 while (s.Step()) | |
827 { | |
828 target.append(s.ColumnString(0)); | |
829 } | |
830 } | |
831 | |
1210 | 832 static void UpgradeDatabase(SQLite::Connection& db, |
833 EmbeddedResources::FileResourceId script) | |
834 { | |
835 std::string upgrade; | |
836 EmbeddedResources::GetFileResource(upgrade, script); | |
837 db.BeginTransaction(); | |
838 db.Execute(upgrade); | |
839 db.CommitTransaction(); | |
840 } | |
841 | |
183 | 842 |
843 DatabaseWrapper::DatabaseWrapper(const std::string& path, | |
844 IServerIndexListener& listener) : | |
845 listener_(listener) | |
846 { | |
847 db_.Open(path); | |
848 Open(); | |
849 } | |
850 | |
851 DatabaseWrapper::DatabaseWrapper(IServerIndexListener& listener) : | |
852 listener_(listener) | |
853 { | |
854 db_.OpenInMemory(); | |
855 Open(); | |
856 } | |
857 | |
858 void DatabaseWrapper::Open() | |
859 { | |
374 | 860 // Performance tuning of SQLite with PRAGMAs |
861 // http://www.sqlite.org/pragma.html | |
862 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;"); | |
863 db_.Execute("PRAGMA JOURNAL_MODE=WAL;"); | |
864 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); | |
865 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); | |
866 //db_.Execute("PRAGMA TEMP_STORE=memory"); | |
867 | |
183 | 868 if (!db_.DoesTableExist("GlobalProperties")) |
869 { | |
870 LOG(INFO) << "Creating the database"; | |
871 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
872 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 873 db_.Execute(query); |
874 } | |
875 | |
567 | 876 // Check the version of the database |
252 | 877 std::string version = GetGlobalProperty(GlobalProperty_DatabaseSchemaVersion, "Unknown"); |
878 bool ok = false; | |
879 try | |
880 { | |
881 LOG(INFO) << "Version of the Orthanc database: " << version; | |
882 unsigned int v = boost::lexical_cast<unsigned int>(version); | |
883 | |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
884 /** |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
885 * History of the database versions: |
1210 | 886 * - Orthanc before Orthanc 0.3.0 (inclusive) had no version |
887 * - Version 2: only Orthanc 0.3.1 | |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
888 * - Version 3: from Orthanc 0.3.2 to Orthanc 0.7.2 (inclusive) |
1210 | 889 * - Version 4: from Orthanc 0.7.3 to Orthanc 0.8.4 (inclusive) |
890 * - Version 5: from Orthanc 0.8.5 (inclusive) | |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
891 **/ |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
892 |
1158 | 893 // This version of Orthanc is only compatible with versions 3, 4 and 5 of the DB schema |
894 ok = (v == 3 || v == 4 || v == 5); | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
895 |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
896 if (v == 3) |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
897 { |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
898 LOG(WARNING) << "Upgrading database version from 3 to 4"; |
1210 | 899 UpgradeDatabase(db_, EmbeddedResources::UPGRADE_DATABASE_3_TO_4); |
1158 | 900 v = 4; |
901 } | |
902 | |
903 if (v == 4) | |
904 { | |
905 LOG(WARNING) << "Upgrading database version from 4 to 5"; | |
1210 | 906 UpgradeDatabase(db_, EmbeddedResources::UPGRADE_DATABASE_4_TO_5); |
1158 | 907 v = 5; |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
908 } |
252 | 909 } |
910 catch (boost::bad_lexical_cast&) | |
911 { | |
1210 | 912 ok = false; |
252 | 913 } |
914 | |
915 if (!ok) | |
916 { | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
917 LOG(ERROR) << "Incompatible version of the Orthanc database: " << version; |
252 | 918 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
919 } | |
920 | |
183 | 921 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; |
922 db_.Register(signalRemainingAncestor_); | |
923 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
1158 | 924 db_.Register(new Internals::SignalResourceDeleted(listener_)); |
183 | 925 } |
238 | 926 |
927 uint64_t DatabaseWrapper::GetResourceCount(ResourceType resourceType) | |
928 { | |
929 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
930 "SELECT COUNT(*) FROM Resources WHERE resourceType=?"); | |
931 s.BindInt(0, resourceType); | |
932 | |
933 if (!s.Step()) | |
934 { | |
935 throw OrthancException(ErrorCode_InternalError); | |
936 } | |
937 | |
938 int64_t c = s.ColumnInt(0); | |
939 assert(!s.Step()); | |
940 | |
941 return c; | |
942 } | |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
943 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
944 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
945 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
946 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
947 "SELECT patientId FROM PatientRecyclingOrder ORDER BY seq ASC LIMIT 1"); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
948 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
949 if (!s.Step()) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
950 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
951 // No patient remaining or all the patients are protected |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
952 return false; |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
953 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
954 else |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
955 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
956 internalId = s.ColumnInt(0); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
957 return true; |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
958 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
959 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
960 |
268
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
961 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
962 int64_t patientIdToAvoid) |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
963 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
964 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
965 "SELECT patientId FROM PatientRecyclingOrder " |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
966 "WHERE patientId != ? ORDER BY seq ASC LIMIT 1"); |
585 | 967 s.BindInt64(0, patientIdToAvoid); |
268
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
968 |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
969 if (!s.Step()) |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
970 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
971 // No patient remaining or all the patients are protected |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
972 return false; |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
973 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
974 else |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
975 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
976 internalId = s.ColumnInt(0); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
977 return true; |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
978 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
979 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
980 |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
981 bool DatabaseWrapper::IsProtectedPatient(int64_t internalId) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
982 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
983 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
984 "SELECT * FROM PatientRecyclingOrder WHERE patientId = ?"); |
585 | 985 s.BindInt64(0, internalId); |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
986 return !s.Step(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
987 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
988 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
989 void DatabaseWrapper::SetProtectedPatient(int64_t internalId, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
990 bool isProtected) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
991 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
992 if (isProtected) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
993 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
994 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM PatientRecyclingOrder WHERE patientId=?"); |
585 | 995 s.BindInt64(0, internalId); |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
996 s.Run(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
997 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
998 else if (IsProtectedPatient(internalId)) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
999 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1000 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO PatientRecyclingOrder VALUES(NULL, ?)"); |
585 | 1001 s.BindInt64(0, internalId); |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1002 s.Run(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1003 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1004 else |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1005 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1006 // Nothing to do: The patient is already unprotected |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1007 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
1008 } |
310 | 1009 |
1010 | |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
1011 |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
1012 void DatabaseWrapper::ClearTable(const std::string& tableName) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
1013 { |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
1014 db_.Execute("DELETE FROM " + tableName); |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
1015 } |
513 | 1016 |
1017 | |
1018 bool DatabaseWrapper::IsExistingResource(int64_t internalId) | |
1019 { | |
1020 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
1021 "SELECT * FROM Resources WHERE internalId=?"); | |
585 | 1022 s.BindInt64(0, internalId); |
513 | 1023 return s.Step(); |
1024 } | |
521 | 1025 |
1026 | |
1162 | 1027 void DatabaseWrapper::LookupIdentifier(std::list<int64_t>& result, |
1028 const DicomTag& tag, | |
1029 const std::string& value) | |
521 | 1030 { |
1162 | 1031 if (!tag.IsIdentifier()) |
1032 { | |
1033 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
1034 } | |
1035 | |
521 | 1036 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
1162 | 1037 "SELECT id FROM DicomIdentifiers WHERE tagGroup=? AND tagElement=? and value=?"); |
521 | 1038 |
1039 s.BindInt(0, tag.GetGroup()); | |
1040 s.BindInt(1, tag.GetElement()); | |
1041 s.BindString(2, value); | |
1042 | |
1043 result.clear(); | |
1044 | |
1045 while (s.Step()) | |
1046 { | |
1047 result.push_back(s.ColumnInt64(0)); | |
1048 } | |
1049 } | |
1050 | |
1051 | |
1162 | 1052 void DatabaseWrapper::LookupIdentifier(std::list<int64_t>& result, |
1053 const std::string& value) | |
521 | 1054 { |
1055 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
1162 | 1056 "SELECT id FROM DicomIdentifiers WHERE value=?"); |
521 | 1057 |
1058 s.BindString(0, value); | |
1059 | |
1060 result.clear(); | |
1061 | |
1062 while (s.Step()) | |
1063 { | |
1064 result.push_back(s.ColumnInt64(0)); | |
1065 } | |
1066 } | |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1067 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1068 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1069 void DatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& result, |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1070 int64_t id) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1071 { |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1072 result.clear(); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1073 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1074 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type, value FROM Metadata WHERE id=?"); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1075 s.BindInt64(0, id); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1076 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1077 while (s.Step()) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1078 { |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1079 MetadataType key = static_cast<MetadataType>(s.ColumnInt(0)); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1080 result[key] = s.ColumnString(1); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1081 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1082 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
1083 |
183 | 1084 } |