Skip to content

Commit

Permalink
db_mysql/avpops: Properly return DOUBLE/DECIMAL values
Browse files Browse the repository at this point in the history
This patch fixes avp_db_query() calls which fetch data from DOUBLE
columns in MySQL, which would otherwise return some strange errors.

Many thanks to Calvin Ellison and Brett Nemeroff for the report and
helping come up with the final fix!

(cherry picked from commit 0105ab4)
  • Loading branch information
liviuchircu committed Mar 10, 2020
1 parent 7cb7b57 commit 88288a3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion db/db_query.c
Expand Up @@ -141,7 +141,7 @@ int db_do_raw_query(const db_con_t* _h, const str* _s, db_res_t** _r,
if(_r) {
int tmp = store_result(_h, _r);
if (tmp < 0) {
LM_ERR("error while storing result");
LM_ERR("error while storing result\n");
return tmp;
}
}
Expand Down
6 changes: 6 additions & 0 deletions modules/avpops/avpops_db.c
Expand Up @@ -544,6 +544,12 @@ int db_query_avp_print_results(struct sip_msg *msg, const db_res_t *db_res,
avp_val.n =
(int)RES_ROWS(db_res)[i].values[j].val.bigint_val;
break;
case DB_DOUBLE:
avp_type |= AVP_VAL_STR;
avp_val.s.s = double2str(
RES_ROWS(db_res)[i].values[j].val.double_val,
&avp_val.s.len);
break;
default:
LM_WARN("Unknown type %d\n",
RES_ROWS(db_res)[i].values[j].type);
Expand Down
8 changes: 4 additions & 4 deletions modules/db_mysql/res.c
Expand Up @@ -76,17 +76,17 @@ int db_mysql_get_columns(const db_con_t* _h, db_res_t* _r)
case MYSQL_TYPE_SHORT:
case MYSQL_TYPE_LONG:
case MYSQL_TYPE_INT24:
case MYSQL_TYPE_DECIMAL:
#if MYSQL_VERSION_ID > 49999
case MYSQL_TYPE_NEWDECIMAL:
#endif
case MYSQL_TYPE_TIMESTAMP:
LM_DBG("use DB_INT result type\n");
RES_TYPES(_r)[col] = DB_INT;
break;

case MYSQL_TYPE_FLOAT:
case MYSQL_TYPE_DOUBLE:
case MYSQL_TYPE_DECIMAL:
#if MYSQL_VERSION_ID > 49999
case MYSQL_TYPE_NEWDECIMAL:
#endif
LM_DBG("use DB_DOUBLE result type\n");
RES_TYPES(_r)[col] = DB_DOUBLE;
break;
Expand Down
11 changes: 11 additions & 0 deletions ut.h
Expand Up @@ -288,6 +288,17 @@ static inline char* sint2str(long l, int* len)
return p;
}

static inline char* double2str(double d, int* len)
{
static int buf;

buf = (buf + 1) % INT2STR_BUF_NO;
*len = snprintf(int2str_buf[buf], INT2STR_MAX_LEN - 1, "%lf", d);
int2str_buf[buf][*len] = '\0';

return int2str_buf[buf];
}


/* faster memchr version */
static inline char* q_memchr(char* p, int c, unsigned int size)
Expand Down

0 comments on commit 88288a3

Please sign in to comment.