From d4506f8a041385f7695b04b1ddfacb894d05da5c Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 26 Jan 2015 11:04:46 +0100 Subject: support variable-size results --- src/include/taler_db_lib.h | 90 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 72 insertions(+), 18 deletions(-) (limited to 'src/include/taler_db_lib.h') diff --git a/src/include/taler_db_lib.h b/src/include/taler_db_lib.h index 41b46264e..6e2b2b2c0 100644 --- a/src/include/taler_db_lib.h +++ b/src/include/taler_db_lib.h @@ -13,13 +13,12 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see */ - - /** * @file include/taler_db_lib.h * @brief helper functions for DB interactions * @author Sree Harsha Totakura * @author Florian Dold + * @author Christian Grothoff */ #ifndef TALER_DB_LIB_H_ @@ -28,16 +27,6 @@ #include #include "taler_util.h" -#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 } -#define TALER_DB_QUERY_PARAM_PTR(x) { (x), sizeof (*(x)), 1 } -#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 } - - -#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL } -#define TALER_DB_RESULT_SPEC(name, dst) { (void *) (dst), sizeof (*(dst)), (name) } -#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name) } - - /** * Description of a DB query parameter. */ @@ -47,10 +36,12 @@ struct TALER_DB_QueryParam * Data or NULL */ const void *data; + /** - * Size of 'data' + * Size of @e data */ size_t size; + /** * Non-null if this is not the last parameter. * This allows for null as sentinal value. @@ -58,6 +49,27 @@ struct TALER_DB_QueryParam int more; }; +/** + * End of query parameter specification. + */ +#define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 } + +/** + * Generate fixed-size query parameter with size given explicitly. + * + * @param x pointer to the query parameter to pass + * @param s number of bytes of @a x to use for the query + */ +#define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 } + +/** + * Generate fixed-size query parameter with size determined + * by variable type. + * + * @param x pointer to the query parameter to pass. + */ +#define TALER_DB_QUERY_PARAM_PTR(x) TALER_DB_QUERY_PARAM_PTR_SIZED(x, sizeof (*(x))) + /** * Description of a DB result cell. @@ -70,7 +82,9 @@ struct TALER_DB_ResultSpec void *dst; /** - * Allowed size for the data. + * Allowed size for the data, 0 for variable-size + * (in this case, the type of @e dst is a `void **` + * and we need to allocate a buffer of the right size). */ size_t dst_size; @@ -78,9 +92,47 @@ struct TALER_DB_ResultSpec * Field name of the desired result. */ char *fname; + + /** + * Actual size of the result. + */ + size_t *result_size; + }; +/** + * End of result parameter specification. + */ +#define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL, NULL } + +/** + * We expect a fixed-size result, with size given explicitly + * + * @param name name of the field in the table + * @param dst point to where to store the result + * @param s number of bytes we should use in @a dst + */ +#define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name), NULL } + +/** + * We expect a fixed-size result, with size determined by the type of `* dst` + * + * @param name name of the field in the table + * @param dst point to where to store the result, type fits expected result size + */ +#define TALER_DB_RESULT_SPEC(name, dst) TALER_DB_RESULT_SPEC_SIZED(name, dst, sizeof (*(dst))) + +/** + * Variable-size result expected. + * + * @param name name of the field in the table + * @param dst where to store the result (of type void **), to be allocated + * @param sptr pointer to a `size_t` for where to store the size of @a dst + */ +#define TALER_DB_RESULT_SPEC_VAR(name, dst, sptr) { (void *) (dst), 0, (name), sptr } + + /** * Execute a prepared statement. */ @@ -96,12 +148,14 @@ TALER_DB_exec_prepared (PGconn *db_conn, * is returned. * * @return - * GNUNET_YES if all results could be extracted - * GNUNET_NO if at least one result was NULL - * GNUNET_SYSERR if a result was invalid (non-existing field) + * #GNUNET_YES if all results could be extracted + * #GNUNET_NO if at least one result was NULL + * #GNUNET_SYSERR if a result was invalid (non-existing field) */ int -TALER_DB_extract_result (PGresult *result, struct TALER_DB_ResultSpec *rs, int row); +TALER_DB_extract_result (PGresult *result, + struct TALER_DB_ResultSpec *rs, + int row); int -- cgit v1.2.3