terom@21: #ifndef EVSQL_H terom@21: #define EVSQL_H terom@21: terom@21: /* terom@21: * An event-based (Postgre)SQL client API using libevent terom@21: */ terom@21: terom@21: // XXX: libpq terom@21: #include terom@21: #include terom@21: terom@21: /* terom@21: * The generic context handle terom@21: */ terom@21: struct evsql; terom@21: terom@21: /* terom@21: * A query handle terom@21: */ terom@21: struct evsql_query; terom@21: terom@21: /* terom@23: * Query parameter info. terom@23: * terom@23: * Use the EVSQL_PARAM_* macros to define the value of list terom@23: */ terom@21: struct evsql_query_params { terom@23: // nonzero to get results in binary format terom@23: int result_binary; terom@23: terom@23: // the list of parameters, terminated by { 0, 0 } terom@23: struct evsql_query_param { terom@23: // the textual or binary value for this parameter terom@23: char *value; terom@23: terom@23: // the explicit length of the parameter if it's binary. Must be non-zero for NULL values. terom@23: int length; terom@23: } list[]; terom@23: }; terom@23: terom@23: // macros for defining evsql_query_params terom@23: #define EVSQL_PARAM_NULL { NULL, 1 } terom@23: #define EVSQL_PARAM_TEXT(value) { value, 0 } terom@23: #define EVSQL_PARAM_BINARY(value, length) { value, length } terom@21: terom@21: /* terom@21: * Result type terom@21: */ terom@21: struct evsql_result_info { terom@21: struct evsql *evsql; terom@21: terom@21: int error; terom@21: terom@21: union { terom@21: // XXX: libpq terom@21: PGresult *pq; terom@21: terom@21: } result; terom@21: }; terom@21: terom@21: /* terom@21: * Callback for handling query-level errors. terom@21: * terom@21: * The query has completed, either succesfully or unsuccesfully (look at info.error). terom@21: * info.result contains the result à la the evsql's type. terom@21: */ terom@21: typedef void (*evsql_query_cb)(struct evsql_result_info info, void *arg); terom@21: terom@21: /* terom@21: * Callback for handling connection-level errors. terom@21: * terom@21: * The SQL context/connection suffered an error. It is not valid anymore, and may not be used. terom@21: */ terom@21: typedef void (*evsql_error_cb)(struct evsql *evsql, void *arg); terom@21: terom@21: /* terom@21: * Create a new PostgreSQL/libpq(evpq) -based evsql using the given conninfo. terom@21: */ terom@21: struct evsql *evsql_new_pq (struct event_base *ev_base, const char *pq_conninfo, evsql_error_cb error_fn, void *cb_arg); terom@21: terom@21: /* terom@21: * Queue the given query for execution. terom@21: */ terom@21: struct evsql_query *evsql_query (struct evsql *evsql, const char *command, evsql_query_cb query_fn, void *cb_arg); terom@21: terom@21: /* terom@23: * Same, but uses the SQL-level support for binding parameters. terom@23: */ terom@23: struct evsql_query *evsql_query_params (struct evsql *evsql, const char *command, struct evsql_query_params params, evsql_query_cb query_fn, void *cb_arg); terom@23: terom@23: /* terom@21: * Close a connection. Callbacks for waiting queries will not be run. terom@21: * terom@21: * XXX: not implemented yet. terom@21: */ terom@21: void evsql_close (struct evsql *evsql); terom@21: terom@21: #endif /* EVSQL_H */