1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2009 */
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((char *)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. */
50 static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
52 struct strbuf *res = arg;
55 /* For second and subsequent results, insert \n */
57 if (res->string != NULL)
58 res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
62 /* For multiple fields, include the field name too */
63 for (i = 0; i < argc; i++)
65 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
66 res->string = lf_quote(US azColName[i], value, Ustrlen(value), res->string,
67 &res->size, &res->len);
73 res->string = string_append(res->string, &res->size, &res->len, 1,
74 (argv[0] != NULL)? argv[0]:"<NULL>");
77 res->string[res->len] = 0;
83 sqlite_find(void *handle, uschar *filename, uschar *query, int length,
84 uschar **result, uschar **errmsg, BOOL *do_cache)
87 struct strbuf res = { NULL, 0, 0 };
89 ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
92 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
96 if (res.string == NULL) *do_cache = FALSE;
104 /*************************************************
105 * Close entry point *
106 *************************************************/
108 /* See local README for interface description. */
110 static void sqlite_close(void *handle)
112 sqlite3_close(handle);
117 /*************************************************
118 * Quote entry point *
119 *************************************************/
121 /* From what I have found so far, the only character that needs to be quoted
122 for sqlite is the single quote, and it is quoted by doubling.
125 s the string to be quoted
126 opt additional option text or NULL if none
128 Returns: the processed string or NULL for a bad option
132 sqlite_quote(uschar *s, uschar *opt)
139 if (opt != NULL) return NULL; /* No options recognized */
141 while ((c = *t++) != 0) if (c == '\'') count++;
143 if (count == 0) return s;
144 t = quoted = store_get(Ustrlen(s) + count + 1);
146 while ((c = *s++) != 0)
148 if (c == '\'') *t++ = '\'';
158 /*************************************************
159 * Version reporting entry point *
160 *************************************************/
162 /* See local README for interface description. */
164 #include "../version.h"
167 sqlite_version_report(FILE *f)
169 fprintf(f, "Library version: SQLite: Compile: %s\n"
171 SQLITE_VERSION, sqlite3_libversion());
173 fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
177 static lookup_info _lookup_info = {
178 US"sqlite", /* lookup name */
179 lookup_absfilequery, /* query-style lookup, starts with file name */
180 sqlite_open, /* open function */
181 NULL, /* no check function */
182 sqlite_find, /* find function */
183 sqlite_close, /* close function */
184 NULL, /* no tidy function */
185 sqlite_quote, /* quoting function */
186 sqlite_version_report /* version reporting */
190 #define sqlite_lookup_module_info _lookup_module_info
193 static lookup_info *_lookup_list[] = { &_lookup_info };
194 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
196 /* End of lookups/sqlite.c */