|
terom@12
|
1 |
#include <stdio.h> |
|
terom@12
|
2 |
|
|
terom@12
|
3 |
#include "evpq.h" |
|
terom@12
|
4 |
#include "lib/log.h" |
|
terom@12
|
5 |
|
|
terom@12
|
6 |
#define CONNINFO_DEFAULT "dbname=test" |
|
terom@12
|
7 |
#define QUERY_DEFAULT "SELECT a, b FROM foo" |
|
terom@12
|
8 |
|
|
terom@12
|
9 |
void cb_connected (struct evpq_conn *conn, void *arg) { |
|
terom@12
|
10 |
INFO("[evpq_test] connected"); |
|
terom@12
|
11 |
|
|
terom@12
|
12 |
if (evpq_query(conn, QUERY_DEFAULT)) |
|
terom@12
|
13 |
FATAL("evpq_query"); |
|
terom@12
|
14 |
} |
|
terom@12
|
15 |
|
|
terom@12
|
16 |
void cb_result (struct evpq_conn *conn, PGresult *result, void *arg) { |
|
terom@12
|
17 |
|
|
terom@12
|
18 |
INFO("[evpq_test] result: %s", PQresStatus(PQresultStatus(result))); |
|
terom@12
|
19 |
|
|
terom@12
|
20 |
// fatal error? |
|
terom@12
|
21 |
if (PQresultStatus(result) != PGRES_TUPLES_OK) |
|
terom@12
|
22 |
FATAL("error: %s", PQresultErrorMessage(result)); |
|
terom@12
|
23 |
|
|
terom@12
|
24 |
// dump it to stdout |
|
terom@12
|
25 |
PQprintOpt popt = { |
|
terom@12
|
26 |
.header = 1, |
|
terom@12
|
27 |
.align = 1, |
|
terom@12
|
28 |
.standard = 0, |
|
terom@12
|
29 |
.html3 = 0, |
|
terom@12
|
30 |
.expanded = 1, |
|
terom@12
|
31 |
.pager = 0, |
|
terom@12
|
32 |
.fieldSep = "|", |
|
terom@12
|
33 |
.tableOpt = NULL, |
|
terom@12
|
34 |
.caption = NULL, |
|
terom@12
|
35 |
.fieldName = NULL, |
|
terom@12
|
36 |
}; |
|
terom@12
|
37 |
|
|
terom@12
|
38 |
PQprint(stdout, result, &popt); |
|
terom@12
|
39 |
|
|
terom@12
|
40 |
// don't care about the result anymore |
|
terom@12
|
41 |
PQclear(result); |
|
terom@12
|
42 |
} |
|
terom@12
|
43 |
|
|
terom@12
|
44 |
void cb_done (struct evpq_conn *conn, void *arg) { |
|
terom@12
|
45 |
INFO("[evpq_test] done"); |
|
terom@12
|
46 |
} |
|
terom@12
|
47 |
|
|
terom@12
|
48 |
void cb_failure (struct evpq_conn *conn, void *arg) { |
|
terom@12
|
49 |
INFO("[evpq_test] failure"); |
|
terom@12
|
50 |
INFO("\t%s", evpq_error_message(conn)); |
|
terom@12
|
51 |
|
|
terom@12
|
52 |
FATAL("exiting"); |
|
terom@12
|
53 |
} |
|
terom@12
|
54 |
|
|
terom@12
|
55 |
int main (int argc, char **argv) { |
|
terom@12
|
56 |
struct event_base *ev_base = NULL; |
|
terom@12
|
57 |
struct evpq_conn *conn = NULL; |
|
terom@12
|
58 |
const char *conninfo = CONNINFO_DEFAULT; |
|
terom@12
|
59 |
|
|
terom@12
|
60 |
struct evpq_callback_info cb_info = { |
|
terom@12
|
61 |
.fn_connected = cb_connected, |
|
terom@12
|
62 |
.fn_result = cb_result, |
|
terom@12
|
63 |
.fn_done = cb_done, |
|
terom@12
|
64 |
.fn_failure = cb_failure, |
|
terom@12
|
65 |
}; |
|
terom@12
|
66 |
|
|
terom@12
|
67 |
// initialize libevent |
|
terom@12
|
68 |
if ((ev_base = event_base_new()) == NULL) |
|
terom@12
|
69 |
ERROR("event_base_new"); |
|
terom@12
|
70 |
|
|
terom@12
|
71 |
// establish the evpq connection |
|
terom@12
|
72 |
if ((conn = evpq_connect(ev_base, conninfo, cb_info, NULL)) == NULL) |
|
terom@22
|
73 |
ERROR("evpq_connect"); |
|
terom@12
|
74 |
|
|
terom@12
|
75 |
// run libevent |
|
terom@12
|
76 |
INFO("running libevent loop"); |
|
terom@12
|
77 |
|
|
terom@12
|
78 |
if (event_base_dispatch(ev_base)) |
|
terom@12
|
79 |
ERROR("event_base_dispatch"); |
|
terom@12
|
80 |
|
|
terom@12
|
81 |
// clean shutdown |
|
terom@12
|
82 |
|
|
terom@12
|
83 |
error: |
|
terom@12
|
84 |
if (ev_base) |
|
terom@12
|
85 |
event_base_free(ev_base); |
|
terom@12
|
86 |
} |
|
terom@12
|
87 |
|