# HG changeset patch # User Sebastien Jodogne # Date 1415633631 -3600 # Node ID 9b9026560a5f93f27d134edd4c80316bf98625da # Parent c4ae92753d5770674df26ae9f111d58770042ff8 SQLite wrapper is now fully independent of Orthanc diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Connection.cpp --- a/Core/SQLite/Connection.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Connection.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -34,17 +35,19 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" +#include +#endif + #include "Connection.h" +#include "OrthancSQLiteException.h" #include #include #include #include -#include - - namespace Orthanc { namespace SQLite @@ -67,7 +70,7 @@ { if (!db_) { - throw OrthancException("SQLite: The database is not opened"); + throw OrthancSQLiteException("SQLite: The database is not opened"); } } @@ -75,7 +78,7 @@ { if (db_) { - throw OrthancException("SQLite: Connection is already open"); + throw OrthancSQLiteException("SQLite: Connection is already open"); } int err = sqlite3_open(path.c_str(), &db_); @@ -83,7 +86,7 @@ { Close(); db_ = NULL; - throw OrthancException("SQLite: Unable to open the database"); + throw OrthancSQLiteException("SQLite: Unable to open the database"); } // Execute PRAGMAs at this point @@ -129,7 +132,7 @@ { if (i->second->GetReferenceCount() >= 1) { - throw OrthancException("SQLite: This cached statement is already being referred to"); + throw OrthancSQLiteException("SQLite: This cached statement is already being referred to"); } return *i->second; @@ -145,13 +148,16 @@ bool Connection::Execute(const char* sql) { +#if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Connection::Execute " << sql; +#endif + CheckIsOpen(); int error = sqlite3_exec(db_, sql, NULL, NULL, NULL); if (error == SQLITE_ERROR) { - throw OrthancException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_))); + throw OrthancSQLiteException("SQLite Execute error: " + std::string(sqlite3_errmsg(db_))); } else { @@ -272,7 +278,7 @@ { if (!transactionNesting_) { - throw OrthancException("Rolling back a nonexistent transaction"); + throw OrthancSQLiteException("Rolling back a nonexistent transaction"); } transactionNesting_--; @@ -291,7 +297,7 @@ { if (!transactionNesting_) { - throw OrthancException("Committing a nonexistent transaction"); + throw OrthancSQLiteException("Committing a nonexistent transaction"); } transactionNesting_--; @@ -359,7 +365,7 @@ if (err != SQLITE_OK) { delete func; - throw OrthancException("SQLite: Unable to register a function"); + throw OrthancSQLiteException("SQLite: Unable to register a function"); } return func; @@ -368,12 +374,15 @@ void Connection::FlushToDisk() { +#if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Connection::FlushToDisk"; +#endif + int err = sqlite3_wal_checkpoint(db_, NULL); if (err != SQLITE_OK) { - throw OrthancException("SQLite: Unable to flush the database"); + throw OrthancSQLiteException("SQLite: Unable to flush the database"); } } } diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Connection.h --- a/Core/SQLite/Connection.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Connection.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/FunctionContext.cpp --- a/Core/SQLite/FunctionContext.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/FunctionContext.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are @@ -31,8 +32,12 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" +#endif + #include "FunctionContext.h" +#include "OrthancSQLiteException.h" #include @@ -57,7 +62,7 @@ { if (index >= argc_) { - throw OrthancException(ErrorCode_ParameterOutOfRange); + throw OrthancSQLiteException("Parameter out of range"); } } diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/FunctionContext.h --- a/Core/SQLite/FunctionContext.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/FunctionContext.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/IScalarFunction.h --- a/Core/SQLite/IScalarFunction.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/IScalarFunction.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/OrthancSQLiteException.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/SQLite/OrthancSQLiteException.h Mon Nov 10 16:33:51 2014 +0100 @@ -0,0 +1,67 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium + * + * Copyright (c) 2012 The Chromium Authors. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc., the name of the CHU of Liege, + * nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + **/ + + +#pragma once + + +#if ORTHANC_SQLITE_STANDALONE == 1 +#include + +namespace Orthanc +{ + namespace SQLite + { + class OrthancSQLiteException : public ::std::runtime_error + { + public: + OrthancSQLiteException(const std::string& what) : + ::std::runtime_error(what) + { + } + + OrthancSQLiteException(const char* what) : + ::std::runtime_error(what) + { + } + }; + } +} + +#else +# include "../OrthancException.h" +# define OrthancSQLiteException ::Orthanc::OrthancException +#endif diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/README.txt --- a/Core/SQLite/README.txt Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/README.txt Mon Nov 10 16:33:51 2014 +0100 @@ -19,6 +19,17 @@ coding conventions. +Reuse in another software +========================= + +To use the Orthanc SQLite wrapper in another project than Orthanc, you +just have to define the "ORTHANC_SQLITE_STANDALONE" macro. + +All the C++ exceptions generated by the wrapper will be objects of the +class "::Orthanc::SQLite::OrthancSQLiteException", that derives from +the standard exception class "::std::runtime_error". + + Licensing ========= @@ -26,4 +37,4 @@ order to respect the original license of the code. It is pretty straightforward to extract the code from this folder and -to include it in another project. +to include it in another project. diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Statement.cpp --- a/Core/SQLite/Statement.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Statement.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -34,16 +35,17 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" -#include "Statement.h" +#include +#endif +#include "Statement.h" #include "Connection.h" -#include "../Toolbox.h" #include #include #include -#include namespace Orthanc { @@ -54,7 +56,7 @@ bool succeeded = (err == SQLITE_OK || err == SQLITE_ROW || err == SQLITE_DONE); if (!succeeded) { - throw OrthancException("SQLite error code " + boost::lexical_cast(err)); + throw OrthancSQLiteException("SQLite error code " + boost::lexical_cast(err)); } return err; @@ -65,11 +67,11 @@ if (err == SQLITE_RANGE) { // Binding to a non-existent variable is evidence of a serious error. - throw OrthancException("Bind value out of range"); + throw OrthancSQLiteException("Bind value out of range"); } else if (err != SQLITE_OK) { - throw OrthancException("SQLite error code " + boost::lexical_cast(err)); + throw OrthancSQLiteException("SQLite error code " + boost::lexical_cast(err)); } } @@ -108,13 +110,19 @@ bool Statement::Run() { +#if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Statement::Run " << sqlite3_sql(GetStatement()); +#endif + return CheckError(sqlite3_step(GetStatement())) == SQLITE_DONE; } bool Statement::Step() { +#if ORTHANC_SQLITE_STANDALONE != 1 VLOG(1) << "SQLite::Statement::Step " << sqlite3_sql(GetStatement()); +#endif + return CheckError(sqlite3_step(GetStatement())) == SQLITE_ROW; } @@ -206,7 +214,7 @@ ColumnType Statement::GetDeclaredColumnType(int col) const { std::string column_type(sqlite3_column_decltype(GetStatement(), col)); - Toolbox::ToLowerCase(column_type); + std::transform(column_type.begin(), column_type.end(), column_type.begin(), tolower); if (column_type == "integer") return COLUMN_TYPE_INTEGER; diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Statement.h --- a/Core/SQLite/Statement.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Statement.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -36,7 +37,7 @@ #pragma once -#include "../OrthancException.h" +#include "OrthancSQLiteException.h" #include "StatementId.h" #include "StatementReference.h" diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/StatementId.cpp --- a/Core/SQLite/StatementId.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/StatementId.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -34,7 +35,10 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" +#endif + #include "StatementId.h" #include diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/StatementId.h --- a/Core/SQLite/StatementId.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/StatementId.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/StatementReference.cpp --- a/Core/SQLite/StatementReference.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/StatementReference.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -34,13 +35,15 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" +#include +#endif + #include "StatementReference.h" - -#include "../OrthancException.h" +#include "OrthancSQLiteException.h" #include -#include #include "sqlite3.h" namespace Orthanc @@ -65,7 +68,7 @@ { if (database == NULL || sql == NULL) { - throw OrthancException(ErrorCode_ParameterOutOfRange); + throw OrthancSQLiteException("Parameter out of range"); } root_ = NULL; @@ -74,7 +77,7 @@ int error = sqlite3_prepare_v2(database, sql, -1, &statement_, NULL); if (error != SQLITE_OK) { - throw OrthancException("SQLite: " + std::string(sqlite3_errmsg(database))); + throw OrthancSQLiteException("SQLite: " + std::string(sqlite3_errmsg(database))); } assert(IsRoot()); @@ -109,7 +112,9 @@ // an exception because: // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html +#if ORTHANC_SQLITE_STANDALONE != 1 LOG(ERROR) << "Bad value of the reference counter"; +#endif } else if (statement_ != NULL) { @@ -124,7 +129,9 @@ // an exception because: // http://www.parashift.com/c++-faq/dtors-shouldnt-throw.html +#if ORTHANC_SQLITE_STANDALONE != 1 LOG(ERROR) << "Bad value of the reference counter"; +#endif } else { diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/StatementReference.h --- a/Core/SQLite/StatementReference.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/StatementReference.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Transaction.cpp --- a/Core/SQLite/Transaction.cpp Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Transaction.cpp Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. * @@ -34,8 +35,12 @@ **/ +#if ORTHANC_SQLITE_STANDALONE != 1 #include "../PrecompiledHeaders.h" +#endif + #include "Transaction.h" +#include "OrthancSQLiteException.h" namespace Orthanc { @@ -59,13 +64,13 @@ { if (isOpen_) { - throw OrthancException("SQLite: Beginning a transaction twice!"); + throw OrthancSQLiteException("SQLite: Beginning a transaction twice!"); } isOpen_ = connection_.BeginTransaction(); if (!isOpen_) { - throw OrthancException("SQLite: Unable to create a transaction"); + throw OrthancSQLiteException("SQLite: Unable to create a transaction"); } } @@ -73,8 +78,8 @@ { if (!isOpen_) { - throw OrthancException("SQLite: Attempting to roll back a nonexistent transaction. " - "Did you remember to call Begin()?"); + throw OrthancSQLiteException("SQLite: Attempting to roll back a nonexistent transaction. " + "Did you remember to call Begin()?"); } isOpen_ = false; @@ -86,15 +91,15 @@ { if (!isOpen_) { - throw OrthancException("SQLite: Attempting to roll back a nonexistent transaction. " - "Did you remember to call Begin()?"); + throw OrthancSQLiteException("SQLite: Attempting to roll back a nonexistent transaction. " + "Did you remember to call Begin()?"); } isOpen_ = false; if (!connection_.CommitTransaction()) { - throw OrthancException("SQLite: Failure when committing the transaction"); + throw OrthancSQLiteException("SQLite: Failure when committing the transaction"); } } } diff -r c4ae92753d57 -r 9b9026560a5f Core/SQLite/Transaction.h --- a/Core/SQLite/Transaction.h Fri Nov 07 15:52:53 2014 +0100 +++ b/Core/SQLite/Transaction.h Mon Nov 10 16:33:51 2014 +0100 @@ -1,7 +1,8 @@ /** * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, - * Belgium + * + * Copyright (C) 2012-2014 Sebastien Jodogne , + * Medical Physics Department, CHU of Liege, Belgium * * Copyright (c) 2012 The Chromium Authors. All rights reserved. *