Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 193:a1b9d1e1497b
failed attempt to compile with linux standard base
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 13 Nov 2012 14:02:28 +0100 |
parents | c56dc32266e0 |
children | 530a25320461 |
rev | line source |
---|---|
183 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "DatabaseWrapper.h" | |
34 | |
35 #include "../Core/DicomFormat/DicomArray.h" | |
36 #include "EmbeddedResources.h" | |
37 | |
38 #include <glog/logging.h> | |
39 #include <stdio.h> | |
40 | |
41 namespace Orthanc | |
42 { | |
43 | |
44 namespace Internals | |
45 { | |
46 class SignalFileDeleted : public SQLite::IScalarFunction | |
47 { | |
48 private: | |
49 IServerIndexListener& listener_; | |
50 | |
51 public: | |
52 SignalFileDeleted(IServerIndexListener& listener) : | |
53 listener_(listener) | |
54 { | |
55 } | |
56 | |
57 virtual const char* GetName() const | |
58 { | |
59 return "SignalFileDeleted"; | |
60 } | |
61 | |
62 virtual unsigned int GetCardinality() const | |
63 { | |
64 return 1; | |
65 } | |
66 | |
67 virtual void Compute(SQLite::FunctionContext& context) | |
68 { | |
69 listener_.SignalFileDeleted(context.GetStringValue(0)); | |
70 } | |
71 }; | |
72 | |
73 class SignalRemainingAncestor : public SQLite::IScalarFunction | |
74 { | |
75 private: | |
76 bool hasRemainingAncestor_; | |
77 std::string remainingPublicId_; | |
78 ResourceType remainingType_; | |
79 | |
80 public: | |
81 void Reset() | |
82 { | |
83 hasRemainingAncestor_ = false; | |
84 } | |
85 | |
86 virtual const char* GetName() const | |
87 { | |
88 return "SignalRemainingAncestor"; | |
89 } | |
90 | |
91 virtual unsigned int GetCardinality() const | |
92 { | |
93 return 2; | |
94 } | |
95 | |
96 virtual void Compute(SQLite::FunctionContext& context) | |
97 { | |
98 VLOG(1) << "There exists a remaining ancestor with public ID \"" | |
99 << context.GetStringValue(0) | |
100 << "\" of type " | |
101 << context.GetIntValue(1); | |
102 | |
103 if (!hasRemainingAncestor_ || | |
104 remainingType_ >= context.GetIntValue(1)) | |
105 { | |
106 hasRemainingAncestor_ = true; | |
107 remainingPublicId_ = context.GetStringValue(0); | |
108 remainingType_ = static_cast<ResourceType>(context.GetIntValue(1)); | |
109 } | |
110 } | |
111 | |
112 bool HasRemainingAncestor() const | |
113 { | |
114 return hasRemainingAncestor_; | |
115 } | |
116 | |
117 const std::string& GetRemainingAncestorId() const | |
118 { | |
119 assert(hasRemainingAncestor_); | |
120 return remainingPublicId_; | |
121 } | |
122 | |
123 ResourceType GetRemainingAncestorType() const | |
124 { | |
125 assert(hasRemainingAncestor_); | |
126 return remainingType_; | |
127 } | |
128 }; | |
129 } | |
130 | |
131 | |
132 | |
133 void DatabaseWrapper::SetGlobalProperty(const std::string& name, | |
134 const std::string& value) | |
135 { | |
136 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO GlobalProperties VALUES(?, ?)"); | |
137 s.BindString(0, name); | |
138 s.BindString(1, value); | |
139 s.Run(); | |
140 } | |
141 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
142 bool DatabaseWrapper::LookupGlobalProperty(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
143 const std::string& name) |
183 | 144 { |
145 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
146 "SELECT value FROM GlobalProperties WHERE name=?"); | |
147 s.BindString(0, name); | |
148 | |
149 if (!s.Step()) | |
150 { | |
151 return false; | |
152 } | |
153 else | |
154 { | |
155 target = s.ColumnString(0); | |
156 return true; | |
157 } | |
158 } | |
159 | |
160 std::string DatabaseWrapper::GetGlobalProperty(const std::string& name, | |
161 const std::string& defaultValue) | |
162 { | |
163 std::string s; | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
164 if (LookupGlobalProperty(s, name)) |
183 | 165 { |
166 return s; | |
167 } | |
168 else | |
169 { | |
170 return defaultValue; | |
171 } | |
172 } | |
173 | |
174 int64_t DatabaseWrapper::CreateResource(const std::string& publicId, | |
175 ResourceType type) | |
176 { | |
177 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
178 s.BindInt(0, type); | |
179 s.BindString(1, publicId); | |
180 s.Run(); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
181 int64_t id = db_.GetLastInsertRowId(); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
182 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
183 ChangeType changeType; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
184 switch (type) |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
185 { |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
186 case ResourceType_Patient: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
187 changeType = ChangeType_NewPatient; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
188 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
189 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
190 case ResourceType_Study: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
191 changeType = ChangeType_NewStudy; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
192 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
193 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
194 case ResourceType_Series: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
195 changeType = ChangeType_NewSeries; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
196 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
197 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
198 case ResourceType_Instance: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
199 changeType = ChangeType_NewInstance; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
200 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
201 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
202 default: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
203 throw OrthancException(ErrorCode_InternalError); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
204 } |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
205 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
206 LogChange(changeType, id, type); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
207 return id; |
183 | 208 } |
209 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
210 bool DatabaseWrapper::LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
211 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
212 ResourceType& type) |
183 | 213 { |
214 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
215 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
216 s.BindString(0, publicId); | |
217 | |
218 if (!s.Step()) | |
219 { | |
220 return false; | |
221 } | |
222 else | |
223 { | |
224 id = s.ColumnInt(0); | |
225 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
226 | |
227 // Check whether there is a single resource with this public id | |
228 assert(!s.Step()); | |
229 | |
230 return true; | |
231 } | |
232 } | |
233 | |
234 void DatabaseWrapper::AttachChild(int64_t parent, | |
235 int64_t child) | |
236 { | |
237 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
238 s.BindInt(0, parent); | |
239 s.BindInt(1, child); | |
240 s.Run(); | |
241 } | |
242 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
243 void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds, |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
244 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
245 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
246 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
247 s.BindInt(0, id); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
248 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
249 childrenPublicIds = Json::arrayValue; |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
250 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
251 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
252 childrenPublicIds.append(s.ColumnString(0)); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
253 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
254 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
255 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
256 |
183 | 257 void DatabaseWrapper::DeleteResource(int64_t id) |
258 { | |
259 signalRemainingAncestor_->Reset(); | |
260 | |
261 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
262 s.BindInt(0, id); | |
263 s.Run(); | |
264 | |
265 if (signalRemainingAncestor_->HasRemainingAncestor()) | |
266 { | |
267 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), | |
268 signalRemainingAncestor_->GetRemainingAncestorId()); | |
269 } | |
270 } | |
271 | |
272 void DatabaseWrapper::SetMetadata(int64_t id, | |
273 MetadataType type, | |
274 const std::string& value) | |
275 { | |
276 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
277 s.BindInt(0, id); | |
278 s.BindInt(1, type); | |
279 s.BindString(2, value); | |
280 s.Run(); | |
281 } | |
282 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
283 bool DatabaseWrapper::LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
284 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
285 MetadataType type) |
183 | 286 { |
287 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
288 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
289 s.BindInt(0, id); | |
290 s.BindInt(1, type); | |
291 | |
292 if (!s.Step()) | |
293 { | |
294 return false; | |
295 } | |
296 else | |
297 { | |
298 target = s.ColumnString(0); | |
299 return true; | |
300 } | |
301 } | |
302 | |
303 std::string DatabaseWrapper::GetMetadata(int64_t id, | |
304 MetadataType type, | |
305 const std::string& defaultValue) | |
306 { | |
307 std::string s; | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
308 if (LookupMetadata(s, id, type)) |
183 | 309 { |
310 return s; | |
311 } | |
312 else | |
313 { | |
314 return defaultValue; | |
315 } | |
316 } | |
317 | |
318 void DatabaseWrapper::AttachFile(int64_t id, | |
192
c56dc32266e0
refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
319 const std::string& contentName, |
183 | 320 const std::string& fileUuid, |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
321 uint64_t compressedSize, |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
322 uint64_t uncompressedSize, |
183 | 323 CompressionType compressionType) |
324 { | |
325 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)"); | |
326 s.BindInt(0, id); | |
192
c56dc32266e0
refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
327 s.BindString(1, contentName); |
183 | 328 s.BindString(2, fileUuid); |
329 s.BindInt(3, compressedSize); | |
330 s.BindInt(4, uncompressedSize); | |
331 s.BindInt(5, compressionType); | |
332 s.Run(); | |
333 } | |
334 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
335 bool DatabaseWrapper::LookupFile(int64_t id, |
192
c56dc32266e0
refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
336 const std::string& contentName, |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
337 std::string& fileUuid, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
338 uint64_t& compressedSize, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
339 uint64_t& uncompressedSize, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
340 CompressionType& compressionType) |
183 | 341 { |
342 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
192
c56dc32266e0
refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
343 "SELECT uuid, compressedSize, uncompressedSize, compressionType FROM AttachedFiles WHERE id=? AND contentName=?"); |
183 | 344 s.BindInt(0, id); |
192
c56dc32266e0
refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
190
diff
changeset
|
345 s.BindString(1, contentName); |
183 | 346 |
347 if (!s.Step()) | |
348 { | |
349 return false; | |
350 } | |
351 else | |
352 { | |
353 fileUuid = s.ColumnString(0); | |
354 compressedSize = s.ColumnInt(1); | |
355 uncompressedSize = s.ColumnInt(2); | |
356 compressionType = static_cast<CompressionType>(s.ColumnInt(3)); | |
357 return true; | |
358 } | |
359 } | |
360 | |
361 void DatabaseWrapper::SetMainDicomTags(int64_t id, | |
362 const DicomMap& tags) | |
363 { | |
364 DicomArray flattened(tags); | |
365 for (size_t i = 0; i < flattened.GetSize(); i++) | |
366 { | |
367 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
368 s.BindInt(0, id); | |
369 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); | |
370 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); | |
371 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); | |
372 s.Run(); | |
373 } | |
374 } | |
375 | |
376 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
377 int64_t id) | |
378 { | |
379 map.Clear(); | |
380 | |
381 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
382 s.BindInt(0, id); | |
383 while (s.Step()) | |
384 { | |
385 map.SetValue(s.ColumnInt(1), | |
386 s.ColumnInt(2), | |
387 s.ColumnString(3)); | |
388 } | |
389 } | |
390 | |
391 | |
392 bool DatabaseWrapper::GetParentPublicId(std::string& result, | |
393 int64_t id) | |
394 { | |
395 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
396 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
397 s.BindInt(0, id); | |
398 | |
399 if (s.Step()) | |
400 { | |
401 result = s.ColumnString(0); | |
402 return true; | |
403 } | |
404 else | |
405 { | |
406 return false; | |
407 } | |
408 } | |
409 | |
410 | |
411 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, | |
412 int64_t id) | |
413 { | |
414 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
415 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
416 s.BindInt(0, id); | |
417 | |
418 result.clear(); | |
419 | |
420 while (s.Step()) | |
421 { | |
422 result.push_back(s.ColumnString(0)); | |
423 } | |
424 } | |
425 | |
426 | |
427 void DatabaseWrapper::LogChange(ChangeType changeType, | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
428 int64_t internalId, |
183 | 429 ResourceType resourceType, |
430 const boost::posix_time::ptime& date) | |
431 { | |
432 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); | |
433 s.BindInt(0, changeType); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
434 s.BindInt(1, internalId); |
183 | 435 s.BindInt(2, resourceType); |
436 s.BindString(3, boost::posix_time::to_iso_string(date)); | |
437 s.Run(); | |
438 } | |
439 | |
440 | |
441 void DatabaseWrapper::LogExportedInstance(const std::string& remoteModality, | |
442 DicomInstanceHasher& hasher, | |
443 const boost::posix_time::ptime& date) | |
444 { | |
445 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO ExportedInstances VALUES(NULL, ?, ?, ?, ?, ?, ?)"); | |
446 s.BindString(0, remoteModality); | |
447 s.BindString(1, hasher.HashInstance()); | |
448 s.BindString(2, hasher.GetPatientId()); | |
449 s.BindString(3, hasher.GetStudyUid()); | |
450 s.BindString(4, hasher.GetSeriesUid()); | |
451 s.BindString(5, hasher.GetInstanceUid()); | |
452 s.BindString(6, boost::posix_time::to_iso_string(date)); | |
453 s.Run(); | |
454 } | |
455 | |
456 | |
457 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
458 { | |
459 char buf[128]; | |
460 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
461 SQLite::Statement s(db_, buf); | |
462 | |
463 assert(s.Step()); | |
464 int64_t c = s.ColumnInt(0); | |
465 assert(!s.Step()); | |
466 | |
467 return c; | |
468 } | |
469 | |
470 | |
471 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
472 { | |
473 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
474 s.Run(); | |
475 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
476 } | |
477 | |
478 | |
479 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
480 { | |
481 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
482 s.Run(); | |
483 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
484 } | |
485 | |
190 | 486 void DatabaseWrapper::GetAllPublicIds(Json::Value& target, |
487 ResourceType resourceType) | |
488 { | |
489 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
490 s.BindInt(0, resourceType); | |
491 | |
492 target = Json::arrayValue; | |
493 while (s.Step()) | |
494 { | |
495 target.append(s.ColumnString(0)); | |
496 } | |
497 } | |
498 | |
183 | 499 |
500 DatabaseWrapper::DatabaseWrapper(const std::string& path, | |
501 IServerIndexListener& listener) : | |
502 listener_(listener) | |
503 { | |
504 db_.Open(path); | |
505 Open(); | |
506 } | |
507 | |
508 DatabaseWrapper::DatabaseWrapper(IServerIndexListener& listener) : | |
509 listener_(listener) | |
510 { | |
511 db_.OpenInMemory(); | |
512 Open(); | |
513 } | |
514 | |
515 void DatabaseWrapper::Open() | |
516 { | |
517 if (!db_.DoesTableExist("GlobalProperties")) | |
518 { | |
519 LOG(INFO) << "Creating the database"; | |
520 std::string query; | |
521 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE_2); | |
522 db_.Execute(query); | |
523 } | |
524 | |
525 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; | |
526 db_.Register(signalRemainingAncestor_); | |
527 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
528 } | |
529 } |