1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
9 #include "lf_functions.h"
14 /*************************************************
16 *************************************************/
18 /* See local README for interface description. */
21 sqlite_open(const uschar
* filename
, uschar
** errmsg
)
26 if (!filename
|| !*filename
) filename
= sqlite_dbfile
;
28 *errmsg
= US
"absolute file name expected for \"sqlite\" lookup";
29 else if ((ret
= sqlite3_open(CCS filename
, &db
)) != 0)
31 *errmsg
= (void *)sqlite3_errmsg(db
);
32 DEBUG(D_lookup
) debug_printf_indent("Error opening database: %s\n", *errmsg
);
35 sqlite3_busy_timeout(db
, 1000 * sqlite_lock_timeout
);
40 /*************************************************
42 *************************************************/
44 /* See local README for interface description. */
47 sqlite_callback(void *arg
, int argc
, char **argv
, char **azColName
)
49 gstring
* res
= *(gstring
**)arg
;
51 /* For second and subsequent results, insert \n */
54 res
= string_catn(res
, US
"\n", 1);
58 /* For multiple fields, include the field name too */
59 for (int i
= 0; i
< argc
; i
++)
61 uschar
*value
= US((argv
[i
] != NULL
)? argv
[i
]:"<NULL>");
62 res
= lf_quote(US azColName
[i
], value
, Ustrlen(value
), res
);
67 res
= string_cat(res
, argv
[0] ? US argv
[0] : US
"<NULL>");
69 *(gstring
**)arg
= res
;
75 sqlite_find(void * handle
, const uschar
* filename
, const uschar
* query
,
76 int length
, uschar
** result
, uschar
** errmsg
, uint
* do_cache
)
81 ret
= sqlite3_exec(handle
, CS query
, sqlite_callback
, &res
, (char **)errmsg
);
84 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg
);
88 if (!res
) *do_cache
= 0;
90 *result
= string_from_gstring(res
);
96 /*************************************************
98 *************************************************/
100 /* See local README for interface description. */
102 static void sqlite_close(void *handle
)
104 sqlite3_close(handle
);
109 /*************************************************
110 * Quote entry point *
111 *************************************************/
113 /* From what I have found so far, the only character that needs to be quoted
114 for sqlite is the single quote, and it is quoted by doubling.
117 s the string to be quoted
118 opt additional option text or NULL if none
120 Returns: the processed string or NULL for a bad option
124 sqlite_quote(uschar
*s
, uschar
*opt
)
131 if (opt
!= NULL
) return NULL
; /* No options recognized */
133 while ((c
= *t
++) != 0) if (c
== '\'') count
++;
135 if (count
== 0) return s
;
136 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1, is_tainted(s
));
138 while ((c
= *s
++) != 0)
140 if (c
== '\'') *t
++ = '\'';
150 /*************************************************
151 * Version reporting entry point *
152 *************************************************/
154 /* See local README for interface description. */
156 #include "../version.h"
159 sqlite_version_report(FILE *f
)
161 fprintf(f
, "Library version: SQLite: Compile: %s\n"
163 SQLITE_VERSION
, sqlite3_libversion());
165 fprintf(f
, " Exim version %s\n", EXIM_VERSION_STR
);
169 static lookup_info _lookup_info
= {
170 US
"sqlite", /* lookup name */
171 lookup_absfilequery
, /* query-style lookup, starts with file name */
172 sqlite_open
, /* open function */
173 NULL
, /* no check function */
174 sqlite_find
, /* find function */
175 sqlite_close
, /* close function */
176 NULL
, /* no tidy function */
177 sqlite_quote
, /* quoting function */
178 sqlite_version_report
/* version reporting */
182 #define sqlite_lookup_module_info _lookup_module_info
185 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
186 lookup_module_info sqlite_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
188 /* End of lookups/sqlite.c */