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(uschar *filename, uschar **errmsg)
26 ret = sqlite3_open(CS filename, &db);
29 *errmsg = (void *)sqlite3_errmsg(db);
30 debug_printf_indent("Error opening database: %s\n", *errmsg);
33 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
38 /*************************************************
40 *************************************************/
42 /* See local README for interface description. */
45 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
47 gstring * res = *(gstring **)arg;
49 /* For second and subsequent results, insert \n */
52 res = string_catn(res, US"\n", 1);
56 /* For multiple fields, include the field name too */
57 for (int i = 0; i < argc; i++)
59 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
60 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
65 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
67 *(gstring **)arg = res;
73 sqlite_find(void *handle, uschar *filename, const uschar *query, int length,
74 uschar **result, uschar **errmsg, uint *do_cache)
79 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
82 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
86 if (!res) *do_cache = 0;
88 *result = string_from_gstring(res);
94 /*************************************************
96 *************************************************/
98 /* See local README for interface description. */
100 static void sqlite_close(void *handle)
102 sqlite3_close(handle);
107 /*************************************************
108 * Quote entry point *
109 *************************************************/
111 /* From what I have found so far, the only character that needs to be quoted
112 for sqlite is the single quote, and it is quoted by doubling.
115 s the string to be quoted
116 opt additional option text or NULL if none
118 Returns: the processed string or NULL for a bad option
122 sqlite_quote(uschar *s, uschar *opt)
129 if (opt != NULL) return NULL; /* No options recognized */
131 while ((c = *t++) != 0) if (c == '\'') count++;
133 if (count == 0) return s;
134 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
136 while ((c = *s++) != 0)
138 if (c == '\'') *t++ = '\'';
148 /*************************************************
149 * Version reporting entry point *
150 *************************************************/
152 /* See local README for interface description. */
154 #include "../version.h"
157 sqlite_version_report(FILE *f)
159 fprintf(f, "Library version: SQLite: Compile: %s\n"
161 SQLITE_VERSION, sqlite3_libversion());
163 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
167 static lookup_info _lookup_info = {
168 US"sqlite", /* lookup name */
169 lookup_absfilequery, /* query-style lookup, starts with file name */
170 sqlite_open, /* open function */
171 NULL, /* no check function */
172 sqlite_find, /* find function */
173 sqlite_close, /* close function */
174 NULL, /* no tidy function */
175 sqlite_quote, /* quoting function */
176 sqlite_version_report /* version reporting */
180 #define sqlite_lookup_module_info _lookup_module_info
183 static lookup_info *_lookup_list[] = { &_lookup_info };
184 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
186 /* End of lookups/sqlite.c */