Module: sip-router Branch: master Commit: ed78c0967b69abc6e8333924460641de08b73e8f URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=ed78c096...
Author: Alex Hermann alex@speakup.nl Committer: Alex Hermann alex@speakup.nl Date: Sat Aug 20 09:48:00 2011 +0200
lib/srdb1: introduce helper to convert db_val to pv_spec
---
lib/srdb1/db_ut.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/srdb1/db_ut.h | 12 ++++++ 2 files changed, 111 insertions(+), 0 deletions(-)
diff --git a/lib/srdb1/db_ut.c b/lib/srdb1/db_ut.c index 16e2f4f..fbc037c 100644 --- a/lib/srdb1/db_ut.c +++ b/lib/srdb1/db_ut.c @@ -34,6 +34,7 @@
#include "../../mem/mem.h" #include "../../dprint.h" +#include "../../pvar.h" #include <limits.h> #include <errno.h> #include <stdio.h> @@ -371,3 +372,101 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k LM_ERR("Error in snprintf\n"); return -1; } + +/* + * Convert db_val to pv_spec + */ +int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs) +{ + pv_value_t pv; + str sv = {NULL, 0}; + static str _str_empty = { "", 0 }; + + if(dbval->nul) + { + pv.flags = PV_VAL_NULL; + } else + { + switch(dbval->type) + { + case DB1_STRING: + pv.flags = PV_VAL_STR; + sv.s = (char*)dbval->val.string_val; + sv.len = strlen(sv.s); + break; + case DB1_STR: + pv.flags = PV_VAL_STR; + sv.s = (char*)dbval->val.str_val.s; + sv.len = dbval->val.str_val.len; + break; + case DB1_BLOB: + pv.flags = PV_VAL_STR; + sv.s = (char*)dbval->val.blob_val.s; + sv.len = dbval->val.blob_val.len; + break; + case DB1_INT: + pv.flags = PV_VAL_INT | PV_TYPE_INT; + pv.ri = (int)dbval->val.int_val; + break; + case DB1_DATETIME: + pv.flags = PV_VAL_INT | PV_TYPE_INT; + pv.ri = (int)dbval->val.time_val; + break; + case DB1_BITMAP: + pv.flags = PV_VAL_INT | PV_TYPE_INT; + pv.ri = (int)dbval->val.bitmap_val; + break; + case DB1_BIGINT: + /* BIGINT is stored as string */ + pv.flags = PV_VAL_STR; + pv.rs.len = 21*sizeof(char); + pv.rs.s = (char*)pkg_malloc(pv.rs.len); + if (pv.rs.s==NULL) + { + LM_ERR("no more memory\n"); + return -1; + } + db_longlong2str(dbval->val.ll_val, pv.rs.s, &pv.rs.len); + break; + default: + LM_NOTICE("unknown field type: %d, setting value to null\n", + dbval->type); + pv.flags = PV_VAL_NULL; + } + } + + /* null values are ignored for avp type PV */ + if (pv.flags == PV_VAL_NULL && pvs->type == PVT_AVP) + return 0; + + /* handle string values */ + if(pv.flags == PV_VAL_STR && sv.s) + { + if(sv.len==0) + { + pv.rs = _str_empty; + } else + { + /* create copy of string value in pkg mem */ + pv.rs.s = (char*)pkg_malloc(sv.len*sizeof(char)); + if(pv.rs.s==NULL) + { + LM_ERR("no more memory\n"); + return -1; + } + memcpy(pv.rs.s, sv.s, sv.len); + pv.rs.len = sv.len; + } + } + + /* add value to result pv */ + if (pv_set_spec_value(msg, pvs, 0, &pv) != 0) + { + LM_ERR("Failed to add value to spec\n"); + if (pv.flags == PV_VAL_STR) + pkg_free(pv.rs.s); + return -1; + } + + return 0; +} diff --git a/lib/srdb1/db_ut.h b/lib/srdb1/db_ut.h index 85cbe48..0531dc4 100644 --- a/lib/srdb1/db_ut.h +++ b/lib/srdb1/db_ut.h @@ -54,6 +54,7 @@
#include "db_key.h" #include "db.h" +#include "../../pvar.h"
/** @@ -204,4 +205,15 @@ int db_print_set(const db1_con_t* _c, char* _b, const int _l, const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*));
+ +/** + * Convert db_val to pv_spec_t + * + * \param msg sip msg structure + * \param dbval database value + * \param pvs pv_spec wher to put the database value + * \return 1 on success, 0 on failure + */ +int db_val2pv_spec(struct sip_msg* msg, db_val_t *dbval, pv_spec_t *pvs); + #endif