changeset 47:771b3c4bf5f8

integration OrthancPostgreSQL-2.2->mainline
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Jul 2018 12:28:52 +0200
parents 6a574d810b98 (diff) 4b2dab49b6ea (current diff)
children c9b042ca57a5
files
diffstat 21 files changed, 182 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/MySQL/MySQLDatabase.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLDatabase.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -81,6 +81,19 @@
   }
 
 
+  MySQLDatabase::~MySQLDatabase()
+  {
+    try
+    {
+      Close();
+    }
+    catch (Orthanc::OrthancException&)
+    {
+      // Ignore possible exceptions due to connection loss
+    }
+  }
+
+
   void MySQLDatabase::LogError()
   {
     if (mysql_ != NULL)
@@ -118,6 +131,13 @@
       LOG(ERROR) << "Cannot initialize the MySQL connector";
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
     }
+
+    if (parameters_.GetUnixSocket().empty())
+    {
+      // Fallback to TCP connection if no UNIX socket is provided
+      unsigned int protocol = MYSQL_PROTOCOL_TCP;
+      mysql_options(mysql_, MYSQL_OPT_PROTOCOL, (unsigned int *) &protocol);
+    }
       
     const char* socket = (parameters_.GetUnixSocket().empty() ? NULL :
                           parameters_.GetUnixSocket().c_str());
--- a/Framework/MySQL/MySQLDatabase.h	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLDatabase.h	Wed Jul 18 12:28:52 2018 +0200
@@ -47,10 +47,7 @@
   public:
     MySQLDatabase(const MySQLParameters& parameters);
 
-    virtual ~MySQLDatabase()
-    {
-      Close();
-    }
+    virtual ~MySQLDatabase();
 
     void LogError();
 
--- a/Framework/MySQL/MySQLParameters.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLParameters.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -33,7 +33,13 @@
     password_.clear();
     database_.clear();
     port_ = 3306;
+
+#if defined(_WIN32)
+    unixSocket_.clear();
+#else
     unixSocket_ = "/var/run/mysqld/mysqld.sock";
+#endif
+    
     lock_ = true;
   }
 
@@ -106,7 +112,7 @@
   {
     if (database.empty())
     {
-      LOG(ERROR) << "Empty database name";
+      LOG(ERROR) << "MySQL: Empty database name";
       throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
     
@@ -114,8 +120,8 @@
     {
       if (!isalnum(database [i]))
       {
-        LOG(ERROR) << "Only alphanumeric characters are allowed in a "
-                   << "MySQL database name: \"" << database << "\"";
+        LOG(ERROR) << "MySQL: Only alphanumeric characters are allowed in a "
+                   << "database name: \"" << database << "\"";
         throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);          
       }
     }
@@ -139,6 +145,13 @@
   
   void MySQLParameters::SetUnixSocket(const std::string& socket)
   {
+#if defined(_WIN32)
+    if (!socket.empty())
+    {
+      LOG(WARNING) << "MySQL: Setting an UNIX socket on Windows has no effect";
+    }
+#endif
+    
     unixSocket_ = socket;
   }
 
--- a/Framework/MySQL/MySQLStatement.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLStatement.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -151,6 +151,7 @@
             case 45:   // utf8mb4_general_ci
             case 46:   // utf8mb4_bin
             case 224:  // utf8mb4_unicode_ci  => RECOMMENDED collation
+            case 255:  // utf8mb4_0900_ai_ci  => necessary for MySQL 8.0
               // https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
               orthancType_ = ValueType_Utf8String;
               break;
@@ -395,6 +396,19 @@
   }
 
 
+  MySQLStatement::~MySQLStatement()
+  {
+    try
+    {
+      Close();
+    }
+    catch (Orthanc::OrthancException&)
+    {
+      // Ignore possible exceptions due to connection loss
+    }
+  }
+
+
   MYSQL_STMT* MySQLStatement::GetObject()
   {
     if (statement_ == NULL)
--- a/Framework/MySQL/MySQLStatement.h	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLStatement.h	Wed Jul 18 12:28:52 2018 +0200
@@ -49,10 +49,7 @@
     MySQLStatement(MySQLDatabase& db,
                    const Query& query);
 
-    virtual ~MySQLStatement()
-    {
-      Close();
-    }
+    virtual ~MySQLStatement();
 
     virtual bool IsReadOnly() const
     {
--- a/Framework/MySQL/MySQLTransaction.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/MySQL/MySQLTransaction.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -52,6 +52,7 @@
       }
       catch (Orthanc::OrthancException&)
       {
+        // Ignore possible exceptions due to connection loss
       }
     }
   }
