Add dynamic lookup support
[exim.git] / src / src / lookups / sqlite.c
1 /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */
2
3 /*************************************************
4 *     Exim - an Internet mail transport agent    *
5 *************************************************/
6
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
9
10 #include "../exim.h"
11 #include "lf_functions.h"
12
13 #include <sqlite3.h>
14
15
16 /*************************************************
17 *              Open entry point                  *
18 *************************************************/
19
20 /* See local README for interface description. */
21
22 static void *
23 sqlite_open(uschar *filename, uschar **errmsg)
24 {
25 sqlite3 *db = NULL;
26 int ret;
27
28 ret = sqlite3_open((char *)filename, &db);
29 if (ret != 0)
30   {
31   *errmsg = (void *)sqlite3_errmsg(db);
32   debug_printf("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 struct strbuf {
47   uschar *string;
48   int size;
49   int len;
50 };
51
52 static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
53 {
54 struct strbuf *res = arg;
55 int i;
56
57 /* For second and subsequent results, insert \n */
58
59 if (res->string != NULL)
60   res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
61
62 if (argc > 1)
63   {
64   /* For multiple fields, include the field name too */
65   for (i = 0; i < argc; i++)
66     {
67     uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>");
68     res->string = lf_quote(US azColName[i], value, Ustrlen(value), res->string,
69       &res->size, &res->len);
70     }
71   }
72
73 else
74   {
75   res->string = string_append(res->string, &res->size, &res->len, 1,
76     (argv[0] != NULL)? argv[0]:"<NULL>");
77   }
78
79 res->string[res->len] = 0;
80 return 0;
81 }
82
83
84 static int
85 sqlite_find(void *handle, uschar *filename, uschar *query, int length,
86   uschar **result, uschar **errmsg, BOOL *do_cache)
87 {
88 int ret;
89 struct strbuf res = { NULL, 0, 0 };
90
91 ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
92 if (ret != SQLITE_OK)
93   {
94   debug_printf("sqlite3_exec failed: %s\n", *errmsg);
95   return FAIL;
96   }
97
98 if (res.string == NULL) *do_cache = FALSE;
99
100 *result = res.string;
101 return OK;
102 }
103
104
105
106 /*************************************************
107 *               Close entry point                *
108 *************************************************/
109
110 /* See local README for interface description. */
111
112 static void sqlite_close(void *handle)
113 {
114 sqlite3_close(handle);
115 }
116
117
118
119 /*************************************************
120 *               Quote entry point                *
121 *************************************************/
122
123 /* From what I have found so far, the only character that needs to be quoted
124 for sqlite is the single quote, and it is quoted by doubling.
125
126 Arguments:
127   s          the string to be quoted
128   opt        additional option text or NULL if none
129
130 Returns:     the processed string or NULL for a bad option
131 */
132
133 static uschar *
134 sqlite_quote(uschar *s, uschar *opt)
135 {
136 register int c;
137 int count = 0;
138 uschar *t = s;
139 uschar *quoted;
140
141 if (opt != NULL) return NULL;     /* No options recognized */
142
143 while ((c = *t++) != 0) if (c == '\'') count++;
144
145 if (count == 0) return s;
146 t = quoted = store_get(Ustrlen(s) + count + 1);
147
148 while ((c = *s++) != 0)
149   {
150   if (c == '\'') *t++ = '\'';
151   *t++ = c;
152   }
153
154 *t = 0;
155 return quoted;
156 }
157
158 static lookup_info _lookup_info = {
159   US"sqlite",                    /* lookup name */
160   lookup_absfilequery,           /* query-style lookup, starts with file name */
161   sqlite_open,                   /* open function */
162   NULL,                          /* no check function */
163   sqlite_find,                   /* find function */
164   sqlite_close,                  /* close function */
165   NULL,                          /* no tidy function */
166   sqlite_quote                   /* quoting function */
167 };
168
169 #ifdef DYNLOOKUP
170 #define sqlite_lookup_module_info _lookup_module_info
171 #endif
172
173 static lookup_info *_lookup_list[] = { &_lookup_info };
174 lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
175
176 /* End of lookups/sqlite.c */