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