Mercurial > hg > orthanc
annotate OrthancFramework/Sources/SQLite/Connection.cpp @ 5853:4d932683049d get-scu tip
very first implementation of C-Get SCU
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Tue, 29 Oct 2024 17:25:49 +0100 |
parents | f7adfb22e20e |
children |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
3 * |
3063 | 4 * Copyright (C) 2012-2016 Sebastien Jodogne <s.jodogne@orthanc-labs.com>, |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
5 * Medical Physics Department, CHU of Liege, Belgium |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
6 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
7 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
8 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 9 * |
17 | 10 * Copyright (c) 2012 The Chromium Authors. All rights reserved. |
11 * | |
12 * Redistribution and use in source and binary forms, with or without | |
13 * modification, are permitted provided that the following conditions are | |
14 * met: | |
0 | 15 * |
17 | 16 * * Redistributions of source code must retain the above copyright |
17 * notice, this list of conditions and the following disclaimer. | |
18 * * Redistributions in binary form must reproduce the above | |
19 * copyright notice, this list of conditions and the following disclaimer | |
20 * in the documentation and/or other materials provided with the | |
21 * distribution. | |
22 * * Neither the name of Google Inc., the name of the CHU of Liege, | |
23 * nor the names of its contributors may be used to endorse or promote | |
24 * products derived from this software without specific prior written | |
25 * permission. | |
26 * | |
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
0 | 38 **/ |
39 | |
40 | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
41 #if ORTHANC_SQLITE_STANDALONE != 1 |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
42 #include "../PrecompiledHeaders.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
43 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
44 |
0 | 45 #include "Connection.h" |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
46 #include "OrthancSQLiteException.h" |
0 | 47 |
48 #include <memory> | |
49 #include <cassert> | |
50 #include <string.h> | |
51 | |
1225 | 52 #if ORTHANC_SQLITE_STANDALONE != 1 |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1474
diff
changeset
|
53 #include "../Logging.h" |
1225 | 54 #endif |
55 | |
1474
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
56 #include "sqlite3.h" |
3b68924ffb24
fix path to sqlite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1291
diff
changeset
|
57 |
1225 | 58 |
59 | 59 namespace Orthanc |
0 | 60 { |
61 namespace SQLite | |
62 { | |
63 Connection::Connection() : | |
64 db_(NULL), | |
65 transactionNesting_(0), | |
66 needsRollback_(false) | |
67 { | |
68 } | |
69 | |
70 | |
71 Connection::~Connection() | |
72 { | |
73 Close(); | |
74 } | |
75 | |
76 | |
77 void Connection::CheckIsOpen() const | |
78 { | |
79 if (!db_) | |
80 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
81 throw OrthancSQLiteException(ErrorCode_SQLiteNotOpened); |
0 | 82 } |
83 } | |
84 | |
85 void Connection::Open(const std::string& path) | |
86 { | |
87 if (db_) | |
88 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
89 throw OrthancSQLiteException(ErrorCode_SQLiteAlreadyOpened); |
0 | 90 } |
91 | |
92 int err = sqlite3_open(path.c_str(), &db_); | |
93 if (err != SQLITE_OK) | |
94 { | |
95 Close(); | |
96 db_ = NULL; | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
97 throw OrthancSQLiteException(ErrorCode_SQLiteCannotOpen); |
0 | 98 } |
99 | |
100 // Execute PRAGMAs at this point | |
101 // http://www.sqlite.org/pragma.html | |
102 Execute("PRAGMA FOREIGN_KEYS=ON;"); | |
181
2dece1526c06
simplifying db schema
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
103 Execute("PRAGMA RECURSIVE_TRIGGERS=ON;"); |
0 | 104 } |
105 | |
106 void Connection::OpenInMemory() | |
107 { | |
108 Open(":memory:"); | |
109 } | |
110 | |
111 void Connection::Close() | |
112 { | |
113 ClearCache(); | |
114 | |
115 if (db_) | |
116 { | |
117 sqlite3_close(db_); | |
118 db_ = NULL; | |
119 } | |
120 } | |
121 | |
122 void Connection::ClearCache() | |
123 { | |
124 for (CachedStatements::iterator | |
125 it = cachedStatements_.begin(); | |
656 | 126 it != cachedStatements_.end(); ++it) |
0 | 127 { |
128 delete it->second; | |
129 } | |
130 | |
131 cachedStatements_.clear(); | |
132 } | |
133 | |
134 | |
135 StatementReference& Connection::GetCachedStatement(const StatementId& id, | |
136 const char* sql) | |
137 { | |
138 CachedStatements::iterator i = cachedStatements_.find(id); | |
139 if (i != cachedStatements_.end()) | |
140 { | |
141 if (i->second->GetReferenceCount() >= 1) | |
142 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
143 throw OrthancSQLiteException(ErrorCode_SQLiteStatementAlreadyUsed); |
0 | 144 } |
145 | |
146 return *i->second; | |
147 } | |
148 else | |
149 { | |
150 StatementReference* statement = new StatementReference(db_, sql); | |
151 cachedStatements_[id] = statement; | |
152 return *statement; | |
153 } | |
154 } | |
155 | |
156 | |
157 bool Connection::Execute(const char* sql) | |
158 { | |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
159 #if ORTHANC_SQLITE_STANDALONE != 1 |
4269
c7bd2f21ccc3
new macro CLOG, and sharing more code between logging engines
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4268
diff
changeset
|
160 CLOG(TRACE, SQLITE) << "SQLite::Connection::Execute " << sql; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
161 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
162 |
0 | 163 CheckIsOpen(); |
164 | |
165 int error = sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
166 if (error == SQLITE_ERROR) | |
167 { | |
1585
9a3e03d6a4d5
fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
168 #if ORTHANC_SQLITE_STANDALONE != 1 |
1958
c746e2d42ac8
extended error codes for SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
169 LOG(ERROR) << "SQLite execute error: " << sqlite3_errmsg(db_) |
c746e2d42ac8
extended error codes for SQLite
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
170 << " (" << sqlite3_extended_errcode(db_) << ")"; |
1585
9a3e03d6a4d5
fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
171 #endif |
9a3e03d6a4d5
fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
172 |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
173 throw OrthancSQLiteException(ErrorCode_SQLiteExecute); |
0 | 174 } |
175 else | |
176 { | |
177 return error == SQLITE_OK; | |
178 } | |
179 } | |
180 | |
4304 | 181 bool Connection::Execute(const std::string &sql) |
0 | 182 { |
4304 | 183 return Execute(sql.c_str()); |
0 | 184 } |
185 | |
186 // Info querying ------------------------------------------------------------- | |
187 | |
4304 | 188 bool Connection::IsSQLValid(const char* sql) |
0 | 189 { |
190 sqlite3_stmt* stmt = NULL; | |
191 if (sqlite3_prepare_v2(db_, sql, -1, &stmt, NULL) != SQLITE_OK) | |
192 return false; | |
193 | |
194 sqlite3_finalize(stmt); | |
195 return true; | |
196 } | |
197 | |
198 bool Connection::DoesTableOrIndexExist(const char* name, | |
199 const char* type) const | |
200 { | |
201 // Our SQL is non-mutating, so this cast is OK. | |
202 Statement statement(const_cast<Connection&>(*this), | |
203 "SELECT name FROM sqlite_master WHERE type=? AND name=?"); | |
204 statement.BindString(0, type); | |
205 statement.BindString(1, name); | |
206 return statement.Step(); // Table exists if any row was returned. | |
207 } | |
208 | |
209 bool Connection::DoesTableExist(const char* table_name) const | |
210 { | |
211 return DoesTableOrIndexExist(table_name, "table"); | |
212 } | |
213 | |
214 bool Connection::DoesIndexExist(const char* index_name) const | |
215 { | |
216 return DoesTableOrIndexExist(index_name, "index"); | |
217 } | |
218 | |
219 bool Connection::DoesColumnExist(const char* table_name, const char* column_name) const | |
220 { | |
221 std::string sql("PRAGMA TABLE_INFO("); | |
222 sql.append(table_name); | |
223 sql.append(")"); | |
224 | |
225 // Our SQL is non-mutating, so this cast is OK. | |
226 Statement statement(const_cast<Connection&>(*this), sql.c_str()); | |
227 | |
228 while (statement.Step()) { | |
229 if (!statement.ColumnString(1).compare(column_name)) | |
230 return true; | |
231 } | |
232 return false; | |
233 } | |
234 | |
235 int64_t Connection::GetLastInsertRowId() const | |
236 { | |
237 return sqlite3_last_insert_rowid(db_); | |
238 } | |
239 | |
240 int Connection::GetLastChangeCount() const | |
241 { | |
242 return sqlite3_changes(db_); | |
243 } | |
244 | |
245 int Connection::GetErrorCode() const | |
246 { | |
247 return sqlite3_errcode(db_); | |
248 } | |
249 | |
250 int Connection::GetLastErrno() const | |
251 { | |
252 int err = 0; | |
253 if (SQLITE_OK != sqlite3_file_control(db_, NULL, SQLITE_LAST_ERRNO, &err)) | |
254 return -2; | |
255 | |
256 return err; | |
257 } | |
258 | |
259 const char* Connection::GetErrorMessage() const | |
260 { | |
261 return sqlite3_errmsg(db_); | |
262 } | |
263 | |
264 | |
4304 | 265 int Connection::ExecuteAndReturnErrorCode(const char* sql) |
266 { | |
267 CheckIsOpen(); | |
268 return sqlite3_exec(db_, sql, NULL, NULL, NULL); | |
269 } | |
270 | |
271 bool Connection::HasCachedStatement(const StatementId &id) const | |
272 { | |
273 return cachedStatements_.find(id) != cachedStatements_.end(); | |
274 } | |
275 | |
276 int Connection::GetTransactionNesting() const | |
277 { | |
278 return transactionNesting_; | |
279 } | |
280 | |
0 | 281 bool Connection::BeginTransaction() |
282 { | |
283 if (needsRollback_) | |
284 { | |
285 assert(transactionNesting_ > 0); | |
286 | |
287 // When we're going to rollback, fail on this begin and don't actually | |
288 // mark us as entering the nested transaction. | |
289 return false; | |
290 } | |
291 | |
292 bool success = true; | |
293 if (!transactionNesting_) | |
294 { | |
295 needsRollback_ = false; | |
296 | |
297 Statement begin(*this, SQLITE_FROM_HERE, "BEGIN TRANSACTION"); | |
298 if (!begin.Run()) | |
299 return false; | |
300 } | |
301 transactionNesting_++; | |
302 return success; | |
303 } | |
304 | |
305 void Connection::RollbackTransaction() | |
306 { | |
307 if (!transactionNesting_) | |
308 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
309 throw OrthancSQLiteException(ErrorCode_SQLiteRollbackWithoutTransaction); |
0 | 310 } |
311 | |
312 transactionNesting_--; | |
313 | |
314 if (transactionNesting_ > 0) | |
315 { | |
316 // Mark the outermost transaction as needing rollback. | |
317 needsRollback_ = true; | |
318 return; | |
319 } | |
320 | |
321 DoRollback(); | |
322 } | |
323 | |
324 bool Connection::CommitTransaction() | |
325 { | |
326 if (!transactionNesting_) | |
327 { | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
328 throw OrthancSQLiteException(ErrorCode_SQLiteCommitWithoutTransaction); |
0 | 329 } |
330 transactionNesting_--; | |
331 | |
332 if (transactionNesting_ > 0) | |
333 { | |
334 // Mark any nested transactions as failing after we've already got one. | |
335 return !needsRollback_; | |
336 } | |
337 | |
338 if (needsRollback_) | |
339 { | |
340 DoRollback(); | |
341 return false; | |
342 } | |
343 | |
344 Statement commit(*this, SQLITE_FROM_HERE, "COMMIT"); | |
345 return commit.Run(); | |
346 } | |
347 | |
348 void Connection::DoRollback() | |
349 { | |
350 Statement rollback(*this, SQLITE_FROM_HERE, "ROLLBACK"); | |
351 rollback.Run(); | |
352 needsRollback_ = false; | |
353 } | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | |
360 static void ScalarFunctionCaller(sqlite3_context* rawContext, | |
361 int argc, | |
362 sqlite3_value** argv) | |
363 { | |
364 FunctionContext context(rawContext, argc, argv); | |
365 | |
366 void* payload = sqlite3_user_data(rawContext); | |
367 assert(payload != NULL); | |
368 | |
656 | 369 IScalarFunction& func = *reinterpret_cast<IScalarFunction*>(payload); |
0 | 370 func.Compute(context); |
371 } | |
372 | |
373 | |
374 static void ScalarFunctionDestroyer(void* payload) | |
375 { | |
376 assert(payload != NULL); | |
656 | 377 delete reinterpret_cast<IScalarFunction*>(payload); |
0 | 378 } |
379 | |
380 | |
381 IScalarFunction* Connection::Register(IScalarFunction* func) | |
382 { | |
383 int err = sqlite3_create_function_v2(db_, | |
384 func->GetName(), | |
385 func->GetCardinality(), | |
386 SQLITE_UTF8, | |
387 func, | |
388 ScalarFunctionCaller, | |
389 NULL, | |
390 NULL, | |
391 ScalarFunctionDestroyer); | |
392 | |
393 if (err != SQLITE_OK) | |
394 { | |
395 delete func; | |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
396 throw OrthancSQLiteException(ErrorCode_SQLiteRegisterFunction); |
0 | 397 } |
398 | |
399 return func; | |
400 } | |
401 | |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
402 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
403 void Connection::FlushToDisk() |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
404 { |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
405 #if ORTHANC_SQLITE_STANDALONE != 1 |
4269
c7bd2f21ccc3
new macro CLOG, and sharing more code between logging engines
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4268
diff
changeset
|
406 CLOG(TRACE, SQLITE) << "SQLite::Connection::FlushToDisk"; |
1220
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
407 #endif |
9b9026560a5f
SQLite wrapper is now fully independent of Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
408 |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
409 int err = sqlite3_wal_checkpoint(db_, NULL); |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
410 |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
411 if (err != SQLITE_OK) |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
412 { |
1582
bd1889029cbb
encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
413 throw OrthancSQLiteException(ErrorCode_SQLiteFlush); |
206
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
414 } |
4453a010d0db
flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
181
diff
changeset
|
415 } |
0 | 416 } |
417 } |