Sqlite: new main option sqlite_dbfile
[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 {
78 int ret;
79 gstring * res = NULL;
80
81 ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
82 if (ret != SQLITE_OK)
83   {
84   debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg);
85   return FAIL;
86   }
87
88 if (!res) *do_cache = 0;
89
90 *result = string_from_gstring(res);
91 return OK;
92 }
93
94
95
96 /*************************************************
97 *               Close entry point                *
98 *************************************************/
99
100 /* See local README for interface description. */
101
102 static void sqlite_close(void *handle)
103 {
104 sqlite3_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
114 for sqlite is the single quote, and it is quoted by doubling.
115
116 Arguments:
117   s          the string to be quoted
118   opt        additional option text or NULL if none
119
120 Returns:     the processed string or NULL for a bad option
121 */
122
123 static uschar *
124 sqlite_quote(uschar *s, uschar *opt)
125 {
126 register int c;
127 int count = 0;
128 uschar *t = s;
129 uschar *quoted;
130
131 if (opt != NULL) return NULL;     /* No options recognized */
132
133 while ((c = *t++) != 0) if (c == '\'') count++;
134
135 if (count == 0) return s;
136 t = quoted = store_get(Ustrlen(s) + count + 1, is_tainted(s));
137
138 while ((c = *s++) != 0)
139   {
140   if (c == '\'') *t++ = '\'';
141   *t++ = c;
142   }
143
144 *t = 0;
145 return quoted;
146 }
147
148
149
150 /*************************************************
151 *         Version reporting entry point          *
152 *************************************************/
153
154 /* See local README for interface description. */
155
156 #include "../version.h"
157
158 void
159 sqlite_version_report(FILE *f)
160 {
161 fprintf(f, "Library version: SQLite: Compile: %s\n"
162            "                         Runtime: %s\n",
163         SQLITE_VERSION, sqlite3_libversion());
164 #ifdef DYNLOOKUP
165 fprintf(f, "                         Exim version %s\n", EXIM_VERSION_STR);
166 #endif
167 }
168
169 static 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 */
177   sqlite_quote,                  /* quoting function */
178   sqlite_version_report          /* version reporting */
179 };
180
181 #ifdef DYNLOOKUP
182 #define sqlite_lookup_module_info _lookup_module_info
183 #endif
184
185 static lookup_info *_lookup_list[] = { &_lookup_info };
186 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
187
188 /* End of lookups/sqlite.c */