| 1 | /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.2 2005/08/08 10:48:27 ph10 Exp $ */ |
| 2 | |
| 3 | /************************************************* |
| 4 | * Exim - an Internet mail transport agent * |
| 5 | *************************************************/ |
| 6 | |
| 7 | /* Copyright (c) University of Cambridge 1995 - 2005 */ |
| 8 | /* See the file NOTICE for conditions of use and distribution. */ |
| 9 | |
| 10 | #include "../exim.h" |
| 11 | #include "lf_functions.h" |
| 12 | #include "sqlite.h" |
| 13 | |
| 14 | #ifndef LOOKUP_SQLITE |
| 15 | static void dummy(int x) { dummy(x-1); } |
| 16 | #else |
| 17 | #include <sqlite3.h> |
| 18 | |
| 19 | |
| 20 | /************************************************* |
| 21 | * Open entry point * |
| 22 | *************************************************/ |
| 23 | |
| 24 | /* See local README for interface description. */ |
| 25 | |
| 26 | void * |
| 27 | sqlite_open(uschar *filename, uschar **errmsg) |
| 28 | { |
| 29 | sqlite3 *db = NULL; |
| 30 | int ret; |
| 31 | |
| 32 | ret = sqlite3_open((char *)filename, &db); |
| 33 | if (ret != 0) |
| 34 | { |
| 35 | *errmsg = (void *)sqlite3_errmsg(db); |
| 36 | debug_printf("Error opening database: %s\n", *errmsg); |
| 37 | } |
| 38 | |
| 39 | sqlite3_busy_timeout(db, 1000 * sqlite_lock_timeout); |
| 40 | return db; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /************************************************* |
| 45 | * Find entry point * |
| 46 | *************************************************/ |
| 47 | |
| 48 | /* See local README for interface description. */ |
| 49 | |
| 50 | struct strbuf { |
| 51 | uschar *string; |
| 52 | int size; |
| 53 | int len; |
| 54 | }; |
| 55 | |
| 56 | static int sqlite_callback(void *arg, int argc, char **argv, char **azColName) |
| 57 | { |
| 58 | struct strbuf *res = arg; |
| 59 | int i; |
| 60 | |
| 61 | /* For second and subsequent results, insert \n */ |
| 62 | |
| 63 | if (res->string != NULL) |
| 64 | res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1); |
| 65 | |
| 66 | if (argc > 1) |
| 67 | { |
| 68 | /* For multiple fields, include the field name too */ |
| 69 | for (i = 0; i < argc; i++) |
| 70 | { |
| 71 | uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>"); |
| 72 | res->string = lf_quote(US azColName[i], value, Ustrlen(value), res->string, |
| 73 | &res->size, &res->len); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | else |
| 78 | { |
| 79 | res->string = string_append(res->string, &res->size, &res->len, 1, |
| 80 | (argv[0] != NULL)? argv[0]:"<NULL>"); |
| 81 | } |
| 82 | |
| 83 | res->string[res->len] = 0; |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | int |
| 89 | sqlite_find(void *handle, uschar *filename, uschar *query, int length, |
| 90 | uschar **result, uschar **errmsg, BOOL *do_cache) |
| 91 | { |
| 92 | int ret; |
| 93 | struct strbuf res = { NULL, 0, 0 }; |
| 94 | |
| 95 | ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg); |
| 96 | if (ret != SQLITE_OK) |
| 97 | { |
| 98 | debug_printf("sqlite3_exec failed: %s\n", *errmsg); |
| 99 | return FAIL; |
| 100 | } |
| 101 | |
| 102 | if (res.string == NULL) *do_cache = FALSE; |
| 103 | |
| 104 | *result = res.string; |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | /************************************************* |
| 111 | * Close entry point * |
| 112 | *************************************************/ |
| 113 | |
| 114 | /* See local README for interface description. */ |
| 115 | |
| 116 | void sqlite_close(void *handle) |
| 117 | { |
| 118 | sqlite3_close(handle); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | /************************************************* |
| 124 | * Quote entry point * |
| 125 | *************************************************/ |
| 126 | |
| 127 | /* From what I have found so far, the only character that needs to be quoted |
| 128 | for sqlite is the single quote, and it is quoted by doubling. |
| 129 | |
| 130 | Arguments: |
| 131 | s the string to be quoted |
| 132 | opt additional option text or NULL if none |
| 133 | |
| 134 | Returns: the processed string or NULL for a bad option |
| 135 | */ |
| 136 | |
| 137 | uschar * |
| 138 | sqlite_quote(uschar *s, uschar *opt) |
| 139 | { |
| 140 | register int c; |
| 141 | int count = 0; |
| 142 | uschar *t = s; |
| 143 | uschar *quoted; |
| 144 | |
| 145 | if (opt != NULL) return NULL; /* No options recognized */ |
| 146 | |
| 147 | while ((c = *t++) != 0) if (c == '\'') count++; |
| 148 | |
| 149 | if (count == 0) return s; |
| 150 | t = quoted = store_get(Ustrlen(s) + count + 1); |
| 151 | |
| 152 | while ((c = *s++) != 0) |
| 153 | { |
| 154 | if (c == '\'') *t++ = '\''; |
| 155 | *t++ = c; |
| 156 | } |
| 157 | |
| 158 | *t = 0; |
| 159 | return quoted; |
| 160 | } |
| 161 | |
| 162 | #endif /* LOOKUP_SQLITE */ |
| 163 | |
| 164 | /* End of lookups/sqlite.c */ |