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. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include "lf_functions.h"
16 /*************************************************
18 *************************************************/
20 /* See local README for interface description. */
23 sqlite_open(const uschar * filename, uschar ** errmsg)
28 if (!filename || !*filename)
30 DEBUG(D_lookup) debug_printf_indent("Using sqlite_dbfile: %s\n", sqlite_dbfile);
31 filename = sqlite_dbfile;
33 if (!filename || *filename != '/')
34 *errmsg = US"absolute file name expected for \"sqlite\" lookup";
35 else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
37 *errmsg = (void *)sqlite3_errmsg(db);
40 DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
44 sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
49 /*************************************************
51 *************************************************/
53 /* See local README for interface description. */
56 sqlite_callback(void *arg, int argc, char **argv, char **azColName)
58 gstring * res = *(gstring **)arg;
60 /* For second and subsequent results, insert \n */
63 res = string_catn(res, US"\n", 1);
67 /* For multiple fields, include the field name too */
68 for (int i = 0; i < argc; i++)
70 uschar * value = US(argv[i] ? argv[i] : "<NULL>");
71 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
76 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
78 /* always return a non-null gstring, even for a zero-length string result */
79 *(gstring **)arg = res ? res : string_get(1);
85 sqlite_find(void * handle, const uschar * filename, const uschar * query,
86 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
92 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
95 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
99 if (!res) *do_cache = 0; /* on fail, wipe cache */
101 *result = string_from_gstring(res);
107 /*************************************************
108 * Close entry point *
109 *************************************************/
111 /* See local README for interface description. */
113 static void sqlite_close(void *handle)
115 sqlite3_close(handle);
120 /*************************************************
121 * Quote entry point *
122 *************************************************/
124 /* From what I have found so far, the only character that needs to be quoted
125 for sqlite is the single quote, and it is quoted by doubling.
128 s the string to be quoted
129 opt additional option text or NULL if none
130 idx lookup type index
132 Returns: the processed string or NULL for a bad option
136 sqlite_quote(uschar * s, uschar * opt, unsigned idx)
139 uschar * t = s, * quoted;
141 if (opt) return NULL; /* No options recognized */
143 while ((c = *t++)) if (c == '\'') count++;
146 t = quoted = store_get_quoted(count + 1, s, idx);
150 if (c == '\'') *t++ = '\'';
160 /*************************************************
161 * Version reporting entry point *
162 *************************************************/
164 /* See local README for interface description. */
166 #include "../version.h"
169 sqlite_version_report(gstring * g)
171 g = string_fmt_append(g,
172 "Library version: SQLite: Compile: %s\n"
174 SQLITE_VERSION, sqlite3_libversion());
176 g = string_fmt_append(g,
177 " Exim version %s\n", EXIM_VERSION_STR);
182 static lookup_info _lookup_info = {
183 .name = US"sqlite", /* lookup name */
184 .type = lookup_absfilequery, /* query-style lookup, starts with file name */
185 .open = sqlite_open, /* open function */
186 .check = NULL, /* no check function */
187 .find = sqlite_find, /* find function */
188 .close = sqlite_close, /* close function */
189 .tidy = NULL, /* no tidy function */
190 .quote = sqlite_quote, /* quoting function */
191 .version_report = sqlite_version_report /* version reporting */
195 #define sqlite_lookup_module_info _lookup_module_info
198 static lookup_info *_lookup_list[] = { &_lookup_info };
199 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
201 /* End of lookups/sqlite.c */