comparison Core/SQLite/Connection.cpp @ 1220:9b9026560a5f

SQLite wrapper is now fully independent of Orthanc
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Nov 2014 16:33:51 +0100
parents a811bdf8b8eb
children 4c649dbeb61a
comparison
equal deleted inserted replaced
1219:c4ae92753d57 1220:9b9026560a5f
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, 3 *
4 * Belgium 4 * Copyright (C) 2012-2014 Sebastien Jodogne <s.jodogne@gmail.com>,
5 * Medical Physics Department, CHU of Liege, Belgium
5 * 6 *
6 * Copyright (c) 2012 The Chromium Authors. All rights reserved. 7 * Copyright (c) 2012 The Chromium Authors. All rights reserved.
7 * 8 *
8 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are 10 * modification, are permitted provided that the following conditions are
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 **/ 35 **/
35 36
36 37
38 #if ORTHANC_SQLITE_STANDALONE != 1
37 #include "../PrecompiledHeaders.h" 39 #include "../PrecompiledHeaders.h"
40 #include <glog/logging.h>
41 #endif
42
38 #include "Connection.h" 43 #include "Connection.h"
44 #include "OrthancSQLiteException.h"
39 45
40 #include <memory> 46 #include <memory>
41 #include <cassert> 47 #include <cassert>
42 #include <sqlite3.h> 48 #include <sqlite3.h>
43 #include <string.h> 49 #include <string.h>
44
45 #include <glog/logging.h>
46
47 50
48 namespace Orthanc 51 namespace Orthanc
49 { 52 {
50 namespace SQLite 53 namespace SQLite
51 { 54 {
65 68
66 void Connection::CheckIsOpen() const 69 void Connection::CheckIsOpen() const
67 { 70 {
68 if (!db_) 71 if (!db_)
69 { 72 {
70 throw OrthancException("SQLite: The database is not opened"); 73 throw OrthancSQLiteException("SQLite: The database is not opened");
71 } 74 }
72 } 75 }
73 76
74 void Connection::Open(const std::string& path) 77 void Connection::Open(const std::string& path)
75 { 78 {
76 if (db_) 79 if (db_)
77 { 80 {
78 throw OrthancException("SQLite: Connection is already open"); 81 throw OrthancSQLiteException("SQLite: Connection is already open");
79 } 82 }
80 83
81 int err = sqlite3_open(path.c_str(), &db_); 84 int err = sqlite3_open(path.c_str(), &db_);
82 if (err != SQLITE_OK) 85 if (err != SQLITE_OK)
83 { 86 {
84 Close(); 87 Close();
85 db_ = NULL; 88 db_ = NULL;
86 throw OrthancException("SQLite: Unable to open the database"); 89 throw OrthancSQLiteException("SQLite: Unable to open the database");
87 } 90 }
88 91
89 // Execute PRAGMAs at this point 92 // Execute PRAGMAs at this point
90 // http://www.sqlite.org/pragma.html 93 // http://www.sqlite.org/pragma.html
91 Execute("PRAGMA FOREIGN_KEYS=ON;"); 94 Execute("PRAGMA FOREIGN_KEYS=ON;");
127 CachedStatements::iterator i = cachedStatements_.find(id); 130 CachedStatements::iterator i = cachedStatements_.find(id);
128 if (i != cachedStatements_.end()) 131 if (i != cachedStatements_.end())
129 { 132 {
130 if (i->second->GetReferenceCount() >= 1) 133 if (i->second->GetReferenceCount() >= 1)
131 { 134 {
132 throw OrthancException("SQLite: This cached statement is already being referred to"); 135 throw OrthancSQLiteException("SQLite: This cached statement is already being referred to");
133 } 136 }
134 137
135 return *i->second; 138 return *i->second;
136 } 139 }
137 else 140 else
143 } 146 }
144 147
145 148
146 bool Connection::Execute(const char* sql) 149 bool Connection::Execute(const char* sql)
147 { 150 {
151 #if ORTHANC_SQLITE_STANDALONE != 1
148 VLOG(1) << "SQLite::Connection::Execute " << sql; 152 VLOG(1) << "SQLite::Connection::Execute " << sql;
153 #endif
154
149 CheckIsOpen(); 155 CheckIsOpen();
150 156
151 int error = sqlite3_exec(db_, sql, NULL, NULL, NULL); 157 int error = sqlite3_exec(db_, sql, NULL, NULL, NULL);
152 if (error == SQLITE_ERROR) 158 if (error == SQLITE_ERROR)
153 { 159 {
154 throw OrthancException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_))); 160 throw OrthancSQLiteException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_)));
155 } 161 }
156 else 162 else
157 { 163 {
158 return error == SQLITE_OK; 164 return error == SQLITE_OK;
159 } 165 }
270 276
271 void Connection::RollbackTransaction() 277 void Connection::RollbackTransaction()
272 { 278 {
273 if (!transactionNesting_) 279 if (!transactionNesting_)
274 { 280 {
275 throw OrthancException("Rolling back a nonexistent transaction"); 281 throw OrthancSQLiteException("Rolling back a nonexistent transaction");
276 } 282 }
277 283
278 transactionNesting_--; 284 transactionNesting_--;
279 285
280 if (transactionNesting_ > 0) 286 if (transactionNesting_ > 0)
289 295
290 bool Connection::CommitTransaction() 296 bool Connection::CommitTransaction()
291 { 297 {
292 if (!transactionNesting_) 298 if (!transactionNesting_)
293 { 299 {
294 throw OrthancException("Committing a nonexistent transaction"); 300 throw OrthancSQLiteException("Committing a nonexistent transaction");
295 } 301 }
296 transactionNesting_--; 302 transactionNesting_--;
297 303
298 if (transactionNesting_ > 0) 304 if (transactionNesting_ > 0)
299 { 305 {
357 ScalarFunctionDestroyer); 363 ScalarFunctionDestroyer);
358 364
359 if (err != SQLITE_OK) 365 if (err != SQLITE_OK)
360 { 366 {
361 delete func; 367 delete func;
362 throw OrthancException("SQLite: Unable to register a function"); 368 throw OrthancSQLiteException("SQLite: Unable to register a function");
363 } 369 }
364 370
365 return func; 371 return func;
366 } 372 }
367 373
368 374
369 void Connection::FlushToDisk() 375 void Connection::FlushToDisk()
370 { 376 {
377 #if ORTHANC_SQLITE_STANDALONE != 1
371 VLOG(1) << "SQLite::Connection::FlushToDisk"; 378 VLOG(1) << "SQLite::Connection::FlushToDisk";
379 #endif
380
372 int err = sqlite3_wal_checkpoint(db_, NULL); 381 int err = sqlite3_wal_checkpoint(db_, NULL);
373 382
374 if (err != SQLITE_OK) 383 if (err != SQLITE_OK)
375 { 384 {
376 throw OrthancException("SQLite: Unable to flush the database"); 385 throw OrthancSQLiteException("SQLite: Unable to flush the database");
377 } 386 }
378 } 387 }
379 } 388 }
380 } 389 }