--- a/Framework/PostgreSQL/PostgreSQLDatabase.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -68,6 +68,19 @@
     }
   }
 
+
+  PostgreSQLDatabase::~PostgreSQLDatabase()
+  {
+    try
+    {
+      Close();
+    }
+    catch (Orthanc::OrthancException&)
+    {
+      // Ignore possible exceptions due to connection loss
+    }
+  }
+  
   
   void PostgreSQLDatabase::Open()
   {
--- a/Framework/PostgreSQL/PostgreSQLDatabase.h	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.h	Wed Jul 18 12:28:52 2018 +0200
@@ -50,10 +50,7 @@
     {
     }
 
-    ~PostgreSQLDatabase()
-    {
-      Close();
-    }
+    ~PostgreSQLDatabase();
 
     void Open();
 
--- a/Framework/PostgreSQL/PostgreSQLResult.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLResult.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -105,6 +105,19 @@
   }
 
 
+  PostgreSQLResult::~PostgreSQLResult()
+  {
+    try
+    {
+      Clear();
+    }
+    catch (Orthanc::OrthancException&)
+    {
+      // Ignore possible exceptions due to connection loss
+    }
+  }
+
+
   void PostgreSQLResult::Next()
   {
     position_++;
--- a/Framework/PostgreSQL/PostgreSQLResult.h	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLResult.h	Wed Jul 18 12:28:52 2018 +0200
@@ -46,10 +46,7 @@
   public:
     explicit PostgreSQLResult(PostgreSQLStatement& statement);
 
-    ~PostgreSQLResult()
-    {
-      Clear();
-    }
+    ~PostgreSQLResult();
 
     void Next();
 
--- a/Framework/PostgreSQL/PostgreSQLStatement.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLStatement.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -342,6 +342,19 @@
   }
 
 
+  PostgreSQLStatement::~PostgreSQLStatement()
+  {
+    try
+    {
+      Unprepare();
+    }
+    catch (Orthanc::OrthancException&)
+    {
+      // Ignore possible exceptions due to connection loss
+    }
+  }
+
+
   void PostgreSQLStatement::Run()
   {
     PGresult* result = reinterpret_cast<PGresult*>(Execute());
--- a/Framework/PostgreSQL/PostgreSQLStatement.h	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLStatement.h	Wed Jul 18 12:28:52 2018 +0200
@@ -72,10 +72,7 @@
     PostgreSQLStatement(PostgreSQLDatabase& database,
                         const Query& query);
 
-    ~PostgreSQLStatement()
-    {
-      Unprepare();
-    }
+    ~PostgreSQLStatement();
     
     virtual bool IsReadOnly() const
     {
--- a/Framework/PostgreSQL/PostgreSQLTransaction.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLTransaction.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -49,6 +49,7 @@
       }
       catch (Orthanc::OrthancException&)
       {
+        // Ignore possible exceptions due to connection loss
       }
     }
   }
--- a/MySQL/CMakeLists.txt	Tue Jul 17 08:53:20 2018 +0200
+++ b/MySQL/CMakeLists.txt	Wed Jul 18 12:28:52 2018 +0200
@@ -17,11 +17,45 @@
   
 include(${CMAKE_SOURCE_DIR}/../Resources/CMake/DatabasesPluginConfiguration.cmake)
 
