Commit | Line | Data |
---|---|---|
13b685f9 PH |
1 | /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.1 2005/08/01 13:20:28 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 | return db; | |
40 | } | |
41 | ||
42 | ||
43 | /************************************************* | |
44 | * Find entry point * | |
45 | *************************************************/ | |
46 | ||
47 | /* See local README for interface description. */ | |
48 | ||
49 | struct strbuf { | |
50 | uschar *string; | |
51 | int size; | |
52 | int len; | |
53 | }; | |
54 | ||
55 | static int sqlite_callback(void *arg, int argc, char **argv, char **azColName) | |
56 | { | |
57 | struct strbuf *res = arg; | |
58 | int i; | |
59 | ||
60 | /* For second and subsequent results, insert \n */ | |
61 | ||
62 | if (res->string != NULL) | |
63 | res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1); | |
64 | ||
65 | if (argc > 1) | |
66 | { | |
67 | /* For multiple fields, include the field name too */ | |
68 | for (i = 0; i < argc; i++) | |
69 | { | |
70 | uschar *value = US((argv[i] != NULL)? argv[i]:"<NULL>"); | |
71 | res->string = lf_quote(US azColName[i], value, Ustrlen(value), res->string, | |
72 | &res->size, &res->len); | |
73 | } | |
74 | } | |
75 | ||
76 | else | |
77 | { | |
78 | res->string = string_append(res->string, &res->size, &res->len, 1, | |
79 | (argv[0] != NULL)? argv[0]:"<NULL>"); | |
80 | } | |
81 | ||
82 | res->string[res->len] = 0; | |
83 | return 0; | |
84 | } | |
85 | ||
86 | ||
87 | int | |
88 | sqlite_find(void *handle, uschar *filename, uschar *query, int length, | |
89 | uschar **result, uschar **errmsg, BOOL *do_cache) | |
90 | { | |
91 | int ret; | |
92 | struct strbuf res = { NULL, 0, 0 }; | |
93 | ||
94 | ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg); | |
95 | if (ret != SQLITE_OK) | |
96 | { | |
97 | debug_printf("sqlite3_exec failed: %s\n", *errmsg); | |
98 | return FAIL; | |
99 | } | |
100 | ||
101 | if (res.string == NULL) *do_cache = FALSE; | |
102 | ||
103 | *result = res.string; | |
104 | return OK; | |
105 | } | |
106 | ||
107 | ||
108 | ||
109 | /************************************************* | |
110 | * Close entry point * | |
111 | *************************************************/ | |
112 | ||
113 | /* See local README for interface description. */ | |
114 | ||
115 | void sqlite_close(void *handle) | |
116 | { | |
117 | sqlite3_close(handle); | |
118 | } | |
119 | ||
120 | ||
121 | ||
122 | /************************************************* | |
123 | * Quote entry point * | |
124 | *************************************************/ | |
125 | ||
126 | /* From what I have found so far, the only character that needs to be quoted | |
127 | for sqlite is the single quote, and it is quoted by doubling. | |
128 | ||
129 | Arguments: | |
130 | s the string to be quoted | |
131 | opt additional option text or NULL if none | |
132 | ||
133 | Returns: the processed string or NULL for a bad option | |
134 | */ | |
135 | ||
136 | uschar * | |
137 | sqlite_quote(uschar *s, uschar *opt) | |
138 | { | |
139 | register int c; | |
140 | int count = 0; | |
141 | uschar *t = s; | |
142 | uschar *quoted; | |
143 | ||
144 | if (opt != NULL) return NULL; /* No options recognized */ | |
145 | ||
146 | while ((c = *t++) != 0) if (c == '\'') count++; | |
147 | ||
148 | if (count == 0) return s; | |
149 | t = quoted = store_get(Ustrlen(s) + count + 1); | |
150 | ||
151 | while ((c = *s++) != 0) | |
152 | { | |
153 | if (c == '\'') *t++ = '\''; | |
154 | *t++ = c; | |
155 | } | |
156 | ||
157 | *t = 0; | |
158 | return quoted; | |
159 | } | |
160 | ||
161 | #endif /* LOOKUP_SQLITE */ | |
162 | ||
163 | /* End of lookups/sqlite.c */ |