Mercurial > hg > orthanc
annotate OrthancServer/DatabaseWrapper.cpp @ 238:e4148b0ab1d0
statistics URI
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 30 Nov 2012 16:09:24 +0100 |
parents | 16a4ac70bd8a |
children | bd009f0b1931 |
rev | line source |
---|---|
183 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
4 * Belgium | |
5 * | |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
33 #include "DatabaseWrapper.h" | |
34 | |
35 #include "../Core/DicomFormat/DicomArray.h" | |
36 #include "EmbeddedResources.h" | |
37 | |
38 #include <glog/logging.h> | |
39 #include <stdio.h> | |
40 | |
41 namespace Orthanc | |
42 { | |
43 | |
44 namespace Internals | |
45 { | |
46 class SignalFileDeleted : public SQLite::IScalarFunction | |
47 { | |
48 private: | |
49 IServerIndexListener& listener_; | |
50 | |
51 public: | |
52 SignalFileDeleted(IServerIndexListener& listener) : | |
53 listener_(listener) | |
54 { | |
55 } | |
56 | |
57 virtual const char* GetName() const | |
58 { | |
59 return "SignalFileDeleted"; | |
60 } | |
61 | |
62 virtual unsigned int GetCardinality() const | |
63 { | |
64 return 1; | |
65 } | |
66 | |
67 virtual void Compute(SQLite::FunctionContext& context) | |
68 { | |
69 listener_.SignalFileDeleted(context.GetStringValue(0)); | |
70 } | |
71 }; | |
72 | |
73 class SignalRemainingAncestor : public SQLite::IScalarFunction | |
74 { | |
75 private: | |
76 bool hasRemainingAncestor_; | |
77 std::string remainingPublicId_; | |
78 ResourceType remainingType_; | |
79 | |
80 public: | |
81 void Reset() | |
82 { | |
83 hasRemainingAncestor_ = false; | |
84 } | |
85 | |
86 virtual const char* GetName() const | |
87 { | |
88 return "SignalRemainingAncestor"; | |
89 } | |
90 | |
91 virtual unsigned int GetCardinality() const | |
92 { | |
93 return 2; | |
94 } | |
95 | |
96 virtual void Compute(SQLite::FunctionContext& context) | |
97 { | |
98 VLOG(1) << "There exists a remaining ancestor with public ID \"" | |
99 << context.GetStringValue(0) | |
100 << "\" of type " | |
101 << context.GetIntValue(1); | |
102 | |
103 if (!hasRemainingAncestor_ || | |
104 remainingType_ >= context.GetIntValue(1)) | |
105 { | |
106 hasRemainingAncestor_ = true; | |
107 remainingPublicId_ = context.GetStringValue(0); | |
108 remainingType_ = static_cast<ResourceType>(context.GetIntValue(1)); | |
109 } | |
110 } | |
111 | |
112 bool HasRemainingAncestor() const | |
113 { | |
114 return hasRemainingAncestor_; | |
115 } | |
116 | |
117 const std::string& GetRemainingAncestorId() const | |
118 { | |
119 assert(hasRemainingAncestor_); | |
120 return remainingPublicId_; | |
121 } | |
122 | |
123 ResourceType GetRemainingAncestorType() const | |
124 { | |
125 assert(hasRemainingAncestor_); | |
126 return remainingType_; | |
127 } | |
128 }; | |
129 } | |
130 | |
131 | |
132 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
133 void DatabaseWrapper::SetGlobalProperty(GlobalProperty property, |
183 | 134 const std::string& value) |
135 { | |
136 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO GlobalProperties VALUES(?, ?)"); | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
137 s.BindInt(0, property); |
183 | 138 s.BindString(1, value); |
139 s.Run(); | |
140 } | |
141 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
142 bool DatabaseWrapper::LookupGlobalProperty(std::string& target, |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
143 GlobalProperty property) |
183 | 144 { |
145 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
146 "SELECT value FROM GlobalProperties WHERE property=?"); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
147 s.BindInt(0, property); |
183 | 148 |
149 if (!s.Step()) | |
150 { | |
151 return false; | |
152 } | |
153 else | |
154 { | |
155 target = s.ColumnString(0); | |
156 return true; | |
157 } | |
158 } | |
159 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
160 std::string DatabaseWrapper::GetGlobalProperty(GlobalProperty property, |
183 | 161 const std::string& defaultValue) |
162 { | |
163 std::string s; | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
205
diff
changeset
|
164 if (LookupGlobalProperty(s, property)) |
183 | 165 { |
166 return s; | |
167 } | |
168 else | |
169 { | |
170 return defaultValue; | |
171 } | |
172 } | |
173 | |
174 int64_t DatabaseWrapper::CreateResource(const std::string& publicId, | |
175 ResourceType type) | |
176 { | |
177 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(NULL, ?, ?, NULL)"); | |
178 s.BindInt(0, type); | |
179 s.BindString(1, publicId); | |
180 s.Run(); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
181 int64_t id = db_.GetLastInsertRowId(); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
182 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
183 ChangeType changeType; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
184 switch (type) |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
185 { |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
186 case ResourceType_Patient: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
187 changeType = ChangeType_NewPatient; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
188 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
189 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
190 case ResourceType_Study: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
191 changeType = ChangeType_NewStudy; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
192 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
193 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
194 case ResourceType_Series: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
195 changeType = ChangeType_NewSeries; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
196 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
197 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
198 case ResourceType_Instance: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
199 changeType = ChangeType_NewInstance; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
200 break; |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
201 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
202 default: |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
203 throw OrthancException(ErrorCode_InternalError); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
204 } |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
205 |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
206 LogChange(changeType, id, type); |
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
207 return id; |
183 | 208 } |
209 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
210 bool DatabaseWrapper::LookupResource(const std::string& publicId, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
211 int64_t& id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
212 ResourceType& type) |
183 | 213 { |
214 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
215 "SELECT internalId, resourceType FROM Resources WHERE publicId=?"); | |
216 s.BindString(0, publicId); | |
217 | |
218 if (!s.Step()) | |
219 { | |
220 return false; | |
221 } | |
222 else | |
223 { | |
224 id = s.ColumnInt(0); | |
225 type = static_cast<ResourceType>(s.ColumnInt(1)); | |
226 | |
227 // Check whether there is a single resource with this public id | |
228 assert(!s.Step()); | |
229 | |
230 return true; | |
231 } | |
232 } | |
233 | |
198
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
234 bool DatabaseWrapper::LookupParent(int64_t& parentId, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
235 int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
236 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
237 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
238 "SELECT parentId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
239 s.BindInt(0, resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
240 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
241 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
242 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
243 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
244 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
245 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
246 if (s.ColumnIsNull(0)) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
247 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
248 return false; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
249 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
250 else |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
251 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
252 parentId = s.ColumnInt(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
253 return true; |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
254 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
255 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
256 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
257 std::string DatabaseWrapper::GetPublicId(int64_t resourceId) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
258 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
259 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
260 "SELECT publicId FROM Resources WHERE internalId=?"); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
261 s.BindInt(0, resourceId); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
262 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
263 if (!s.Step()) |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
264 { |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
265 throw OrthancException(ErrorCode_UnknownResource); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
266 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
267 |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
268 return s.ColumnString(0); |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
269 } |
663cc6c46d0a
before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
197
diff
changeset
|
270 |
183 | 271 void DatabaseWrapper::AttachChild(int64_t parent, |
272 int64_t child) | |
273 { | |
274 SQLite::Statement s(db_, SQLITE_FROM_HERE, "UPDATE Resources SET parentId = ? WHERE internalId = ?"); | |
275 s.BindInt(0, parent); | |
276 s.BindInt(1, child); | |
277 s.Run(); | |
278 } | |
279 | |
193
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
280 void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds, |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
281 int64_t id) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
282 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
283 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?"); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
284 s.BindInt(0, id); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
285 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
286 childrenPublicIds = Json::arrayValue; |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
287 while (s.Step()) |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
288 { |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
289 childrenPublicIds.append(s.ColumnString(0)); |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
290 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
291 } |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
292 |
a1b9d1e1497b
failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
192
diff
changeset
|
293 |
183 | 294 void DatabaseWrapper::DeleteResource(int64_t id) |
295 { | |
296 signalRemainingAncestor_->Reset(); | |
297 | |
298 SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Resources WHERE internalId=?"); | |
299 s.BindInt(0, id); | |
300 s.Run(); | |
301 | |
302 if (signalRemainingAncestor_->HasRemainingAncestor()) | |
303 { | |
304 listener_.SignalRemainingAncestor(signalRemainingAncestor_->GetRemainingAncestorType(), | |
305 signalRemainingAncestor_->GetRemainingAncestorId()); | |
306 } | |
307 } | |
308 | |
309 void DatabaseWrapper::SetMetadata(int64_t id, | |
310 MetadataType type, | |
311 const std::string& value) | |
312 { | |
313 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT OR REPLACE INTO Metadata VALUES(?, ?, ?)"); | |
314 s.BindInt(0, id); | |
315 s.BindInt(1, type); | |
316 s.BindString(2, value); | |
317 s.Run(); | |
318 } | |
319 | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
320 bool DatabaseWrapper::LookupMetadata(std::string& target, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
321 int64_t id, |
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
322 MetadataType type) |
183 | 323 { |
324 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
325 "SELECT value FROM Metadata WHERE id=? AND type=?"); | |
326 s.BindInt(0, id); | |
327 s.BindInt(1, type); | |
328 | |
329 if (!s.Step()) | |
330 { | |
331 return false; | |
332 } | |
333 else | |
334 { | |
335 target = s.ColumnString(0); | |
336 return true; | |
337 } | |
338 } | |
339 | |
340 std::string DatabaseWrapper::GetMetadata(int64_t id, | |
341 MetadataType type, | |
342 const std::string& defaultValue) | |
343 { | |
344 std::string s; | |
188
090cefdab1d1
fix because of Windows macros
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
187
diff
changeset
|
345 if (LookupMetadata(s, id, type)) |
183 | 346 { |
347 return s; | |
348 } | |
349 else | |
350 { | |
351 return defaultValue; | |
352 } | |
353 } | |
354 | |
200
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
355 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
356 bool DatabaseWrapper::GetMetadataAsInteger(int& result, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
357 int64_t id, |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
358 MetadataType type) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
359 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
360 std::string s = GetMetadata(id, type, ""); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
361 if (s.size() == 0) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
362 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
363 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
364 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
365 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
366 try |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
367 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
368 result = boost::lexical_cast<int>(s); |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
369 return true; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
370 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
371 catch (boost::bad_lexical_cast&) |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
372 { |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
373 return false; |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
374 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
375 } |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
376 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
377 |
9c58b2b03cf0
refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
199
diff
changeset
|
378 |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
379 void DatabaseWrapper::AddAttachment(int64_t id, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
380 const FileInfo& attachment) |
183 | 381 { |
382 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO AttachedFiles VALUES(?, ?, ?, ?, ?, ?)"); | |
383 s.BindInt(0, id); | |
233 | 384 s.BindInt(1, attachment.GetContentType()); |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
385 s.BindString(2, attachment.GetUuid()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
386 s.BindInt(3, attachment.GetCompressedSize()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
387 s.BindInt(4, attachment.GetUncompressedSize()); |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
388 s.BindInt(5, attachment.GetCompressionType()); |
183 | 389 s.Run(); |
390 } | |
391 | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
392 bool DatabaseWrapper::LookupAttachment(FileInfo& attachment, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
393 int64_t id, |
233 | 394 FileContentType contentType) |
183 | 395 { |
396 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
397 "SELECT uuid, uncompressedSize, compressionType, compressedSize FROM AttachedFiles WHERE id=? AND fileType=?"); |
183 | 398 s.BindInt(0, id); |
197
530a25320461
removal of text as ids in sqlite db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
399 s.BindInt(1, contentType); |
183 | 400 |
401 if (!s.Step()) | |
402 { | |
403 return false; | |
404 } | |
405 else | |
406 { | |
232
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
407 attachment = FileInfo(s.ColumnString(0), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
408 contentType, |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
409 s.ColumnInt(1), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
410 static_cast<CompressionType>(s.ColumnInt(2)), |
5368bbe813cf
refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
231
diff
changeset
|
411 s.ColumnInt(3)); |
183 | 412 return true; |
413 } | |
414 } | |
415 | |
416 void DatabaseWrapper::SetMainDicomTags(int64_t id, | |
417 const DicomMap& tags) | |
418 { | |
419 DicomArray flattened(tags); | |
420 for (size_t i = 0; i < flattened.GetSize(); i++) | |
421 { | |
422 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO MainDicomTags VALUES(?, ?, ?, ?)"); | |
423 s.BindInt(0, id); | |
424 s.BindInt(1, flattened.GetElement(i).GetTag().GetGroup()); | |
425 s.BindInt(2, flattened.GetElement(i).GetTag().GetElement()); | |
426 s.BindString(3, flattened.GetElement(i).GetValue().AsString()); | |
427 s.Run(); | |
428 } | |
429 } | |
430 | |
431 void DatabaseWrapper::GetMainDicomTags(DicomMap& map, | |
432 int64_t id) | |
433 { | |
434 map.Clear(); | |
435 | |
436 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE id=?"); | |
437 s.BindInt(0, id); | |
438 while (s.Step()) | |
439 { | |
440 map.SetValue(s.ColumnInt(1), | |
441 s.ColumnInt(2), | |
442 s.ColumnString(3)); | |
443 } | |
444 } | |
445 | |
446 | |
447 bool DatabaseWrapper::GetParentPublicId(std::string& result, | |
448 int64_t id) | |
449 { | |
450 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
451 "WHERE a.internalId = b.parentId AND b.internalId = ?"); | |
452 s.BindInt(0, id); | |
453 | |
454 if (s.Step()) | |
455 { | |
456 result = s.ColumnString(0); | |
457 return true; | |
458 } | |
459 else | |
460 { | |
461 return false; | |
462 } | |
463 } | |
464 | |
465 | |
466 void DatabaseWrapper::GetChildrenPublicId(std::list<std::string>& result, | |
467 int64_t id) | |
468 { | |
469 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.publicId FROM Resources AS a, Resources AS b " | |
470 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
471 s.BindInt(0, id); | |
472 | |
473 result.clear(); | |
474 | |
475 while (s.Step()) | |
476 { | |
477 result.push_back(s.ColumnString(0)); | |
478 } | |
479 } | |
480 | |
481 | |
199 | 482 void DatabaseWrapper::GetChildrenInternalId(std::list<int64_t>& result, |
483 int64_t id) | |
484 { | |
485 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT a.internalId FROM Resources AS a, Resources AS b " | |
486 "WHERE a.parentId = b.internalId AND b.internalId = ?"); | |
487 s.BindInt(0, id); | |
488 | |
489 result.clear(); | |
490 | |
491 while (s.Step()) | |
492 { | |
493 result.push_back(s.ColumnInt(0)); | |
494 } | |
495 } | |
496 | |
497 | |
183 | 498 void DatabaseWrapper::LogChange(ChangeType changeType, |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
499 int64_t internalId, |
183 | 500 ResourceType resourceType, |
501 const boost::posix_time::ptime& date) | |
502 { | |
503 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); | |
504 s.BindInt(0, changeType); | |
189
ccbc2cf64a0d
record main dicom tags and changes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
505 s.BindInt(1, internalId); |
183 | 506 s.BindInt(2, resourceType); |
507 s.BindString(3, boost::posix_time::to_iso_string(date)); | |
508 s.Run(); | |
509 } | |
510 | |
511 | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
512 void DatabaseWrapper::GetChangesInternal(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
513 SQLite::Statement& s, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
514 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
515 unsigned int maxResults) |
204 | 516 { |
517 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
518 int64_t last = since; |
204 | 519 |
520 while (changes.size() < maxResults && s.Step()) | |
521 { | |
522 int64_t seq = s.ColumnInt(0); | |
523 ChangeType changeType = static_cast<ChangeType>(s.ColumnInt(1)); | |
524 int64_t internalId = s.ColumnInt(2); | |
525 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(3)); | |
526 const std::string& date = s.ColumnString(4); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
527 std::string publicId = GetPublicId(internalId); |
204 | 528 |
529 Json::Value item = Json::objectValue; | |
530 item["Seq"] = static_cast<int>(seq); | |
531 item["ChangeType"] = ToString(changeType); | |
532 item["ResourceType"] = ToString(resourceType); | |
205
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
533 item["ID"] = publicId; |
6ab754744446
logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
204
diff
changeset
|
534 item["Path"] = GetBasePath(resourceType, publicId); |
204 | 535 item["Date"] = date; |
536 last = seq; | |
537 | |
538 changes.append(item); | |
539 } | |
540 | |
541 target = Json::objectValue; | |
542 target["Changes"] = changes; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
543 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
544 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
545 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
546 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
547 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
548 void DatabaseWrapper::GetChanges(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
549 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
550 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
551 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
552 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
553 s.BindInt(0, since); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
554 s.BindInt(1, maxResults + 1); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
555 GetChangesInternal(target, s, since, maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
556 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
557 |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
558 void DatabaseWrapper::GetLastChange(Json::Value& target) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
559 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
560 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
|
561 GetChangesInternal(target, s, 0, 1); |
204 | 562 } |
563 | |
564 | |
231 | 565 void DatabaseWrapper::LogExportedResource(ResourceType resourceType, |
566 const std::string& publicId, | |
567 const std::string& remoteModality, | |
568 const std::string& patientId, | |
569 const std::string& studyInstanceUid, | |
570 const std::string& seriesInstanceUid, | |
571 const std::string& sopInstanceUid, | |
183 | 572 const boost::posix_time::ptime& date) |
573 { | |
231 | 574 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
575 "INSERT INTO ExportedResources VALUES(NULL, ?, ?, ?, ?, ?, ?, ?, ?)"); | |
576 | |
577 s.BindInt(0, resourceType); | |
578 s.BindString(1, publicId); | |
579 s.BindString(2, remoteModality); | |
580 s.BindString(3, patientId); | |
581 s.BindString(4, studyInstanceUid); | |
582 s.BindString(5, seriesInstanceUid); | |
583 s.BindString(6, sopInstanceUid); | |
584 s.BindString(7, boost::posix_time::to_iso_string(date)); | |
585 | |
183 | 586 s.Run(); |
587 } | |
231 | 588 |
589 | |
590 void DatabaseWrapper::GetExportedResources(Json::Value& target, | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
591 SQLite::Statement& s, |
231 | 592 int64_t since, |
593 unsigned int maxResults) | |
594 { | |
595 Json::Value changes = Json::arrayValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
596 int64_t last = since; |
231 | 597 |
598 while (changes.size() < maxResults && s.Step()) | |
599 { | |
600 int64_t seq = s.ColumnInt(0); | |
601 ResourceType resourceType = static_cast<ResourceType>(s.ColumnInt(1)); | |
602 std::string publicId = s.ColumnString(2); | |
603 | |
604 Json::Value item = Json::objectValue; | |
605 item["Seq"] = static_cast<int>(seq); | |
606 item["ResourceType"] = ToString(resourceType); | |
607 item["ID"] = publicId; | |
608 item["Path"] = GetBasePath(resourceType, publicId); | |
609 item["RemoteModality"] = s.ColumnString(3); | |
610 item["Date"] = s.ColumnString(8); | |
611 | |
612 // WARNING: Do not add "break" below and do not reorder the case items! | |
613 switch (resourceType) | |
614 { | |
615 case ResourceType_Instance: | |
616 item["SopInstanceUid"] = s.ColumnString(7); | |
617 | |
618 case ResourceType_Series: | |
619 item["SeriesInstanceUid"] = s.ColumnString(6); | |
620 | |
621 case ResourceType_Study: | |
622 item["StudyInstanceUid"] = s.ColumnString(5); | |
623 | |
624 case ResourceType_Patient: | |
625 item["PatientId"] = s.ColumnString(4); | |
626 break; | |
627 | |
628 default: | |
629 throw OrthancException(ErrorCode_InternalError); | |
630 } | |
631 | |
632 last = seq; | |
633 | |
634 changes.append(item); | |
635 } | |
636 | |
637 target = Json::objectValue; | |
237
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
638 target["Exports"] = changes; |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
639 target["Done"] = !(changes.size() == maxResults && s.Step()); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
640 target["Last"] = static_cast<int>(last); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
641 } |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
642 |
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 void DatabaseWrapper::GetExportedResources(Json::Value& target, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
645 int64_t since, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
646 unsigned int maxResults) |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
647 { |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
648 SQLite::Statement s(db_, SQLITE_FROM_HERE, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
649 "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
650 s.BindInt(0, since); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
651 s.BindInt(1, maxResults + 1); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
652 GetExportedResources(target, s, since, maxResults); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
653 } |
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::GetLastExportedResource(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, |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
659 "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1"); |
16a4ac70bd8a
last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
233
diff
changeset
|
660 GetExportedResources(target, s, 0, 1); |
231 | 661 } |
662 | |
663 | |
183 | 664 |
665 | |
666 int64_t DatabaseWrapper::GetTableRecordCount(const std::string& table) | |
667 { | |
668 char buf[128]; | |
669 sprintf(buf, "SELECT COUNT(*) FROM %s", table.c_str()); | |
670 SQLite::Statement s(db_, buf); | |
671 | |
218 | 672 if (!s.Step()) |
673 { | |
674 throw OrthancException(ErrorCode_InternalError); | |
675 } | |
676 | |
183 | 677 int64_t c = s.ColumnInt(0); |
678 assert(!s.Step()); | |
679 | |
680 return c; | |
681 } | |
682 | |
683 | |
684 uint64_t DatabaseWrapper::GetTotalCompressedSize() | |
685 { | |
686 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(compressedSize) FROM AttachedFiles"); | |
687 s.Run(); | |
688 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
689 } | |
690 | |
691 | |
692 uint64_t DatabaseWrapper::GetTotalUncompressedSize() | |
693 { | |
694 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT SUM(uncompressedSize) FROM AttachedFiles"); | |
695 s.Run(); | |
696 return static_cast<uint64_t>(s.ColumnInt64(0)); | |
697 } | |
698 | |
190 | 699 void DatabaseWrapper::GetAllPublicIds(Json::Value& target, |
700 ResourceType resourceType) | |
701 { | |
702 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?"); | |
703 s.BindInt(0, resourceType); | |
704 | |
705 target = Json::arrayValue; | |
706 while (s.Step()) | |
707 { | |
708 target.append(s.ColumnString(0)); | |
709 } | |
710 } | |
711 | |
183 | 712 |
713 DatabaseWrapper::DatabaseWrapper(const std::string& path, | |
714 IServerIndexListener& listener) : | |
715 listener_(listener) | |
716 { | |
717 db_.Open(path); | |
718 Open(); | |
719 } | |
720 | |
721 DatabaseWrapper::DatabaseWrapper(IServerIndexListener& listener) : | |
722 listener_(listener) | |
723 { | |
724 db_.OpenInMemory(); | |
725 Open(); | |
726 } | |
727 | |
728 void DatabaseWrapper::Open() | |
729 { | |
730 if (!db_.DoesTableExist("GlobalProperties")) | |
731 { | |
732 LOG(INFO) << "Creating the database"; | |
733 std::string query; | |
203
9283552c25df
db refactoring done
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
200
diff
changeset
|
734 EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE); |
183 | 735 db_.Execute(query); |
736 } | |
737 | |
738 signalRemainingAncestor_ = new Internals::SignalRemainingAncestor; | |
739 db_.Register(signalRemainingAncestor_); | |
740 db_.Register(new Internals::SignalFileDeleted(listener_)); | |
741 } | |
238 | 742 |
743 uint64_t DatabaseWrapper::GetResourceCount(ResourceType resourceType) | |
744 { | |
745 SQLite::Statement s(db_, SQLITE_FROM_HERE, | |
746 "SELECT COUNT(*) FROM Resources WHERE resourceType=?"); | |
747 s.BindInt(0, resourceType); | |
748 | |
749 if (!s.Step()) | |
750 { | |
751 throw OrthancException(ErrorCode_InternalError); | |
752 } | |
753 | |
754 int64_t c = s.ColumnInt(0); | |
755 assert(!s.Step()); | |
756 | |
757 return c; | |
758 } | |
183 | 759 } |