Reversed a previous HP-UX compiler option change as I'm told it was not
[exim.git] / src / src / lookups / sqlite.c
CommitLineData
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
15static 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
26void *
27sqlite_open(uschar *filename, uschar **errmsg)
28{
29sqlite3 *db = NULL;
30int ret;
31
32ret = sqlite3_open((char *)filename, &db);
33if (ret != 0)
34 {
35 *errmsg = (void *)sqlite3_errmsg(db);
36 debug_printf("Error opening database: %s\n", *errmsg);
37 }
38
39return db;
40}
41
42
43/*************************************************
44* Find entry point *
45*************************************************/
46
47/* See local README for interface description. */
48
49struct strbuf {
50 uschar *string;
51 int size;
52 int len;
53};
54
55static int sqlite_callback(void *arg, int argc, char **argv, char **azColName)
56{
57struct strbuf *res = arg;
58int i;
59
60/* For second and subsequent results, insert \n */
61
62if (res->string != NULL)
63 res->string = string_cat(res->string, &res->size, &res->len, US"\n", 1);
64
65if (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
76else
77 {
78 res->string = string_append(res->string, &res->size, &res->len, 1,
79 (argv[0] != NULL)? argv[0]:"<NULL>");
80 }
81
82res->string[res->len] = 0;
83return 0;
84}
85
86
87int
88sqlite_find(void *handle, uschar *filename, uschar *query, int length,
89 uschar **result, uschar **errmsg, BOOL *do_cache)
90{
91int ret;
92struct strbuf res = { NULL, 0, 0 };
93
94ret = sqlite3_exec(handle, (char *)query, sqlite_callback, &res, (char **)errmsg);
95if (ret != SQLITE_OK)
96 {
97 debug_printf("sqlite3_exec failed: %s\n", *errmsg);
98 return FAIL;
99 }
100
101if (res.string == NULL) *do_cache = FALSE;
102
103*result = res.string;
104return OK;
105}
106
107
108
109/*************************************************
110* Close entry point *
111*************************************************/
112
113/* See local README for interface description. */
114
115void sqlite_close(void *handle)
116{
117sqlite3_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
127for sqlite is the single quote, and it is quoted by doubling.
128
129Arguments:
130 s the string to be quoted
131 opt additional option text or NULL if none
132
133Returns: the processed string or NULL for a bad option
134*/
135
136uschar *
137sqlite_quote(uschar *s, uschar *opt)
138{
139register int c;
140int count = 0;
141uschar *t = s;
142uschar *quoted;
143
144if (opt != NULL) return NULL; /* No options recognized */
145
146while ((c = *t++) != 0) if (c == '\'') count++;
147
148if (count == 0) return s;
149t = quoted = store_get(Ustrlen(s) + count + 1);
150
151while ((c = *s++) != 0)
152 {
153 if (c == '\'') *t++ = '\'';
154 *t++ = c;
155 }
156
157*t = 0;
158return quoted;
159}
160
161#endif /* LOOKUP_SQLITE */
162
163/* End of lookups/sqlite.c */