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