1 /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.3 2006/02/07 11:19:01 ph10 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2006 */
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
);
39 sqlite3_busy_timeout(db
, 1000 * sqlite_lock_timeout
);
44 /*************************************************
46 *************************************************/
48 /* See local README for interface description. */
56 static int sqlite_callback(void *arg
, int argc
, char **argv
, char **azColName
)
58 struct strbuf
*res
= arg
;
61 /* For second and subsequent results, insert \n */
63 if (res
->string
!= NULL
)
64 res
->string
= string_cat(res
->string
, &res
->size
, &res
->len
, US
"\n", 1);
68 /* For multiple fields, include the field name too */
69 for (i
= 0; i
< argc
; i
++)
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
);
79 res
->string
= string_append(res
->string
, &res
->size
, &res
->len
, 1,
80 (argv
[0] != NULL
)? argv
[0]:"<NULL>");
83 res
->string
[res
->len
] = 0;
89 sqlite_find(void *handle
, uschar
*filename
, uschar
*query
, int length
,
90 uschar
**result
, uschar
**errmsg
, BOOL
*do_cache
)
93 struct strbuf res
= { NULL
, 0, 0 };
95 ret
= sqlite3_exec(handle
, (char *)query
, sqlite_callback
, &res
, (char **)errmsg
);
98 debug_printf("sqlite3_exec failed: %s\n", *errmsg
);
102 if (res
.string
== NULL
) *do_cache
= FALSE
;
104 *result
= res
.string
;
110 /*************************************************
111 * Close entry point *
112 *************************************************/
114 /* See local README for interface description. */
116 void sqlite_close(void *handle
)
118 sqlite3_close(handle
);
123 /*************************************************
124 * Quote entry point *
125 *************************************************/
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.
131 s the string to be quoted
132 opt additional option text or NULL if none
134 Returns: the processed string or NULL for a bad option
138 sqlite_quote(uschar
*s
, uschar
*opt
)
145 if (opt
!= NULL
) return NULL
; /* No options recognized */
147 while ((c
= *t
++) != 0) if (c
== '\'') count
++;
149 if (count
== 0) return s
;
150 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1);
152 while ((c
= *s
++) != 0)
154 if (c
== '\'') *t
++ = '\'';
162 #endif /* LOOKUP_SQLITE */
164 /* End of lookups/sqlite.c */