1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2017 */
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("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;
50 /* For second and subsequent results, insert \n */
53 res = string_catn(res, US"\n", 1);
57 /* For multiple fields, include the field name too */
58 for (i = 0; i < argc; i++)
60 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
61 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
66 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
68 *(gstring **)arg = res;
74 sqlite_find(void *handle, uschar *filename, const uschar *query, int length,
75 uschar **result, uschar **errmsg, uint *do_cache)
80 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
83 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
87 if (!res) *do_cache = 0;
89 *result = string_from_gstring(res);
95 /*************************************************
97 *************************************************/
99 /* See local README for interface description. */
101 static void sqlite_close(void *handle)
103 sqlite3_close(handle);
108 /*************************************************
109 * Quote entry point *
110 *************************************************/
112 /* From what I have found so far, the only character that needs to be quoted
113 for sqlite is the single quote, and it is quoted by doubling.
116 s the string to be quoted
117 opt additional option text or NULL if none
119 Returns: the processed string or NULL for a bad option
123 sqlite_quote(uschar *s, uschar *opt)
130 if (opt != NULL) return NULL; /* No options recognized */
132 while ((c = *t++) != 0) if (c == '\'') count++;
134 if (count == 0) return s;
135 t = quoted = store_get(Ustrlen(s) + count + 1);
137 while ((c = *s++) != 0)
139 if (c == '\'') *t++ = '\'';
149 /*************************************************
150 * Version reporting entry point *
151 *************************************************/
153 /* See local README for interface description. */
155 #include "../version.h"
158 sqlite_version_report(FILE *f)
160 fprintf(f, "Library version: SQLite: Compile: %s\n"
162 SQLITE_VERSION, sqlite3_libversion());
164 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
168 static lookup_info _lookup_info = {
169 US"sqlite", /* lookup name */
170 lookup_absfilequery, /* query-style lookup, starts with file name */
171 sqlite_open, /* open function */
172 NULL, /* no check function */
173 sqlite_find, /* find function */
174 sqlite_close, /* close function */
175 NULL, /* no tidy function */
176 sqlite_quote, /* quoting function */
177 sqlite_version_report /* version reporting */
181 #define sqlite_lookup_module_info _lookup_module_info
184 static lookup_info *_lookup_list[] = { &_lookup_info };
185 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
187 /* End of lookups/sqlite.c */