From 92385bb70537b67ce908a6f4dd3d79d1646b7a03 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Wed, 22 Nov 2017 14:14:39 +0100
Subject: [PATCH] Mysql: Support MariaDB 10.2 headerfiles.

The <mysql/mysql.h> from MariaDB Connector-C no longer includes
<mysql/mysql_version.h>, and in addition doesn't define
MYSQL_SERVER_VERSION anymore.
---
 src/modules/Mysql/acconfig.h   |  3 +++
 src/modules/Mysql/configure.in |  5 +++++
 src/modules/Mysql/mysql.c      | 30 ++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/src/modules/Mysql/acconfig.h b/src/modules/Mysql/acconfig.h
index c322016a5a..8c818ec0bf 100644
--- a/src/modules/Mysql/acconfig.h
+++ b/src/modules/Mysql/acconfig.h
@@ -58,6 +58,9 @@
 /* Define if your mysql.h defines SHUTDOWN_DEFAULT */
 #undef HAVE_SHUTDOWN_DEFAULT
 
+/* Define if your mysql.h defines MARIADB_CLIENT_VERSION */
+#undef HAVE_MARIADB_CLIENT_VERSION
+
 /* Define if MYSQL_FIELD has a charsetnr member */
 #undef HAVE_MYSQL_FIELD_CHARSETNR
 
diff --git a/src/modules/Mysql/configure.in b/src/modules/Mysql/configure.in
index 0a25da1c5f..7bc6b2539b 100644
--- a/src/modules/Mysql/configure.in
+++ b/src/modules/Mysql/configure.in
@@ -170,6 +170,8 @@ fi
   AC_CHECK_MYSQL_OPTIONS(MYSQL_OPT_LOCAL_INFILE)
   AC_CHECK_MYSQL_OPTIONS(SHUTDOWN_DEFAULT)
 
+  AC_CHECK_MYSQL_OPTIONS(MARIADB_CLIENT_VERSION)
+
   # Mysql libs
 
 dnl   if test x"$pike_cv_sys_os" = xWindows_NT ; then
@@ -401,6 +403,9 @@ int main(int argc, char *argv[])
     PIKE_CHECK_MYSQL_FUNC(mysql_port)
     PIKE_CHECK_MYSQL_FUNC(mysql_unix_port)
 
+    # MariaDB API functions:
+    PIKE_CHECK_MYSQL_FUNC(mariadb_get_info)
+
     if test "$pike_cv_mysql_mysql_fetch_lengths" = "yes"; then
       #
       # In 3.20.6b mysql_fetch_lengths() returns an uint *.
diff --git a/src/modules/Mysql/mysql.c b/src/modules/Mysql/mysql.c
index 9ddd7aa632..d8b22887bf 100644
--- a/src/modules/Mysql/mysql.c
+++ b/src/modules/Mysql/mysql.c
@@ -1867,11 +1867,41 @@ static void f_client_info(INT32 args)
 {
   pop_n_elems(args);
 
+#if defined(HAVE_MARIADB_GET_INFO) && defined(HAVE_MARIADB_CLIENT_VERSION)
+  /* MariaDB 10.2 and later (and probably some earlier versions as well). */
+  push_text("MariaDB/");
+  {
+    char *vers = NULL;
+    if (mariadb_get_info(NULL, MARIADB_CLIENT_VERSION, &vers) < 0) {
+      vers = "unknown";
+    }
+    push_text(vers);
+  }
+  f_add(2);
+#else
+#ifndef MYSQL_SERVER_VERSION
+  /* NB: MariaDB 10.2 <mysql/mysql.h> doesn't include this file,
+   *     and also doesn't define MYSQL_SERVER_VERSION.
+   */
+#include <mysql/mysql_version.h>
+#endif
+
 #ifndef MYSQL_COMPILATION_COMMENT
+#ifdef LIBMARIADB
+#define MYSQL_COMPILATION_COMMENT "MariaDB"
+#else
 #define MYSQL_COMPILATION_COMMENT "MySQL (Copyright Abandoned)"
 #endif
+#endif
 
+#ifdef LIBMYSQL_VERSION
+  /* MariaDB 10.2 and later (and probably some earlier versions as well). */
+  push_text(MYSQL_COMPILATION_COMMENT "/" LIBMYSQL_VERSION);
+#else
+  /* Older MySQL and MariaDB. */
   push_text(MYSQL_COMPILATION_COMMENT "/" MYSQL_SERVER_VERSION);
+#endif
+#endif
 }
 
 /*! @endmodule
-- 
GitLab