Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 2401:a051aba0037c
Use "GBK" (frequently used in China) as an alias for "GB18030"
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 02 Sep 2017 11:50:08 +0200 |
parents | a3a65de1840f |
children | 878b59270859 |
rev | line source |
---|---|
183 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1286
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
183 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
34 #include "PrecompiledHeadersServer.h" |
183 | 35 #include "DatabaseWrapper.h" |
36 | |
37 #include "../Core/DicomFormat/DicomArray.h" | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1432
diff
changeset
|
38 #include "../Core/Logging.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 | |
1847 | 259 DatabaseWrapper::DatabaseWrapper(const std::string& path) : |
260 listener_(NULL), | |
261 base_(db_), | |
262 signalRemainingAncestor_(NULL), | |
263 version_(0) | |
183 | 264 { |
265 db_.Open(path); | |
266 } | |
267 | |
1847 | 268 DatabaseWrapper::DatabaseWrapper() : |
269 listener_(NULL), | |
270 base_(db_), | |
271 signalRemainingAncestor_(NULL), | |
272 version_(0) | |
183 | 273 { |
274 db_.OpenInMemory(); | |
275 } | |
276 | |
277 void DatabaseWrapper::Open() | |
278 { | |
1717
3926e6317a43
SetIdentifierTagInternal
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1712
diff
changeset
|
279 db_.Execute("PRAGMA ENCODING=\"UTF-8\";"); |
3926e6317a43
SetIdentifierTagInternal
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1712
diff
changeset
|
280 |
374 | 281 // Performance tuning of SQLite with PRAGMAs |
282 // http://www.sqlite.org/pragma.html | |
283 db_.Execute("PRAGMA SYNCHRONOUS=NORMAL;"); | |
284 db_.Execute("PRAGMA JOURNAL_MODE=WAL;"); | |
285 db_.Execute("PRAGMA LOCKING_MODE=EXCLUSIVE;"); | |
286 db_.Execute("PRAGMA WAL_AUTOCHECKPOINT=1000;"); | |
287 //db_.Execute("PRAGMA TEMP_STORE=memory"); | |
288 | |
183 | 289 if (!db_.DoesTableExist("GlobalProperties")) |
290 { | |
291 LOG(INFO) << "Creating the database"; | |
292 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
293 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 294 db_.Execute(query); |
295 } | |
296 | |
567 | 297 // 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
|
298 std::string tmp; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
299 if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion)) |
1239 | 300 { |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
301 tmp = "Unknown"; |
1239 | 302 } |
303 | |
252 | 304 bool ok = false; |
305 try | |
306 { | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
307 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
|
308 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
|
309 ok = true; |
252 | 310 } |
311 catch (boost::bad_lexical_cast&) | |
312 { | |
313 } | |
314 | |
315 if (!ok) | |
316 { | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
317 LOG(ERROR) << "Incompatible version of the Orthanc database: " << tmp; |
252 | 318 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
319 } | |
320 | |
183 | 321 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; |
322 db_.Register(signalRemainingAncestor_); | |
1247 | 323 } |
324 | |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
325 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
326 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
|
327 EmbeddedResources::FileResourceId script) |
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 std::string upgrade; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
330 EmbeddedResources::GetFileResource(upgrade, script); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
331 db.BeginTransaction(); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
332 db.Execute(upgrade); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
333 db.CommitTransaction(); |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
334 } |
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 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
337 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
|
338 IStorageArea& storageArea) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
339 { |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
340 if (targetVersion != 6) |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
341 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
342 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
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 |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
345 // 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
|
346 // 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
|
347 if (version_ != 3 && |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
348 version_ != 4 && |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
349 version_ != 5 && |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
350 version_ != 6) |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
351 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
352 throw OrthancException(ErrorCode_IncompatibleDatabaseVersion); |
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 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
355 if (version_ == 3) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
356 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
357 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
|
358 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
|
359 version_ = 4; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
360 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
361 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
362 if (version_ == 4) |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
363 { |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
364 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
|
365 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
|
366 version_ = 5; |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
367 } |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
368 |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
369 if (version_ == 5) |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
370 { |
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
371 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
|
372 // No change in the DB schema, the step from version 5 to 6 only |
1710 | 373 // consists in reconstructing the main DICOM tags information |
374 // (as more tags got included). | |
1668
de1413733c97
reconstructing main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1665
diff
changeset
|
375 db_.BeginTransaction(); |
2120 | 376 ServerToolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Patient); |
377 ServerToolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Study); | |
378 ServerToolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Series); | |
379 ServerToolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Instance); | |
1710 | 380 db_.Execute("UPDATE GlobalProperties SET value=\"6\" WHERE property=" + |
381 boost::lexical_cast<std::string>(GlobalProperty_DatabaseSchemaVersion) + ";"); | |
1668
de1413733c97
reconstructing main dicom tags
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1665
diff
changeset
|
382 db_.CommitTransaction(); |
1664
0c58f189782d
prepare migration to db v6
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1615
diff
changeset
|
383 version_ = 6; |
1729 | 384 } |
1615
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
385 } |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
386 |
c40fe92a68e7
Primitives to upgrade the database version in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1509
diff
changeset
|
387 |
1432
0ac74fa21db8
rename IServerIndexListener as IDatabaseListener
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1407
diff
changeset
|
388 void DatabaseWrapper::SetListener(IDatabaseListener& listener) |
1247 | 389 { |
390 listener_ = &listener; | |
391 db_.Register(new Internals::SignalFileDeleted(listener)); | |
392 db_.Register(new Internals::SignalResourceDeleted(listener)); | |
183 | 393 } |
238 | 394 |
413
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
395 |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
396 void DatabaseWrapper::ClearTable(const std::string& tableName) |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
397 { |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
398 db_.Execute("DELETE FROM " + tableName); |
47d63c941902
clearing /exports and /changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
399
diff
changeset
|
399 } |
513 | 400 |
401 | |
1671
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
402 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
403 int64_t resourceId) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
404 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
405 bool found; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
406 ErrorCode error = base_.LookupParent(found, parentId, resourceId); |
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 if (error != ErrorCode_Success) |
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 throw OrthancException(error); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
411 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
412 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
413 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
414 return found; |
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 } |
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 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
419 ResourceType DatabaseWrapper::GetResourceType(int64_t resourceId) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
420 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
421 ResourceType result; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
422 ErrorCode code = base_.GetResourceType(result, resourceId); |
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 if (code == ErrorCode_Success) |
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 return result; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
427 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
428 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
429 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
430 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
431 } |
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 |
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 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
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 std::string id; |
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 if (base_.GetPublicId(id, resourceId)) |
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 return id; |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
442 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
443 else |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
444 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
445 throw OrthancException(ErrorCode_UnknownResource); |
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 } |
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 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
450 void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& target /*out*/, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
451 bool& done /*out*/, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
452 int64_t since, |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
453 uint32_t maxResults) |
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 ErrorCode code = base_.GetChanges(target, done, since, maxResults); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
456 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
457 if (code != ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
458 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
459 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
460 } |
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 |
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 void DatabaseWrapper::GetLastChange(std::list<ServerIndexChange>& target /*out*/) |
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 ErrorCode code = base_.GetLastChange(target); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
467 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
468 if (code != ErrorCode_Success) |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
469 { |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
470 throw OrthancException(code); |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
471 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
472 } |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
473 |
2f2e2ec17bc4
sample database plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1670
diff
changeset
|
474 |
1301 | 475 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
|
476 int64_t id) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
477 { |
1301 | 478 target.clear(); |
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 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
|
481 s.BindInt64(0, id); |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
482 |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
483 while (s.Step()) |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
484 { |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
485 MetadataType key = static_cast<MetadataType>(s.ColumnInt(0)); |
1301 | 486 target[key] = s.ColumnString(1); |
1006
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
487 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
488 } |
649d47854314
proper handling of metadata in Store
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
831
diff
changeset
|
489 |
183 | 490 } |