|
1 #ifndef EVPQ_H |
|
2 #define EVPQ_H |
|
3 |
|
4 /* |
|
5 * Convenience functions for using libpq (the PostgreSQL client library) with libevent. |
|
6 */ |
|
7 |
|
8 #include <event2/event.h> |
|
9 #include <postgresql/libpq-fe.h> |
|
10 |
|
11 /* |
|
12 * Our PGconn context wrapper. |
|
13 */ |
|
14 struct evpq_conn; |
|
15 |
|
16 /* |
|
17 * Callback functions used |
|
18 */ |
|
19 struct evpq_callback_info { |
|
20 /* |
|
21 * This evpq_conn has connected succesfully \o/ |
|
22 */ |
|
23 void (*fn_connected)(struct evpq_conn *conn, void *arg); |
|
24 |
|
25 /* |
|
26 * Got a result. |
|
27 */ |
|
28 void (*fn_result)(struct evpq_conn *conn, PGresult *result, void *arg); |
|
29 |
|
30 /* |
|
31 * No more results for the query |
|
32 */ |
|
33 void (*fn_done)(struct evpq_conn *conn, void *arg); |
|
34 |
|
35 /* |
|
36 * Something caused this evpq_conn to fail :( |
|
37 * |
|
38 * XXX: add a `what` arg? |
|
39 */ |
|
40 void (*fn_failure)(struct evpq_conn *conn, void *arg); |
|
41 |
|
42 /* |
|
43 * Arg to pass through to all callbacks. |
|
44 */ |
|
45 void *cb_arg; |
|
46 }; |
|
47 |
|
48 /* |
|
49 * evpq_conn states |
|
50 */ |
|
51 enum evpq_state { |
|
52 EVPQ_INIT, |
|
53 |
|
54 EVPQ_CONNECT, |
|
55 EVPQ_CONNECTED, |
|
56 |
|
57 EVPQ_QUERY, |
|
58 |
|
59 EVPQ_FAILURE, |
|
60 }; |
|
61 |
|
62 /* |
|
63 * Create a new evpq connection. |
|
64 * |
|
65 * This corresponds directly to PQconnectStart, and handles all the libevent setup/polling needed. |
|
66 * |
|
67 * The connection will initially be in the EVPQ_CONNECT state, and will then either callback via fn_connected |
|
68 * (EVPQ_CONNECTED) or fn_failure (EVPQ_FAILURE). |
|
69 * |
|
70 * cb_info contains the callback functions (and the user argument) to use. |
|
71 */ |
|
72 struct evpq_conn *evpq_connect (struct event_base *ev_base, const char *conninfo, const struct evpq_callback_info cb_info); |
|
73 |
|
74 /* |
|
75 * Execute a query. |
|
76 * |
|
77 * This corresponds directly to PQsendQuery. This evpq must be in the EVPQ_CONNECTED state, so you must wait after |
|
78 * calling evpq_connect, and you may not run two queries at the same time. |
|
79 * |
|
80 * The query will result in a series of fn_result (EVPQ_RESULT) calls (if multiple queries in the query string), |
|
81 * followed by a fn_done (EVPQ_CONNECTED). |
|
82 */ |
|
83 int evpq_query (struct evpq_conn *conn, const char *command); |
|
84 |
|
85 /* |
|
86 * Get the actual PGconn. |
|
87 * |
|
88 * This can safely be used to access all of the normal PQ functions. |
|
89 */ |
|
90 const PGconn *evpq_pgconn (struct evpq_conn *conn); |
|
91 |
|
92 // convenience wrappers |
|
93 #define evpq_error_message(conn) PQerrorMessage(evpq_pgconn(conn)) |
|
94 |
|
95 |
|
96 #endif /* EVPQ_H */ |