Version reporting & module ABI change.
[exim.git] / src / src / lookups / mysql.c
index fbd8127a28357ae9a89fce5640ee374ec41be34e..755676e8f37e53b7abdca5392ce8e78976be5474 100644 (file)
@@ -1,10 +1,10 @@
-/* $Cambridge: exim/src/src/lookups/mysql.c,v 1.1 2004/10/07 13:10:01 ph10 Exp $ */
+/* $Cambridge: exim/src/src/lookups/mysql.c,v 1.7 2010/03/05 15:59:29 nm4 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2004 */
+/* Copyright (c) University of Cambridge 1995 - 2009 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* Thanks to Paul Kelly for contributing the original code for these
@@ -13,22 +13,6 @@ functions. */
 
 #include "../exim.h"
 #include "lf_functions.h"
-#include "mysql.h"       /* The local header */
-
-
-/* We can't just compile this code and allow the library mechanism to omit the
-functions if they are not wanted, because we need to have the MYSQL header
-available for compiling. Therefore, compile these functions only if
-LOOKUP_MYSQL is defined. However, some compilers don't like compiling empty
-modules, so keep them happy with a dummy when skipping the rest. Make it
-reference itself to stop picky compilers complaining that it is unused, and put
-in a dummy argument to stop even pickier compilers complaining about infinite
-loops. */
-
-#ifndef LOOKUP_MYSQL
-static void dummy(int x) { dummy(x-1); }
-#else
-
 
 #include <mysql.h>       /* The system header */
 
@@ -51,7 +35,7 @@ static mysql_connection *mysql_connections = NULL;
 
 /* See local README for interface description. */
 
-void *
+static void *
 mysql_open(uschar *filename, uschar **errmsg)
 {
 return (void *)(1);    /* Just return something non-null */
@@ -65,7 +49,7 @@ return (void *)(1);    /* Just return something non-null */
 
 /* See local README for interface description. */
 
-void
+static void
 mysql_tidy(void)
 {
 mysql_connection *cn;
@@ -202,7 +186,7 @@ if (cn == NULL)
   if (mysql_real_connect(mysql_handle,
       /*  host        user         passwd     database */
       CS sdata[0], CS sdata[2], CS sdata[3], CS sdata[1],
-      port, CS socket, 0) == NULL)
+      port, CS socket, CLIENT_MULTI_RESULTS) == NULL)
     {
     *errmsg = string_sprintf("MYSQL connection failed: %s",
       mysql_error(mysql_handle));
@@ -291,6 +275,21 @@ while ((mysql_row_data = mysql_fetch_row(mysql_result)) != NULL)
     }
   }
 
+/* more results? -1 = no, >0 = error, 0 = yes (keep looping)
+   This is needed because of the CLIENT_MULTI_RESULTS on mysql_real_connect(),
+   we don't expect any more results. */
+
+while((i = mysql_next_result(mysql_handle)) >= 0) {
+   if(i == 0) {   /* Just ignore more results */
+     DEBUG(D_lookup) debug_printf("MYSQL: got unexpected more results\n");
+     continue;
+   }
+
+   *errmsg = string_sprintf("MYSQL: lookup result error when checking for more results: %s\n",
+       mysql_error(mysql_handle));
+   goto MYSQL_EXIT;
+}
+
 /* If result is NULL then no data has been found and so we return FAIL.
 Otherwise, we must terminate the string which has been built; string_cat()
 always leaves enough room for a terminating zero. */
@@ -338,32 +337,16 @@ else
 *************************************************/
 
 /* See local README for interface description. The handle and filename
-arguments are not used. Loop through a list of servers while the query is
-deferred with a retryable error. */
+arguments are not used. The code to loop through a list of servers while the
+query is deferred with a retryable error is now in a separate function that is
+shared with other SQL lookups. */
 
-int
+static int
 mysql_find(void *handle, uschar *filename, uschar *query, int length,
   uschar **result, uschar **errmsg, BOOL *do_cache)
 {
-int sep = 0;
-uschar *server;
-uschar *list = mysql_servers;
-uschar buffer[512];
-
-DEBUG(D_lookup) debug_printf("MYSQL query: %s\n", query);
-
-while ((server = string_nextinlist(&list, &sep, buffer, sizeof(buffer))) != NULL)
-  {
-  BOOL defer_break = FALSE;
-  int rc = perform_mysql_search(query, server, result, errmsg, &defer_break,
-    do_cache);
-  if (rc != DEFER || defer_break) return rc;
-  }
-
-if (mysql_servers == NULL)
-  *errmsg = US"no MYSQL servers defined (mysql_servers option)";
-
-return DEFER;
+return lf_sqlperform(US"MySQL", US"mysql_servers", mysql_servers, query,
+  result, errmsg, do_cache, perform_mysql_search);
 }
 
 
@@ -388,7 +371,7 @@ Arguments:
 Returns:     the processed string or NULL for a bad option
 */
 
-uschar *
+static uschar *
 mysql_quote(uschar *s, uschar *opt)
 {
 register int c;
@@ -431,6 +414,45 @@ return quoted;
 }
 
 
-#endif  /* MYSQL_LOOKUP */
+/*************************************************
+*         Version reporting entry point          *
+*************************************************/
+
+/* See local README for interface description. */
+
+#include "../version.h"
+
+void
+mysql_version_report(FILE *f)
+{
+fprintf(f, "Library version: MySQL: Compile: %s [%s]\n"
+           "                        Runtime: %s\n",
+        MYSQL_SERVER_VERSION, MYSQL_COMPILATION_COMMENT,
+        mysql_get_client_info());
+#ifdef DYNLOOKUP
+fprintf(f, "                        Exim version %s\n", EXIM_VERSION_STR);
+#endif
+}
+
+/* These are the lookup_info blocks for this driver */
+
+static lookup_info mysql_lookup_info = {
+  US"mysql",                     /* lookup name */
+  lookup_querystyle,             /* query-style lookup */
+  mysql_open,                    /* open function */
+  NULL,                          /* no check function */
+  mysql_find,                    /* find function */
+  NULL,                          /* no close function */
+  mysql_tidy,                    /* tidy function */
+  mysql_quote,                   /* quoting function */
+  mysql_version_report           /* version reporting */
+};
+
+#ifdef DYNLOOKUP
+#define mysql_lookup_module_info _lookup_module_info
+#endif
+
+static lookup_info *_lookup_list[] = { &mysql_lookup_info };
+lookup_module_info mysql_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
 
 /* End of lookups/mysql.c */