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