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