1 /* $Cambridge: exim/src/src/lookups/lf_sqlperform.c,v 1.1 2007/08/23 10:16:51 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2007 */
8 /* See the file NOTICE for conditions of use and distribution. */
12 #include "lf_functions.h"
16 /*************************************************
17 * Call SQL server(s) to run an actual query *
18 *************************************************/
20 /* All the SQL lookups are of the same form, with a list of servers to try
21 until one can be accessed. It is now also possible to provide the server data
22 as part of the query. This function manages server selection and looping; each
23 lookup has its own function for actually performing the lookup.
26 name the lookup name, e.g. "MySQL"
27 optionname the name of the servers option, e.g. "mysql_servers"
28 optserverlist the value of the servers option
30 result where to pass back the result
31 errmsg where to pass back an error message
32 do_cache to be set FALSE if data is changed
33 func the lookup function to call
35 Returns: the return from the lookup function, or DEFER
39 lf_sqlperform(uschar *name, uschar *optionname, uschar *optserverlist,
40 uschar *query, uschar **result, uschar **errmsg, BOOL *do_cache,
41 int(*fn)(uschar *, uschar *, uschar **, uschar **, BOOL *, BOOL *))
47 BOOL defer_break = FALSE;
49 DEBUG(D_lookup) debug_printf("%s query: %s\n", name, query);
51 /* Handle queries that do not have server information at the start. */
53 if (Ustrncmp(query, "servers", 7) != 0)
56 serverlist = optserverlist;
57 while ((server = string_nextinlist(&serverlist, &sep, buffer,
58 sizeof(buffer))) != NULL)
60 rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache);
61 if (rc != DEFER || defer_break) return rc;
63 if (optserverlist == NULL)
64 *errmsg = string_sprintf("no %s servers defined (%s option)", name,
68 /* Handle queries that do have server information at the start. */
79 while (isspace(*s)) s++;
82 *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
85 while (isspace(*s)) s++;
90 *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
97 *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
102 qserverlist = string_sprintf("%.*s", ss - s, s);
105 while ((qserver = string_nextinlist(&qserverlist, &qsep, qbuffer,
106 sizeof(qbuffer))) != NULL)
108 if (Ustrchr(qserver, '/') != NULL)
112 int len = Ustrlen(qserver);
115 serverlist = optserverlist;
116 while ((server = string_nextinlist(&serverlist, &sep, buffer,
117 sizeof(buffer))) != NULL)
119 if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
125 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
126 qserver, optionname);
131 rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache);
132 if (rc != DEFER || defer_break) return rc;
139 /* End of lf_sqlperform.c */