Compiler masochism compliance.
[exim.git] / src / src / lookups / sqlite.c
CommitLineData
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 22static void *
13b685f9
PH
23sqlite_open(uschar *filename, uschar **errmsg)
24{
25sqlite3 *db = NULL;
26int ret;
27
28ret = sqlite3_open((char *)filename, &db);
29if (ret != 0)
30 {
31 *errmsg = (void *)sqlite3_errmsg(db);
32 debug_printf("Error opening database: %s\n", *errmsg);
33 }
34
31480e42 35sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
13b685f9
PH
36return db;
37}
38
39
40/*************************************************
41* Find entry point *
42*************************************************/
43
44/* See local README for interface description. */
45
46struct strbuf {
47 uschar *string;
48 int size;
49 int len;
50};
51
52static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
53{
54struct strbuf *res = arg;
55int i;
56
57/* For second and subsequent results, insert \n */
58
59if (res->string != NULL)
60 res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
61
62if (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
73else
74 {
75 res->string = string_append(res->string, &res->size, &res->len, 1,
76 (argv[0] != NULL)? argv[0]:"<NULL>");
77 }
78
79res->string[res->len] = 0;
80return 0;
81}
82
83
e6d225ae 84static int
13b685f9
PH
85sqlite_find(void *handle, uschar *filename, uschar *query, int length,
86 uschar **result, uschar **errmsg, BOOL *do_cache)
87{
88int ret;
89struct strbuf res = { NULL, 0, 0 };
90
91ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
92if (ret != SQLITE_OK)
93 {
94 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
95 return FAIL;
96 }
97
98if (res.string == NULL) *do_cache = FALSE;
99
100*result = res.string;
101return OK;
102}
103
104
105
106/*************************************************
107* Close entry point *
108*************************************************/
109
110/* See local README for interface description. */
111
e6d225ae 112static void sqlite_close(void *handle)
13b685f9
PH
113{
114sqlite3_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
124for sqlite is the single quote, and it is quoted by doubling.
125
126Arguments:
127 s the string to be quoted
128 opt additional option text or NULL if none
129
130Returns: the processed string or NULL for a bad option
131*/
132
e6d225ae 133static uschar *
13b685f9
PH
134sqlite_quote(uschar *s, uschar *opt)
135{
136register int c;
137int count = 0;
138uschar *t = s;
139uschar *quoted;
140
141if (opt != NULL) return NULL; /* No options recognized */
142
143while ((c = *t++) != 0) if (c == '\'') count++;
144
145if (count == 0) return s;
146t = quoted = store_get(Ustrlen(s) + count + 1);
147
148while ((c = *s++) != 0)
149 {
150 if (c == '\'') *t++ = '\'';
151 *t++ = c;
152 }
153
154*t = 0;
155return 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
168void
169sqlite_version_report(FILE *f)
170{
171fprintf(f, "Library version: SQLite: Compile: %s\n"
172 " Runtime: %s\n",
173 SQLITE_VERSION, sqlite3_libversion());
174#ifdef DYNLOOKUP
175fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
176#endif
177}
178
e6d225ae
DW
179static 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
195static lookup_info *_lookup_list[] = { &_lookup_info };
196lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
13b685f9
PH
197
198/* End of lookups/sqlite.c */