summaryrefslogtreecommitdiffstatsabout
path: root/utility/database.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility/database.c')
-rw-r--r--utility/database.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/utility/database.c b/utility/database.c
index 45022da..c4e4bc9 100644
--- a/utility/database.c
+++ b/utility/database.c
@@ -4,6 +4,7 @@
4#include "apr_strings.h" 4#include "apr_strings.h"
5 5
6#include "util.h" 6#include "util.h"
7#include "mysql/mysql.h"
7 8
8struct config_dbd_t { 9struct config_dbd_t {
9 const apr_dbd_driver_t *driver; 10 const apr_dbd_driver_t *driver;
@@ -27,9 +28,10 @@ apr_status_t database_connect(config_t *cfg)
27 } 28 }
28 rv = apr_dbd_get_driver(cfg->pool, cfg->dbdriver, &(cfg->dbconn->driver)); 29 rv = apr_dbd_get_driver(cfg->pool, cfg->dbdriver, &(cfg->dbconn->driver));
29 if (rv) { 30 if (rv) {
31
30 logging_log(cfg, LOGLEVEL_ERROR, 32 logging_log(cfg, LOGLEVEL_ERROR,
31 "Could not load database driver %s. Error %d", cfg->dbdriver, 33 "Could not load database driver %s. Error %s", cfg->dbdriver,
32 rv); 34 logging_strerror(rv));
33 return rv; 35 return rv;
34 } 36 }
35 37
@@ -37,7 +39,7 @@ apr_status_t database_connect(config_t *cfg)
37 &(cfg->dbconn->dbd)); 39 &(cfg->dbconn->dbd));
38 if (rv) { 40 if (rv) {
39 logging_log(cfg, LOGLEVEL_ERROR, 41 logging_log(cfg, LOGLEVEL_ERROR,
40 "Could not connect to database. Error %d", rv); 42 "Could not connect to database. Error %s", logging_strerror(rv));
41 return rv; 43 return rv;
42 } 44 }
43 45
@@ -55,7 +57,7 @@ static apr_dbd_prepared_t *database_prepare_insert(config_t *cfg, apr_pool_t *p)
55 char *sql; 57 char *sql;
56 int i, f; 58 int i, f;
57 struct iovec *vec; 59 struct iovec *vec;
58 apr_dbd_prepared_t *stmt; 60 apr_dbd_prepared_t *stmt = NULL;
59 int nfs = cfg->output_fields->nelts; 61 int nfs = cfg->output_fields->nelts;
60 config_output_field_t *ofields; 62 config_output_field_t *ofields;
61 63
@@ -87,14 +89,14 @@ static apr_dbd_prepared_t *database_prepare_insert(config_t *cfg, apr_pool_t *p)
87 89
88 sql = apr_pstrcatv(p, vec, i+2, NULL); 90 sql = apr_pstrcatv(p, vec, i+2, NULL);
89 91
90 printf("SQL: %s\n", sql); 92 logging_log(cfg, LOGLEVEL_DEBUG, "Generated SQL: %s", sql);
91 93
92 rv = apr_dbd_prepare(cfg->dbconn->driver, cfg->pool, cfg->dbconn->dbd, sql, 94 rv = apr_dbd_prepare(cfg->dbconn->driver, cfg->pool, cfg->dbconn->dbd, sql,
93 "INSERT", &stmt); 95 "INSERT", &stmt);
94 96
95 if (rv) { 97 if (rv) {
96 printf("DB Error: %s\n", apr_dbd_error(cfg->dbconn->driver, 98 logging_log(cfg, LOGLEVEL_ERROR, "Unable to Prepare SQL insert: %s",
97 cfg->dbconn->dbd, rv)); 99 apr_dbd_error(cfg->dbconn->driver, cfg->dbconn->dbd, rv));
98 return NULL; 100 return NULL;
99 } 101 }
100 return stmt; 102 return stmt;
@@ -109,6 +111,10 @@ apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data)
109 // Prepare statement 111 // Prepare statement
110 if (!cfg->dbconn->stmt) { 112 if (!cfg->dbconn->stmt) {
111 cfg->dbconn->stmt = database_prepare_insert(cfg, p); 113 cfg->dbconn->stmt = database_prepare_insert(cfg, p);
114 if (!cfg->dbconn->stmt) {
115 logging_log(cfg, LOGLEVEL_NOISE, "Unable to prepare SQL statement");
116 return APR_EINVAL;
117 }
112 cfg->dbconn->args = apr_palloc(cfg->pool, nfs * sizeof(char *)); 118 cfg->dbconn->args = apr_palloc(cfg->pool, nfs * sizeof(char *));
113 } 119 }
114 for (f=0; f<nfs; f++) { 120 for (f=0; f<nfs; f++) {
@@ -117,8 +123,8 @@ apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data)
117 rv = apr_dbd_pquery(cfg->dbconn->driver, p, cfg->dbconn->dbd, &f, 123 rv = apr_dbd_pquery(cfg->dbconn->driver, p, cfg->dbconn->dbd, &f,
118 cfg->dbconn->stmt, nfs, cfg->dbconn->args); 124 cfg->dbconn->stmt, nfs, cfg->dbconn->args);
119 if (rv) { 125 if (rv) {
120 printf("DB Error: %s\n", apr_dbd_error(cfg->dbconn->driver, 126 logging_log(cfg, LOGLEVEL_ERROR, "Unable to Insert SQL: %s",
121 cfg->dbconn->dbd, rv)); 127 apr_dbd_error(cfg->dbconn->driver, cfg->dbconn->dbd, rv));
122 return rv; 128 return rv;
123 } 129 }
124 return APR_SUCCESS; 130 return APR_SUCCESS;