Module: kamailio
Branch: master
Commit: 723a6c9a1cf5cc56239cd167417178323767d8d4
URL: 
https://github.com/kamailio/kamailio/commit/723a6c9a1cf5cc56239cd167417178323767d8d4

Author: Tyler Moore <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2025-12-08T11:13:43+01:00

htable: support for 32bit and 64bit integer keys

  - update ht_db_load_table() to support loading integer
    field types into a hash table as string keys

---

Modified: src/modules/htable/doc/htable.xml
Modified: src/modules/htable/ht_db.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/723a6c9a1cf5cc56239cd167417178323767d8d4.diff
Patch: 
https://github.com/kamailio/kamailio/commit/723a6c9a1cf5cc56239cd167417178323767d8d4.patch

---

diff --git a/src/modules/htable/doc/htable.xml 
b/src/modules/htable/doc/htable.xml
index 7b567588977..93e4b4a355f 100644
--- a/src/modules/htable/doc/htable.xml
+++ b/src/modules/htable/doc/htable.xml
@@ -50,8 +50,8 @@
            <holder>http://www.asipto.com</holder>
        </copyright>
        <copyright>
-               <year>2023</year>
-               <holder>Tyler Moore (devopsec), dOpenSource</holder>
+               <year>2023-2025</year>
+               <holder>Tyler Moore, dOpenSource</holder>
        </copyright>
     </bookinfo>
     <toc></toc>
diff --git a/src/modules/htable/ht_db.c b/src/modules/htable/ht_db.c
index 8b0c667411d..caa9931b8b2 100644
--- a/src/modules/htable/ht_db.c
+++ b/src/modules/htable/ht_db.c
@@ -25,6 +25,7 @@
 #include "../../core/usr_avp.h"
 #include "../../core/ut.h"
 #include "../../lib/srdb1/db.h"
+#include "../../lib/srdb1/db_ut.h"
 
 #include "ht_db.h"
 
@@ -208,6 +209,8 @@ int ht_db_load_table(ht_t *ht, str *dbtable, int mode)
        long now;
        int ncols;
        int c;
+#define KEY_BUF_SIZE 32
+       char key_buf[KEY_BUF_SIZE];
 
        if(ht_db_con == NULL) {
                LM_ERR("no db connection\n");
@@ -286,6 +289,8 @@ int ht_db_load_table(ht_t *ht, str *dbtable, int mode)
                                goto error;
                        }
 
+                       memset(key_buf, 0, KEY_BUF_SIZE);
+
                        switch(RES_ROWS(db_res)[i].values[0].type) {
                                case DB1_STR:
                                        kname.s = 
(RES_ROWS(db_res)[i].values[0].val.str_val.s);
@@ -317,8 +322,77 @@ int ht_db_load_table(ht_t *ht, str *dbtable, int mode)
                                        }
                                        kname.len = strlen(kname.s);
                                        break;
+                               case DB1_INT:
+                                       if(RES_ROWS(db_res)[i].values[0].nul) {
+                                               LM_ERR("htable [%.*s] row [%d] 
has NULL key\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.len = KEY_BUF_SIZE;
+                                       
if(db_int2str(RES_ROWS(db_res)[i].values[0].val.int_val,
+                                                          key_buf, &kname.len)
+                                                       < 0) {
+                                               LM_ERR("htable [%.*s] row [%d]: 
error while converting "
+                                                          "DB int to string\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.s = key_buf;
+                                       break;
+                               case DB1_UINT:
+                                       if(RES_ROWS(db_res)[i].values[0].nul) {
+                                               LM_ERR("htable [%.*s] row [%d] 
has NULL key\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.len = KEY_BUF_SIZE;
+                                       
if(db_uint2str(RES_ROWS(db_res)[i].values[0].val.uint_val,
+                                                          key_buf, &kname.len)
+                                                       < 0) {
+                                               LM_ERR("htable [%.*s] row [%d]: 
error while converting "
+                                                          "DB unsigned int to 
string\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.s = key_buf;
+                                       break;
+                               case DB1_BIGINT:
+                                       if(RES_ROWS(db_res)[i].values[0].nul) {
+                                               LM_ERR("htable [%.*s] row [%d] 
has NULL key\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.len = KEY_BUF_SIZE;
+                                       
if(db_longlong2str(RES_ROWS(db_res)[i].values[0].val.ll_val,
+                                                          key_buf, &kname.len)
+                                                       < 0) {
+                                               LM_ERR("htable [%.*s] row [%d]: 
error while converting "
+                                                          "DB big signed int 
to string\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.s = key_buf;
+                                       break;
+                               case DB1_UBIGINT:
+                                       if(RES_ROWS(db_res)[i].values[0].nul) {
+                                               LM_ERR("htable [%.*s] row [%d] 
has NULL key\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.len = KEY_BUF_SIZE;
+                                       if(db_ulonglong2str(
+                                                          
RES_ROWS(db_res)[i].values[0].val.ull_val,
+                                                          key_buf, &kname.len)
+                                                       < 0) {
+                                               LM_ERR("htable [%.*s] row [%d]: 
error while converting "
+                                                          "DB big unsigned int 
to string\n",
+                                                               ht->name.len, 
ht->name.s, i);
+                                               goto error;
+                                       }
+                                       kname.s = key_buf;
+                                       break;
                                default:
-                                       LM_ERR("key type must be string 
(type=%d)\n",
+                                       LM_ERR("key type unsupported 
(type=%d)\n",
                                                        
RES_ROWS(db_res)[i].values[0].type);
                                        goto error;
                        }

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to