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