changeset 46:6a574d810b98

Compatibility with MySQL 8.0
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Jul 2018 12:27:40 +0200
parents 95f0f57f8920
children 771b3c4bf5f8
files Framework/MySQL/MySQLDatabase.cpp Framework/MySQL/MySQLDatabase.h Framework/MySQL/MySQLParameters.cpp Framework/MySQL/MySQLStatement.cpp Framework/MySQL/MySQLStatement.h Framework/MySQL/MySQLTransaction.cpp Framework/PostgreSQL/PostgreSQLDatabase.cpp Framework/PostgreSQL/PostgreSQLDatabase.h Framework/PostgreSQL/PostgreSQLResult.cpp Framework/PostgreSQL/PostgreSQLResult.h Framework/PostgreSQL/PostgreSQLStatement.cpp Framework/PostgreSQL/PostgreSQLStatement.h Framework/PostgreSQL/PostgreSQLTransaction.cpp MySQL/NEWS MySQL/UnitTests/UnitTestsMain.cpp Resources/CMake/DatabasesPluginConfiguration.cmake
diffstat 16 files changed, 118 insertions(+), 34 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/MySQL/MySQLDatabase.cpp	Tue Jul 17 08:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLDatabase.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLDatabase.h	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLParameters.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLStatement.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLStatement.h	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/MySQL/MySQLTransaction.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.h	Wed Jul 18 12:27:40 2018 +0200
@@ -50,10 +50,7 @@
     {
     }
 
-    ~PostgreSQLDatabase()
-    {
-      Close();
-    }
+    ~PostgreSQLDatabase();
 
     void Open();
 
--- a/Framework/PostgreSQL/PostgreSQLResult.cpp	Tue Jul 17 08:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLResult.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLResult.h	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLStatement.cpp	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLStatement.h	Wed Jul 18 12:27:40 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:50:53 2018 +0200
+++ b/Framework/PostgreSQL/PostgreSQLTransaction.cpp	Wed Jul 18 12:27:40 2018 +0200
@@ -49,6 +49,7 @@
       }
       catch (Orthanc::OrthancException&)
       {
+        // Ignore possible exceptions due to connection loss
       }
     }
   }
--- a/MySQL/NEWS	Tue Jul 17 08:50:53 2018 +0200
+++ b/MySQL/NEWS	Wed Jul 18 12:27:40 2018 +0200
@@ -1,6 +1,8 @@
 Pending changes in the mainline
 ===============================
 
+* Compatibility with MySQL 8.0
+* Improvement in the configuration of UNIX socket
 
 
 Release 1.0 (2018-07-17)
--- a/MySQL/UnitTests/UnitTestsMain.cpp	Tue Jul 17 08:50:53 2018 +0200
+++ b/MySQL/UnitTests/UnitTestsMain.cpp	Wed Jul 18 12:27:40 2018 +0200
@@ -190,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;
   }
 
@@ -222,7 +227,8 @@
   
   if (args.size() == 4)
   {
-    // UNIX flavor
+    // UNIX socket flavor
+    globalParameters_.SetHost("");
     globalParameters_.SetUnixSocket(args[0]);
     globalParameters_.SetUsername(args[1]);
     globalParameters_.SetPassword(args[2]);
@@ -230,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
   {
--- a/Resources/CMake/DatabasesPluginConfiguration.cmake	Tue Jul 17 08:50:53 2018 +0200
+++ b/Resources/CMake/DatabasesPluginConfiguration.cmake	Wed Jul 18 12:27:40 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()