update to pre-4.87 master
[exim.git] / src / src / lookups / sqlite.c
CommitLineData
13b685f9
PH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
bfe645c1 5/* Copyright (c) University of Cambridge 1995 - 2015 */
13b685f9
PH
6/* See the file NOTICE for conditions of use and distribution. */
7
8#include "../exim.h"
9#include "lf_functions.h"
13b685f9 10
13b685f9
PH
11#include <sqlite3.h>
12
13
14/*************************************************
15* Open entry point *
16*************************************************/
17
18/* See local README for interface description. */
19
e6d225ae 20static void *
13b685f9
PH
21sqlite_open(uschar *filename, uschar **errmsg)
22{
23sqlite3 *db = NULL;
24int ret;
25
26ret = sqlite3_open((char *)filename, &db);
27if (ret != 0)
28 {
29 *errmsg = (void *)sqlite3_errmsg(db);
30 debug_printf("Error opening database: %s\n", *errmsg);
31 }
32
31480e42 33sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
13b685f9
PH
34return db;
35}
36
37
38/*************************************************
39* Find entry point *
40*************************************************/
41
42/* See local README for interface description. */
43
44struct strbuf {
45 uschar *string;
46 int size;
47 int len;
48};
49
50static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
51{
52struct strbuf *res = arg;
53int i;
54
55/* For second and subsequent results, insert \n */
56
57if (res->string != NULL)
58 res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
59
60if (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
71else
72 {
73 res->string = string_append(res->string, &res->size, &res->len, 1,
74 (argv[0] != NULL)? argv[0]:"<NULL>");
75 }
76
77res->string[res->len] = 0;
78return 0;
79}
80
81
e6d225ae 82static int
bfe645c1
JH
83sqlite_find(void *handle, uschar *filename, const uschar *query, int length,
84 uschar **result, uschar **errmsg, uint *do_cache)
13b685f9
PH
85{
86int ret;
87struct strbuf res = { NULL, 0, 0 };
88
89ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
90if (ret != SQLITE_OK)
91 {
92 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
93 return FAIL;
94 }
95
bfe645c1 96if (res.string == NULL) *do_cache = 0;
13b685f9
PH
97
98*result = res.string;
99return OK;
100}
101
102
103
104/*************************************************
105* Close entry point *
106*************************************************/
107
108/* See local README for interface description. */
109
e6d225ae 110static void sqlite_close(void *handle)
13b685f9
PH
111{
112sqlite3_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
122for sqlite is the single quote, and it is quoted by doubling.
123
124Arguments:
125 s the string to be quoted
126 opt additional option text or NULL if none
127
128Returns: the processed string or NULL for a bad option
129*/
130
e6d225ae 131static uschar *
13b685f9
PH
132sqlite_quote(uschar *s, uschar *opt)
133{
134register int c;
135int count = 0;
136uschar *t = s;
137uschar *quoted;
138
139if (opt != NULL) return NULL; /* No options recognized */
140
141while ((c = *t++) != 0) if (c == '\'') count++;
142
143if (count == 0) return s;
144t = quoted = store_get(Ustrlen(s) + count + 1);
145
146while ((c = *s++) != 0)
147 {
148 if (c == '\'') *t++ = '\'';
149 *t++ = c;
150 }
151
152*t = 0;
153return quoted;
154}
155
6545de78
PP
156
157
158/*************************************************
159* Version reporting entry point *
160*************************************************/
161
162/* See local README for interface description. */
163
164#include "../version.h"
165
166void
167sqlite_version_report(FILE *f)
168{
169fprintf(f, "Library version: SQLite: Compile: %s\n"
170 " Runtime: %s\n",
171 SQLITE_VERSION, sqlite3_libversion());
172#ifdef DYNLOOKUP
173fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
174#endif
175}
176
e6d225ae
DW
177static 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 */
6545de78
PP
185 sqlite_quote, /* quoting function */
186 sqlite_version_report /* version reporting */
e6d225ae
DW
187};
188
189#ifdef DYNLOOKUP
190#define sqlite_lookup_module_info _lookup_module_info
191#endif
192
193static lookup_info *_lookup_list[] = { &_lookup_info };
194lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
13b685f9
PH
195
196/* End of lookups/sqlite.c */