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