| 404 | |
| 405 | |
| 406 | int db_do_replace(const db_con_t* _h, const db_key_t* _k, const db_val_t* _v, |
| 407 | const int _n, int (*val2str) (const db_con_t*, const db_val_t*, char*, |
| 408 | int*), int (*submit_query)(const db_con_t* _h, const str* _c)) |
| 409 | { |
| 410 | int off, ret; |
| 411 | |
| 412 | if (!_h || !_k || !_v || !val2str|| !submit_query) { |
| 413 | LM_ERR("invalid parameter value\n"); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | ret = snprintf(sql_buf, SQL_BUF_LEN, "replace into %.*s (", |
| 418 | CON_TABLE(_h)->len, CON_TABLE(_h)->s); |
| 419 | if (ret < 0 || ret >= SQL_BUF_LEN) goto error; |
| 420 | off = ret; |
| 421 | |
| 422 | ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); |
| 423 | if (ret < 0) return -1; |
| 424 | off += ret; |
| 425 | |
| 426 | ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); |
| 427 | if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; |
| 428 | off += ret; |
| 429 | |
| 430 | ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, |
| 431 | val2str); |
| 432 | if (ret < 0) return -1; |
| 433 | off += ret; |
| 434 | |
| 435 | if (off + 2 > SQL_BUF_LEN) goto error; |
| 436 | sql_buf[off++] = ')'; |
| 437 | sql_buf[off] = '\0'; |
| 438 | sql_str.s = sql_buf; |
| 439 | sql_str.len = off; |
| 440 | |
| 441 | if (submit_query(_h, &sql_str) < 0) { |
| 442 | LM_ERR("error while submitting query\n"); |
| 443 | return -2; |
| 444 | } |
| 445 | return 0; |
| 446 | |
| 447 | error: |
| 448 | LM_ERR("error while preparing replace operation\n"); |
| 449 | return -1; |
| 450 | } |
no test coverage detected