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
,
82 ret
= sqlite3_exec(handle
, CS query
, sqlite_callback
, &res
, (char **)errmsg
);
85 debug_printf_indent("sqlite3_exec failed: %s\n", *errmsg
);
89 if (!res
) *do_cache
= 0;
91 *result
= string_from_gstring(res
);
97 /*************************************************
99 *************************************************/
101 /* See local README for interface description. */
103 static void sqlite_close(void *handle
)
105 sqlite3_close(handle
);
110 /*************************************************
111 * Quote entry point *
112 *************************************************/
114 /* From what I have found so far, the only character that needs to be quoted
115 for sqlite is the single quote, and it is quoted by doubling.
118 s the string to be quoted
119 opt additional option text or NULL if none
121 Returns: the processed string or NULL for a bad option
125 sqlite_quote(uschar
*s
, uschar
*opt
)
132 if (opt
!= NULL
) return NULL
; /* No options recognized */
134 while ((c
= *t
++) != 0) if (c
== '\'') count
++;
136 if (count
== 0) return s
;
137 t
= quoted
= store_get(Ustrlen(s
) + count
+ 1, is_tainted(s
));
139 while ((c
= *s
++) != 0)
141 if (c
== '\'') *t
++ = '\'';
151 /*************************************************
152 * Version reporting entry point *
153 *************************************************/
155 /* See local README for interface description. */
157 #include "../version.h"
160 sqlite_version_report(FILE *f
)
162 fprintf(f
, "Library version: SQLite: Compile: %s\n"
164 SQLITE_VERSION
, sqlite3_libversion());
166 fprintf(f
, " Exim version %s\n", EXIM_VERSION_STR
);
170 static lookup_info _lookup_info
= {
171 US
"sqlite", /* lookup name */
172 lookup_absfilequery
, /* query-style lookup, starts with file name */
173 sqlite_open
, /* open function */
174 NULL
, /* no check function */
175 sqlite_find
, /* find function */
176 sqlite_close
, /* close function */
177 NULL
, /* no tidy function */
178 sqlite_quote
, /* quoting function */
179 sqlite_version_report
/* version reporting */
183 #define sqlite_lookup_module_info _lookup_module_info
186 static lookup_info
*_lookup_list
[] = { &_lookup_info
};
187 lookup_module_info sqlite_lookup_module_info
= { LOOKUP_MODULE_INFO_MAGIC
, _lookup_list
, 1 };
189 /* End of lookups/sqlite.c */