Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 224:4eb0c7ce86c9
refactoring for store
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 29 Nov 2012 22:22:00 +0100 |
parents | 0200cd330582 |
children | 8098448bd827 |
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 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
133 void DatabaseWrapper::SetGlobalProperty(GlobalProperty property, |
183 | 134 const std::string& value) |
135 { | |
136 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
|
137 s.BindInt(0, property); |
183 | 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, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
143 GlobalProperty property) |
183 | 144 { |
145 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
146 "SELECT value FROM GlobalProperties WHERE property=?"); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
147 s.BindInt(0, property); |
183 | 148 |
149 if (!s.Step()) | |
150 { | |
151 return false; | |
152 } | |
153 else | |
154 { | |
155 target = s.ColumnString(0); | |
156 return true; | |
157 } | |
158 } | |
159 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
160 std::string DatabaseWrapper::GetGlobalProperty(GlobalProperty property, |
183 | 161 const std::string& defaultValue) |
162 { | |
163 std::string s; | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
164 if (LookupGlobalProperty(s, property)) |
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 | |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
234 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
235 int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
236 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
237 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
238 "SELECT parentId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
239 s.BindInt(0, resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
240 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
241 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
242 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
243 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
244 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
245 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
246 if (s.ColumnIsNull(0)) |
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 return false; |
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 else |
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 parentId = s.ColumnInt(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
253 return true; |
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 } |
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 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
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 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
260 "SELECT publicId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
261 s.BindInt(0, resourceId); |
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 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
264 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
265 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
266 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
267 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
268 return s.ColumnString(0); |
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 |
183 | 271 void DatabaseWrapper::AttachChild(int64_t parent, |
272 int64_t child) | |
273 { | |
274 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
275 s.BindInt(0, parent); | |
276 s.BindInt(1, child); | |
277 s.Run(); | |
278 } | |
279 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
280 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
|
281 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
282 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
283 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
|
284 s.BindInt(0, id); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
285 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
286 childrenPublicIds = Json::arrayValue; |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
287 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
288 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
289 childrenPublicIds.append(s.ColumnString(0)); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
290 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
291 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
292 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
293 |
183 | 294 void DatabaseWrapper::DeleteResource(int64_t id) |
295 { | |
296 signalRemainingAncestor_->Reset(); | |
297 | |
298 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
299 s.BindInt(0, id); | |
300 s.Run(); | |
301 | |
302 if (signalRemainingAncestor_->HasRemainingAncestor()) | |
303 { | |
304 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), | |
305 signalRemainingAncestor_->GetRemainingAncestorId()); | |
306 } | |
307 } | |
308 | |
309 void DatabaseWrapper::SetMetadata(int64_t id, | |
310 MetadataType type, | |
311 const std::string& value) | |
312 { | |
313 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
314 s.BindInt(0, id); | |
315 s.BindInt(1, type); | |
316 s.BindString(2, value); | |
317 s.Run(); | |
318 } | |
319 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
320 bool DatabaseWrapper::LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
321 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
322 MetadataType type) |
183 | 323 { |
324 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
325 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
326 s.BindInt(0, id); | |
327 s.BindInt(1, type); | |
328 | |
329 if (!s.Step()) | |
330 { | |
331 return false; | |
332 } | |
333 else | |
334 { | |
335 target = s.ColumnString(0); | |
336 return true; | |
337 } | |
338 } | |
339 | |
340 std::string DatabaseWrapper::GetMetadata(int64_t id, | |
341 MetadataType type, | |
342 const std::string& defaultValue) | |
343 { | |
344 std::string s; | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
345 if (LookupMetadata(s, id, type)) |
183 | 346 { |
347 return s; | |
348 } | |
349 else | |
350 { | |
351 return defaultValue; | |
352 } | |
353 } | |
354 | |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
355 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
356 bool DatabaseWrapper::GetMetadataAsInteger(int& result, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
357 int64_t id, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
358 MetadataType type) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
359 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
360 std::string s = GetMetadata(id, type, ""); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
361 if (s.size() == 0) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
362 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
363 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
364 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
365 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
366 try |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
367 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
368 result = boost::lexical_cast<int>(s); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
369 return true; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
370 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
371 catch (boost::bad_lexical_cast&) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
372 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
373 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
374 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
375 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
376 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
377 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
378 |
183 | 379 void DatabaseWrapper::AttachFile(int64_t id, |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
380 AttachedFileType contentType, |
183 | 381 const std::string& fileUuid, |
187
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
382 uint64_t compressedSize, |
8e673a65564d
refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
183
diff
changeset
|
383 uint64_t uncompressedSize, |
183 | 384 CompressionType compressionType) |
385 { | |
386 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)"); | |
387 s.BindInt(0, id); | |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
388 s.BindInt(1, contentType); |
183 | 389 s.BindString(2, fileUuid); |
390 s.BindInt(3, compressedSize); | |
391 s.BindInt(4, uncompressedSize); | |
392 s.BindInt(5, compressionType); | |
393 s.Run(); | |
394 } | |
395 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
396 bool DatabaseWrapper::LookupFile(int64_t id, |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
397 AttachedFileType contentType, |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
398 std::string& fileUuid, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
399 uint64_t& compressedSize, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
400 uint64_t& uncompressedSize, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
401 CompressionType& compressionType) |
183 | 402 { |
403 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
404 "SELECT uuid, compressedSize, uncompressedSize, compressionType FROM AttachedFiles WHERE id=? AND fileType=?"); |
183 | 405 s.BindInt(0, id); |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
406 s.BindInt(1, contentType); |
183 | 407 |
408 if (!s.Step()) | |
409 { | |
410 return false; | |
411 } | |
412 else | |
413 { | |
414 fileUuid = s.ColumnString(0); | |
415 compressedSize = s.ColumnInt(1); | |
416 uncompressedSize = s.ColumnInt(2); | |
417 compressionType = static_cast<CompressionType>(s.ColumnInt(3)); | |
418 return true; | |
419 } | |
420 } | |
421 | |
422 void DatabaseWrapper::SetMainDicomTags(int64_t id, | |
423 const DicomMap& tags) | |
424 { | |
425 DicomArray flattened(tags); | |
426 for (size_t i = 0; i < flattened.GetSize(); i++) | |
427 { | |
428 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
429 s.BindInt(0, id); | |
430 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); | |
431 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); | |
432 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); | |
433 s.Run(); | |
434 } | |
435 } | |
436 | |
437 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
438 int64_t id) | |
439 { | |
440 map.Clear(); | |
441 | |
442 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
443 s.BindInt(0, id); | |
444 while (s.Step()) | |
445 { | |
446 map.SetValue(s.ColumnInt(1), | |
447 s.ColumnInt(2), | |
448 s.ColumnString(3)); | |
449 } | |
450 } | |
451 | |
452 | |
453 bool DatabaseWrapper::GetParentPublicId(std::string& result, | |
454 int64_t id) | |
455 { | |
456 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
457 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
458 s.BindInt(0, id); | |
459 | |
460 if (s.Step()) | |
461 { | |
462 result = s.ColumnString(0); | |
463 return true; | |
464 } | |
465 else | |
466 { | |
467 return false; | |
468 } | |
469 } | |
470 | |
471 | |
472 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, | |
473 int64_t id) | |
474 { | |
475 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
476 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
477 s.BindInt(0, id); | |
478 | |
479 result.clear(); | |
480 | |
481 while (s.Step()) | |
482 { | |
483 result.push_back(s.ColumnString(0)); | |
484 } | |
485 } | |
486 | |
487 | |
199 | 488 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result, |
489 int64_t id) | |
490 { | |
491 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " | |
492 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
493 s.BindInt(0, id); | |
494 | |
495 result.clear(); | |
496 | |
497 while (s.Step()) | |
498 { | |
499 result.push_back(s.ColumnInt(0)); | |
500 } | |
501 } | |
502 | |
503 | |
183 | 504 void DatabaseWrapper::LogChange(ChangeType changeType, |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
505 int64_t internalId, |
183 | 506 ResourceType resourceType, |
507 const boost::posix_time::ptime& date) | |
508 { | |
509 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); | |
510 s.BindInt(0, changeType); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
511 s.BindInt(1, internalId); |
183 | 512 s.BindInt(2, resourceType); |
513 s.BindString(3, boost::posix_time::to_iso_string(date)); | |
514 s.Run(); | |
515 } | |
516 | |
517 | |
204 | 518 void DatabaseWrapper::GetChanges(Json::Value& target, |
519 int64_t since, | |
520 unsigned int maxResults) | |
521 { | |
522 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); | |
523 s.BindInt(0, since); | |
524 s.BindInt(1, maxResults + 1); | |
525 | |
526 Json::Value changes = Json::arrayValue; | |
527 int64_t last = 0; | |
528 | |
529 while (changes.size() < maxResults && s.Step()) | |
530 { | |
531 int64_t seq = s.ColumnInt(0); | |
532 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); | |
533 int64_t internalId = s.ColumnInt(2); | |
534 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); | |
535 const std::string& date = s.ColumnString(4); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
536 std::string publicId = GetPublicId(internalId); |
204 | 537 |
538 Json::Value item = Json::objectValue; | |
539 item["Seq"] = static_cast<int>(seq); | |
540 item["ChangeType"] = ToString(changeType); | |
541 item["ResourceType"] = ToString(resourceType); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
542 item["ID"] = publicId; |
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
543 item["Path"] = GetBasePath(resourceType, publicId); |
204 | 544 item["Date"] = date; |
545 last = seq; | |
546 | |
547 changes.append(item); | |
548 } | |
549 | |
550 target = Json::objectValue; | |
551 target["Changes"] = changes; | |
552 target["PendingChanges"] = (changes.size() == maxResults && s.Step()); | |
553 target["LastSeq"] = static_cast<int>(last); | |
554 } | |
555 | |
556 | |
183 | 557 void DatabaseWrapper::LogExportedInstance(const std::string& remoteModality, |
558 DicomInstanceHasher& hasher, | |
559 const boost::posix_time::ptime& date) | |
560 { | |
561 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO ExportedInstances VALUES(NULL, ?, ?, ?, ?, ?, ?)"); | |
562 s.BindString(0, remoteModality); | |
563 s.BindString(1, hasher.HashInstance()); | |
564 s.BindString(2, hasher.GetPatientId()); | |
565 s.BindString(3, hasher.GetStudyUid()); | |
566 s.BindString(4, hasher.GetSeriesUid()); | |
567 s.BindString(5, hasher.GetInstanceUid()); | |
568 s.BindString(6, boost::posix_time::to_iso_string(date)); | |
569 s.Run(); | |
570 } | |
571 | |
572 | |
573 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
574 { | |
575 char buf[128]; | |
576 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
577 SQLite::Statement s(db_, buf); | |
578 | |
218 | 579 if (!s.Step()) |
580 { | |
581 throw OrthancException(ErrorCode_InternalError); | |
582 } | |
583 | |
183 | 584 int64_t c = s.ColumnInt(0); |
585 assert(!s.Step()); | |
586 | |
587 return c; | |
588 } | |
589 | |
590 | |
591 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
592 { | |
593 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
594 s.Run(); | |
595 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
596 } | |
597 | |
598 | |
599 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
600 { | |
601 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
602 s.Run(); | |
603 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
604 } | |
605 | |
190 | 606 void DatabaseWrapper::GetAllPublicIds(Json::Value& target, |
607 ResourceType resourceType) | |
608 { | |
609 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
610 s.BindInt(0, resourceType); | |
611 | |
612 target = Json::arrayValue; | |
613 while (s.Step()) | |
614 { | |
615 target.append(s.ColumnString(0)); | |
616 } | |
617 } | |
618 | |
183 | 619 |
620 DatabaseWrapper::DatabaseWrapper(const std::string& path, | |
621 IServerIndexListener& listener) : | |
622 listener_(listener) | |
623 { | |
624 db_.Open(path); | |
625 Open(); | |
626 } | |
627 | |
628 DatabaseWrapper::DatabaseWrapper(IServerIndexListener& listener) : | |
629 listener_(listener) | |
630 { | |
631 db_.OpenInMemory(); | |
632 Open(); | |
633 } | |
634 | |
635 void DatabaseWrapper::Open() | |
636 { | |
637 if (!db_.DoesTableExist("GlobalProperties")) | |
638 { | |
639 LOG(INFO) << "Creating the database"; | |
640 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
641 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 642 db_.Execute(query); |
643 } | |
644 | |
645 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; | |
646 db_.Register(signalRemainingAncestor_); | |
647 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
648 } | |
649 } |