5 * Convenience functions for using libpq (the PostgreSQL client library) with libevent.
8 #include <event2/event.h>
9 #include <postgresql/libpq-fe.h>
12 * Our PGconn context wrapper.
17 * Callback functions used
19 struct evpq_callback_info {
21 * This evpq_conn has connected succesfully \o/
23 void (*fn_connected)(struct evpq_conn *conn, void *arg);
28 void (*fn_result)(struct evpq_conn *conn, PGresult *result, void *arg);
31 * No more results for the query
33 void (*fn_done)(struct evpq_conn *conn, void *arg);
36 * The evpq_conn has suffered a complete failure.
38 * Most likely, this means that the connection to the server was lost, or not established at all.
40 * XXX: add a `what` arg?
42 void (*fn_failure)(struct evpq_conn *conn, void *arg);
60 * Create a new evpq connection.
62 * This corresponds directly to PQconnectStart, and handles all the libevent setup/polling needed.
64 * The connection will initially be in the EVPQ_CONNECT state, and will then either callback via fn_connected
65 * (EVPQ_CONNECTED) or fn_failure (EVPQ_FAILURE).
67 * cb_info contains the callback functions (and the user argument) to use.
69 struct evpq_conn *evpq_connect (struct event_base *ev_base, const char *conninfo, const struct evpq_callback_info cb_info, void *cb_arg);
74 * This corresponds directly to PQsendQuery. This evpq must be in the EVPQ_CONNECTED state, so you must wait after
75 * calling evpq_connect, and you may not run two queries at the same time.
77 * The query will result in a series of fn_result (EVPQ_RESULT) calls (if multiple queries in the query string),
78 * followed by a fn_done (EVPQ_CONNECTED).
80 int evpq_query (struct evpq_conn *conn, const char *command);
83 * Execute a query with params.
85 * See evpq_query and PQsendQueryParams/PQexecParams
87 int evpq_query_params (struct evpq_conn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
90 * Connection state ?? la evpq.
92 enum evpq_state evpq_state (struct evpq_conn *conn);
95 * Get the actual PGconn.
97 * This can safely be used to access all of the normal PQ functions.
99 const PGconn *evpq_pgconn (struct evpq_conn *conn);
101 // convenience wrappers
102 #define evpq_error_message(conn) PQerrorMessage(evpq_pgconn(conn))
105 * Release the evpq_conn, closing all connections and freeing all resources.
107 * You must call this yourself in all cases after evpq_connect returns an evpq_conn.
109 void evpq_release (struct evpq_conn *conn);