Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 369:4632a044746e
simplification of the code
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 19 Feb 2013 12:09:03 +0100 |
parents | 6ab6cdeedf4e |
children | 42e87c17cab8 |
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" | |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
36 #include "../Core/Uuid.h" |
183 | 37 #include "EmbeddedResources.h" |
38 | |
39 #include <glog/logging.h> | |
40 #include <stdio.h> | |
41 | |
42 namespace Orthanc | |
43 { | |
44 | |
45 namespace Internals | |
46 { | |
47 class SignalFileDeleted : public SQLite::IScalarFunction | |
48 { | |
49 private: | |
50 IServerIndexListener& listener_; | |
51 | |
52 public: | |
53 SignalFileDeleted(IServerIndexListener& listener) : | |
54 listener_(listener) | |
55 { | |
56 } | |
57 | |
58 virtual const char* GetName() const | |
59 { | |
60 return "SignalFileDeleted"; | |
61 } | |
62 | |
63 virtual unsigned int GetCardinality() const | |
64 { | |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
65 return 5; |
183 | 66 } |
67 | |
68 virtual void Compute(SQLite::FunctionContext& context) | |
69 { | |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
70 FileInfo info(context.GetStringValue(0), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
71 static_cast<FileContentType>(context.GetIntValue(1)), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
72 static_cast<uint64_t>(context.GetInt64Value(2)), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
73 static_cast<CompressionType>(context.GetIntValue(3)), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
74 static_cast<uint64_t>(context.GetInt64Value(4))); |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
75 |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
76 listener_.SignalFileDeleted(info); |
183 | 77 } |
78 }; | |
79 | |
80 class SignalRemainingAncestor : public SQLite::IScalarFunction | |
81 { | |
82 private: | |
83 bool hasRemainingAncestor_; | |
84 std::string remainingPublicId_; | |
85 ResourceType remainingType_; | |
86 | |
87 public: | |
88 void Reset() | |
89 { | |
90 hasRemainingAncestor_ = false; | |
91 } | |
92 | |
93 virtual const char* GetName() const | |
94 { | |
95 return "SignalRemainingAncestor"; | |
96 } | |
97 | |
98 virtual unsigned int GetCardinality() const | |
99 { | |
100 return 2; | |
101 } | |
102 | |
103 virtual void Compute(SQLite::FunctionContext& context) | |
104 { | |
105 VLOG(1) << "There exists a remaining ancestor with public ID \"" | |
106 << context.GetStringValue(0) | |
107 << "\" of type " | |
108 << context.GetIntValue(1); | |
109 | |
110 if (!hasRemainingAncestor_ || | |
111 remainingType_ >= context.GetIntValue(1)) | |
112 { | |
113 hasRemainingAncestor_ = true; | |
114 remainingPublicId_ = context.GetStringValue(0); | |
115 remainingType_ = static_cast<ResourceType>(context.GetIntValue(1)); | |
116 } | |
117 } | |
118 | |
119 bool HasRemainingAncestor() const | |
120 { | |
121 return hasRemainingAncestor_; | |
122 } | |
123 | |
124 const std::string& GetRemainingAncestorId() const | |
125 { | |
126 assert(hasRemainingAncestor_); | |
127 return remainingPublicId_; | |
128 } | |
129 | |
130 ResourceType GetRemainingAncestorType() const | |
131 { | |
132 assert(hasRemainingAncestor_); | |
133 return remainingType_; | |
134 } | |
135 }; | |
136 } | |
137 | |
138 | |
139 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
140 void DatabaseWrapper::SetGlobalProperty(GlobalProperty property, |
183 | 141 const std::string& value) |
142 { | |
143 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
|
144 s.BindInt(0, property); |
183 | 145 s.BindString(1, value); |
146 s.Run(); | |
147 } | |
148 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
149 bool DatabaseWrapper::LookupGlobalProperty(std::string& target, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
150 GlobalProperty property) |
183 | 151 { |
152 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
153 "SELECT value FROM GlobalProperties WHERE property=?"); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
154 s.BindInt(0, property); |
183 | 155 |
156 if (!s.Step()) | |
157 { | |
158 return false; | |
159 } | |
160 else | |
161 { | |
162 target = s.ColumnString(0); | |
163 return true; | |
164 } | |
165 } | |
166 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
167 std::string DatabaseWrapper::GetGlobalProperty(GlobalProperty property, |
183 | 168 const std::string& defaultValue) |
169 { | |
170 std::string s; | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
171 if (LookupGlobalProperty(s, property)) |
183 | 172 { |
173 return s; | |
174 } | |
175 else | |
176 { | |
177 return defaultValue; | |
178 } | |
179 } | |
180 | |
181 int64_t DatabaseWrapper::CreateResource(const std::string& publicId, | |
182 ResourceType type) | |
183 { | |
184 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
185 s.BindInt(0, type); | |
186 s.BindString(1, publicId); | |
187 s.Run(); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
188 int64_t id = db_.GetLastInsertRowId(); |
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 ChangeType changeType; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
191 switch (type) |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
192 { |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
193 case ResourceType_Patient: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
194 changeType = ChangeType_NewPatient; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
195 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
196 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
197 case ResourceType_Study: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
198 changeType = ChangeType_NewStudy; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
199 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
200 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
201 case ResourceType_Series: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
202 changeType = ChangeType_NewSeries; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
203 break; |
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 case ResourceType_Instance: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
206 changeType = ChangeType_NewInstance; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
207 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
208 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
209 default: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
210 throw OrthancException(ErrorCode_InternalError); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
211 } |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
212 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
213 LogChange(changeType, id, type); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
214 return id; |
183 | 215 } |
216 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
217 bool DatabaseWrapper::LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
218 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
219 ResourceType& type) |
183 | 220 { |
221 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
222 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
223 s.BindString(0, publicId); | |
224 | |
225 if (!s.Step()) | |
226 { | |
227 return false; | |
228 } | |
229 else | |
230 { | |
231 id = s.ColumnInt(0); | |
232 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
233 | |
234 // Check whether there is a single resource with this public id | |
235 assert(!s.Step()); | |
236 | |
237 return true; | |
238 } | |
239 } | |
240 | |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
241 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
242 int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
243 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
244 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
245 "SELECT parentId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
246 s.BindInt(0, resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
247 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
248 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
249 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
250 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
251 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
252 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
253 if (s.ColumnIsNull(0)) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
254 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
255 return false; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
256 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
257 else |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
258 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
259 parentId = s.ColumnInt(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
260 return true; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
261 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
262 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
263 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
264 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
265 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
266 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
267 "SELECT publicId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
268 s.BindInt(0, resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
269 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
270 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
271 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
272 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
273 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
274 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
275 return s.ColumnString(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
276 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
277 |
304 | 278 |
279 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) | |
280 { | |
281 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
282 "SELECT resourceType FROM Resources WHERE internalId=?"); | |
283 s.BindInt(0, resourceId); | |
284 | |
285 if (!s.Step()) | |
286 { | |
287 throw OrthancException(ErrorCode_UnknownResource); | |
288 } | |
289 | |
290 return static_cast<ResourceType>(s.ColumnInt(0)); | |
291 } | |
292 | |
293 | |
183 | 294 void DatabaseWrapper::AttachChild(int64_t parent, |
295 int64_t child) | |
296 { | |
297 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
298 s.BindInt(0, parent); | |
299 s.BindInt(1, child); | |
300 s.Run(); | |
301 } | |
302 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
303 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
|
304 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
305 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
306 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
|
307 s.BindInt(0, id); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
308 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
309 childrenPublicIds = Json::arrayValue; |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
310 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
311 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
312 childrenPublicIds.append(s.ColumnString(0)); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
313 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
314 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
315 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
316 |
183 | 317 void DatabaseWrapper::DeleteResource(int64_t id) |
318 { | |
319 signalRemainingAncestor_->Reset(); | |
320 | |
321 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
322 s.BindInt(0, id); | |
323 s.Run(); | |
324 | |
325 if (signalRemainingAncestor_->HasRemainingAncestor()) | |
326 { | |
327 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), | |
328 signalRemainingAncestor_->GetRemainingAncestorId()); | |
329 } | |
330 } | |
331 | |
332 void DatabaseWrapper::SetMetadata(int64_t id, | |
333 MetadataType type, | |
334 const std::string& value) | |
335 { | |
336 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
337 s.BindInt(0, id); | |
338 s.BindInt(1, type); | |
339 s.BindString(2, value); | |
340 s.Run(); | |
341 } | |
342 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
343 bool DatabaseWrapper::LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
344 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
345 MetadataType type) |
183 | 346 { |
347 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
348 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
349 s.BindInt(0, id); | |
350 s.BindInt(1, type); | |
351 | |
352 if (!s.Step()) | |
353 { | |
354 return false; | |
355 } | |
356 else | |
357 { | |
358 target = s.ColumnString(0); | |
359 return true; | |
360 } | |
361 } | |
362 | |
363 std::string DatabaseWrapper::GetMetadata(int64_t id, | |
364 MetadataType type, | |
365 const std::string& defaultValue) | |
366 { | |
367 std::string s; | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
368 if (LookupMetadata(s, id, type)) |
183 | 369 { |
370 return s; | |
371 } | |
372 else | |
373 { | |
374 return defaultValue; | |
375 } | |
376 } | |
377 | |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
378 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
379 bool DatabaseWrapper::GetMetadataAsInteger(int& result, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
380 int64_t id, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
381 MetadataType type) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
382 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
383 std::string s = GetMetadata(id, type, ""); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
384 if (s.size() == 0) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
385 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
386 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
387 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
388 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
389 try |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
390 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
391 result = boost::lexical_cast<int>(s); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
392 return true; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
393 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
394 catch (boost::bad_lexical_cast&) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
395 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
396 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
397 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
398 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
399 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
400 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
401 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
402 void DatabaseWrapper::AddAttachment(int64_t id, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
403 const FileInfo& attachment) |
183 | 404 { |
405 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)"); | |
406 s.BindInt(0, id); | |
233 | 407 s.BindInt(1, attachment.GetContentType()); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
408 s.BindString(2, attachment.GetUuid()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
409 s.BindInt(3, attachment.GetCompressedSize()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
410 s.BindInt(4, attachment.GetUncompressedSize()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
411 s.BindInt(5, attachment.GetCompressionType()); |
183 | 412 s.Run(); |
413 } | |
414 | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
415 bool DatabaseWrapper::LookupAttachment(FileInfo& attachment, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
416 int64_t id, |
233 | 417 FileContentType contentType) |
183 | 418 { |
419 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
420 "SELECT uuid, uncompressedSize, compressionType, compressedSize FROM AttachedFiles WHERE id=? AND fileType=?"); |
183 | 421 s.BindInt(0, id); |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
422 s.BindInt(1, contentType); |
183 | 423 |
424 if (!s.Step()) | |
425 { | |
426 return false; | |
427 } | |
428 else | |
429 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
430 attachment = FileInfo(s.ColumnString(0), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
431 contentType, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
432 s.ColumnInt(1), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
433 static_cast<CompressionType>(s.ColumnInt(2)), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
434 s.ColumnInt(3)); |
183 | 435 return true; |
436 } | |
437 } | |
438 | |
439 void DatabaseWrapper::SetMainDicomTags(int64_t id, | |
440 const DicomMap& tags) | |
441 { | |
442 DicomArray flattened(tags); | |
443 for (size_t i = 0; i < flattened.GetSize(); i++) | |
444 { | |
445 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
446 s.BindInt(0, id); | |
447 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); | |
448 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); | |
449 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); | |
450 s.Run(); | |
451 } | |
452 } | |
453 | |
454 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
455 int64_t id) | |
456 { | |
457 map.Clear(); | |
458 | |
459 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
460 s.BindInt(0, id); | |
461 while (s.Step()) | |
462 { | |
463 map.SetValue(s.ColumnInt(1), | |
464 s.ColumnInt(2), | |
465 s.ColumnString(3)); | |
466 } | |
467 } | |
468 | |
469 | |
470 bool DatabaseWrapper::GetParentPublicId(std::string& result, | |
471 int64_t id) | |
472 { | |
473 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
474 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
475 s.BindInt(0, id); | |
476 | |
477 if (s.Step()) | |
478 { | |
479 result = s.ColumnString(0); | |
480 return true; | |
481 } | |
482 else | |
483 { | |
484 return false; | |
485 } | |
486 } | |
487 | |
488 | |
489 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, | |
490 int64_t id) | |
491 { | |
492 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
493 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
494 s.BindInt(0, id); | |
495 | |
496 result.clear(); | |
497 | |
498 while (s.Step()) | |
499 { | |
500 result.push_back(s.ColumnString(0)); | |
501 } | |
502 } | |
503 | |
504 | |
199 | 505 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result, |
506 int64_t id) | |
507 { | |
508 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " | |
509 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
510 s.BindInt(0, id); | |
511 | |
512 result.clear(); | |
513 | |
514 while (s.Step()) | |
515 { | |
516 result.push_back(s.ColumnInt(0)); | |
517 } | |
518 } | |
519 | |
520 | |
183 | 521 void DatabaseWrapper::LogChange(ChangeType changeType, |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
522 int64_t internalId, |
183 | 523 ResourceType resourceType, |
524 const boost::posix_time::ptime& date) | |
525 { | |
526 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); | |
527 s.BindInt(0, changeType); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
528 s.BindInt(1, internalId); |
183 | 529 s.BindInt(2, resourceType); |
530 s.BindString(3, boost::posix_time::to_iso_string(date)); | |
531 s.Run(); | |
532 } | |
533 | |
534 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
535 void DatabaseWrapper::GetChangesInternal(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
536 SQLite::Statement& s, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
537 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
538 unsigned int maxResults) |
204 | 539 { |
540 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
541 int64_t last = since; |
204 | 542 |
543 while (changes.size() < maxResults && s.Step()) | |
544 { | |
545 int64_t seq = s.ColumnInt(0); | |
546 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); | |
547 int64_t internalId = s.ColumnInt(2); | |
548 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); | |
549 const std::string& date = s.ColumnString(4); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
550 std::string publicId = GetPublicId(internalId); |
204 | 551 |
552 Json::Value item = Json::objectValue; | |
553 item["Seq"] = static_cast<int>(seq); | |
554 item["ChangeType"] = ToString(changeType); | |
555 item["ResourceType"] = ToString(resourceType); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
556 item["ID"] = publicId; |
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
557 item["Path"] = GetBasePath(resourceType, publicId); |
204 | 558 item["Date"] = date; |
559 last = seq; | |
560 | |
561 changes.append(item); | |
562 } | |
563 | |
564 target = Json::objectValue; | |
565 target["Changes"] = changes; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
566 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
567 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
568 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
569 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
570 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
571 void DatabaseWrapper::GetChanges(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
572 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
573 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
574 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
575 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
576 s.BindInt(0, since); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
577 s.BindInt(1, maxResults + 1); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
578 GetChangesInternal(target, s, since, maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
579 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
580 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
581 void DatabaseWrapper::GetLastChange(Json::Value& target) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
582 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
583 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
584 GetChangesInternal(target, s, 0, 1); |
204 | 585 } |
586 | |
587 | |
231 | 588 void DatabaseWrapper::LogExportedResource(ResourceType resourceType, |
589 const std::string& publicId, | |
590 const std::string& remoteModality, | |
591 const std::string& patientId, | |
592 const std::string& studyInstanceUid, | |
593 const std::string& seriesInstanceUid, | |
594 const std::string& sopInstanceUid, | |
183 | 595 const boost::posix_time::ptime& date) |
596 { | |
231 | 597 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
598 "INSERT INTO ExportedResources VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
599 | |
600 s.BindInt(0, resourceType); | |
601 s.BindString(1, publicId); | |
602 s.BindString(2, remoteModality); | |
603 s.BindString(3, patientId); | |
604 s.BindString(4, studyInstanceUid); | |
605 s.BindString(5, seriesInstanceUid); | |
606 s.BindString(6, sopInstanceUid); | |
607 s.BindString(7, boost::posix_time::to_iso_string(date)); | |
608 | |
183 | 609 s.Run(); |
610 } | |
231 | 611 |
612 | |
613 void DatabaseWrapper::GetExportedResources(Json::Value& target, | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
614 SQLite::Statement& s, |
231 | 615 int64_t since, |
616 unsigned int maxResults) | |
617 { | |
618 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
619 int64_t last = since; |
231 | 620 |
621 while (changes.size() < maxResults && s.Step()) | |
622 { | |
623 int64_t seq = s.ColumnInt(0); | |
624 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(1)); | |
625 std::string publicId = s.ColumnString(2); | |
626 | |
627 Json::Value item = Json::objectValue; | |
628 item["Seq"] = static_cast<int>(seq); | |
629 item["ResourceType"] = ToString(resourceType); | |
630 item["ID"] = publicId; | |
631 item["Path"] = GetBasePath(resourceType, publicId); | |
632 item["RemoteModality"] = s.ColumnString(3); | |
633 item["Date"] = s.ColumnString(8); | |
634 | |
635 // WARNING: Do not add "break" below and do not reorder the case items! | |
636 switch (resourceType) | |
637 { | |
638 case ResourceType_Instance: | |
639 item["SopInstanceUid"] = s.ColumnString(7); | |
640 | |
641 case ResourceType_Series: | |
642 item["SeriesInstanceUid"] = s.ColumnString(6); | |
643 | |
644 case ResourceType_Study: | |
645 item["StudyInstanceUid"] = s.ColumnString(5); | |
646 | |
647 case ResourceType_Patient: | |
648 item["PatientId"] = s.ColumnString(4); | |
649 break; | |
650 | |
651 default: | |
652 throw OrthancException(ErrorCode_InternalError); | |
653 } | |
654 | |
655 last = seq; | |
656 | |
657 changes.append(item); | |
658 } | |
659 | |
660 target = Json::objectValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
661 target["Exports"] = changes; |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
662 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
663 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
664 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
665 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
666 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
667 void DatabaseWrapper::GetExportedResources(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
668 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
669 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
670 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
671 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
672 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
673 s.BindInt(0, since); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
674 s.BindInt(1, maxResults + 1); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
675 GetExportedResources(target, s, since, maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
676 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
677 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
678 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
679 void DatabaseWrapper::GetLastExportedResource(Json::Value& target) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
680 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
681 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
682 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
683 GetExportedResources(target, s, 0, 1); |
231 | 684 } |
685 | |
686 | |
183 | 687 |
688 | |
689 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
690 { | |
691 char buf[128]; | |
692 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
693 SQLite::Statement s(db_, buf); | |
694 | |
218 | 695 if (!s.Step()) |
696 { | |
697 throw OrthancException(ErrorCode_InternalError); | |
698 } | |
699 | |
183 | 700 int64_t c = s.ColumnInt(0); |
701 assert(!s.Step()); | |
702 | |
703 return c; | |
704 } | |
705 | |
706 | |
707 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
708 { | |
709 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
710 s.Run(); | |
711 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
712 } | |
713 | |
714 | |
715 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
716 { | |
717 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
718 s.Run(); | |
719 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
720 } | |
721 | |
190 | 722 void DatabaseWrapper::GetAllPublicIds(Json::Value& target, |
723 ResourceType resourceType) | |
724 { | |
725 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
726 s.BindInt(0, resourceType); | |
727 | |
728 target = Json::arrayValue; | |
729 while (s.Step()) | |
730 { | |
731 target.append(s.ColumnString(0)); | |
732 } | |
733 } | |
734 | |
183 | 735 |
736 DatabaseWrapper::DatabaseWrapper(const std::string& path, | |
737 IServerIndexListener& listener) : | |
738 listener_(listener) | |
739 { | |
740 db_.Open(path); | |
741 Open(); | |
742 } | |
743 | |
744 DatabaseWrapper::DatabaseWrapper(IServerIndexListener& listener) : | |
745 listener_(listener) | |
746 { | |
747 db_.OpenInMemory(); | |
748 Open(); | |
749 } | |
750 | |
751 void DatabaseWrapper::Open() | |
752 { | |
753 if (!db_.DoesTableExist("GlobalProperties")) | |
754 { | |
755 LOG(INFO) << "Creating the database"; | |
756 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
757 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 758 db_.Execute(query); |
759 } | |
760 | |
252 | 761 // Sanity check of the version of the database |
762 std::string version = GetGlobalProperty(GlobalProperty_DatabaseSchemaVersion, "Unknown"); | |
763 bool ok = false; | |
764 try | |
765 { | |
766 LOG(INFO) << "Version of the Orthanc database: " << version; | |
767 unsigned int v = boost::lexical_cast<unsigned int>(version); | |
768 | |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
769 // This version of Orthanc is only compatible with version 3 of |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
770 // the DB schema (since Orthanc 0.3.2) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
771 ok = (v == 3); |
252 | 772 } |
773 catch (boost::bad_lexical_cast&) | |
774 { | |
775 } | |
776 | |
777 if (!ok) | |
778 { | |
779 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); | |
780 } | |
781 | |
183 | 782 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; |
783 db_.Register(signalRemainingAncestor_); | |
784 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
785 } | |
238 | 786 |
787 uint64_t DatabaseWrapper::GetResourceCount(ResourceType resourceType) | |
788 { | |
789 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
790 "SELECT COUNT(*) FROM Resources WHERE resourceType=?"); | |
791 s.BindInt(0, resourceType); | |
792 | |
793 if (!s.Step()) | |
794 { | |
795 throw OrthancException(ErrorCode_InternalError); | |
796 } | |
797 | |
798 int64_t c = s.ColumnInt(0); | |
799 assert(!s.Step()); | |
800 | |
801 return c; | |
802 } | |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
803 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
804 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
805 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
806 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
807 "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
|
808 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
809 if (!s.Step()) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
810 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
811 // No patient remaining or all the patients are protected |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
812 return false; |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
813 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
814 else |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
815 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
816 internalId = s.ColumnInt(0); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
817 return true; |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
818 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
819 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
820 |
268
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
821 bool DatabaseWrapper::SelectPatientToRecycle(int64_t& internalId, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
822 int64_t patientIdToAvoid) |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
823 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
824 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
825 "SELECT patientId FROM PatientRecyclingOrder " |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
826 "WHERE patientId != ? ORDER BY seq ASC LIMIT 1"); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
827 s.BindInt(0, patientIdToAvoid); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
828 |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
829 if (!s.Step()) |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
830 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
831 // No patient remaining or all the patients are protected |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
832 return false; |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
833 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
834 else |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
835 { |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
836 internalId = s.ColumnInt(0); |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
837 return true; |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
838 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
839 } |
4bc02e2254ec
preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
262
diff
changeset
|
840 |
262
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
841 bool DatabaseWrapper::IsProtectedPatient(int64_t internalId) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
842 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
843 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
844 "SELECT * FROM PatientRecyclingOrder WHERE patientId = ?"); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
845 s.BindInt(0, internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
846 return !s.Step(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
847 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
848 |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
849 void DatabaseWrapper::SetProtectedPatient(int64_t internalId, |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
850 bool isProtected) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
851 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
852 if (isProtected) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
853 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
854 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM PatientRecyclingOrder WHERE patientId=?"); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
855 s.BindInt(0, internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
856 s.Run(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
857 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
858 else if (IsProtectedPatient(internalId)) |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
859 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
860 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO PatientRecyclingOrder VALUES(NULL, ?)"); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
861 s.BindInt(0, internalId); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
862 s.Run(); |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
863 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
864 else |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
865 { |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
866 // Nothing to do: The patient is already unprotected |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
867 } |
2354560daf2f
primitives for recycling patients
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
252
diff
changeset
|
868 } |
310 | 869 |
870 | |
871 uint64_t DatabaseWrapper::IncrementGlobalSequence(GlobalProperty property) | |
872 { | |
873 std::string oldValue; | |
874 | |
875 if (LookupGlobalProperty(oldValue, property)) | |
876 { | |
877 uint64_t oldNumber; | |
878 | |
879 try | |
880 { | |
881 oldNumber = boost::lexical_cast<uint64_t>(oldValue); | |
882 SetGlobalProperty(property, boost::lexical_cast<std::string>(oldNumber + 1)); | |
883 return oldNumber + 1; | |
884 } | |
885 catch (boost::bad_lexical_cast&) | |
886 { | |
887 throw OrthancException(ErrorCode_InternalError); | |
888 } | |
889 } | |
890 else | |
891 { | |
892 // Initialize the sequence at "1" | |
893 SetGlobalProperty(property, "1"); | |
894 return 1; | |
895 } | |
896 } | |
183 | 897 } |