From 159de3960df69f8d2ea3b0c4c94ebdee7f61f6af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Monrouzeau?= Date: Tue, 7 Jun 2016 13:12:31 +0200 Subject: [PATCH] Added new function kore_pgsql_v_query_params(). Same as kore_pgsql_query_params but takes a va_list as last parameter (non-v version takes a variable list of parameters). Lets people write easier to call wrappers around the query calls. I use it in a wrapper that takes next states (error, current, continue) as arguments in a handler with multiple async queries. --- includes/pgsql.h | 2 ++ src/pgsql.c | 28 ++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/includes/pgsql.h b/includes/pgsql.h index 66d4faf..e43468e 100644 --- a/includes/pgsql.h +++ b/includes/pgsql.h @@ -67,6 +67,8 @@ void kore_pgsql_continue(struct http_request *, struct kore_pgsql *); int kore_pgsql_query(struct kore_pgsql *, const char *); int kore_pgsql_query_params(struct kore_pgsql *, const char *, int, u_int8_t, ...); +int kore_pgsql_v_query_params(struct kore_pgsql *, + const char *, int, u_int8_t, va_list); int kore_pgsql_register(const char *, const char *); int kore_pgsql_ntuples(struct kore_pgsql *); void kore_pgsql_logerror(struct kore_pgsql *); diff --git a/src/pgsql.c b/src/pgsql.c index 2e95235..d24ec70 100644 --- a/src/pgsql.c +++ b/src/pgsql.c @@ -150,11 +150,10 @@ kore_pgsql_query(struct kore_pgsql *pgsql, const char *query) } int -kore_pgsql_query_params(struct kore_pgsql *pgsql, - const char *query, int result, u_int8_t count, ...) +kore_pgsql_v_query_params(struct kore_pgsql *pgsql, + const char *query, int result, u_int8_t count, va_list args) { u_int8_t i; - va_list args; char **values; int *lengths, *formats, ret; @@ -164,8 +163,6 @@ kore_pgsql_query_params(struct kore_pgsql *pgsql, } if (count > 0) { - va_start(args, count); - lengths = kore_calloc(count, sizeof(int)); formats = kore_calloc(count, sizeof(int)); values = kore_calloc(count, sizeof(char *)); @@ -208,9 +205,6 @@ kore_pgsql_query_params(struct kore_pgsql *pgsql, ret = KORE_RESULT_OK; cleanup: - if (count > 0) - va_end(args); - kore_mem_free(values); kore_mem_free(lengths); kore_mem_free(formats); @@ -218,6 +212,24 @@ cleanup: return (ret); } +int +kore_pgsql_query_params(struct kore_pgsql *pgsql, + const char *query, int result, u_int8_t count, ...) +{ + int ret; + va_list args; + + if (count > 0) + va_start(args, count); + + ret = kore_pgsql_v_query_params(pgsql, query, result, count, args); + + if (count > 0) + va_end(args); + + return (ret); +} + int kore_pgsql_register(const char *dbname, const char *connstring) {