1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description. */
21 sqlite_open(const uschar * filename, uschar ** errmsg)
26 if ((ret = sqlite3_open(CCS filename, &db)) != 0)
28 *errmsg = (void *)sqlite3_errmsg(db);
29 debug_printf_indent("Error opening database: %s\n", *errmsg);
32 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
37 /*************************************************
39 *************************************************/
41 /* See local README for interface description. */
44 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
46 gstring * res = *(gstring **)arg;
48 /* For second and subsequent results, insert \n */
51 res = string_catn(res, US"\n", 1);
55 /* For multiple fields, include the field name too */
56 for (int i = 0; i < argc; i++)
58 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
59 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
64 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
66 *(gstring **)arg = res;
72 sqlite_find(void * handle, const uschar * filename, const uschar * query,
73 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
78 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
81 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
85 if (!res) *do_cache = 0;
87 *result = string_from_gstring(res);
93 /*************************************************
95 *************************************************/
97 /* See local README for interface description. */
99 static void sqlite_close(void *handle)
101 sqlite3_close(handle);
106 /*************************************************
107 * Quote entry point *
108 *************************************************/
110 /* From what I have found so far, the only character that needs to be quoted
111 for sqlite is the single quote, and it is quoted by doubling.
114 s the string to be quoted
115 opt additional option text or NULL if none
117 Returns: the processed string or NULL for a bad option
121 sqlite_quote(uschar *s, uschar *opt)
128 if (opt != NULL) return NULL; /* No options recognized */
130 while ((c = *t++) != 0) if (c == '\'') count++;
132 if (count == 0) return s;
133 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
135 while ((c = *s++) != 0)
137 if (c == '\'') *t++ = '\'';
147 /*************************************************
148 * Version reporting entry point *
149 *************************************************/
151 /* See local README for interface description. */
153 #include "../version.h"
156 sqlite_version_report(FILE *f)
158 fprintf(f, "Library version: SQLite: Compile: %s\n"
160 SQLITE_VERSION, sqlite3_libversion());
162 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
166 static lookup_info _lookup_info = {
167 US"sqlite", /* lookup name */
168 lookup_absfilequery, /* query-style lookup, starts with file name */
169 sqlite_open, /* open function */
170 NULL, /* no check function */
171 sqlite_find, /* find function */
172 sqlite_close, /* close function */
173 NULL, /* no tidy function */
174 sqlite_quote, /* quoting function */
175 sqlite_version_report /* version reporting */
179 #define sqlite_lookup_module_info _lookup_module_info
182 static lookup_info *_lookup_list[] = { &_lookup_info };
183 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
185 /* End of lookups/sqlite.c */