Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 1776:3c28f5f6c9b7
rename
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 13 Nov 2015 11:36:51 +0100 |
parents | 54d78925cbb6 |
children | 559956d5ceb2 |
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" |
1669
a412ad57f0f9
refactoring of sample plugins, OrthancPluginReconstructMainDicomTags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1668
diff
changeset
|
40 #include "ServerToolbox.h" |
183 | 41 |
42 #include <stdio.h> | |
1247 | 43 #include <boost/lexical_cast.hpp> |
183 | 44 |
45 namespace Orthanc | |
46 { | |
47 | |
48 namespace Internals | |
49 { | |
50 class SignalFileDeleted : public SQLite::IScalarFunction | |
51 { | |
52 private: | |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
53 IDatabaseListener& listener_; |
183 | 54 |
55 public: | |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
56 SignalFileDeleted(IDatabaseListener& listener) : |
183 | 57 listener_(listener) |
58 { | |
59 } | |
60 | |
61 virtual const char* GetName() const | |
62 { | |
63 return "SignalFileDeleted"; | |
64 } | |
65 | |
66 virtual unsigned int GetCardinality() const | |
67 { | |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
68 return 7; |
183 | 69 } |
70 | |
71 virtual void Compute(SQLite::FunctionContext& context) | |
72 { | |
694
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
73 std::string uncompressedMD5, compressedMD5; |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
74 |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
75 if (!context.IsNullValue(5)) |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
76 { |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
77 uncompressedMD5 = context.GetStringValue(5); |
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 |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
80 if (!context.IsNullValue(6)) |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
81 { |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
82 compressedMD5 = context.GetStringValue(6); |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
83 } |
72dc919a028c
upgrade database from v3 to v4
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
693
diff
changeset
|
84 |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
85 FileInfo info(context.GetStringValue(0), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
86 static_cast<FileContentType>(context.GetIntValue(1)), |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
87 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
|
88 uncompressedMD5, |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
89 static_cast<CompressionType>(context.GetIntValue(3)), |
693
01d8611c4a60
md5 for attached files
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
90 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
|
91 compressedMD5); |
273
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
92 |
d384af918264
more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
268
diff
changeset
|
93 listener_.SignalFileDeleted(info); |
183 | 94 } |
95 }; | |
96 | |
1158 | 97 class SignalResourceDeleted : public SQLite::IScalarFunction |
98 { | |
99 private: | |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
100 IDatabaseListener& listener_; |
1158 | 101 |
102 public: | |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
103 SignalResourceDeleted(IDatabaseListener& listener) : |
1158 | 104 listener_(listener) |
105 { | |
106 } | |
107 | |
108 virtual const char* GetName() const | |
109 { | |
110 return "SignalResourceDeleted"; | |
111 } | |
112 | |
113 virtual unsigned int GetCardinality() const | |
114 { | |
115 return 2; | |
116 } | |
117 | |
118 virtual void Compute(SQLite::FunctionContext& context) | |
119 { | |
120 ResourceType type = static_cast<ResourceType>(context.GetIntValue(1)); | |
1198 | 121 ServerIndexChange change(ChangeType_Deleted, type, context.GetStringValue(0)); |
122 listener_.SignalChange(change); | |
1158 | 123 } |
124 }; | |
125 | |
183 | 126 class SignalRemainingAncestor : public SQLite::IScalarFunction |
127 { | |
128 private: | |
129 bool hasRemainingAncestor_; | |
130 std::string remainingPublicId_; | |
131 ResourceType remainingType_; | |
132 | |
133 public: | |
660 | 134 SignalRemainingAncestor() : |
135 hasRemainingAncestor_(false) | |
136 { | |
137 } | |
138 | |
183 | 139 void Reset() |
140 { | |
141 hasRemainingAncestor_ = false; | |
142 } | |
143 | |
144 virtual const char* GetName() const | |
145 { | |
146 return "SignalRemainingAncestor"; | |
147 } | |
148 | |
149 virtual unsigned int GetCardinality() const | |
150 { | |
151 return 2; | |
152 } | |
153 | |
154 virtual void Compute(SQLite::FunctionContext& context) | |
155 { | |
156 VLOG(1) << "There exists a remaining ancestor with public ID \"" | |
157 << context.GetStringValue(0) | |
158 << "\" of type " | |
159 << context.GetIntValue(1); | |
160 | |
161 if (!hasRemainingAncestor_ || | |
162 remainingType_ >= context.GetIntValue(1)) | |
163 { | |
164 hasRemainingAncestor_ = true; | |
165 remainingPublicId_ = context.GetStringValue(0); | |
166 remainingType_ = static_cast<ResourceType>(context.GetIntValue(1)); | |
167 } | |
168 } | |
169 | |
170 bool HasRemainingAncestor() const | |
171 { | |
172 return hasRemainingAncestor_; | |
173 } | |
174 | |
175 const std::string& GetRemainingAncestorId() const | |
176 { | |
177 assert(hasRemainingAncestor_); | |
178 return remainingPublicId_; | |
179 } | |
180 | |
181 ResourceType GetRemainingAncestorType() const | |
182 { | |
183 assert(hasRemainingAncestor_); | |
184 return remainingType_; | |
185 } | |
186 }; | |
187 } | |
188 | |
189 | |
1241 | 190 |
191 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
|
192 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
193 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
194 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); |
585 | 195 s.BindInt64(0, id); |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
196 |
1241 | 197 childrenPublicIds.clear(); |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
198 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
199 { |
1241 | 200 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
|
201 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
202 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
203 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
204 |
183 | 205 void DatabaseWrapper::DeleteResource(int64_t id) |
206 { | |
207 signalRemainingAncestor_->Reset(); | |
208 | |
209 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
585 | 210 s.BindInt64(0, id); |
183 | 211 s.Run(); |
212 | |
1247 | 213 if (signalRemainingAncestor_->HasRemainingAncestor() && |
214 listener_ != NULL) | |
183 | 215 { |
1247 | 216 listener_->SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), |
217 signalRemainingAncestor_->GetRemainingAncestorId()); | |
183 | 218 } |
219 } | |
220 | |
221 | |
1301 | 222 bool DatabaseWrapper::GetParentPublicId(std::string& target, |
183 | 223 int64_t id) |
224 { | |
225 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
226 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
585 | 227 s.BindInt64(0, id); |
183 | 228 |
229 if (s.Step()) | |
230 { | |
1301 | 231 target = s.ColumnString(0); |
183 | 232 return true; |
233 } | |
234 else | |
235 { | |
236 return false; | |
237 } | |
238 } | |
239 | |
240 | |
241 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
242 { | |
243 char buf[128]; | |
244 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
245 SQLite::Statement s(db_, buf); | |
246 | |
218 | 247 if (!s.Step()) |
248 { | |
249 throw OrthancException(ErrorCode_InternalError); | |
250 } | |
251 | |
183 | 252 int64_t c = s.ColumnInt(0); |
253 assert(!s.Step()); | |
254 | |
255 return c; | |
256 } | |
257 | |
258 | |
1670
16955f8fec4d
refactoring: DatabaseWrapperBase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1669
diff
changeset
|
259 DatabaseWrapper::DatabaseWrapper(const std::string& path) : listener_(NULL), base_(db_) |
183 | 260 { |
261 db_.Open(path); | |
262 } | |
263 | |
1670
16955f8fec4d
refactoring: DatabaseWrapperBase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1669
diff
changeset
|
264 DatabaseWrapper::DatabaseWrapper() : listener_(NULL), base_(db_) |
183 | 265 { |
266 db_.OpenInMemory(); | |
267 } | |
268 | |
269 void DatabaseWrapper::Open() | |
270 { | |
1717
3926e6317a43
SetIdentifierTagInternal
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1712
diff
changeset
|
271 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); |
3926e6317a43
SetIdentifierTagInternal
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1712
diff
changeset
|
272 |
374 | 273 // Performance tuning of SQLite with PRAGMAs |
274 // http://www.sqlite.org/pragma.html | |
275 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;"); | |
276 db_.Execute("PRAGMA JOURNAL_MODE=WAL;"); | |
277 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); | |
278 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); | |
279 //db_.Execute("PRAGMA TEMP_STORE=memory"); | |
280 | |
183 | 281 if (!db_.DoesTableExist("GlobalProperties")) |
282 { | |
283 LOG(INFO) << "Creating the database"; | |
284 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
285 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 286 db_.Execute(query); |
287 } | |
288 | |
567 | 289 // 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
|
290 std::string tmp; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
291 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion)) |
1239 | 292 { |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
293 tmp = "Unknown"; |
1239 | 294 } |
295 | |
252 | 296 bool ok = false; |
297 try | |
298 { | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
299 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
|
300 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
|
301 ok = true; |
252 | 302 } |
303 catch (boost::bad_lexical_cast&) | |
304 { | |
305 } | |
306 | |
307 if (!ok) | |
308 { | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
309 LOG(ERROR) << "Incompatible version of the Orthanc database: " << tmp; |
252 | 310 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
311 } | |
312 | |
183 | 313 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; |
314 db_.Register(signalRemainingAncestor_); | |
1247 | 315 } |
316 | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
317 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
318 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
|
319 EmbeddedResources::FileResourceId script) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
320 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
321 std::string upgrade; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
322 EmbeddedResources::GetFileResource(upgrade, script); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
323 db.BeginTransaction(); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
324 db.Execute(upgrade); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
325 db.CommitTransaction(); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
326 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
327 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
328 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
329 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
|
330 IStorageArea& storageArea) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
331 { |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
332 if (targetVersion != 6) |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
333 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
334 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
335 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
336 |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
337 // 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
|
338 // 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
|
339 if (version_ != 3 && |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
340 version_ != 4 && |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
341 version_ != 5 && |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
342 version_ != 6) |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
343 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
344 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
345 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
346 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
347 if (version_ == 3) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
348 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
349 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
|
350 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
|
351 version_ = 4; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
352 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
353 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
354 if (version_ == 4) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
355 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
356 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
|
357 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
|
358 version_ = 5; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
359 } |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
360 |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
361 if (version_ == 5) |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
362 { |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
363 LOG(WARNING) << "Upgrading database version from 5 to 6"; |
1668
de1413733c97
reconstructing main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1665
diff
changeset
|
364 // No change in the DB schema, the step from version 5 to 6 only |
1710 | 365 // consists in reconstructing the main DICOM tags information |
366 // (as more tags got included). | |
1668
de1413733c97
reconstructing main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1665
diff
changeset
|
367 db_.BeginTransaction(); |
1710 | 368 Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Patient); |
1669
a412ad57f0f9
refactoring of sample plugins, OrthancPluginReconstructMainDicomTags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1668
diff
changeset
|
369 Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Study); |
a412ad57f0f9
refactoring of sample plugins, OrthancPluginReconstructMainDicomTags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1668
diff
changeset
|
370 Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Series); |
1710 | 371 Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Instance); |
372 db_.Execute("UPDATE GlobalProperties SET value=\"6\" WHERE property=" + | |
373 boost::lexical_cast<std::string>(GlobalProperty_DatabaseSchemaVersion) + ";"); | |
1668
de1413733c97
reconstructing main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1665
diff
changeset
|
374 db_.CommitTransaction(); |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
375 version_ = 6; |
1729 | 376 } |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
377 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
378 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
379 |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
380 void DatabaseWrapper::SetListener(IDatabaseListener& listener) |
1247 | 381 { |
382 listener_ = &listener; | |
383 db_.Register(new Internals::SignalFileDeleted(listener)); | |
384 db_.Register(new Internals::SignalResourceDeleted(listener)); | |
183 | 385 } |
238 | 386 |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
387 |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
388 void DatabaseWrapper::ClearTable(const std::string& tableName) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
389 { |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
390 db_.Execute("DELETE FROM " + tableName); |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
391 } |
513 | 392 |
393 | |
1671
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
394 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
395 int64_t resourceId) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
396 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
397 bool found; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
398 ErrorCode error = base_.LookupParent(found, parentId, resourceId); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
399 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
400 if (error != ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
401 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
402 throw OrthancException(error); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
403 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
404 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
405 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
406 return found; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
407 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
408 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
409 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
410 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
411 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
412 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
413 ResourceType result; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
414 ErrorCode code = base_.GetResourceType(result, resourceId); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
415 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
416 if (code == ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
417 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
418 return result; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
419 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
420 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
421 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
422 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
423 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
424 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
425 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
426 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
427 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
428 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
429 std::string id; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
430 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
431 if (base_.GetPublicId(id, resourceId)) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
432 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
433 return id; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
434 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
435 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
436 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
437 throw OrthancException(ErrorCode_UnknownResource); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
438 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
439 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
440 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
441 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
442 void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& target /*out*/, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
443 bool& done /*out*/, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
444 int64_t since, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
445 uint32_t maxResults) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
446 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
447 ErrorCode code = base_.GetChanges(target, done, since, maxResults); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
448 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
449 if (code != ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
450 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
451 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
452 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
453 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
454 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
455 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
456 void DatabaseWrapper::GetLastChange(std::list<ServerIndexChange>& target /*out*/) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
457 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
458 ErrorCode code = base_.GetLastChange(target); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
459 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
460 if (code != ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
461 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
462 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
463 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
464 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
465 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
466 |
1301 | 467 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
|
468 int64_t id) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
469 { |
1301 | 470 target.clear(); |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
471 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
472 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
|
473 s.BindInt64(0, id); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
474 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
475 while (s.Step()) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
476 { |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
477 MetadataType key = static_cast<MetadataType>(s.ColumnInt(0)); |
1301 | 478 target[key] = s.ColumnString(1); |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
479 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
480 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
481 |
183 | 482 } |