Merge branch 'debian'
[hcoop/debian/exim4.git] / src / lookups / sqlite.c
CommitLineData
420a0d19
CE
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
2ea97746 5/* Copyright (c) University of Cambridge 1995 - 2018 */
420a0d19
CE
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
20static void *
21sqlite_open(uschar *filename, uschar **errmsg)
22{
23sqlite3 *db = NULL;
24int ret;
25
2ea97746 26ret = sqlite3_open(CS filename, &db);
420a0d19
CE
27if (ret != 0)
28 {
29 *errmsg = (void *)sqlite3_errmsg(db);
30 debug_printf("Error opening database: %s\n", *errmsg);
31 }
32
33sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout);
34return db;
35}
36
37
38/*************************************************
39* Find entry point *
40*************************************************/
41
42/* See local README for interface description. */
43
2ea97746
CE
44static int
45sqlite_callback(void *arg, int argc, char **argv, char **azColName)
420a0d19 46{
2ea97746 47gstring * res = *(gstring **)arg;
420a0d19
CE
48int i;
49
50/* For second and subsequent results, insert \n */
51
2ea97746
CE
52if (res)
53 res = string_catn(res, US"\n", 1);
420a0d19
CE
54
55if (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>");
2ea97746 61 res = lf_quote(US azColName[i], value, Ustrlen(value), res);
420a0d19
CE
62 }
63 }
64
65else
2ea97746 66 res = string_cat(res, argv[0] ? US argv[0] : US "<NULL>");
420a0d19 67
2ea97746 68*(gstring **)arg = res;
420a0d19
CE
69return 0;
70}
71
72
73static int
2ea97746
CE
74sqlite_find(void *handle, uschar *filename, const uschar *query, int length,
75 uschar **result, uschar **errmsg, uint *do_cache)
420a0d19
CE
76{
77int ret;
2ea97746 78gstring * res = NULL;
420a0d19 79
2ea97746 80ret = sqlite3_exec(handle, CS query, sqlite_callback, &res, (char **)errmsg);
420a0d19
CE
81if (ret != SQLITE_OK)
82 {
83 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
84 return FAIL;
85 }
86
2ea97746 87if (!res) *do_cache = 0;
420a0d19 88
2ea97746 89*result = string_from_gstring(res);
420a0d19
CE
90return OK;
91}
92
93
94
95/*************************************************
96* Close entry point *
97*************************************************/
98
99/* See local README for interface description. */
100
101static void sqlite_close(void *handle)
102{
103sqlite3_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
113for sqlite is the single quote, and it is quoted by doubling.
114
115Arguments:
116 s the string to be quoted
117 opt additional option text or NULL if none
118
119Returns: the processed string or NULL for a bad option
120*/
121
122static uschar *
123sqlite_quote(uschar *s, uschar *opt)
124{
125register int c;
126int count = 0;
127uschar *t = s;
128uschar *quoted;
129
130if (opt != NULL) return NULL; /* No options recognized */
131
132while ((c = *t++) != 0) if (c == '\'') count++;
133
134if (count == 0) return s;
135t = quoted = store_get(Ustrlen(s) + count + 1);
136
137while ((c = *s++) != 0)
138 {
139 if (c == '\'') *t++ = '\'';
140 *t++ = c;
141 }
142
143*t = 0;
144return 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
157void
158sqlite_version_report(FILE *f)
159{
160fprintf(f, "Library version: SQLite: Compile: %s\n"
161 " Runtime: %s\n",
162 SQLITE_VERSION, sqlite3_libversion());
163#ifdef DYNLOOKUP
164fprintf(f, " Exim version %s\n", EXIM_VERSION_STR);
165#endif
166}
167
168static 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
184static lookup_info *_lookup_list[] = { &_lookup_info };
185lookup_module_info sqlite_lookup_module_info = { LOOKUP_MODULE_INFO_MAGIC, _lookup_list, 1 };
186
187/* End of lookups/sqlite.c */