changeset 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 c4ae92753d57
children 2255e66da726
files Core/SQLite/Connection.cpp Core/SQLite/Connection.h Core/SQLite/FunctionContext.cpp Core/SQLite/FunctionContext.h Core/SQLite/IScalarFunction.h Core/SQLite/OrthancSQLiteException.h Core/SQLite/README.txt Core/SQLite/Statement.cpp Core/SQLite/Statement.h Core/SQLite/StatementId.cpp Core/SQLite/StatementId.h Core/SQLite/StatementReference.cpp Core/SQLite/StatementReference.h Core/SQLite/Transaction.cpp Core/SQLite/Transaction.h
diffstat 15 files changed, 183 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- 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 <s.jodogne@gmail.com>,
+ * 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 <glog/logging.h>
+#endif
+
 #include "Connection.h"
+#include "OrthancSQLiteException.h"
 
 #include <memory>
 #include <cassert>
 #include <sqlite3.h>
 #include <string.h>
 
-#include <glog/logging.h>
-
-
 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");
       }
     }
   }
--- 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 <s.jodogne@gmail.com>,
+ * Medical Physics Department, CHU of Liege, Belgium
  *
  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
  *
--- 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 <s.jodogne@gmail.com>,
+ * 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 <sqlite3.h>
 
@@ -57,7 +62,7 @@
     {
       if (index >= argc_)
       {
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
+        throw OrthancSQLiteException("Parameter out of range");
       }
     }
 
--- 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 <s.jodogne@gmail.com>,
+ * 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
--- 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 <s.jodogne@gmail.com>,
+ * 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
--- /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 <s.jodogne@gmail.com>,
+ * 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 <stdexcept>
+
+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
--- 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. 
--- 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 <s.jodogne@gmail.com>,
+ * 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 <glog/logging.h>
+#endif
 
+#include "Statement.h"
 #include "Connection.h"
-#include "../Toolbox.h"
 
 #include <boost/lexical_cast.hpp>
 #include <sqlite3.h>
 #include <string.h>
-#include <glog/logging.h>
 
 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<std::string>(err));
+        throw OrthancSQLiteException("SQLite error code " + boost::lexical_cast<std::string>(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<std::string>(err));
+        throw OrthancSQLiteException("SQLite error code " + boost::lexical_cast<std::string>(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;
--- 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 <s.jodogne@gmail.com>,
+ * 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"
 
--- 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 <s.jodogne@gmail.com>,
+ * 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 <string.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 <s.jodogne@gmail.com>,
+ * Medical Physics Department, CHU of Liege, Belgium
  *
  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
  *
--- 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 <s.jodogne@gmail.com>,
+ * 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 <glog/logging.h>
+#endif
+
 #include "StatementReference.h"
-
-#include "../OrthancException.h"
+#include "OrthancSQLiteException.h"
 
 #include <cassert>
-#include <glog/logging.h>
 #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
         {
--- 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 <s.jodogne@gmail.com>,
+ * Medical Physics Department, CHU of Liege, Belgium
  *
  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
  *
--- 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 <s.jodogne@gmail.com>,
+ * 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");
       }
     }
   }
--- 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 <s.jodogne@gmail.com>,
+ * Medical Physics Department, CHU of Liege, Belgium
  *
  * Copyright (c) 2012 The Chromium Authors. All rights reserved.
  *