/* * Delete a row from the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _n: number of key=value pairs */ int db_mysql_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { int off, ret; if (!_h) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n, val2str); if (ret < 0) return -1; off += ret; } *(sql_buf + off) = '\0'; if (db_mysql_submit_query(_h, sql_buf) < 0) { LM_ERR("error while submitting query\n"); return -2; } return 0; error: LM_ERR("error in snprintf\n"); return -1; }
/* * Query table for specified rows * _h: structure representing database connection * _k: key names * _op: operators * _v: values of the keys that must match * _c: column names to return * _n: number of key=values pairs to compare * _nc: number of columns to return * _o: order by the specified column */ int db_mysql_query(db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, int _n, int _nc, db_key_t _o, db_res_t** _r) { int off, ret; if (!_h) { LM_ERR("invalid parameter value\n"); return -1; } if (!_c) { ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; } else { ret = snprintf(sql_buf, SQL_BUF_LEN, "select "); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %s ", CON_TABLE(_h)); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n, val2str); if (ret < 0) return -1;; off += ret; } if (_o) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %s", _o); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } *(sql_buf + off) = '\0'; if (db_mysql_submit_query(_h, sql_buf) < 0) { LM_ERR("error while submitting query\n"); return -2; } if(_r) return db_mysql_store_result(_h, _r); return 0; error: LM_ERR("error in snprintf\n"); return -1; }
int db_do_delete( db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n, int (*val2str) ( db_con_t*, db_val_t*, char*, int*), int (*submit_query)( db_con_t* _h, str* _c),str *query_holder) { int off, ret; str sql_str; char sql_buf[SQL_BUF_LEN]; if (!_h || !val2str || (!submit_query && !query_holder)) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "delete from %.*s", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n, val2str); if (ret < 0) return -1; off += ret; } if (off + 1 > SQL_BUF_LEN) goto error; sql_buf[off] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (submit_query) { if (submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query\n"); return -2; } } else { query_holder->s = pkg_malloc(off); if (!query_holder->s) { LM_ERR("no more pkg mem\n"); return -2; } memcpy(query_holder->s,sql_buf,off); query_holder->len = off; } return 0; error: LM_ERR("error while preparing delete operation\n"); return -1; }
int db_do_update(const db_con_t* _h, const db_key_t* _k, const db_op_t* _o, const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n, const int _un, int (*val2str) (const db_con_t*, const db_val_t*, char*, int*), int (*submit_query)(const db_con_t* _h, const str* _c)) { int off, ret; if (!_h || !_uk || !_uv || !_un || !val2str || !submit_query) { LM_ERR("invalid parameter value\n"); goto err_exit; } ret = snprintf(sql_buf, SQL_BUF_LEN, "update %.*s set ", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un, val2str); if (ret < 0) goto err_exit; off += ret; if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n, val2str); if (ret < 0) goto err_exit; off += ret; } if (off + 1 > SQL_BUF_LEN) goto error; sql_buf[off] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query\n"); CON_OR_RESET(_h); return -2; } CON_OR_RESET(_h); return 0; error: LM_ERR("error while preparing update operation\n"); err_exit: CON_OR_RESET(_h); return -1; }
int db_do_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o, const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n, const int _un, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*), int (*submit_query)(const db1_con_t* _h, const str* _c)) { int off, ret; if (!_h || !_uk || !_uv || !_un || !val2str || !submit_query) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, sql_buffer_size, "update %s%.*s%s set ", CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h)); if (ret < 0 || ret >= sql_buffer_size) goto error; off = ret; ret = db_print_set(_h, sql_buf + off, sql_buffer_size - off, _uk, _uv, _un, val2str); if (ret < 0) return -1; off += ret; if (_n) { ret = snprintf(sql_buf + off, sql_buffer_size - off, " where "); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, sql_buffer_size - off, _k, _o, _v, _n, val2str); if (ret < 0) return -1; off += ret; } if (off + 1 > sql_buffer_size) goto error; sql_buf[off] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (db_do_submit_query(_h, &sql_str, submit_query) < 0) { LM_ERR("error while submitting query\n"); return -2; } return 0; error: LM_ERR("error while preparing update operation\n"); return -1; }
int db_do_query(const db_con_t* _h, const db_key_t* _k, const db_op_t* _op, const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc, const db_key_t _o, db_res_t** _r, int (*val2str) (const db_con_t*, const db_val_t*, char*, int* _len), int (*submit_query)(const db_con_t*, const str*), int (*store_result)(const db_con_t* _h, db_res_t** _r)) { int off, ret; if (!_h || !val2str || !submit_query || (_r && !store_result)) { LM_ERR("invalid parameter value\n"); goto err_exit; } if (!_c) { ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; } else { ret = snprintf(sql_buf, SQL_BUF_LEN, "select "); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); if (ret < 0) goto err_exit; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n, val2str); if (ret < 0) goto err_exit; off += ret; } if (_o) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %.*s", _o->len, _o->s); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } /* * Null-terminate the string for the postgres driver. Its query function * don't support a length parameter, so they need this for the correct * function of strlen. This zero is not included in the 'str' length. * We need to check the length here, otherwise we could overwrite the buffer * boundaries if off is equal to SQL_BUF_LEN. */ if (off + 1 >= SQL_BUF_LEN) goto error; sql_buf[off + 1] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query - [%.*s]\n",sql_str.len,sql_str.s); goto err_exit; } if(_r) { int tmp = store_result(_h, _r); if (tmp < 0) { LM_ERR("error while storing result for query [%.*s]\n",sql_str.len,sql_str.s); CON_OR_RESET(_h); return tmp; } } CON_OR_RESET(_h); return 0; error: LM_ERR("error while preparing query\n"); err_exit: CON_OR_RESET(_h); return -1; }
int db_do_query( db_con_t* _h, db_key_t* _k, db_op_t* _op, db_val_t* _v, db_key_t* _c, int _n, int _nc, db_key_t _o, db_res_t** _r, int (*val2str) ( db_con_t*, db_val_t*, char*, int* _len), int (*submit_query)( db_con_t*, str*), int (*store_result)( db_con_t* _h, db_res_t** _r),str *query_holder) { int off, ret; str sql_str; char sql_buf[SQL_BUF_LEN]; if (!_h || !val2str || (!submit_query && !query_holder) || (_r && !store_result)) { LM_ERR("invalid parameter value\n"); return -1; } if (!_c) { ret = snprintf(sql_buf, SQL_BUF_LEN, "select * from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; } else { ret = snprintf(sql_buf, SQL_BUF_LEN, "select "); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %.*s ", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } if (_n) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n, val2str); if (ret < 0) return -1;; off += ret; } if (_o) { ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " order by %.*s", _o->len, _o->s); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; } /* * Null-terminate the string for the postgres driver. Its query function * don't support a length parameter, so they need this for the correct * function of strlen. This zero is not included in the 'str' length. * We need to check the length here, otherwise we could overwrite the buffer * boundaries if off is equal to SQL_BUF_LEN. */ if (off + 1 >= SQL_BUF_LEN) goto error; sql_buf[off + 1] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (submit_query) { if (submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query\n"); return -2; } } else { query_holder->s = pkg_malloc(off); if (!query_holder->s) { LM_ERR("no more pkg mem\n"); return -2; } memcpy(query_holder->s,sql_buf,off); query_holder->len = off; } if(_r) { int tmp = store_result(_h, _r); if (tmp < 0) { LM_ERR("error while storing result\n"); return tmp; } } return 0; error: LM_ERR("error while preparing query\n"); return -1; }
static int db_do_query_internal(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op, const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc, const db_key_t _o, db1_res_t** _r, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int* _len), int (*submit_query)(const db1_con_t*, const str*), int (*store_result)(const db1_con_t* _h, db1_res_t** _r), int _l) { int off, ret; if (!_h || !val2str || !submit_query || !store_result) { LM_ERR("invalid parameter value\n"); return -1; } if (!_c) { ret = snprintf(sql_buf, sql_buffer_size, "select * from %s%.*s%s ", CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h)); if (ret < 0 || ret >= sql_buffer_size) goto error; off = ret; } else { ret = snprintf(sql_buf, sql_buffer_size, "select "); if (ret < 0 || ret >= sql_buffer_size) goto error; off = ret; ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _c, _nc, CON_TQUOTESZ(_h)); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, sql_buffer_size - off, "from %s%.*s%s ", CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h)); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; } if (_n) { ret = snprintf(sql_buf + off, sql_buffer_size - off, "where "); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; ret = db_print_where(_h, sql_buf + off, sql_buffer_size - off, _k, _op, _v, _n, val2str); if (ret < 0) return -1;; off += ret; } if (_o) { ret = snprintf(sql_buf + off, sql_buffer_size - off, " order by %.*s", _o->len, _o->s); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; } if (_l) { ret = snprintf(sql_buf + off, sql_buffer_size - off, " for update"); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; } /* * Null-terminate the string for the postgres driver. Its query function * don't support a length parameter, so they need this for the correct * function of strlen. This zero is not included in the 'str' length. * We need to check the length here, otherwise we could overwrite the buffer * boundaries if off is equal to sql_buffer_size. */ if (off + 1 >= sql_buffer_size) goto error; sql_buf[off + 1] = '\0'; sql_str.s = sql_buf; sql_str.len = off; if (db_do_submit_query(_h, &sql_str, submit_query) < 0) { LM_ERR("error while submitting query\n"); return -2; } if(_r) { int tmp = store_result(_h, _r); if (tmp < 0) { LM_ERR("error while storing result"); return tmp; } } return 0; error: LM_ERR("error while preparing query\n"); return -1; }