src/evsql.h
branchnew-evsql
changeset 55 0b92d553400a
parent 54 7dc8ff496da1
equal deleted inserted replaced
54:7dc8ff496da1 55:0b92d553400a
    18  *  -   evsql_trans()
    18  *  -   evsql_trans()
    19  *      -   evsql_trans_abort()
    19  *      -   evsql_trans_abort()
    20  *      -   evsql_trans_error_cb()
    20  *      -   evsql_trans_error_cb()
    21  *      -   evsql_trans_ready_cb()
    21  *      -   evsql_trans_ready_cb()
    22  *
    22  *
    23  *  -   evsql_query(), \ref evsql_param_* + evsql_query_params(), evsql_query_exec()
    23  *  -   evsql_query(), \ref evsql_param_ + evsql_query_params(), evsql_query_exec()
    24  *      -   evsql_query_abort()
    24  *      -   evsql_query_abort()
    25  *      -   evsql_query_cb()
    25  *      -   evsql_query_cb()
    26  *          -   \ref evsql_result_*
    26  *          -   \ref evsql_result_
    27  *          -   evsql_result_free()
    27  *          -   evsql_result_free()
    28  *
    28  *
    29  *  -   evsql_trans_commit()
    29  *  -   evsql_trans_commit()
    30  *      -   evsql_trans_done_cb()
    30  *      -   evsql_trans_done_cb()
    31  *
    31  *
    73 /**
    73 /**
    74  * @struct evsql_result
    74  * @struct evsql_result
    75  *
    75  *
    76  * Opaque result handle received by evsql_query_cb(), and used with the \ref evsql_result_ functions
    76  * Opaque result handle received by evsql_query_cb(), and used with the \ref evsql_result_ functions
    77  *
    77  *
    78  * @see evsql_query_cb()
    78  * @see evsql_query_cb
    79  * @see \ref evsql_result_
    79  * @see \ref evsql_result_
    80  */
    80  */
    81 struct evsql_result;
    81 struct evsql_result;
    82 
    82 
    83 /**
    83 /**
   154     /** The type type */
   154     /** The type type */
   155     enum evsql_item_type type;
   155     enum evsql_item_type type;
   156 
   156 
   157     /** Various flags */
   157     /** Various flags */
   158     struct evsql_item_flags {
   158     struct evsql_item_flags {
   159         /**
   159         /** The value may be NULL @see evsql_result_next */
   160          * The value may be NULL; behaviour varies for query and result 
       
   161          * XXX: document
       
   162          */
       
   163         bool null_ok : 1;
   160         bool null_ok : 1;
   164     } flags;
   161     } flags;
   165 };
   162 };
   166 
   163 
   167 /**
   164 /**
   324 /**
   321 /**
   325  * Callback for handling query results.
   322  * Callback for handling query results.
   326  *
   323  *
   327  * The query has completed, either succesfully or unsuccesfully.
   324  * The query has completed, either succesfully or unsuccesfully.
   328  *
   325  *
   329  * Use the \ref evsql_result_* functions to manipulate the results, and call evsql_result_free() (or equivalent) once done.
   326  * Use the \ref evsql_result_ functions to manipulate the results, and call evsql_result_free() (or equivalent) once done.
   330  *
   327  *
   331  * @param res The result handle that must be result_free'd after use
   328  * @param res The result handle that must be result_free'd after use
   332  * @param arg The void* passed to \ref evsql_query_*
   329  * @param arg The void* passed to \ref evsql_query_
   333  *
   330  *
   334  * @see evsql_query
   331  * @see evsql_query
   335  */
   332  */
   336 typedef void (*evsql_query_cb)(struct evsql_result *res, void *arg);
   333 typedef void (*evsql_query_cb)(struct evsql_result *res, void *arg);
   337 
   334 
   346  */
   343  */
   347 typedef void (*evsql_error_cb)(struct evsql *evsql, void *arg);
   344 typedef void (*evsql_error_cb)(struct evsql *evsql, void *arg);
   348 
   345 
   349 /**
   346 /**
   350  * Callback for handling transaction-level errors. This may be called at any time during a transaction's lifetime,
   347  * Callback for handling transaction-level errors. This may be called at any time during a transaction's lifetime,
   351  * including from within the \ref evsql_query_* functions (but not always).
   348  * including from within the \ref evsql_query_ functions (but not always).
   352  *
   349  *
   353  * The transaction is not useable anymore.
   350  * The transaction is not useable anymore.
   354  *
   351  *
   355  * @param trans the transaction in question
   352  * @param trans the transaction in question
   356  * @param arg the void* passed to evsql_trans
   353  * @param arg the void* passed to evsql_trans
   358  * @see evsql_trans
   355  * @see evsql_trans
   359  */
   356  */
   360 typedef void (*evsql_trans_error_cb)(struct evsql_trans *trans, void *arg);
   357 typedef void (*evsql_trans_error_cb)(struct evsql_trans *trans, void *arg);
   361 
   358 
   362 /**
   359 /**
   363  * Callback for handling evsql_trans/evsql_query_abort completion. The transaction is ready for use with \ref evsql_query_*.
   360  * Callback for handling evsql_trans/evsql_query_abort completion. The transaction is ready for use with \ref evsql_query_.
   364  *
   361  *
   365  * @param trans the transaction in question
   362  * @param trans the transaction in question
   366  * @param arg the void* passed to evsql_trans
   363  * @param arg the void* passed to evsql_trans
   367  *
   364  *
   368  * @see evsql_trans
   365  * @see evsql_trans
   400  */
   397  */
   401 
   398 
   402 /**
   399 /**
   403  * Create a new PostgreSQL/libpq (evpq) -based evsql using the given conninfo.
   400  * Create a new PostgreSQL/libpq (evpq) -based evsql using the given conninfo.
   404  *
   401  *
   405  * The given conninfo must stay valid for the duration of the evsql's lifetime.
   402  * The given \a pq_conninfo pointer must stay valid for the duration of the evsql's lifetime.
   406  *
   403  *
   407  * See the libpq reference manual for the syntax of pq_conninfo
   404  * See the libpq reference manual for the syntax of pq_conninfo
   408  *
   405  *
   409  * @param ev_base the libevent base to use
   406  * @param ev_base the libevent base to use
   410  * @param pq_conninfo the libpq connection information
   407  * @param pq_conninfo the libpq connection information
   422 /**
   419 /**
   423  * Close a connection. Callbacks for waiting queries will not be run.
   420  * Close a connection. Callbacks for waiting queries will not be run.
   424  *
   421  *
   425  * XXX: not implemented yet.
   422  * XXX: not implemented yet.
   426  *
   423  *
   427  * @param evsql the context handle from \ref evsql_new_*
   424  * @param evsql the context handle from \ref evsql_new_
   428  */
   425  */
   429 void evsql_close (struct evsql *evsql);
   426 void evsql_close (struct evsql *evsql);
   430 
   427 
   431 // @}
   428 // @}
   432 
   429 
   439  */
   436  */
   440 
   437 
   441 /**
   438 /**
   442  * Queue the given query for execution.
   439  * Queue the given query for execution.
   443  *
   440  *
   444  * If a trans is given (i.e. not NULL), then the transaction must be idle, and the query will be executed in that
   441  * If \a trans is given (i.e. not NULL), then the transaction must be idle, and the query will be executed in that
   445  * transaction's context. Otherwise, the query will be executed without a transaction using an idle connection, or
   442  * transaction's context. Otherwise, the query will be executed without a transaction using an idle connection, or
   446  * enqueued for later execution.
   443  * enqueued for later execution.
   447  *
   444  *
   448  * Once the query is complete (got a result, got an error, the connection failed), then the evsql_query_cb() will be called.
   445  * Once the query is complete (got a result, got an error, the connection failed), then \a query_fn will be called.
   449  * The callback can use the \ref evsql_result_* functions to manipulate it.
   446  * The callback can use the \ref evsql_result_ functions to manipulate the query results.
   450  *
   447  *
   451  * The returned evsql_query handle can be passed to evsql_query_abort at any point before query_fn being called. 
   448  * The returned evsql_query handle can be passed to evsql_query_abort at any point before \a query_fn being called. 
   452  *
   449  *
   453  * @param evsql the context handle from \ref evsql_new_*
   450  * @param evsql the context handle from \ref evsql_new_
   454  * @param trans the optional transaction handle from evsql_trans
   451  * @param trans the optional transaction handle from evsql_trans
   455  * @param command the raw SQL command itself
   452  * @param command the raw SQL command itself
   456  * @param query_fn the evsql_query_cb() to call once the query is complete
   453  * @param query_fn the evsql_query_cb() to call once the query is complete
   457  * @param cb_arg the void* passed to the above
   454  * @param cb_arg the void* passed to the above
   458  * @return the evsql_query handle that can be used to abort the query
   455  * @return the evsql_query handle that can be used to abort the query
   460 struct evsql_query *evsql_query (struct evsql *evsql, struct evsql_trans *trans, const char *command, evsql_query_cb query_fn, void *cb_arg);
   457 struct evsql_query *evsql_query (struct evsql *evsql, struct evsql_trans *trans, const char *command, evsql_query_cb query_fn, void *cb_arg);
   461 
   458 
   462 /**
   459 /**
   463  * Execute the given SQL query using the list of parameter types/values given via evsql_query_params.
   460  * Execute the given SQL query using the list of parameter types/values given via evsql_query_params.
   464  *
   461  *
   465  * Use the EVSQL_PARAMS macros to declare the struct, and the \ref evsql_param_* functions to populate the values.
   462  * Use the EVSQL_PARAMS macros to declare \a params, and the \ref evsql_param_ functions to populate the values.
   466  *
   463  *
   467  * See evsql_query for more info about behaviour.
   464  * See evsql_query() for more info about behaviour.
   468  *
   465  *
   469  * XXX: explain SQL param syntax...
   466  * See the <a href="http://www.postgresql.org/docs/8.3/static/libpq-exec.html#LIBPQ-EXEC-MAIN">libpq PQexecParams tip</a>
   470  *
   467  * for the parameter syntax to use.
   471  * @param evsql the context handle from \ref evsql_new_*
   468  *
       
   469  * @param evsql the context handle from \ref evsql_new_
   472  * @param trans the optional transaction handle from evsql_trans
   470  * @param trans the optional transaction handle from evsql_trans
   473  * @param command the SQL command to bind the parameters to
   471  * @param command the SQL command to bind the parameters to
   474  * @param params the parameter types and values
   472  * @param params the parameter types and values
   475  * @param query_fn the evsql_query_cb() to call once the query is complete
   473  * @param query_fn the evsql_query_cb() to call once the query is complete
   476  * @param cb_arg the void* passed to the above
   474  * @param cb_arg the void* passed to the above
   480     const char *command, const struct evsql_query_params *params, 
   478     const char *command, const struct evsql_query_params *params, 
   481     evsql_query_cb query_fn, void *cb_arg
   479     evsql_query_cb query_fn, void *cb_arg
   482 );
   480 );
   483 
   481 
   484 /**
   482 /**
   485  * Execute the given query_info's SQL query with the values given as variable arguments, using the evsql_query_info to
   483  * Execute the given \a query_info's SQL query with the values given as variable arguments, using the \a query_info to
   486  * resolve the types.
   484  * resolve the types.
   487  *
   485  *
   488  * See evsql_query for more info about behaviour.
   486  * See evsql_query() for more info about behaviour.
   489  *
   487  *
   490  * @param evsql the context handle from \ref evsql_new_*
   488  * @param evsql the context handle from \ref evsql_new_
   491  * @param trans the optional transaction handle from evsql_trans
   489  * @param trans the optional transaction handle from evsql_trans
   492  * @param query_info the SQL query information
   490  * @param query_info the SQL query information
   493  * @param query_fn the evsql_query_cb() to call once the query is complete
   491  * @param query_fn the evsql_query_cb() to call once the query is complete
   494  * @param cb_arg the void* passed to the above
   492  * @param cb_arg the void* passed to the above
   495  * @see evsql_query
   493  * @see evsql_query
   499     evsql_query_cb query_fn, void *cb_arg,
   497     evsql_query_cb query_fn, void *cb_arg,
   500     ...
   498     ...
   501 );
   499 );
   502 
   500 
   503 /**
   501 /**
   504  * Abort a query returned by \ref evsql_query_* that has not yet completed (query_fn has not been called yet).
   502  * Abort a \a query returned by \ref evsql_query_ that has not yet completed (query_fn has not been called yet).
   505  *
   503  *
   506  * The actual query itself may or may not be aborted (and hence may or may not be executed on the server), but query_fn
   504  * The actual query itself may or may not be aborted (and hence may or may not be executed on the server), but \a query_fn
   507  * will not be called anymore, and the query will dispose of itself and any results returned.
   505  * will not be called anymore, and the query will dispose of itself and any results returned.
   508  *
   506  *
   509  * If the query is part of a transaction, then trans must be given, and the query must be the query that is currently
   507  * If the \a query is part of a transaction, then \a trans must be given, and the query must be the query that is currently
   510  * executing on that trans. The transaction's ready_fn will be called once the query has been aborted and the
   508  * executing on that trans. The transaction's \a ready_fn will be called once the query has been aborted and the
   511  * transaction is now idle again.
   509  * transaction is now idle again.
   512  *
   510  *
   513  * @param trans if the query is part of a transaction, then it MUST be given here
   511  * @param trans if the query is part of a transaction, then it MUST be given here
   514  * @param query the in-progress query to abort
   512  * @param query the in-progress query to abort
   515  */
   513  */
   516 void evsql_query_abort (struct evsql_trans *trans, struct evsql_query *query);
   514 void evsql_query_abort (struct evsql_trans *trans, struct evsql_query *query);
   517 
   515 
   518 /**
   516 /**
   519  * Print out a textual dump of the given query and parameters using DEBUG
   517  * Print out a textual dump of the given \a sql query and \a params using DEBUG
   520  *
   518  *
   521  * @param sql the SQL query command
   519  * @param sql the SQL query command
   522  * @param params the list of parameter types and values
   520  * @param params the list of parameter types and values
   523  */
   521  */
   524 void evsql_query_debug (const char *sql, const struct evsql_query_params *params);
   522 void evsql_query_debug (const char *sql, const struct evsql_query_params *params);
   535 
   533 
   536 /**
   534 /**
   537  * Create a new transaction.
   535  * Create a new transaction.
   538  *
   536  *
   539  * A transaction will be allocated its own connection, and the "BEGIN TRANSACTION ..." query will be sent (use the
   537  * A transaction will be allocated its own connection, and the "BEGIN TRANSACTION ..." query will be sent (use the
   540  * trans_type argument to specify this). 
   538  * \a type argument to specify this). 
   541  *
   539  *
   542  * Once the transaction has been opened, the given evsql_trans_ready_cb() will be triggered, and the transaction can then
   540  * Once the transaction has been opened, the given \a ready_fn will be triggered, and the transaction can then
   543  * be used (see \ref evsql_query_*).
   541  * be used (see \ref evsql_query_).
   544  *
   542  *
   545  * If, at any point, the transaction-connection fails, any pending query will be forgotten (i.e. the query callback
   543  * If, at any point, the transaction-connection fails, any pending query will be forgotten (i.e. the query callback
   546  * will NOT be called), and the given evsql_trans_error_cb() will be called. Note that this includes some, but not all,
   544  * will NOT be called), and the given \a error_fn will be called. Note that this includes some, but not all,
   547  * cases where \ref evsql_query_* returns an error.
   545  * cases where \ref evsql_query_ returns an error.
   548  *
   546  *
   549  * Once you are done with the transaction, call either evsql_trans_commit() or evsql_trans_abort().
   547  * Once you are done with the transaction, call either evsql_trans_commit() or evsql_trans_abort().
   550  *
   548  *
   551  * @param evsql the context handle from \ref evsql_new_*
   549  * @param evsql the context handle from \ref evsql_new_
   552  * @param type the type of transaction to create
   550  * @param type the type of transaction to create
   553  * @param error_fn the evsql_trans_error_cb() to call if this transaction fails
   551  * @param error_fn the evsql_trans_error_cb() to call if this transaction fails
   554  * @param ready_fn the evsql_trans_ready_cb() to call once this transaction is ready for use
   552  * @param ready_fn the evsql_trans_ready_cb() to call once this transaction is ready for use
   555  * @param done_fn the evsql_trans_done_cb() to call once this transaction has been commmited
   553  * @param done_fn the evsql_trans_done_cb() to call once this transaction has been commmited
   556  * @param cb_arg the void* to pass to the above
   554  * @param cb_arg the void* to pass to the above
   565 
   563 
   566 /**
   564 /**
   567  * Commit a transaction using "COMMIT TRANSACTION".
   565  * Commit a transaction using "COMMIT TRANSACTION".
   568  *
   566  *
   569  * The transaction must be idle, just like for evsql_query. Once the transaction has been commited, the transaction's
   567  * The transaction must be idle, just like for evsql_query. Once the transaction has been commited, the transaction's
   570  * done_fn will be called, after which the transaction must not be used anymore.
   568  * \a done_fn will be called, after which the transaction must not be used anymore.
   571  *
   569  *
   572  * You cannot abort a COMMIT, calling trans_abort on trans after a succesful trans_commit is an error.
   570  * You cannot abort a COMMIT, calling trans_abort() on trans after a succesful trans_commit is an error.
   573  *
   571  *
   574  * Note that done_fn will never be called directly, always indirectly via the event loop.
   572  * Note that \a done_fn will never be called directly, always indirectly via the event loop.
   575  *
   573  *
   576  * @param trans the transaction handle from evsql_trans to commit
   574  * @param trans the transaction handle from evsql_trans to commit
   577  * @see evsql_trans
   575  * @see evsql_trans
   578  */
   576  */
   579 int evsql_trans_commit (struct evsql_trans *trans);
   577 int evsql_trans_commit (struct evsql_trans *trans);
   582  * Abort a transaction, using "ROLLBACK TRANSACTION".
   580  * Abort a transaction, using "ROLLBACK TRANSACTION".
   583  *
   581  *
   584  * No more transaction callbacks will be called. If there was a query running, it will be aborted, and the transaction
   582  * No more transaction callbacks will be called. If there was a query running, it will be aborted, and the transaction
   585  * then rollback'd.
   583  * then rollback'd.
   586  *
   584  *
   587  * You cannot abort a COMMIT, calling trans_abort on trans after a call to trans_commit is an error.
   585  * You cannot abort a COMMIT, calling trans_abort on \a trans after a call to trans_commit is an error.
   588  * 
   586  * 
   589  * Do not call evsql_trans_abort from within evsql_trans_error_cb()!
   587  * Do not call evsql_trans_abort from within evsql_trans_error_cb()!
   590  *
   588  *
   591  * @param trans the transaction from evsql_trans to abort
   589  * @param trans the transaction from evsql_trans to abort
   592  * @see evsql_trans
   590  * @see evsql_trans
   692 
   690 
   693 /**
   691 /**
   694  * Reads the next result row from the result prepared using evsql_result_begin. Stores the field values into to given
   692  * Reads the next result row from the result prepared using evsql_result_begin. Stores the field values into to given
   695  * pointer arguments based on the evsql_result_info given to evsql_result_begin.
   693  * pointer arguments based on the evsql_result_info given to evsql_result_begin.
   696  *
   694  *
       
   695  * If a field is NULL, and the result_info's evsql_item_type has flags.null_ok set, the given pointer is left
       
   696  * untouched, otherwise, an error is returned.
       
   697  *
   697  * @param res the result handle previous prepared using evsql_result_begin
   698  * @param res the result handle previous prepared using evsql_result_begin
   698  * @param ... a set of pointers corresponding to the evsql_result_info specified using evsql_result_begin
   699  * @param ... a set of pointers corresponding to the evsql_result_info specified using evsql_result_begin
   699  * @return >0 when a row was read, zero when there are no more rows left, and -err on error
   700  * @return >0 when a row was read, zero when there are no more rows left, and -err on error
   700  */
   701  */
   701 int evsql_result_next (struct evsql_result *res, ...);
   702 int evsql_result_next (struct evsql_result *res, ...);
   732 
   733 
   733 /**
   734 /**
   734  * Get the number of columns in the data results from the query
   735  * Get the number of columns in the data results from the query
   735  *
   736  *
   736  * @param res the result handle passed to evsql_query_cb()
   737  * @param res the result handle passed to evsql_query_cb()
   737  * @return the number of columns. XXX: zero if no data?
   738  * @return the number of columns, presumeably zero if there were no results
   738  */
   739  */
   739 size_t evsql_result_cols (const struct evsql_result *res);
   740 size_t evsql_result_cols (const struct evsql_result *res);
   740 
   741 
   741 /**
   742 /**
   742  * Get the number of rows affected by an UPDATE/INSERT/etc query.
   743  * Get the number of rows affected by an UPDATE/INSERT/etc query.