1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2015 */
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 zero if data is changed
31 func the lookup function to call
33 Returns: the return from the lookup function, or DEFER
37 lf_sqlperform(const uschar *name, const uschar *optionname,
38 const uschar *optserverlist, const uschar *query,
39 uschar **result, uschar **errmsg, uint *do_cache,
40 int(*fn)(const uschar *, uschar *, uschar **, uschar **, BOOL *, uint *))
44 const uschar *serverlist;
46 BOOL defer_break = FALSE;
48 DEBUG(D_lookup) debug_printf("%s query: %s\n", name, query);
50 /* Handle queries that do not have server information at the start. */
52 if (Ustrncmp(query, "servers", 7) != 0)
55 serverlist = optserverlist;
56 while ((server = string_nextinlist(&serverlist, &sep, buffer,
57 sizeof(buffer))) != NULL)
59 rc = (*fn)(query, server, result, errmsg, &defer_break, do_cache);
60 if (rc != DEFER || defer_break) return rc;
62 if (optserverlist == NULL)
63 *errmsg = string_sprintf("no %s servers defined (%s option)", name,
67 /* Handle queries that do have server information at the start. */
73 const uschar *qserverlist;
78 while (isspace(*s)) s++;
81 *errmsg = string_sprintf("missing = after \"servers\" in %s lookup", name);
84 while (isspace(*s)) s++;
89 *errmsg = string_sprintf("missing ; after \"servers=\" in %s lookup",
96 *errmsg = string_sprintf("\"servers=\" defines no servers in \"%s\"",
101 qserverlist = string_sprintf("%.*s", (int)(ss - s), s);
104 while ((qserver = string_nextinlist(&qserverlist, &qsep, qbuffer,
105 sizeof(qbuffer))) != NULL)
107 if (Ustrchr(qserver, '/') != NULL)
111 int len = Ustrlen(qserver);
114 serverlist = optserverlist;
115 while ((server = string_nextinlist(&serverlist, &sep, buffer,
116 sizeof(buffer))) != NULL)
118 if (Ustrncmp(server, qserver, len) == 0 && server[len] == '/')
124 *errmsg = string_sprintf("%s server \"%s\" not found in %s", name,
125 qserver, optionname);
130 rc = (*fn)(ss+1, server, result, errmsg, &defer_break, do_cache);
131 if (rc != DEFER || defer_break) return rc;
138 /* End of lf_sqlperform.c */