diff options
author | Christian Grothoff <christian@grothoff.org> | 2015-01-26 11:04:46 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2015-01-26 11:04:46 +0100 |
commit | d4506f8a041385f7695b04b1ddfacb894d05da5c (patch) | |
tree | a4a0c87cbe822427e214e803a94a5a42551e9511 | |
parent | c2b32e75dc84814c5f7a03e54c4d18213119b4b4 (diff) |
support variable-size results
-rw-r--r-- | src/include/taler_db_lib.h | 90 | ||||
-rw-r--r-- | src/util/db.c | 57 |
2 files changed, 111 insertions, 36 deletions
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 <http://www.gnu.org/licenses/> */ - - /** * @file include/taler_db_lib.h * @brief helper functions for DB interactions * @author Sree Harsha Totakura <sreeharsha@totakura.in> * @author Florian Dold + * @author Christian Grothoff */ #ifndef TALER_DB_LIB_H_ @@ -28,16 +27,6 @@ #include <libpq-fe.h> #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,10 +92,48 @@ 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. */ PGresult * @@ -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 diff --git a/src/util/db.c b/src/util/db.c index a0b234a06..c048a30ff 100644 --- a/src/util/db.c +++ b/src/util/db.c @@ -13,15 +13,13 @@ You should have received a copy of the GNU General Public License along with TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> */ - - /** * @file util/db.c * @brief helper functions for DB interactions * @author Sree Harsha Totakura <sreeharsha@totakura.in> * @author Florian Dold + * @author Christian Grothoff */ - #include "platform.h" #include <gnunet/gnunet_util_lib.h> #include "taler_db_lib.h" @@ -39,12 +37,11 @@ TALER_DB_exec_prepared (PGconn *db_conn, unsigned i; /* count the number of parameters */ - { const struct TALER_DB_QueryParam *x; for (len = 0, x = params; x->more; - len +=1, x += 1); + len++, x++); } /* new scope to allow stack allocation without alloca */ @@ -61,20 +58,22 @@ TALER_DB_exec_prepared (PGconn *db_conn, param_formats[i] = 1; } return PQexecPrepared (db_conn, name, len, - (const char **) param_values, param_lengths, param_formats, 1); + (const char **) param_values, + param_lengths, + param_formats, 1); } } /** * Extract results from a query result according to the given specification. - * If colums are NULL, the destination is not modified, and GNUNET_NO + * If colums are NULL, the destination is not modified, and #GNUNET_NO * 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, @@ -82,35 +81,56 @@ TALER_DB_extract_result (PGresult *result, int row) { int had_null = GNUNET_NO; + size_t len; + unsigned int i; + unsigned int j; - for (; NULL != rs->fname; rs += 1) + for (i=0; NULL != rs[i].fname; i++) { int fnum; - fnum = PQfnumber (result, rs->fname); + + fnum = PQfnumber (result, rs[i].fname); if (fnum < 0) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' does not exist in result\n", rs->fname); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "field '%s' does not exist in result\n", + rs->fname); return GNUNET_SYSERR; } /* if a field is null, continue but * remember that we now return a different result */ - if (PQgetisnull (result, row, fnum)) { had_null = GNUNET_YES; continue; } const char *res; - if (rs->dst_size != PQgetlength (result, row, fnum)) + len = PQgetlength (result, row, fnum); + if ( (0 != rs[i].dst_size) && + (rs[i].dst_size != len) ) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' has wrong size (got %u, expected %u)\n", - rs->fname, (int) PQgetlength (result, row, fnum), (int) rs->dst_size); + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "field '%s' has wrong size (got %d, expected %d)\n", + rs[i].fname, + (int) len, + (int) rs->dst_size); + for (j=0;j<i;j++) + if (0 == rs[i].dst_size) + { + GNUNET_free (rs[i].dst); + rs[i].dst = NULL; + *rs[i].result_size = 0; + } return GNUNET_SYSERR; } res = PQgetvalue (result, row, fnum); GNUNET_assert (NULL != res); - memcpy (rs->dst, res, rs->dst_size); + if (0 == rs->dst_size) + *(void**) rs->dst = GNUNET_malloc (*rs->result_size = len); + memcpy (rs->dst, + res, + len); } if (GNUNET_YES == had_null) return GNUNET_NO; @@ -124,6 +144,7 @@ TALER_DB_field_isnull (PGresult *result, const char *fname) { int fnum; + fnum = PQfnumber (result, fname); GNUNET_assert (fnum >= 0); if (PQgetisnull (result, row, fnum)) |