1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* Copyright (c) The Exim Maintainers 2020 */
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) filename = sqlite_dbfile;
29 *errmsg = US"absolute file name expected for \"sqlite\" lookup";
30 else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
32 *errmsg = (void *)sqlite3_errmsg(db);
33 DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
36 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
41 /*************************************************
43 *************************************************/
45 /* See local README for interface description. */
48 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
50 gstring * res = *(gstring **)arg;
52 /* For second and subsequent results, insert \n */
55 res = string_catn(res, US"\n", 1);
59 /* For multiple fields, include the field name too */
60 for (int i = 0; i < argc; i++)
62 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
63 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
68 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
70 *(gstring **)arg = res;
76 sqlite_find(void * handle, const uschar * filename, const uschar * query,
77 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
83 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
86 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
90 if (!res) *do_cache = 0;
92 *result = string_from_gstring(res);
98 /*************************************************
100 *************************************************/
102 /* See local README for interface description. */
104 static void sqlite_close(void *handle)
106 sqlite3_close(handle);
111 /*************************************************
112 * Quote entry point *
113 *************************************************/
115 /* From what I have found so far, the only character that needs to be quoted
116 for sqlite is the single quote, and it is quoted by doubling.
119 s the string to be quoted
120 opt additional option text or NULL if none
122 Returns: the processed string or NULL for a bad option
126 sqlite_quote(uschar *s, uschar *opt)
133 if (opt != NULL) return NULL; /* No options recognized */
135 while ((c = *t++) != 0) if (c == '\'') count++;
137 if (count == 0) return s;
138 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
140 while ((c = *s++) != 0)
142 if (c == '\'') *t++ = '\'';
152 /*************************************************
153 * Version reporting entry point *
154 *************************************************/
156 /* See local README for interface description. */
158 #include "../version.h"
161 sqlite_version_report(FILE *f)
163 fprintf(f, "Library version: SQLite: Compile: %s\n"
165 SQLITE_VERSION, sqlite3_libversion());
167 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
171 static lookup_info _lookup_info = {
172 .name = US"sqlite", /* lookup name */
173 .type = lookup_absfilequery, /* query-style lookup, starts with file name */
174 .open = sqlite_open, /* open function */
175 .check = NULL, /* no check function */
176 .find = sqlite_find, /* find function */
177 .close = sqlite_close, /* close function */
178 .tidy = NULL, /* no tidy function */
179 .quote = sqlite_quote, /* quoting function */
180 .version_report = sqlite_version_report /* version reporting */
184 #define sqlite_lookup_module_info _lookup_module_info
187 static lookup_info *_lookup_list[] = { &_lookup_info };
188 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
190 /* End of lookups/sqlite.c */