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