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