Sqlite: new main option sqlite_dbfile
[exim.git] / src / src / lookups / sqlite.c
CommitLineData
13b685f9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
f9ba5e22 5/* Copyright (c) University of Cambridge 1995 - 2018 */
13b685f9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "lf_functions.h"
13b685f9 10
13b685f9
PH
11#include <sqlite3.h>
12
13
14/*************************************************
15* Open entry point *
16*************************************************/
17
18/* See local README for interface description. */
19
e6d225ae 20static void *
d447dbd1 21sqlite_open(const uschar * filename, uschar ** errmsg)
13b685f9
PH
22{
23sqlite3 *db = NULL;
24int ret;
25
298849d8
JH
26if (!filename || !*filename) filename = sqlite_dbfile;
27if (*filename != '/')
28 *errmsg = US"absolute file name expected for \"sqlite\" lookup";
29else if ((ret = sqlite3_open(CCS filename, &db)) != 0)
13b685f9
PH
30 {
31 *errmsg = (void *)sqlite3_errmsg(db);
298849d8 32 DEBUG(D_lookup) debug_printf_indent("Error opening database: %s\n", *errmsg);
13b685f9
PH
33 }
34
31480e42 35sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
13b685f9
PH
36return db;
37}
38
39
40/*************************************************
41* Find entry point *
42*************************************************/
43
44/* See local README for interface description. */
45
acec9514
JH
46static int
47sqlite_callback(void *arg, int argc, char **argv, char **azColName)
13b685f9 48{
acec9514 49gstring * res = *(gstring **)arg;
13b685f9
PH
50
51/* For second and subsequent results, insert \n */
52
acec9514
JH
53if (res)
54 res = string_catn(res, US"\n", 1);
13b685f9
PH
55
56if (argc > 1)
57 {
58 /* For multiple fields, include the field name too */
d7978c0f 59 for (int i = 0; i < argc; i++)
13b685f9
PH
60 {
61 uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
acec9514 62 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
13b685f9
PH
63 }
64 }
65
66else
acec9514 67 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
13b685f9 68
acec9514 69*(gstring **)arg = res;
13b685f9
PH
70return 0;
71}
72
73
e6d225ae 74static int
d447dbd1
JH
75sqlite_find(void * handle, const uschar * filename, const uschar * query,
76 int length, uschar ** result, uschar ** errmsg, uint * do_cache)
13b685f9
PH
77{
78int ret;
acec9514 79gstring * res = NULL;
13b685f9 80
5903c6ff 81ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
13b685f9
PH
82if (ret != SQLITE_OK)
83 {
42c7f0b4 84 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
13b685f9
PH
85 return FAIL;
86 }
87
acec9514 88if (!res) *do_cache = 0;
13b685f9 89
acec9514 90*result = string_from_gstring(res);
13b685f9
PH
91return OK;
92}
93
94
95
96/*************************************************
97* Close entry point *
98*************************************************/
99
100/* See local README for interface description. */
101
e6d225ae 102static void sqlite_close(void *handle)
13b685f9
PH
103{
104sqlite3_close(handle);
105}
106
107
108
109/*************************************************
110* Quote entry point *
111*************************************************/
112
113/* From what I have found so far, the only character that needs to be quoted
114for sqlite is the single quote, and it is quoted by doubling.
115
116Arguments:
117 s the string to be quoted
118 opt additional option text or NULL if none
119
120Returns: the processed string or NULL for a bad option
121*/
122
e6d225ae 123static uschar *
13b685f9
PH
124sqlite_quote(uschar *s, uschar *opt)
125{
126register int c;
127int count = 0;
128uschar *t = s;
129uschar *quoted;
130
131if (opt != NULL) return NULL; /* No options recognized */
132
133while ((c = *t++) != 0) if (c == '\'') count++;
134
135if (count == 0) return s;
f3ebb786 136t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
13b685f9
PH
137
138while ((c = *s++) != 0)
139 {
140 if (c == '\'') *t++ = '\'';
141 *t++ = c;
142 }
143
144*t = 0;
145return quoted;
146}
147
6545de78
PP
148
149
150/*************************************************
151* Version reporting entry point *
152*************************************************/
153
154/* See local README for interface description. */
155
156#include "../version.h"
157
158void
159sqlite_version_report(FILE *f)
160{
161fprintf(f, "Library version: SQLite: Compile: %s\n"
162 " Runtime: %s\n",
163 SQLITE_VERSION, sqlite3_libversion());
164#ifdef DYNLOOKUP
165fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
166#endif
167}
168
e6d225ae
DW
169static lookup_info _lookup_info = {
170 US"sqlite", /* lookup name */
171 lookup_absfilequery, /* query-style lookup, starts with file name */
172 sqlite_open, /* open function */
173 NULL, /* no check function */
174 sqlite_find, /* find function */
175 sqlite_close, /* close function */
176 NULL, /* no tidy function */
6545de78
PP
177 sqlite_quote, /* quoting function */
178 sqlite_version_report /* version reporting */
e6d225ae
DW
179};
180
181#ifdef DYNLOOKUP
182#define sqlite_lookup_module_info _lookup_module_info
183#endif
184
185static lookup_info *_lookup_list[] = { &_lookup_info };
186lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
13b685f9
PH
187
188/* End of lookups/sqlite.c */