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