/* * Convert a str to a db value, does not copy strings * The postgresql module uses a custom escape function for BLOBs. * If the _s is linked in the db_val result, it will be returned zero */ int db_postgres_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l) { static str dummy_string = {"", 0}; char *x; if (!_v) { LM_ERR("invalid parameter value\n"); return -1; } if (!_s) { memset(_v, 0, sizeof(db_val_t)); /* Initialize the string pointers to a dummy empty * string so that we do not crash when the NULL flag * is set but the module does not check it properly */ VAL_STR(_v) = dummy_string; VAL_TYPE(_v) = _t; VAL_NULL(_v) = 1; return 0; } VAL_NULL(_v) = 0; switch(_t) { case DB_INT: LM_DBG("converting INT [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("failed to convert INT value from string\n"); return -2; } else { VAL_TYPE(_v) = DB_INT; return 0; } break; case DB_BIGINT: LM_DBG("converting BIGINT [%s]\n", _s); if (db_str2bigint(_s, &VAL_BIGINT(_v)) < 0) { LM_ERR("failed to convert BIGINT value from string\n"); return -2; } else { VAL_TYPE(_v) = DB_BIGINT; return 0; } break; case DB_BITMAP: LM_DBG("converting BITMAP [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("failed to convert BITMAP value from string\n"); return -3; } else { VAL_TYPE(_v) = DB_BITMAP; return 0; } break; case DB_DOUBLE: LM_DBG("converting DOUBLE [%s]\n", _s); if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) { LM_ERR("failed to convert DOUBLE value from string\n"); return -4; } else { VAL_TYPE(_v) = DB_DOUBLE; return 0; } break; case DB_STRING: LM_DBG("converting STRING [%s]\n", _s); VAL_STRING(_v) = _s; VAL_TYPE(_v) = DB_STRING; VAL_FREE(_v) = 1; return 0; case DB_STR: LM_DBG("converting STR [%.*s]\n", _l, _s); VAL_STR(_v).s = (char*)_s; VAL_STR(_v).len = _l; VAL_TYPE(_v) = DB_STR; VAL_FREE(_v) = 1; return 0; case DB_DATETIME: LM_DBG("converting DATETIME [%s]\n", _s); if (db_str2time(_s, &VAL_TIME(_v)) < 0) { LM_ERR("failed to convert datetime\n"); return -5; } else { VAL_TYPE(_v) = DB_DATETIME; return 0; } break; case DB_BLOB: LM_DBG("converting BLOB [%.*s]\n", _l, _s); /* PQunescapeBytea: Converts a string representation of binary data * into binary data - the reverse of PQescapeBytea. * This is needed when retrieving bytea data in text format, * but not when retrieving it in binary format. */ x = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len) ); VAL_BLOB(_v).s = pkg_malloc( VAL_BLOB(_v).len+1 ); if (VAL_BLOB(_v).s==NULL) { LM_ERR("failed to allocate pkg for BLOB\n"); return -6; } memcpy( VAL_BLOB(_v).s, x, VAL_BLOB(_v).len); VAL_BLOB(_v).s[VAL_BLOB(_v).len]='\0'; free(x); VAL_TYPE(_v) = DB_BLOB; VAL_FREE(_v) = 1; LM_DBG("got blob len %d\n", _l); return 0; } return -6; }
/** * Does not copy strings */ int bdb_str2val(db_type_t _t, db_val_t* _v, char* _s, int _l) { static str dummy_string = {"", 0}; if(!_s) { memset(_v, 0, sizeof(db_val_t)); /* Initialize the string pointers to a dummy empty * string so that we do not crash when the NULL flag * is set but the module does not check it properly */ VAL_STRING(_v) = dummy_string.s; VAL_STR(_v) = dummy_string; VAL_BLOB(_v) = dummy_string; VAL_TYPE(_v) = _t; VAL_NULL(_v) = 1; return 0; } VAL_NULL(_v) = 0; switch(_t) { case DB1_INT: if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("Error while converting INT value from string\n"); return -2; } else { VAL_TYPE(_v) = DB1_INT; return 0; } break; case DB1_BIGINT: LM_ERR("BIGINT not supported"); return -1; case DB1_BITMAP: if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("Error while converting BITMAP value from string\n"); return -3; } else { VAL_TYPE(_v) = DB1_BITMAP; return 0; } break; case DB1_DOUBLE: if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) { LM_ERR("Error while converting DOUBLE value from string\n"); return -4; } else { VAL_TYPE(_v) = DB1_DOUBLE; return 0; } break; case DB1_STRING: VAL_STRING(_v) = _s; VAL_TYPE(_v) = DB1_STRING; VAL_FREE(_v) = 1; if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) ) VAL_NULL(_v) = 1; return 0; case DB1_STR: VAL_STR(_v).s = (char*)_s; VAL_STR(_v).len = _l; VAL_TYPE(_v) = DB1_STR; VAL_FREE(_v) = 1; if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) ) VAL_NULL(_v) = 1; return 0; case DB1_DATETIME: if (db_str2time(_s, &VAL_TIME(_v)) < 0) { LM_ERR("Error converting datetime\n"); return -5; } else { VAL_TYPE(_v) = DB1_DATETIME; return 0; } break; case DB1_BLOB: VAL_BLOB(_v).s = _s; VAL_TYPE(_v) = DB1_BLOB; LM_DBG("got blob len %d\n", _l); return 0; } return -6; }
int put_value_in_result( char * start, int len , db_res_t * res , int cur_col, int cur_line ) { db_val_t * row; LM_DBG("Found value: %.*s\n",len,start); row = res->rows[cur_line].values; row[cur_col].type = res->col.types[cur_col]; if( len == 0 && (res->col.types[cur_col] != DB_BLOB ) && (res->col.types[cur_col] != DB_STRING ) && (res->col.types[cur_col] != DB_STR ) ) { row[cur_col].nul = 1; return 0; } switch(res->col.types[cur_col]) { case( DB_INT): CHECK( sscanf(start,"%d",&row[cur_col].val.int_val), 1, error); break; case( DB_BIGINT): CHECK( sscanf(start,"%lld",&row[cur_col].val.bigint_val), 1, error); break; case( DB_DOUBLE): CHECK( sscanf(start,"%lf",&row[cur_col].val.double_val), 1, error); break; case( DB_STRING): row[cur_col].val.string_val = start; break; case( DB_STR): case( DB_BLOB): row[cur_col].val.blob_val.s = start; row[cur_col].val.blob_val.len = len; break; case( DB_DATETIME): CHECK( db_str2time(start,&row[cur_col].val.time_val), 0, error); break; default: break; } return 0; error: LM_ERR("Unable to parse value: %.*s\n",len,start); return -1; }
/* * Convert a str to a db value, does not copy strings * The postgresql module uses a custom escape function for BLOBs, * so the common db_str2val function from db_ut.h could not used. * If the _s is linked in the db_val result, it will be returned zero */ int pg_str2val(db_type_t _t, db_val_t* _v, char* _s, int _l) { static str dummy_string = {"", 0}; #ifdef PARANOID if (!_v) { LM_ERR("db_val_t parameter cannot be NULL\n"); } #endif if (!_s) { memset(_v, 0, sizeof(db_val_t)); /* Initialize the string pointers to a dummy empty * string so that we do not crash when the NULL flag * is set but the module does not check it properly */ VAL_STRING(_v) = dummy_string.s; VAL_STR(_v) = dummy_string; VAL_BLOB(_v) = dummy_string; VAL_TYPE(_v) = _t; VAL_NULL(_v) = 1; return 0; } VAL_NULL(_v) = 0; switch(_t) { case DB_INT: LM_DBG("converting INT [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("failed to convert INT value from string\n"); return -2; } else { VAL_TYPE(_v) = DB_INT; return 0; } break; case DB_BITMAP: LM_DBG("converting BITMAP [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("failed to convert BITMAP value from string\n"); return -3; } else { VAL_TYPE(_v) = DB_BITMAP; return 0; } break; case DB_DOUBLE: LM_DBG("converting DOUBLE [%s]\n", _s); if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) { LM_ERR("failed to convert DOUBLE value from string\n"); return -4; } else { VAL_TYPE(_v) = DB_DOUBLE; return 0; } break; case DB_STRING: LM_DBG("converting STRING [%s]\n", _s); VAL_STRING(_v) = _s; VAL_TYPE(_v) = DB_STRING; return 0; case DB_STR: LM_DBG("converting STR [%s]\n", _s); VAL_STR(_v).s = (char*)_s; VAL_STR(_v).len = _l; VAL_TYPE(_v) = DB_STR; _s = 0; return 0; case DB_DATETIME: LM_DBG("converting DATETIME [%s]\n", _s); if (db_str2time(_s, &VAL_TIME(_v)) < 0) { LM_ERR("failed to convert datetime\n"); return -5; } else { VAL_TYPE(_v) = DB_DATETIME; return 0; } break; case DB_BLOB: LM_DBG("converting BLOB [%s]\n", _s); /* PQunescapeBytea: Converts a string representation of binary data * into binary data — the reverse of PQescapeBytea. * This is needed when retrieving bytea data in text format, * but not when retrieving it in binary format. */ VAL_BLOB(_v).s = (char*)PQunescapeBytea((unsigned char*)_s, (size_t*)(void*)&(VAL_BLOB(_v).len) ); VAL_TYPE(_v) = DB_BLOB; LM_DBG("got blob len %d\n", _l); return 0; } return -6; }
/* * Convert str to db value, does not copy strings */ int db_mysql_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l) { static str dummy_string = {"", 0}; if (!_v) { LM_ERR("invalid parameter value\n"); return -1; } if (!_s) { memset(_v, 0, sizeof(db_val_t)); /* Initialize the string pointers to a dummy empty * string so that we do not crash when the NULL flag * is set but the module does not check it properly */ VAL_STRING(_v) = dummy_string.s; VAL_STR(_v) = dummy_string; VAL_BLOB(_v) = dummy_string; VAL_TYPE(_v) = _t; VAL_NULL(_v) = 1; return 0; } VAL_NULL(_v) = 0; switch(_t) { case DB_INT: LM_DBG("converting INT [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("error while converting integer value from string\n"); return -2; } else { VAL_TYPE(_v) = DB_INT; return 0; } break; case DB_BIGINT: LM_DBG("converting INT BIG[%s]\n", _s); if (db_str2bigint(_s, &VAL_BIGINT(_v)) < 0) { LM_ERR("error while converting big integer value from string\n"); return -2; } else { VAL_TYPE(_v) = DB_BIGINT; return 0; } break; case DB_BITMAP: LM_DBG("converting BITMAP [%s]\n", _s); if (db_str2int(_s, &VAL_INT(_v)) < 0) { LM_ERR("error while converting bitmap value from string\n"); return -3; } else { VAL_TYPE(_v) = DB_BITMAP; return 0; } break; case DB_DOUBLE: LM_DBG("converting DOUBLE [%s]\n", _s); if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) { LM_ERR("error while converting double value from string\n"); return -4; } else { VAL_TYPE(_v) = DB_DOUBLE; return 0; } break; case DB_STRING: LM_DBG("converting STRING [%s]\n", _s); VAL_STRING(_v) = _s; VAL_TYPE(_v) = DB_STRING; return 0; case DB_STR: LM_DBG("converting STR [%.*s]\n", _l, _s); VAL_STR(_v).s = (char*)_s; VAL_STR(_v).len = _l; VAL_TYPE(_v) = DB_STR; return 0; case DB_DATETIME: LM_DBG("converting DATETIME [%s]\n", _s); if (db_str2time(_s, &VAL_TIME(_v)) < 0) { LM_ERR("error while converting datetime value from string\n"); return -5; } else { VAL_TYPE(_v) = DB_DATETIME; return 0; } break; case DB_BLOB: LM_DBG("converting BLOB [%.*s]\n", _l, _s); VAL_BLOB(_v).s = (char*)_s; VAL_BLOB(_v).len = _l; VAL_TYPE(_v) = DB_BLOB; return 0; } return -6; }
/** * Convert a row from result into db API representation */ int db_sqlite_convert_row(const db_con_t* _h, db_res_t* _res, db_row_t* _r) { int col; db_val_t* _v; const char* db_value; if ((!_h) || (!_res) || (!_r)) { LM_ERR("invalid parameter value\n"); return -1; } if (!CON_SQLITE_PS(_h)) { LM_ERR("conn has no prepared statement! sqlite requires one\n"); return -1; } /* Save the number of columns in the ROW structure */ ROW_N(_r) = RES_COL_N(_res); for(col=0; col < RES_COL_N(_res); col++) { _v = &(ROW_VALUES(_r)[col]); if (sqlite3_column_type(CON_SQLITE_PS(_h), col) == SQLITE_NULL) { VAL_NULL(_v) = 1; continue; } switch (RES_TYPES(_res)[col]) { case DB_INT: VAL_INT(_v) = sqlite3_column_int(CON_SQLITE_PS(_h), col); VAL_TYPE(_v) = DB_INT; break; case DB_BIGINT: VAL_BIGINT(_v) = sqlite3_column_int64(CON_SQLITE_PS(_h), col); VAL_TYPE(_v) = DB_BIGINT; break; case DB_DATETIME: db_value = (char *)sqlite3_column_text(CON_SQLITE_PS(_h), col); if (db_str2time(db_value, &VAL_TIME(_v)) < 0) { LM_ERR("error while converting datetime value from string\n"); return -1; } VAL_TYPE(_v) = DB_DATETIME; break; case DB_DOUBLE: VAL_DOUBLE(_v) = sqlite3_column_double(CON_SQLITE_PS(_h), col); VAL_TYPE(_v) = DB_DOUBLE; break; case DB_BLOB: VAL_BLOB(_v).len = sqlite3_column_bytes(CON_SQLITE_PS(_h), col); db_value = sqlite3_column_blob(CON_SQLITE_PS(_h), col); VAL_BLOB(_v).s = pkg_malloc(VAL_BLOB(_v).len+1); memcpy(VAL_BLOB(_v).s, db_value, VAL_BLOB(_v).len); VAL_BLOB(_v).s[VAL_BLOB(_v).len]='\0'; VAL_TYPE(_v) = DB_BLOB; break; case DB_STRING: VAL_STR(_v).len = sqlite3_column_bytes(CON_SQLITE_PS(_h), col); db_value = (char *)sqlite3_column_text(CON_SQLITE_PS(_h), col); VAL_STR(_v).s = pkg_malloc(VAL_STR(_v).len+1); memcpy(VAL_STR(_v).s, db_value, VAL_STR(_v).len); VAL_STR(_v).s[VAL_STR(_v).len]='\0'; VAL_TYPE(_v) = DB_STR; break; default: LM_ERR("invalid type for sqlite!\n"); return -1; } } return 0; }