Module: sip-router
Branch: master
Commit: f67171cf3be1ade0e1c9dd013d560c87b3fd4359
URL:
http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=f67171c…
Author: Victor Seva <linuxmaniac(a)torreviejawireless.org>
Committer: Victor Seva <linuxmaniac(a)torreviejawireless.org>
Date: Thu Oct 10 15:21:47 2013 +0200
usrloc: db_update contact by instance + reg-id
---
modules/usrloc/ucontact.c | 203 ++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 201 insertions(+), 2 deletions(-)
diff --git a/modules/usrloc/ucontact.c b/modules/usrloc/ucontact.c
index 9cc2e06..323dc4b 100644
--- a/modules/usrloc/ucontact.c
+++ b/modules/usrloc/ucontact.c
@@ -679,18 +679,21 @@ int db_update_ucontact_addr(ucontact_t* _c)
vals1[n1].type = DB1_STR;
vals1[n1].nul = 0;
vals1[n1].val.str_val = *_c->aor;
+ LM_DBG("aor:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s);
n1++;
keys1[n1] = &contact_col;
vals1[n1].type = DB1_STR;
vals1[n1].nul = 0;
vals1[n1].val.str_val = _c->c;
+ LM_DBG("contact:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s);
n1++;
keys1[n1] = &callid_col;
vals1[n1].type = DB1_STR;
vals1[n1].nul = 0;
vals1[n1].val.str_val = _c->callid;
+ LM_DBG("callid:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s);
n1++;
vals2[0].type = DB1_DATETIME;
@@ -866,6 +869,7 @@ int db_update_ucontact_ruid(ucontact_t* _c)
vals1[n1].type = DB1_STR;
vals1[n1].nul = 0;
vals1[n1].val.str_val = _c->ruid;
+ LM_DBG("ruid:%.*s\n", vals1[n1].val.str_val.len, vals1[n1].val.str_val.s);
n1++;
n2 = 0;
@@ -1008,7 +1012,197 @@ int db_update_ucontact_ruid(ucontact_t* _c)
adomain.len = _c->aor->s +
_c->aor->len - adomain.s;
}
-
+
+ uldb_delete_attrs(_c->domain, &auser,
+ &adomain, &_c->ruid);
+ uldb_insert_attrs(_c->domain, &auser,
+ &adomain, &_c->ruid, _c->xavp);
+ } else {
+ uldb_delete_attrs(_c->domain, &auser,
+ NULL, &_c->ruid);
+ uldb_insert_attrs(_c->domain, &auser,
+ NULL, &_c->ruid, _c->xavp);
+ }
+ }
+
+ return 0;
+}
+
+/*!
+ * \brief Update contact in the database by instance reg_id
+ * \param _c updated contact
+ * \return 0 on success, -1 on failure
+ */
+int db_update_ucontact_instance(ucontact_t* _c)
+{
+ str auser;
+ str adomain;
+ db_key_t keys1[2];
+ db_val_t vals1[2];
+ int n1;
+
+ db_key_t keys2[13];
+ db_val_t vals2[13];
+ int n2;
+
+
+ if (_c->flags & FL_MEM) {
+ return 0;
+ }
+
+ if(_c->instance.len<=0) {
+ LM_ERR("updating record in database failed - empty instance\n");
+ return -1;
+ }
+
+ n1 = 0;
+ keys1[n1] = &instance_col;
+ vals1[n1].type = DB1_STR;
+ vals1[n1].nul = 0;
+ vals1[n1].val.str_val = _c->instance;
+ LM_DBG("instance:%.*s\n", vals1[n1].val.str_val.len,
vals1[n1].val.str_val.s);
+ n1++;
+
+ keys1[n1] = ®_id_col;
+ vals1[n1].type = DB1_INT;
+ vals1[n1].nul = 0;
+ vals1[n1].val.int_val = (int)_c->reg_id;
+ LM_DBG("reg-id:%d\n", vals1[n1].val.int_val);
+ n1++;
+
+ n2 = 0;
+ keys2[n2] = &expires_col;
+ vals2[n2].type = DB1_DATETIME;
+ vals2[n2].nul = 0;
+ vals2[n2].val.time_val = _c->expires;
+ n2++;
+
+ keys2[n2] = &q_col;
+ vals2[n2].type = DB1_DOUBLE;
+ vals2[n2].nul = 0;
+ vals2[n2].val.double_val = q2double(_c->q);
+ n2++;
+
+ keys2[n2] = &cseq_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.int_val = _c->cseq;
+ n2++;
+
+ keys2[n2] = &flags_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.bitmap_val = _c->flags;
+ n2++;
+
+ keys2[n2] = &cflags_col;
+ vals2[n2].type = DB1_INT;
+ vals2[n2].nul = 0;
+ vals2[n2].val.bitmap_val = _c->cflags;
+ n2++;
+
+ keys2[n2] = &user_agent_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->user_agent;
+ n2++;
+
+ keys2[n2] = &received_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->received.s == 0) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->received;
+ }
+ n2++;
+
+ keys2[n2] = &path_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->path.s == 0) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->path;
+ }
+ n2++;
+
+ keys2[n2] = &sock_col;
+ vals2[n2].type = DB1_STR;
+ if (_c->sock) {
+ vals2[n2].val.str_val = _c->sock->sock_str;
+ vals2[n2].nul = 0;
+ } else {
+ vals2[n2].nul = 1;
+ }
+ n2++;
+
+ keys2[n2] = &methods_col;
+ vals2[n2].type = DB1_BITMAP;
+ if (_c->methods == 0xFFFFFFFF) {
+ vals2[n2].nul = 1;
+ } else {
+ vals2[n2].val.bitmap_val = _c->methods;
+ vals2[n2].nul = 0;
+ }
+ n2++;
+
+ keys2[n2] = &last_mod_col;
+ vals2[n2].type = DB1_DATETIME;
+ vals2[n2].nul = 0;
+ vals2[n2].val.time_val = _c->last_modified;
+ n2++;
+
+ keys2[n2] = &callid_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val = _c->callid;
+ n2++;
+
+ keys2[n2] = &contact_col;
+ vals2[n2].type = DB1_STR;
+ vals2[n2].nul = 0;
+ vals2[n2].val.str_val.s = _c->c.s;
+ vals2[n2].val.str_val.len = _c->c.len;
+ LM_DBG("contact:%.*s\n", vals2[n2].val.str_val.len, vals2[n2].val.str_val.s);
+ n2++;
+
+ if (ul_dbf.use_table(ul_dbh, _c->domain) < 0) {
+ LM_ERR("sql use_table failed\n");
+ return -1;
+ }
+
+ if (ul_dbf.update(ul_dbh, keys1, 0, vals1, keys2, vals2, n1, n2) < 0) {
+ LM_ERR("updating database failed\n");
+ return -1;
+ }
+
+ if (ul_db_check_update==1 && ul_dbf.affected_rows) {
+ LM_DBG("update affected_rows 0\n");
+ /* supposed to be an UPDATE, but if affected rows is 0, then try
+ * to do an INSERT */
+ if(ul_dbf.affected_rows(ul_dbh)==0) {
+ LM_DBG("affected rows by UPDATE was 0, doing an INSERT\n");
+ if(db_insert_ucontact(_c)<0)
+ return -1;
+ }
+ }
+
+ /* delete old db attrs and add the current list */
+ if (ul_xavp_contact_name.s) {
+ auser = *_c->aor;
+ if (use_domain) {
+ adomain.s = memchr(_c->aor->s, '@', _c->aor->len);
+ if (adomain.s==0) {
+ auser.len = 0;
+ adomain = *_c->aor;
+ } else {
+ auser.len = adomain.s - _c->aor->s;
+ adomain.s++;
+ adomain.len = _c->aor->s +
+ _c->aor->len - adomain.s;
+ }
+
uldb_delete_attrs(_c->domain, &auser,
&adomain, &_c->ruid);
uldb_insert_attrs(_c->domain, &auser,
@@ -1032,7 +1226,12 @@ int db_update_ucontact_ruid(ucontact_t* _c)
int db_update_ucontact(ucontact_t* _c)
{
if(ul_db_ops_ruid==0)
- return db_update_ucontact_addr(_c);
+ if (_c->instance.len<=0) {
+ return db_update_ucontact_addr(_c);
+ }
+ else {
+ return db_update_ucontact_instance(_c);
+ }
else
return db_update_ucontact_ruid(_c);
}