src/evsql.h
changeset 21 e5da1d428e3e
child 23 1dee73ae4ad0
equal deleted inserted replaced
20:f0ef6d8880b4 21:e5da1d428e3e
       
     1 #ifndef EVSQL_H
       
     2 #define EVSQL_H
       
     3 
       
     4 /*
       
     5  * An event-based (Postgre)SQL client API using libevent
       
     6  */
       
     7 
       
     8 // XXX: libpq
       
     9 #include <postgresql/libpq-fe.h>
       
    10 #include <event2/event.h>
       
    11 
       
    12 /*
       
    13  * The generic context handle
       
    14  */
       
    15 struct evsql;
       
    16 
       
    17 /*
       
    18  * A query handle
       
    19  */
       
    20 struct evsql_query;
       
    21 
       
    22 /*
       
    23  * Query parameter info
       
    24  * /
       
    25 struct evsql_query_params {
       
    26     int count;
       
    27     const char * const *values;
       
    28     const int *lengths;
       
    29     const int *formats;
       
    30     int result_format;
       
    31 }; */
       
    32 
       
    33 /*
       
    34  * Result type
       
    35  */
       
    36 struct evsql_result_info {
       
    37     struct evsql *evsql;
       
    38     
       
    39     int error;
       
    40 
       
    41     union {
       
    42         // XXX: libpq
       
    43         PGresult *pq;
       
    44 
       
    45     } result;
       
    46 };
       
    47 
       
    48 /*
       
    49  * Callback for handling query-level errors.
       
    50  *
       
    51  * The query has completed, either succesfully or unsuccesfully (look at info.error).
       
    52  * info.result contains the result à la the evsql's type.
       
    53  */
       
    54 typedef void (*evsql_query_cb)(struct evsql_result_info info, void *arg);
       
    55 
       
    56 /*
       
    57  * Callback for handling connection-level errors.
       
    58  *
       
    59  * The SQL context/connection suffered an error. It is not valid anymore, and may not be used.
       
    60  */
       
    61 typedef void (*evsql_error_cb)(struct evsql *evsql, void *arg);
       
    62 
       
    63 /*
       
    64  * Create a new PostgreSQL/libpq(evpq) -based evsql using the given conninfo.
       
    65  */
       
    66 struct evsql *evsql_new_pq (struct event_base *ev_base, const char *pq_conninfo, evsql_error_cb error_fn, void *cb_arg);
       
    67 
       
    68 /*
       
    69  * Queue the given query for execution.
       
    70  */
       
    71 struct evsql_query *evsql_query (struct evsql *evsql, const char *command, evsql_query_cb query_fn, void *cb_arg);
       
    72 
       
    73 /*
       
    74  * Close a connection. Callbacks for waiting queries will not be run.
       
    75  *
       
    76  * XXX: not implemented yet.
       
    77  */
       
    78 void evsql_close (struct evsql *evsql);
       
    79 
       
    80 #endif /* EVSQL_H */