summaryrefslogtreecommitdiffstats
path: root/src/mod_log_sql_dbd.c
diff options
context:
space:
mode:
authorGravatar Edward Rudd 2008-09-21 15:54:12 +0000
committerGravatar Edward Rudd 2008-09-21 15:54:12 +0000
commitba30ceeb705e9b4d40ce0d98f6a4e047d47ce919 (patch)
tree5768679317a303031c80be6cba683b6addeb07ac /src/mod_log_sql_dbd.c
parentd33662354f64354c601ae257bd5b1b043c484d97 (diff)
moved all modules source to src subdirectory.. Moved header files into include subdirectory
cleaned up makefiles.
Diffstat (limited to 'src/mod_log_sql_dbd.c')
-rw-r--r--src/mod_log_sql_dbd.c132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/mod_log_sql_dbd.c b/src/mod_log_sql_dbd.c
new file mode 100644
index 0000000..c78a128
--- /dev/null
+++ b/src/mod_log_sql_dbd.c
@@ -0,0 +1,132 @@
1/* $Id: mod_log_sql_dbi.c 120 2004-04-17 15:14:12Z urkle@drip.ws $ */
2
3#if defined(WITH_APACHE20)
4# include "apache20.h"
5#else
6# error Unsupported Apache version
7#endif
8
9
10#ifdef HAVE_CONFIG_H
11/* Undefine these to prevent conflicts between Apache ap_config_auto.h and
12 * my config.h. Only really needed for Apache < 2.0.48, but it can't hurt.
13 */
14#undef PACKAGE_BUGREPORT
15#undef PACKAGE_NAME
16#undef PACKAGE_STRING
17#undef PACKAGE_TARNAME
18#undef PACKAGE_VERSION
19
20#include "config.h"
21#endif
22
23#include "mod_log_sql.h"
24
25#include "apr_dbd.h"
26#include "mod_dbd.h"
27
28typedef struct {
29 ap_dbd_t *dbd;
30} request_config_t;
31
32LOGSQL_MODULE_FORWARD(dbd);
33
34static ap_dbd_t *(*dbd_acquire_fn)(request_rec*) = NULL;
35
36static ap_dbd_t *log_sql_dbd_getconnection(request_rec *r)
37{
38 request_config_t *rconf = ap_get_module_config(r->request_config, &LOGSQL_MODULE(dbd));
39 if (!rconf) {
40 rconf = apr_pcalloc(r->pool, sizeof(request_config_t));
41 ap_set_module_config(r->request_config, &LOGSQL_MODULE(dbd), (void *)rconf);
42 rconf->dbd = dbd_acquire_fn(r);
43 }
44 return rconf->dbd;
45}
46
47/* Connect to the database */
48static logsql_opendb_ret log_sql_dbd_connect(server_rec *s, logsql_dbconnection *db)
49{
50 // We are using mod_dbd so we don't do anything here
51 if (!dbd_acquire_fn) {
52 // no mod_dbd return failure
53 log_error(APLOG_MARK,APLOG_ERR,0, s,"mod_log_sql_dbd: mod_dbd is not loaded or available");
54 return LOGSQL_OPENDB_FAIL;
55 } else {
56 return LOGSQL_OPENDB_SUCCESS;
57 }
58}
59
60/* Close the DB link */
61static void log_sql_dbd_close(logsql_dbconnection *db)
62{
63 // mod_dbd handles this, so do nothing
64}
65
66/* Routine to escape the 'dangerous' characters that would otherwise
67 * corrupt the INSERT string: ', \, and "
68 */
69static const char *log_sql_dbd_escape(request_rec *r, const char *from_str, apr_pool_t *p,
70 logsql_dbconnection *db)
71{
72 // Acquire a DBD connection from mod_dbd
73 ap_dbd_t *dbd = log_sql_dbd_getconnection(r);
74 if (!dbd) return NULL;
75
76 if (!from_str)
77 return NULL;
78
79 return apr_pstrcat(p, "'",apr_dbd_escape(dbd->driver, p, from_str, dbd->handle),"'",NULL);
80}
81
82/* Run an insert query and return a categorized error or success */
83static logsql_query_ret log_sql_dbd_query(request_rec *r,logsql_dbconnection *db,
84 const char *query)
85{
86 int ret;
87 const char *err;
88 int affected;
89 // Acquire a DBD connection from mod_dbd
90 ap_dbd_t *dbd = log_sql_dbd_getconnection(r);
91 if (!dbd) return LOGSQL_QUERY_NOLINK;
92
93 // Run the query
94 ret = apr_dbd_query(dbd->driver, dbd->handle, &affected, query);
95 if (ret == 0) {
96 return LOGSQL_QUERY_SUCCESS;
97 } else {
98 // attempt to detect error message
99 err = apr_dbd_error(dbd->driver, dbd->handle, ret);
100 log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "DB Returned error: (%d) %s", ret, err);
101 // Unable to check if "NO SUCH TABLE" due to apr_dbd not mapping error codes to a standard set.
102 return LOGSQL_QUERY_FAIL;
103 }
104}
105
106/* Create table table_name of type table_type. */
107static logsql_table_ret log_sql_dbd_create(request_rec *r, logsql_dbconnection *db,
108 logsql_tabletype table_type, const char *table_name)
109{
110 return LOGSQL_TABLE_FAIL;
111}
112
113static const char *supported_drivers[] = {"dbd",NULL};
114static logsql_dbdriver log_sql_dbd_driver = {
115 "dbd",
116 supported_drivers,
117 log_sql_dbd_connect,/* open DB connection */
118 log_sql_dbd_close, /* close DB connection */
119 log_sql_dbd_escape, /* escape query */
120 log_sql_dbd_query, /* insert query */
121 log_sql_dbd_create /* create table */
122};
123
124LOGSQL_REGISTER(dbd) {
125 dbd_acquire_fn = APR_RETRIEVE_OPTIONAL_FN(ap_dbd_acquire);
126 if (dbd_acquire_fn == NULL) {
127 log_error(APLOG_MARK,APLOG_ERR,0,s,"You must load mod_dbd to enable AuthDBD functions");
128 }
129
130 log_sql_register_driver(p,&log_sql_dbd_driver);
131 LOGSQL_REGISTER_RETURN;
132}