+
+if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
+  execute_process(
+    COMMAND 
+    ${PYTHON_EXECUTABLE} ${ORTHANC_ROOT}/Resources/WindowsResources.py
+    ${ORTHANC_PLUGIN_VERSION} "MySQL storage area plugin" OrthancMySQLStorage.dll
+    "MySQL as a database back-end to Orthanc (storage area)"
+    ERROR_VARIABLE Failure
+    OUTPUT_FILE ${AUTOGENERATED_DIR}/StorageVersion.rc
+    )
+
+  if (Failure)
+    message(FATAL_ERROR "Error while computing the version information: ${Failure}")
+  endif()
+
+  execute_process(
+    COMMAND 
+    ${PYTHON_EXECUTABLE} ${ORTHANC_ROOT}/Resources/WindowsResources.py
+    ${ORTHANC_PLUGIN_VERSION} "MySQL index plugin" OrthancMySQLIndex.dll
+    "MySQL as a database back-end to Orthanc (index)"
+    ERROR_VARIABLE Failure
+    OUTPUT_FILE ${AUTOGENERATED_DIR}/IndexVersion.rc
+    )
+
+  if (Failure)
+    message(FATAL_ERROR "Error while computing the version information: ${Failure}")
+  endif()
+
+  set(INDEX_RESOURCES ${AUTOGENERATED_DIR}/IndexVersion.rc)
+  set(STORAGE_RESOURCES ${AUTOGENERATED_DIR}/StorageVersion.rc)
+endif()
+
+
 EmbedResources(
   MYSQL_PREPARE_INDEX ${CMAKE_SOURCE_DIR}/Plugins/PrepareIndex.sql
   )
 
 add_library(OrthancMySQLIndex SHARED
+  ${INDEX_RESOURCES}
   ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp
   Plugins/IndexPlugin.cpp
   Plugins/MySQLIndex.cpp
@@ -32,6 +66,7 @@
 
 add_library(OrthancMySQLStorage SHARED
   ${ORTHANC_DATABASES_ROOT}/Framework/Plugins/PluginInitialization.cpp
+  ${STORAGE_RESOURCES}
   Plugins/MySQLStorageArea.cpp
   Plugins/StoragePlugin.cpp
 
--- a/MySQL/NEWS	Tue Jul 17 08:53:20 2018 +0200
+++ b/MySQL/NEWS	Wed Jul 18 12:28:52 2018 +0200
@@ -1,4 +1,11 @@
 Pending changes in the mainline
 ===============================
 
+* Compatibility with MySQL 8.0
+* Improvement in the configuration of UNIX socket
+
+
+Release 1.0 (2018-07-17)
+========================
+
 * Initial release
--- a/MySQL/Plugins/IndexPlugin.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/MySQL/Plugins/IndexPlugin.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -23,7 +23,9 @@
 #include "../../Framework/MySQL/MySQLDatabase.h"
 #include "../../Framework/Plugins/PluginInitialization.h"
 
+#include <Core/HttpClient.h>
 #include <Core/Logging.h>
+#include <Core/Toolbox.h>
 
 static std::auto_ptr<OrthancDatabases::MySQLIndex> backend_;
 
@@ -37,6 +39,9 @@
       return -1;
     }
 
+    Orthanc::Toolbox::InitializeOpenSsl();
+    Orthanc::HttpClient::GlobalInitialize();
+
     OrthancPlugins::OrthancConfiguration configuration(context);
 
     if (!configuration.IsSection("MySQL"))
@@ -88,6 +93,8 @@
 
     backend_.reset(NULL);
     OrthancDatabases::MySQLDatabase::GlobalFinalization();
+    Orthanc::HttpClient::GlobalFinalize();
+    Orthanc::Toolbox::FinalizeOpenSsl();
   }
 
 
--- a/MySQL/Plugins/StoragePlugin.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/MySQL/Plugins/StoragePlugin.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -23,7 +23,9 @@
 #include "../../Framework/MySQL/MySQLDatabase.h"
 #include "../../Framework/Plugins/PluginInitialization.h"
 
+#include <Core/HttpClient.h>
 #include <Core/Logging.h>
+#include <Core/Toolbox.h>
 
 
 extern "C"
@@ -35,6 +37,9 @@
       return -1;
     }
 
+    Orthanc::Toolbox::InitializeOpenSsl();
+    Orthanc::HttpClient::GlobalInitialize();
+
     OrthancPlugins::OrthancConfiguration configuration(context);
 
     if (!configuration.IsSection("MySQL"))
@@ -82,6 +87,8 @@
 
     OrthancDatabases::StorageBackend::Finalize();
     OrthancDatabases::MySQLDatabase::GlobalFinalization();
+    Orthanc::HttpClient::GlobalFinalize();
+    Orthanc::Toolbox::FinalizeOpenSsl();
   }
 
 
--- a/MySQL/UnitTests/UnitTestsMain.cpp	Tue Jul 17 08:53:20 2018 +0200
+++ b/MySQL/UnitTests/UnitTestsMain.cpp	Wed Jul 18 12:28:52 2018 +0200
@@ -31,7 +31,9 @@
 #include "../../Framework/MySQL/MySQLTransaction.h"
 #include "../../Framework/Plugins/IndexUnitTests.h"
 
+#include <Core/HttpClient.h>
 #include <Core/Logging.h>
+#include <Core/Toolbox.h>
 
 #include <gtest/gtest.h>
 
