1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2022 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
10 #include "lf_functions.h"
15 /*************************************************
17 *************************************************/
19 /* See local README for interface description. */
22 sqlite_open(const uschar * filename, uschar ** errmsg)
27 if (!filename || !*filename)
29 DEBUG(D_lookup) debug_printf_indent("Using sqlite_dbfile: %s\n", sqlite_dbfile);
30 filename = sqlite_dbfile;
32 if (!filename || *filename != '/')
33 *errmsg = US"absolute file name expected for \"sqlite\" lookup";
34 else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
36 *errmsg = (void *)sqlite3_errmsg(db);
39 DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
43 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
48 /*************************************************
50 *************************************************/
52 /* See local README for interface description. */
55 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
57 gstring * res = *(gstring **)arg;
59 /* For second and subsequent results, insert \n */
62 res = string_catn(res, US"\n", 1);
66 /* For multiple fields, include the field name too */
67 for (int i = 0; i < argc; i++)
69 uschar * value = US(argv[i] ? argv[i] : "<NULL>");
70 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
75 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
77 /* always return a non-null gstring, even for a zero-length string result */
78 *(gstring **)arg = res ? res : string_get(1);
84 sqlite_find(void * handle, const uschar * filename, const uschar * query,
85 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
91 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
94 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
98 if (!res) *do_cache = 0; /* on fail, wipe cache */
100 *result = string_from_gstring(res);
106 /*************************************************
107 * Close entry point *
108 *************************************************/
110 /* See local README for interface description. */
112 static void sqlite_close(void *handle)
114 sqlite3_close(handle);
119 /*************************************************
120 * Quote entry point *
121 *************************************************/
123 /* From what I have found so far, the only character that needs to be quoted
124 for sqlite is the single quote, and it is quoted by doubling.
127 s the string to be quoted
128 opt additional option text or NULL if none
129 idx lookup type index
131 Returns: the processed string or NULL for a bad option
135 sqlite_quote(uschar * s, uschar * opt, unsigned idx)
138 uschar * t = s, * quoted;
140 if (opt) return NULL; /* No options recognized */
142 while ((c = *t++)) if (c == '\'') count++;
145 t = quoted = store_get_quoted(count + 1, s, idx);
149 if (c == '\'') *t++ = '\'';
159 /*************************************************
160 * Version reporting entry point *
161 *************************************************/
163 /* See local README for interface description. */
165 #include "../version.h"
168 sqlite_version_report(gstring * g)
170 g = string_fmt_append(g,
171 "Library version: SQLite: Compile: %s\n"
173 SQLITE_VERSION, sqlite3_libversion());
175 g = string_fmt_append(g,
176 " Exim version %s\n", EXIM_VERSION_STR);
181 static lookup_info _lookup_info = {
182 .name = US"sqlite", /* lookup name */
183 .type = lookup_absfilequery, /* query-style lookup, starts with file name */
184 .open = sqlite_open, /* open function */
185 .check = NULL, /* no check function */
186 .find = sqlite_find, /* find function */
187 .close = sqlite_close, /* close function */
188 .tidy = NULL, /* no tidy function */
189 .quote = sqlite_quote, /* quoting function */
190 .version_report = sqlite_version_report /* version reporting */
194 #define sqlite_lookup_module_info _lookup_module_info
197 static lookup_info *_lookup_list[] = { &_lookup_info };
198 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
200 /* End of lookups/sqlite.c */