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