Commit | Line | Data |
---|---|---|
0a49a7a4 | 1 | /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */ |
13b685f9 PH |
2 | |
3 | /************************************************* | |
4 | * Exim - an Internet mail transport agent * | |
5 | *************************************************/ | |
6 | ||
0a49a7a4 | 7 | /* Copyright (c) University of Cambridge 1995 - 2009 */ |
13b685f9 PH |
8 | /* See the file NOTICE for conditions of use and distribution. */ |
9 | ||
10 | #include "../exim.h" | |
11 | #include "lf_functions.h" | |
13b685f9 | 12 | |
13b685f9 PH |
13 | #include <sqlite3.h> |
14 | ||
15 | ||
16 | /************************************************* | |
17 | * Open entry point * | |
18 | *************************************************/ | |
19 | ||
20 | /* See local README for interface description. */ | |
21 | ||
e6d225ae | 22 | static void * |
13b685f9 PH |
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 | ||
31480e42 | 35 | sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout); |
13b685f9 PH |
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 | ||
e6d225ae | 84 | static int |
13b685f9 PH |
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 | ||
e6d225ae | 112 | static void sqlite_close(void *handle) |
13b685f9 PH |
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 | ||
e6d225ae | 133 | static uschar * |
13b685f9 PH |
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 | ||
6545de78 PP |
158 | |
159 | ||
160 | /************************************************* | |
161 | * Version reporting entry point * | |
162 | *************************************************/ | |
163 | ||
164 | /* See local README for interface description. */ | |
165 | ||
166 | #include "../version.h" | |
167 | ||
168 | void | |
169 | sqlite_version_report(FILE *f) | |
170 | { | |
171 | fprintf(f, "Library version: SQLite: Compile: %s\n" | |
172 | " Runtime: %s\n", | |
173 | SQLITE_VERSION, sqlite3_libversion()); | |
174 | #ifdef DYNLOOKUP | |
175 | fprintf(f, " Exim version %s\n", EXIM_VERSION_STR); | |
176 | #endif | |
177 | } | |
178 | ||
e6d225ae DW |
179 | static lookup_info _lookup_info = { |
180 | US"sqlite", /* lookup name */ | |
181 | lookup_absfilequery, /* query-style lookup, starts with file name */ | |
182 | sqlite_open, /* open function */ | |
183 | NULL, /* no check function */ | |
184 | sqlite_find, /* find function */ | |
185 | sqlite_close, /* close function */ | |
186 | NULL, /* no tidy function */ | |
6545de78 PP |
187 | sqlite_quote, /* quoting function */ |
188 | sqlite_version_report /* version reporting */ | |
e6d225ae DW |
189 | }; |
190 | ||
191 | #ifdef DYNLOOKUP | |
192 | #define sqlite_lookup_module_info _lookup_module_info | |
193 | #endif | |
194 | ||
195 | static lookup_info *_lookup_list[] = { &_lookup_info }; | |
196 | lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 }; | |
13b685f9 PH |
197 | |
198 | /* End of lookups/sqlite.c */ |