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