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