summaryrefslogtreecommitdiffstatsabout
path: root/utility/database.c
diff options
context:
space:
mode:
authorEdward Rudd <urkle@outoforder.cc>2008-10-23 06:29:36 (GMT)
committer Edward Rudd <urkle@outoforder.cc>2008-10-23 06:29:36 (GMT)
commitaed9f10440d8789f99919c3a6f91e8d76bbc44dc (patch)
tree2560bad5b4959cddc090916e8e522b37ce74df75 /utility/database.c
parent0ddd719a72469f732a881c93d4c804e9aca787fe (diff)
more parsing implementation (Custom functions)
initial DB inserting (w/ prepared query)
Diffstat (limited to 'utility/database.c')
-rw-r--r--utility/database.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/utility/database.c b/utility/database.c
index 5fece50..e7650aa 100644
--- a/utility/database.c
+++ b/utility/database.c
@@ -1,2 +1,97 @@
1#include "database.h" 1#include "database.h"
2#include "apu.h"
3#include "apr_dbd.h"
4#include "apr_strings.h"
2 5
6struct config_dbd_t {
7 const apr_dbd_driver_t *driver;
8 apr_dbd_t *dbd;
9 apr_dbd_prepared_t *stmt;
10 const char **args;
11};
12
13void database_init(apr_pool_t *p)
14{
15 apr_dbd_init(p);
16}
17
18apr_status_t database_connect(config_t *cfg)
19{
20 apr_status_t rv;
21 if (!cfg->dbconn) {
22 cfg->dbconn = apr_palloc(cfg->pool, sizeof(config_dbd_t));
23 }
24 rv = apr_dbd_get_driver(cfg->pool, cfg->dbdriver, &(cfg->dbconn->driver));
25 if (rv)
26 return rv;
27
28 rv = apr_dbd_open(cfg->dbconn->driver, cfg->pool, cfg->dbparams,
29 &(cfg->dbconn->dbd));
30 if (rv)
31 return rv;
32
33 return APR_SUCCESS;
34}
35
36apr_status_t database_disconnect(config_t *cfg)
37{
38 return apr_dbd_close(cfg->dbconn->driver, cfg->dbconn->dbd);
39}
40
41apr_status_t database_insert(config_t *cfg, apr_pool_t *p, apr_table_t *data)
42{
43 apr_status_t rv;
44 int f, nfs;
45 config_output_field_t *ofields;
46 ofields = (config_output_field_t *)cfg->output_fields->elts;
47 nfs = cfg->output_fields->nelts;
48 // Prepare statement
49 if (!cfg->dbconn->stmt) {
50 char *sql;
51 int i;
52 struct iovec *vec;
53 vec = apr_palloc(p, (nfs*2 + 5) * sizeof(struct iovec));
54 sql = apr_palloc(p, (nfs*3));
55 vec[0].iov_base = "INSERT INTO ";
56 vec[0].iov_len = 12;
57 vec[1].iov_base = (void *)cfg->table;
58 vec[1].iov_len = strlen(cfg->table);
59 vec[2].iov_base = " (";
60 vec[2].iov_len = 2;
61 for (i=3, f=0; f<nfs; f++, i+=2) {
62 vec[i].iov_base = (void *)ofields[f].field;
63 vec[i].iov_len = strlen(vec[i].iov_base);
64 vec[i+1].iov_base = ",";
65 vec[i+1].iov_len = 1;
66 memcpy(&sql[f*3], "%s,", 3);
67 }
68 sql[nfs*3-1] = '\0';
69 vec[i-1].iov_base = ") VALUES (";
70 vec[i-1].iov_len = 10;
71 vec[i].iov_base = sql;
72 vec[i].iov_len = nfs*3-1;
73 vec[i+1].iov_base = ")";
74 vec[i+1].iov_len = 1;
75 sql = apr_pstrcatv(p, vec, i+2, NULL);
76 printf("SQL: %s\n", sql);
77 cfg->dbconn->args = apr_palloc(cfg->pool, nfs * sizeof(char *));
78 rv = apr_dbd_prepare(cfg->dbconn->driver, cfg->pool, cfg->dbconn->dbd,
79 sql, "INSERT", &(cfg->dbconn->stmt));
80 if (rv) {
81 printf("DB Error: %s\n", apr_dbd_error(cfg->dbconn->driver,
82 cfg->dbconn->dbd, rv));
83 return rv;
84 }
85 }
86 for (f=0; f<nfs; f++) {
87 cfg->dbconn->args[f] = apr_table_get(data, ofields[f].field);
88 }
89 rv = apr_dbd_pquery(cfg->dbconn->driver, p, cfg->dbconn->dbd, &f,
90 cfg->dbconn->stmt, nfs, cfg->dbconn->args);
91 if (rv) {
92 printf("DB Error: %s\n", apr_dbd_error(cfg->dbconn->driver,
93 cfg->dbconn->dbd, rv));
94 return rv;
95 }
96 return APR_SUCCESS;
97}