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