Module: kamailio
Branch: master
Commit: fee3637648b137391fec7a8ec862b977333ca4be
URL:
https://github.com/kamailio/kamailio/commit/fee3637648b137391fec7a8ec862b97…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2016-05-13T08:08:35+02:00
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
---
Modified: lib/srdb1/db_res.c
Modified: lib/srdb1/db_res.h
---
Diff:
https://github.com/kamailio/kamailio/commit/fee3637648b137391fec7a8ec862b97…
Patch:
https://github.com/kamailio/kamailio/commit/fee3637648b137391fec7a8ec862b97…
---
diff --git a/lib/srdb1/db_res.c b/lib/srdb1/db_res.c
index b07965b..d3b9729 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 c424d9d..5fe0796 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 */