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