summaryrefslogtreecommitdiffstatsabout
path: root/utility/database.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility/database.c')
-rw-r--r--utility/database.c66
1 files changed, 59 insertions, 7 deletions
diff --git a/utility/database.c b/utility/database.c
index af8db97..98c203b 100644
--- a/utility/database.c
+++ b/utility/database.c
@@ -10,6 +10,7 @@ struct config_dbd_t {
10 const apr_dbd_driver_t *driver; 10 const apr_dbd_driver_t *driver;
11 apr_dbd_t *dbd; 11 apr_dbd_t *dbd;
12 apr_dbd_prepared_t *stmt; 12 apr_dbd_prepared_t *stmt;
13 apr_dbd_transaction_t *txn;
13 const char **args; 14 const char **args;
14}; 15};
15 16
@@ -30,8 +31,8 @@ apr_status_t database_connect(config_t *cfg)
30 if (rv) { 31 if (rv) {
31 32
32 logging_log(cfg, LOGLEVEL_ERROR, 33 logging_log(cfg, LOGLEVEL_ERROR,
33 "DB: Could not load database driver %s. Error %s", cfg->dbdriver, 34 "DB: Could not load database driver %s. Error %s",
34 logging_strerror(rv)); 35 cfg->dbdriver, logging_strerror(rv));
35 return rv; 36 return rv;
36 } 37 }
37 38
@@ -39,7 +40,8 @@ apr_status_t database_connect(config_t *cfg)
39 &(cfg->dbconn->dbd)); 40 &(cfg->dbconn->dbd));
40 if (rv) { 41 if (rv) {
41 logging_log(cfg, LOGLEVEL_ERROR, 42 logging_log(cfg, LOGLEVEL_ERROR,
42 "DB: Could not connect to database. Error (%d)%s", rv, logging_strerror(rv)); 43 "DB: Could not connect to database. Error (%d)%s", rv,
44 logging_strerror(rv));
43 return rv; 45 return rv;
44 } 46 }
45 47
@@ -57,7 +59,7 @@ static apr_dbd_prepared_t *database_prepare_insert(config_t *cfg, apr_pool_t *p)
57 char *sql; 59 char *sql;
58 int i, f; 60 int i, f;
59 struct iovec *vec; 61 struct iovec *vec;
60 apr_dbd_prepared_t *stmt = NULL; 62 apr_dbd_prepared_t *stmt= NULL;
61 int nfs = cfg->output_fields->nelts; 63 int nfs = cfg->output_fields->nelts;
62 config_output_field_t *ofields; 64 config_output_field_t *ofields;
63 65
@@ -95,12 +97,14 @@ static apr_dbd_prepared_t *database_prepare_insert(config_t *cfg, apr_pool_t *p)
95 "INSERT", &stmt); 97 "INSERT", &stmt);
96 98
97 if (rv) { 99 if (rv) {
98 logging_log(cfg, LOGLEVEL_NOISE, "DB: Unable to Prepare SQL insert: %s", 100 logging_log(cfg, LOGLEVEL_NOISE,
99 apr_dbd_error(cfg->dbconn->driver, cfg->dbconn->dbd, rv)); 101 "DB: Unable to Prepare SQL insert: %s", apr_dbd_error(
102 cfg->dbconn->driver, cfg->dbconn->dbd, rv));
100 return NULL; 103 return NULL;
101 } 104 }
102 return stmt; 105 return stmt;
103} 106}
107
104apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data) 108apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data)
105{ 109{
106 apr_status_t rv; 110 apr_status_t rv;
@@ -129,10 +133,25 @@ apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data)
129 return APR_SUCCESS; 133 return APR_SUCCESS;
130} 134}
131 135
132/** @todo implement transactions */
133apr_status_t database_trans_start(config_t *cfg, apr_pool_t *p) 136apr_status_t database_trans_start(config_t *cfg, apr_pool_t *p)
134{ 137{
135#if HAVE_APR_DBD_TRANSACTION_MODE_GET 138#if HAVE_APR_DBD_TRANSACTION_MODE_GET
139 apr_status_t rv;
140 if (!cfg->transactions)
141 return APR_SUCCESS;
142 if (cfg->dbconn->txn) {
143 logging_log(cfg, LOGLEVEL_NOISE,
144 "Transaction Already Started. Something is BROKE");
145 return APR_EINVAL;
146 }
147 logging_log(cfg, LOGLEVEL_DEBUG, "DB: Starting Transaction");
148 rv = apr_dbd_transaction_start(cfg->dbconn->driver, p, cfg->dbconn->dbd,
149 &cfg->dbconn->txn);
150 if (rv)
151 logging_log(cfg, LOGLEVEL_NOISE,
152 "DB: Error Starting Transaction: (%d)%s", rv, apr_dbd_error(
153 cfg->dbconn->driver, cfg->dbconn->dbd, rv));
154 return rv;
136#else 155#else
137 return APR_SUCCESS; 156 return APR_SUCCESS;
138#endif 157#endif
@@ -141,6 +160,23 @@ apr_status_t database_trans_start(config_t *cfg, apr_pool_t *p)
141apr_status_t database_trans_stop(config_t *cfg, apr_pool_t *p) 160apr_status_t database_trans_stop(config_t *cfg, apr_pool_t *p)
142{ 161{
143#if HAVE_APR_DBD_TRANSACTION_MODE_GET 162#if HAVE_APR_DBD_TRANSACTION_MODE_GET
163 apr_status_t rv;
164 if (!cfg->transactions)
165 return APR_SUCCESS;
166 if (!cfg->dbconn->txn) {
167 logging_log(cfg, LOGLEVEL_NOISE,
168 "No Transaction Started. Something is BROKE");
169 return APR_EINVAL;
170 }
171 logging_log(cfg, LOGLEVEL_DEBUG, "DB: Stopping Transaction");
172 rv = apr_dbd_transaction_end(cfg->dbconn->driver, p, cfg->dbconn->txn);
173 if (rv)
174 logging_log(cfg, LOGLEVEL_NOISE,
175 "DB: Error Stopping Transaction: (%d)%s", rv, apr_dbd_error(
176 cfg->dbconn->driver, cfg->dbconn->dbd, rv));
177
178 cfg->dbconn->txn = NULL;
179 return rv;
144#else 180#else
145 return APR_SUCCESS; 181 return APR_SUCCESS;
146#endif 182#endif
@@ -149,6 +185,22 @@ apr_status_t database_trans_stop(config_t *cfg, apr_pool_t *p)
149apr_status_t database_trans_abort(config_t *cfg) 185apr_status_t database_trans_abort(config_t *cfg)
150{ 186{
151#if HAVE_APR_DBD_TRANSACTION_MODE_GET 187#if HAVE_APR_DBD_TRANSACTION_MODE_GET
188 apr_status_t rv;
189 if (!cfg->transactions)
190 return APR_SUCCESS;
191 if (!cfg->dbconn->txn) {
192 logging_log(cfg, LOGLEVEL_NOISE,
193 "No Transaction Started. Something is BROKE");
194 return APR_EINVAL;
195 }
196 logging_log(cfg, LOGLEVEL_NOTICE, "DB: Aborting Transaction");
197 rv = apr_dbd_transaction_mode_set(cfg->dbconn->driver, cfg->dbconn->txn,
198 APR_DBD_TRANSACTION_ROLLBACK);
199 if (rv)
200 logging_log(cfg, LOGLEVEL_NOISE,
201 "DB: Error Aborting Transaction: (%d)%s", rv, apr_dbd_error(
202 cfg->dbconn->driver, cfg->dbconn->dbd, rv));
203 return rv;
152#else 204#else
153 return APR_SUCCESS; 205 return APR_SUCCESS;
154#endif 206#endif