1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
6 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
14 /*************************************************
15 * Call SQL server(s) to run an actual query *
16 *************************************************/
18 /* All the SQL lookups are of the same form, with a list of servers to try
19 until one can be accessed. It is now also possible to provide the server data
20 as part of the query. This function manages server selection and looping; each
21 lookup has its own function for actually performing the lookup.
24 name the lookup name, e.g. "MySQL"
25 optionname the name of the servers option, e.g. "mysql_servers"
26 optserverlist the value of the servers option
28 result where to pass back the result
29 errmsg where to pass back an error message
30 do_cache to be set FALSE if data is changed
31 func the lookup function to call
33 Returns: the return from the lookup function, or DEFER
37 lf_sqlperform(uschar *name, uschar *optionname, uschar *optserverlist,
38 uschar *query, uschar **result, uschar **errmsg, BOOL *do_cache,
39 int(*fn)(uschar *, uschar *, uschar **, uschar **, BOOL *, BOOL *))
45 BOOL defer_break = FALSE;
47 DEBUG(D_lookup) debug_printf("%s query: %s\n", name, query);
49 /* Handle queries that do not have server information at the start. */
51 if (Ustrncmp(query, "servers", 7) != 0)
54 serverlist = optserverlist;
55 while ((server = string_nextinlist(&serverlist, &sep, buffer,
56 sizeof(buffer))) != NULL)
58 rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache);
59 if (rc != DEFER || defer_break) return rc;
61 if (optserverlist == NULL)
62 *errmsg = string_sprintf("no %s servers defined (%s option)", name,
66 /* Handle queries that do have server information at the start. */
77 while (isspace(*s)) s++;
80 *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
83 while (isspace(*s)) s++;
88 *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
95 *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
100 qserverlist = string_sprintf("%.*s", ss - s, s);
103 while ((qserver = string_nextinlist(&qserverlist, &qsep, qbuffer,
104 sizeof(qbuffer))) != NULL)
106 if (Ustrchr(qserver, '/') != NULL)
110 int len = Ustrlen(qserver);
113 serverlist = optserverlist;
114 while ((server = string_nextinlist(&serverlist, &sep, buffer,
115 sizeof(buffer))) != NULL)
117 if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
123 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
124 qserver, optionname);
129 rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache);
130 if (rc != DEFER || defer_break) return rc;
137 /* End of lf_sqlperform.c */