From fee3637648b137391fec7a8ec862b977333ca4be Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Fri, 13 May 2016 08:08:35 +0200 Subject: [PATCH] lib/srdb1: support for db result with allocated column names - a db connector module can allocate column names in the result, in that case it must set the flag: RES_COL_FLAGS(res) |= DB1_FCOL_FREE; - the flag is per result, all column names must be allocated or not - following the discussion on GH #611 --- lib/srdb1/db_res.c | 4 ++++ lib/srdb1/db_res.h | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/srdb1/db_res.c b/lib/srdb1/db_res.c index b07965bd05f..d3b9729300f 100644 --- a/lib/srdb1/db_res.c +++ b/lib/srdb1/db_res.c @@ -81,6 +81,10 @@ int db_free_columns(db1_res_t* _r) for(col = 0; col < RES_COL_N(_r); col++) { if (RES_NAMES(_r)[col]!=NULL) { LM_DBG("freeing RES_NAMES[%d] at %p\n", col, RES_NAMES(_r)[col]); + /* free column name if it was allocated */ + if ((RES_COL_FLAGS(_r) & DB1_FCOL_FREE) && RES_NAMES(_r)[col]->s != NULL) { + pkg_free(RES_NAMES(_r)[col]->s); + } pkg_free((str *)RES_NAMES(_r)[col]); RES_NAMES(_r)[col] = NULL; } diff --git a/lib/srdb1/db_res.h b/lib/srdb1/db_res.h index c424d9d6a18..5fe0796f7b8 100644 --- a/lib/srdb1/db_res.h +++ b/lib/srdb1/db_res.h @@ -38,8 +38,12 @@ struct db_row; +/* -- column name flags -- */ +/* column name must be freed when db result is destroyed */ +#define DB1_FCOL_FREE (1<<1) + /** - * This type represents a result returned by db_query function (see below). The + * This type represents a result returned by db_query function (see below). The * result can consist of zero or more rows (see db_row_t description). * * Note: A variable of type db1_res_t returned by db_query function uses dynamicaly @@ -54,6 +58,7 @@ typedef struct db1_res { db_key_t* names; /**< Column names */ db_type_t* types; /**< Column types */ int n; /**< Number of columns */ + int cflags; /**< Flags of columns */ } col; struct db_row* rows; /**< Rows */ int n; /**< Number of rows in current fetch */ @@ -69,6 +74,8 @@ typedef struct db1_res { #define RES_TYPES(re) ((re)->col.types) /** Return the number of columns */ #define RES_COL_N(re) ((re)->col.n) +/** Return the flags of columns */ +#define RES_COL_FLAGS(re) ((re)->col.cflags) /** Return the result rows */ #define RES_ROWS(re) ((re)->rows) /** Return the number of current result rows */