1 /* $Cambridge: exim/src/src/lookups/sqlite.c,v 1.5 2009/11/16 19:50:38 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
11 #include "lf_functions.h"
16 /*************************************************
18 *************************************************/
20 /* See local README for interface description. */
23 sqlite_open(uschar
*filename
, uschar
**errmsg
)
28 ret
= sqlite3_open((char *)filename
, &db
);
31 *errmsg
= (void *)sqlite3_errmsg(db
);
32 debug_printf("Error opening database: %s\n", *errmsg
);
35 sqlite3_busy_timeout(db
, 1000 * sqlite_lock_timeout
);
40 /*************************************************
42 *************************************************/
44 /* See local README for interface description. */
52 static int sqlite_callback(void *arg
, int argc
, char **argv
, char **azColName
)
54 struct strbuf
*res
= arg
;
57 /* For second and subsequent results, insert \n */
59 if (res
->string
!= NULL
)
60 res
->string
= string_cat(res
->string
, &res
->size
, &res
->len
, US
"\n", 1);
64 /* For multiple fields, include the field name too */
65 for (i
= 0; i
< argc
; i
++)
67 uschar
*value
= US((argv
[i
] != NULL
)? argv
[i
]:"<NULL>");
68 res
->string
= lf_quote(US azColName
[i
], value
, Ustrlen(value
), res
->string
,
69 &res
->size
, &res
->len
);
75 res
->string
= string_append(res
->string
, &res
->size
, &res
->len
, 1,
76 (argv
[0] != NULL
)? argv
[0]:"<NULL>");
79 res
->string
[res
->len
] = 0;
85 sqlite_find(void *handle
, uschar
*filename
, uschar
*query
, int length
,
86 uschar
**result
, uschar
**errmsg
, BOOL
*do_cache
)
89 struct strbuf res
= { NULL
, 0, 0 };
91 ret
= sqlite3_exec(handle
, (char *)query
, sqlite_callback
, &res
, (char **)errmsg
);
94 debug_printf("sqlite3_exec failed: %s\n", *errmsg
);
98 if (res
.string
== NULL
) *do_cache
= FALSE
;
100 *result
= res
.string
;
106 /*************************************************
107 * Close entry point *
108 *************************************************/
110 /* See local README for interface description. */
112 static void sqlite_close(void *handle
)
114 sqlite3_close(handle
);
119 /*************************************************
120 * Quote entry point *
121 *************************************************/
123 /* From what I have found so far, the only character that needs to be quoted
124 for sqlite is the single quote, and it is quoted by doubling.
127 s the string to be quoted
128 opt additional option text or NULL if none
130 Returns: the processed string or NULL for a bad option
134 sqlite_quote(uschar
*s
, uschar
*opt
)
141 if (opt
!= NULL
) return NULL
; /* No options recognized */
143 while ((c
= *t
++) != 0) if (c
== '\'') count
++;
145 if (count
== 0) return s
;
146 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1);
148 while ((c
= *s
++) != 0)
150 if (c
== '\'') *t
++ = '\'';
160 /*************************************************
161 * Version reporting entry point *
162 *************************************************/
164 /* See local README for interface description. */
166 #include "../version.h"
169 sqlite_version_report(FILE *f
)
171 fprintf(f
, "Library version: SQLite: Compile: %s\n"
173 SQLITE_VERSION
, sqlite3_libversion());
175 fprintf(f
, " Exim version %s\n", EXIM_VERSION_STR
);
179 static lookup_info _lookup_info
= {
180 US
"sqlite", /* lookup name */
181 lookup_absfilequery
, /* query-style lookup, starts with file name */
182 sqlite_open
, /* open function */
183 NULL
, /* no check function */
184 sqlite_find
, /* find function */
185 sqlite_close
, /* close function */
186 NULL
, /* no tidy function */
187 sqlite_quote
, /* quoting function */
188 sqlite_version_report
/* version reporting */
192 #define sqlite_lookup_module_info _lookup_module_info
195 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
196 lookup_module_info sqlite_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
198 /* End of lookups/sqlite.c */