Skip to content

Commit

Permalink
lib/srdb1: db_table_version() can handle DB1_BIGINT and DB1_DOUBLE va…
Browse files Browse the repository at this point in the history
…lues

- in case of views or other database engine, the type for version value
  can be different that DB1_INT. If it is a number, cast it to int
- extracted from GH #1186 by Emmanuel Schmidbauer
  <emmanuel@getweave.com>
  • Loading branch information
miconda committed Jul 13, 2017
1 parent b94ba19 commit d0ac747
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/lib/srdb1/db.c
Expand Up @@ -374,7 +374,8 @@ int db_table_version(const db_func_t* dbf, db1_con_t* connection, const str* tab
str *version = &version_table;
str tmp1 = str_init(TABLENAME_COLUMN);
str tmp2 = str_init(VERSION_COLUMN);
int ret;
int ret = 0;
int val_type;

if (!dbf||!connection || !table || !table->s) {
LM_CRIT("invalid parameter value\n");
Expand Down Expand Up @@ -412,16 +413,26 @@ int db_table_version(const db_func_t* dbf, db1_con_t* connection, const str* tab
}

ver = ROW_VALUES(RES_ROWS(res));
if ( VAL_TYPE(ver)!=DB1_INT || VAL_NULL(ver) ) {
val_type = VAL_TYPE(ver);
if ( (val_type!=DB1_INT && val_type!=DB1_DOUBLE && val_type!=DB1_BIGINT)
|| VAL_NULL(ver) ) {
LM_ERR("invalid type (%d) or nul (%d) version "
"columns for %.*s\n", VAL_TYPE(ver), VAL_NULL(ver),
table->len, ZSW(table->s));
dbf->free_result(connection, res);
return -1;
}

ret = VAL_INT(ver);
if (val_type == DB1_INT) {
ret = VAL_INT(ver);
} else if (val_type == DB1_BIGINT) {
ret = (int)VAL_BIGINT(ver);
} else if (val_type == DB1_DOUBLE) {
ret = (int)VAL_DOUBLE(ver);
}

dbf->free_result(connection, res);

return ret;
}

Expand Down

1 comment on commit d0ac747

@eschmidbauer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be backported to stable 5 version?

Please sign in to comment.