1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) The Exim Maintainers 2020 - 2024 */
6 /* Copyright (c) University of Cambridge 1995 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8 /* SPDX-License-Identifier: GPL-2.0-or-later */
10 /* This header file contains macro definitions for one possible hintsdb
13 /* ********************* sqlite3 interface ************************ */
18 # define EXIM_DB sqlite3
20 # define EXIM_CURSOR int
22 # /* The datum type used for queries */
23 # define EXIM_DATUM blob
25 /* Some text for messages */
26 # define EXIM_DBTYPE "sqlite3"
28 # /* Access functions */
31 exim_lockfile_needed(void)
33 return FALSE; /* We do transaction; no extra locking needed */
36 /* EXIM_DBOPEN - return pointer to an EXIM_DB, NULL if failed */
37 static inline EXIM_DB *
38 exim_dbopen_multi(const uschar * name, const uschar * dirname, int flags,
42 int ret, sflags = flags & O_RDWR ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY;
43 if (flags & O_CREAT) sflags |= SQLITE_OPEN_CREATE;
44 if ((ret = sqlite3_open_v2(CCS name, &dbp, sflags, NULL)) == SQLITE_OK)
46 sqlite3_busy_timeout(dbp, 5000);
48 ret = sqlite3_exec(dbp,
49 "CREATE TABLE IF NOT EXISTS tbl (ky TEXT PRIMARY KEY, dat BLOB);",
55 // fprintf(stderr, "sqlite3_open_v2: %s\n", sqlite3_errmsg(dbp));
56 return ret == SQLITE_OK ? dbp : NULL;
60 exim_dbtransaction_start(EXIM_DB * dbp)
62 return sqlite3_exec(dbp, "BEGIN TRANSACTION;", NULL, NULL, NULL) == SQLITE_OK;
65 static inline EXIM_DB *
66 exim_dbopen__(const uschar * name, const uschar * dirname, int flags,
69 EXIM_DB * dbp = exim_dbopen_multi(name, dirname, flags, mode);
70 if (!dbp || exim_dbtransaction_start(dbp))
76 /* EXIM_DBGET - returns TRUE if successful, FALSE otherwise */
77 /* note we alloc'n'copy - the caller need not do so */
78 /* result has a NUL appended, but the length is as per the DB */
81 exim_dbget__(EXIM_DB * dbp, const uschar * s, EXIM_DATUM * res)
83 sqlite3_stmt * statement;
86 res->len = (size_t) -1;
87 /* fprintf(stderr, "exim_dbget__(%s)\n", s); */
88 if ((ret = sqlite3_prepare_v2(dbp, CCS s, -1, &statement, NULL)) != SQLITE_OK)
90 /* fprintf(stderr, "prepare fail: %s\n", sqlite3_errmsg(dbp)); */
93 if (sqlite3_step(statement) != SQLITE_ROW)
95 /* fprintf(stderr, "step fail: %s\n", sqlite3_errmsg(dbp)); */
96 sqlite3_finalize(statement);
100 res->len = sqlite3_column_bytes(statement, 0);
101 # ifdef COMPILE_UTILITY
102 res->data = malloc(res->len);
104 res->data = store_get(res->len, GET_TAINTED);
106 memcpy(res->data, sqlite3_column_blob(statement, 0), res->len);
107 res->data[res->len] = '\0';
108 /* fprintf(stderr, "res %d bytes: '%.*s'\n", (int)res->len, (int)res->len, res->data); */
109 sqlite3_finalize(statement);
114 exim_dbget(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res)
116 # define FMT "SELECT dat FROM tbl WHERE ky = '%.*s';"
121 # ifdef COMPILE_UTILITY
122 /* fprintf(stderr, "exim_dbget(k len %d '%.*s')\n", (int)key->len, (int)key->len, key->data); */
123 qry = malloc(i = snprintf(NULL, 0, FMT, (int) key->len, key->data));
124 snprintf(CS qry, i, FMT, (int) key->len, key->data);
125 ret = exim_dbget__(dbp, qry, res);
128 /* fprintf(stderr, "exim_dbget(k len %d '%.*s')\n", (int)key->len, (int)key->len, key->data); */
129 qry = string_sprintf(FMT, (int) key->len, key->data);
130 ret = exim_dbget__(dbp, qry, res);
138 # define EXIM_DBPUTB_OK 0
139 # define EXIM_DBPUTB_DUP (-1)
142 exim_s_dbp(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data, const uschar * alt)
144 int hlen = data->len * 2, off = 0, res;
145 # define FMT "INSERT OR %s INTO tbl (ky,dat) VALUES ('%.*s', X'%.*s');"
146 # ifdef COMPILE_UTILITY
147 uschar * hex = malloc(hlen+1);
149 uschar * hex = store_get(hlen+1, data->data);
153 for (const uschar * s = data->data, * t = s + data->len; s < t; s++, off += 2)
154 sprintf(CS hex + off, "%02X", *s);
156 # ifdef COMPILE_UTILITY
157 res = snprintf(CS hex, 0, FMT, alt, (int) key->len, key->data, hlen, hex);
159 snprintf(CS qry, res, FMT, alt, (int) key->len, key->data, hlen, hex);
160 /* fprintf(stderr, "exim_s_dbp(%s)\n", qry); */
161 res = sqlite3_exec(dbp, CS qry, NULL, NULL, NULL);
165 qry = string_sprintf(FMT, alt, (int) key->len, key->data, hlen, hex);
166 /* fprintf(stderr, "exim_s_dbp(%s)\n", qry); */
167 res = sqlite3_exec(dbp, CS qry, NULL, NULL, NULL);
168 /* fprintf(stderr, "exim_s_dbp res %d\n", res); */
171 if (res != SQLITE_OK)
172 fprintf(stderr, "sqlite3_exec: %s\n", sqlite3_errmsg(dbp));
174 return res == SQLITE_OK ? EXIM_DBPUTB_OK : EXIM_DBPUTB_DUP;
178 /* EXIM_DBPUT - returns nothing useful, assumes replace mode */
181 exim_dbput(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
183 /* fprintf(stderr, "exim_dbput()\n"); */
184 (void) exim_s_dbp(dbp, key, data, US"REPLACE");
188 /* EXIM_DBPUTB - non-overwriting for use by dbmbuild */
190 /* Returns from EXIM_DBPUTB */
193 exim_dbputb(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * data)
195 return exim_s_dbp(dbp, key, data, US"ABORT");
200 exim_dbdel(EXIM_DB * dbp, EXIM_DATUM * key)
202 # define FMT "DELETE FROM tbl WHERE ky = '%.*s';"
206 # ifdef COMPILE_UTILITY
207 res = snprintf(NULL, 0, FMT, (int) key->len, key->data); /* res excludes nul */
209 snprintf(CS qry, res, FMT, (int) key->len, key->data);
210 res = sqlite3_exec(dbp, CS qry, NULL, NULL, NULL);
213 qry = string_sprintf(FMT, (int) key->len, key->data);
214 res = sqlite3_exec(dbp, CS qry, NULL, NULL, NULL);
222 /* EXIM_DBCREATE_CURSOR - initialize for scanning operation */
223 /* Cursors are inefficiently emulated by repeating searches */
225 static inline EXIM_CURSOR *
226 exim_dbcreate_cursor(EXIM_DB * dbp)
228 # ifdef COMPILE_UTILITY
229 EXIM_CURSOR * c = malloc(sizeof(int));
231 EXIM_CURSOR * c = store_malloc(sizeof(int));
238 /* Note that we return the (next) key, not the record value */
240 exim_dbscan(EXIM_DB * dbp, EXIM_DATUM * key, EXIM_DATUM * res, BOOL first,
241 EXIM_CURSOR * cursor)
243 # define FMT "SELECT ky FROM tbl ORDER BY ky LIMIT 1 OFFSET %d;"
248 # ifdef COMPILE_UTILITY
249 qry = malloc((i = snprintf(NULL, 0, FMT, *cursor)));
250 snprintf(CS qry, i-1, FMT, *cursor);
251 /* fprintf(stderr, "exim_dbscan(%s)\n", qry); */
252 ret = exim_dbget__(dbp, qry, key);
254 /* fprintf(stderr, "exim_dbscan ret %c\n", ret ? 'T':'F'); */
256 qry = string_sprintf(FMT, *cursor);
257 /* fprintf(stderr, "exim_dbscan(%s)\n", qry); */
258 ret = exim_dbget__(dbp, qry, key);
259 /* fprintf(stderr, "exim_dbscan ret %c\n", ret ? 'T':'F'); */
261 if (ret) *cursor = *cursor + 1;
266 /* EXIM_DBDELETE_CURSOR - terminate scanning operation. */
268 exim_dbdelete_cursor(EXIM_CURSOR * cursor)
270 # ifdef COMPILE_UTILITY
280 exim_dbclose_multi(EXIM_DB * dbp)
285 exim_dbtransaction_commit(EXIM_DB * dbp)
287 (void) sqlite3_exec(dbp, "COMMIT TRANSACTION;", NULL, NULL, NULL);
290 exim_dbclose__(EXIM_DB * dbp)
292 exim_dbtransaction_commit(dbp);
293 exim_dbclose_multi(dbp);
300 exim_datum_data_get(EXIM_DATUM * dp)
301 { return US dp->data; }
303 exim_datum_data_set(EXIM_DATUM * dp, void * s)
307 exim_datum_size_get(EXIM_DATUM * dp)
310 exim_datum_size_set(EXIM_DATUM * dp, unsigned n)
316 exim_datum_init(EXIM_DATUM * dp)
317 { dp->data = NULL; } /* compiler quietening */
319 /* No free needed for a datum */
322 exim_datum_free(EXIM_DATUM * dp)
327 # define EXIM_DB_RLIMIT 150
330 /* End of hints_sqlite.h */