@@ -188,14 +190,19 @@
 {
   if (argc < 5)
   {
-    std::cerr << "Usage (UNIX):    " << argv[0] << " <socket> <username> <password> <database>"
-              << std::endl
-              << "Usage (Windows): " << argv[0] << " <host> <port> <username> <password> <database>"
-              << std::endl << std::endl
-              << "Example (UNIX):    " << argv[0] << " /var/run/mysqld/mysqld.sock root root orthanctest"
-              << std::endl
-              << "Example (Windows): " << argv[0] << " localhost 3306 root root orthanctest"
-              << std::endl << std::endl;
+    std::cerr
+#if !defined(_WIN32)
+      << "Usage (UNIX socket):      " << argv[0] << " <socket> <username> <password> <database>"
+      << std::endl
+#endif
+      << "Usage (TCP connection):   " << argv[0] << " <host> <port> <username> <password> <database>"
+      << std::endl << std::endl
+#if !defined(_WIN32)
+      << "Example (UNIX socket):    " << argv[0] << " /var/run/mysqld/mysqld.sock root root orthanctest"
+      << std::endl
+#endif
+      << "Example (TCP connection): " << argv[0] << " localhost 3306 root root orthanctest"
+      << std::endl << std::endl;
     return -1;
   }
 
@@ -215,10 +222,13 @@
   Orthanc::Logging::Initialize();
   Orthanc::Logging::EnableInfoLevel(true);
   Orthanc::Logging::EnableTraceLevel(true);
+  Orthanc::Toolbox::InitializeOpenSsl();
+  Orthanc::HttpClient::GlobalInitialize();
   
   if (args.size() == 4)
   {
-    // UNIX flavor
+    // UNIX socket flavor
+    globalParameters_.SetHost("");
     globalParameters_.SetUnixSocket(args[0]);
     globalParameters_.SetUsername(args[1]);
     globalParameters_.SetPassword(args[2]);
@@ -226,12 +236,15 @@
   }
   else if (args.size() == 5)
   {
-    // Windows flavor
+    // TCP connection flavor
     globalParameters_.SetHost(args[0]);
     globalParameters_.SetPort(boost::lexical_cast<unsigned int>(args[1]));
     globalParameters_.SetUsername(args[2]);
     globalParameters_.SetPassword(args[3]);
     globalParameters_.SetDatabase(args[4]);
+
+    // Force the use of TCP on localhost, even if UNIX sockets are available
+    globalParameters_.SetUnixSocket("");
   }
   else
   {
@@ -246,9 +259,10 @@
 
   int result = RUN_ALL_TESTS();
 
+  Orthanc::HttpClient::GlobalFinalize();
+  Orthanc::Toolbox::FinalizeOpenSsl();
+  OrthancDatabases::MySQLDatabase::GlobalFinalization();
   Orthanc::Logging::Finalize();
 
-  OrthancDatabases::MySQLDatabase::GlobalFinalization();
-
   return result;
 }
--- a/PostgreSQL/CMakeLists.txt	Tue Jul 17 08:53:20 2018 +0200
+++ b/PostgreSQL/CMakeLists.txt	Wed Jul 18 12:28:52 2018 +0200
@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 2.8)
 project(OrthancPostgreSQL)
 
-set(ORTHANC_PLUGIN_VERSION "2.2")
+set(ORTHANC_PLUGIN_VERSION "mainline")
 
 if (ORTHANC_PLUGIN_VERSION STREQUAL "mainline")
   set(ORTHANC_FRAMEWORK_VERSION "mainline")
--- a/Resources/CMake/DatabasesFrameworkConfiguration.cmake	Tue Jul 17 08:53:20 2018 +0200
+++ b/Resources/CMake/DatabasesFrameworkConfiguration.cmake	Wed Jul 18 12:28:52 2018 +0200
@@ -35,11 +35,11 @@
   set(ENABLE_CRYPTO_OPTIONS ON)
   set(ENABLE_SSL ON)
   set(ENABLE_ZLIB ON)
-  set(ENABLE_LOCALE ON)           # iconv is needed
+  set(ENABLE_LOCALE ON)      # iconv is needed
+  set(ENABLE_WEB_CLIENT ON)  # libcurl is needed
 
   if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
     set(ENABLE_OPENSSL_ENGINES ON)
-    set(ENABLE_WEB_CLIENT ON)       # libcurl is needed if targetting Windows
   endif()
 endif()
 
--- a/Resources/CMake/DatabasesPluginConfiguration.cmake	Tue Jul 17 08:53:20 2018 +0200
+++ b/Resources/CMake/DatabasesPluginConfiguration.cmake	Wed Jul 18 12:28:52 2018 +0200
@@ -33,7 +33,7 @@
     message(FATAL_ERROR "Unsupported version of the Orthanc plugin SDK: ${ORTHANC_SDK_VERSION}")
   endif()
 else ()
-  CHECK_INCLUDE_FILE_CXX(orthanc/OrthancCppDatabasePlugin.h HAVE_ORTHANC_H)
+  CHECK_INCLUDE_FILE_CXX(orthanc/OrthancCDatabasePlugin.h HAVE_ORTHANC_H)
   if (NOT HAVE_ORTHANC_H)
     message(FATAL_ERROR "Please install the headers of the Orthanc plugins SDK")
   endif()