1 /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.1 2005/08/01 13:20:28 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2005 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
15 static void dummy(int x
) { dummy(x
-1); }
20 /*************************************************
22 *************************************************/
24 /* See local README for interface description. */
27 sqlite_open(uschar
*filename
, uschar
**errmsg
)
32 ret
= sqlite3_open((char *)filename
, &db
);
35 *errmsg
= (void *)sqlite3_errmsg(db
);
36 debug_printf("Error opening database: %s\n", *errmsg
);
43 /*************************************************
45 *************************************************/
47 /* See local README for interface description. */
55 static int sqlite_callback(void *arg
, int argc
, char **argv
, char **azColName
)
57 struct strbuf
*res
= arg
;
60 /* For second and subsequent results, insert \n */
62 if (res
->string
!= NULL
)
63 res
->string
= string_cat(res
->string
, &res
->size
, &res
->len
, US
"\n", 1);
67 /* For multiple fields, include the field name too */
68 for (i
= 0; i
< argc
; i
++)
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
);
78 res
->string
= string_append(res
->string
, &res
->size
, &res
->len
, 1,
79 (argv
[0] != NULL
)? argv
[0]:"<NULL>");
82 res
->string
[res
->len
] = 0;
88 sqlite_find(void *handle
, uschar
*filename
, uschar
*query
, int length
,
89 uschar
**result
, uschar
**errmsg
, BOOL
*do_cache
)
92 struct strbuf res
= { NULL
, 0, 0 };
94 ret
= sqlite3_exec(handle
, (char *)query
, sqlite_callback
, &res
, (char **)errmsg
);
97 debug_printf("sqlite3_exec failed: %s\n", *errmsg
);
101 if (res
.string
== NULL
) *do_cache
= FALSE
;
103 *result
= res
.string
;
109 /*************************************************
110 * Close entry point *
111 *************************************************/
113 /* See local README for interface description. */
115 void sqlite_close(void *handle
)
117 sqlite3_close(handle
);
122 /*************************************************
123 * Quote entry point *
124 *************************************************/
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.
130 s the string to be quoted
131 opt additional option text or NULL if none
133 Returns: the processed string or NULL for a bad option
137 sqlite_quote(uschar
*s
, uschar
*opt
)
144 if (opt
!= NULL
) return NULL
; /* No options recognized */
146 while ((c
= *t
++) != 0) if (c
== '\'') count
++;
148 if (count
== 0) return s
;
149 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1);
151 while ((c
= *s
++) != 0)
153 if (c
== '\'') *t
++ = '\'';
161 #endif /* LOOKUP_SQLITE */
163 /* End of lookups/sqlite.c */