src/evpq.h
author Tero Marttila <terom@fixme.fi>
Sat, 13 Dec 2008 20:58:27 +0200
branchnew-evsql
changeset 55 0b92d553400a
parent 23 1dee73ae4ad0
permissions -rw-r--r--
evsql: more improvements
     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      * The evpq_conn has suffered a complete failure.
    37      *
    38      * Most likely, this means that the connection to the server was lost, or not established at all.
    39      *
    40      * XXX: add a `what` arg?
    41      */
    42     void (*fn_failure)(struct evpq_conn *conn, void *arg);
    43 };
    44 
    45 /*
    46  * evpq_conn states
    47  */
    48 enum evpq_state {
    49     EVPQ_INIT,
    50 
    51     EVPQ_CONNECT,
    52     EVPQ_CONNECTED,
    53 
    54     EVPQ_QUERY,
    55 
    56     EVPQ_FAILURE,
    57 };
    58 
    59 /*
    60  * Create a new evpq connection.
    61  *
    62  * This corresponds directly to PQconnectStart, and handles all the libevent setup/polling needed.
    63  *
    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).
    66  *
    67  * cb_info contains the callback functions (and the user argument) to use.
    68  */
    69 struct evpq_conn *evpq_connect (struct event_base *ev_base, const char *conninfo, const struct evpq_callback_info cb_info, void *cb_arg);
    70 
    71 /*
    72  * Execute a query.
    73  *
    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.
    76  *
    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).
    79  */
    80 int evpq_query (struct evpq_conn *conn, const char *command);
    81 
    82 /*
    83  * Execute a query with params.
    84  *
    85  * See evpq_query and PQsendQueryParams/PQexecParams
    86  */
    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);
    88 
    89 /*
    90  * Connection state ?? la evpq.
    91  */
    92 enum evpq_state evpq_state (struct evpq_conn *conn);
    93 
    94 /*
    95  * Get the actual PGconn.
    96  *
    97  * This can safely be used to access all of the normal PQ functions.
    98  */
    99 const PGconn *evpq_pgconn (struct evpq_conn *conn);
   100 
   101 // convenience wrappers
   102 #define evpq_error_message(conn) PQerrorMessage(evpq_pgconn(conn))
   103 
   104 /*
   105  * Release the evpq_conn, closing all connections and freeing all resources.
   106  *
   107  * You must call this yourself in all cases after evpq_connect returns an evpq_conn.
   108  */
   109 void evpq_release (struct evpq_conn *conn);
   110 
   111 #endif /* EVPQ_H */