X-Git-Url: https://git.exim.org/exim.git/blobdiff_plain/d7d7b7b91dd75cec636fc144da7e27eed860f971..e9477a08d2d1f528b1f127f1d563d77e2cf24a22:/src/src/lookups/mysql.c diff --git a/src/src/lookups/mysql.c b/src/src/lookups/mysql.c index eace574e4..9511d2acb 100644 --- a/src/src/lookups/mysql.c +++ b/src/src/lookups/mysql.c @@ -1,10 +1,8 @@ -/* $Cambridge: exim/src/src/lookups/mysql.c,v 1.3 2006/02/07 11:19:01 ph10 Exp $ */ - /************************************************* * Exim - an Internet mail transport agent * *************************************************/ -/* Copyright (c) University of Cambridge 1995 - 2006 */ +/* 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 +11,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 /* The system header */ @@ -51,7 +33,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 +47,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 +184,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 +273,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 +335,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 +369,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 +412,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 */