Testsuite: output changes resulting
[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 75sqlite_find(void * handle, const uschar * filename, const uschar * query,
67a57a5a
JH
76 int length, uschar ** result, uschar ** errmsg, uint * do_cache,
77 const uschar * opts)
13b685f9
PH
78{
79int ret;
acec9514 80gstring * res = NULL;
13b685f9 81
137ae145 82ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, CSS errmsg);
13b685f9
PH
83if (ret != SQLITE_OK)
84 {
42c7f0b4 85 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
13b685f9
PH
86 return FAIL;
87 }
88
acec9514 89if (!res) *do_cache = 0;
13b685f9 90
acec9514 91*result = string_from_gstring(res);
13b685f9
PH
92return OK;
93}
94
95
96
97/*************************************************
98* Close entry point *
99*************************************************/
100
101/* See local README for interface description. */
102
e6d225ae 103static void sqlite_close(void *handle)
13b685f9
PH
104{
105sqlite3_close(handle);
106}
107
108
109
110/*************************************************
111* Quote entry point *
112*************************************************/
113
114/* From what I have found so far, the only character that needs to be quoted
115for sqlite is the single quote, and it is quoted by doubling.
116
117Arguments:
118 s the string to be quoted
119 opt additional option text or NULL if none
120
121Returns: the processed string or NULL for a bad option
122*/
123
e6d225ae 124static uschar *
13b685f9
PH
125sqlite_quote(uschar *s, uschar *opt)
126{
127register int c;
128int count = 0;
129uschar *t = s;
130uschar *quoted;
131
132if (opt != NULL) return NULL; /* No options recognized */
133
134while ((c = *t++) != 0) if (c == '\'') count++;
135
136if (count == 0) return s;
f3ebb786 137t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
13b685f9
PH
138
139while ((c = *s++) != 0)
140 {
141 if (c == '\'') *t++ = '\'';
142 *t++ = c;
143 }
144
145*t = 0;
146return quoted;
147}
148
6545de78
PP
149
150
151/*************************************************
152* Version reporting entry point *
153*************************************************/
154
155/* See local README for interface description. */
156
157#include "../version.h"
158
159void
160sqlite_version_report(FILE *f)
161{
162fprintf(f, "Library version: SQLite: Compile: %s\n"
163 " Runtime: %s\n",
164 SQLITE_VERSION, sqlite3_libversion());
165#ifdef DYNLOOKUP
166fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
167#endif
168}
169
e6d225ae 170static lookup_info _lookup_info = {
9f400174
JH
171 .name = US"sqlite", /* lookup name */
172 .type = lookup_absfilequery, /* query-style lookup, starts with file name */
173 .open = sqlite_open, /* open function */
174 .check = NULL, /* no check function */
175 .find = sqlite_find, /* find function */
176 .close = sqlite_close, /* close function */
177 .tidy = NULL, /* no tidy function */
178 .quote = sqlite_quote, /* quoting function */
179 .version_report = sqlite_version_report /* version reporting */
e6d225ae
DW
180};
181
182#ifdef DYNLOOKUP
183#define sqlite_lookup_module_info _lookup_module_info
184#endif
185
186static lookup_info *_lookup_list[] = { &_lookup_info };
187lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
13b685f9
PH
188
189/* End of lookups/sqlite.c */