Support mysql stored procedures. Fixes: #965
[exim.git] / src / src / lookups / mysql.c
index a6fd5965ba6cbb0762e4b5be97f690ec80b38041..f2e9a1565bb461a2326ea45d8c59e238592a034c 100644 (file)
@@ -1,10 +1,10 @@
-/* $Cambridge: exim/src/src/lookups/mysql.c,v 1.2 2005/01/04 10:00:44 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 - 2005 */
+/* 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
@@ -202,7 +202,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 +291,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 +353,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
 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);
 }