/* * Store name of table that will be used by * subsequent database functions */ int flat_use_table(db1_con_t* h, const str* t) { struct flat_con* con; if (!h || !t || !t->s) { LM_ERR("invalid parameter value\n"); return -1; } if (CON_TABLE(h)->s != t->s) { if (CON_TAIL(h)) { /* Decrement the reference count * of the connection but do not remove * it from the connection pool */ con = (struct flat_con*)CON_TAIL(h); con->ref--; } CON_TAIL(h) = (unsigned long) flat_get_connection((char*)CON_TABLE(h)->s, (char*)t->s); if (!CON_TAIL(h)) { return -1; } } return 0; }
int dbt_use_table(db_con_t* _h, const char* _t) { char* ptr; int l; if ((!_h) || (!_t)) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_use_table: Invalid parameter value\n"); #endif return -1; } l = strlen(_t) + 1; ptr = (char*)pkg_malloc(l); if (!ptr) { LOG(L_ERR, "DBT:dbt_use_table: No memory left\n"); return -2; } memcpy(ptr, _t, l); if (CON_TABLE(_h)) pkg_free(CON_TABLE(_h)); CON_TABLE(_h) = ptr; return 0; }
/* * Store name of table that will be used by * subsequent database functions */ int use_table(db_con_t* _h, const char* _t) { if(CON_TABLE(_h)) aug_free((char*)CON_TABLE(_h)); CON_TABLE(_h) = aug_strdup((char *) _t, _h); return 0; }
/* * 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: nmber of key=values pairs to compare * _nc: number of columns to return * _o: order by the specified column */ int db_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, rv; if (!_c) { off = snprintf(sql_buf, SQL_BUF_LEN, "select * from %s ", CON_TABLE(_h)); } else { off = snprintf(sql_buf, SQL_BUF_LEN, "select "); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _c, _nc); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "from %s ", CON_TABLE(_h)); } if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _op, _v, _n); } if (_o) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, "order by %s", _o); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_query(): Error while submitting query\n"); return -2; } rv = get_result(_h, _r); free_query(_h); commit_transaction(_h); return(rv); }
/** * Insert a row into a specified table, update on duplicate key. * \param _h structure representing database connection * \param _k key names * \param _v values of the keys * \param _n number of key=value pairs */ int erlang_srdb1_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { ei_x_buff argbuf; int retcode; if ((!_h) || (!_k) || (!_v) || (!_n)) { LM_ERR("invalid parameter value\n"); return -1; } LM_DBG("erlang_srdb1_insert_update table %.*s\n",CON_TABLE(_h)->len, CON_TABLE(_h)->s); ei_x_new(&argbuf); //encode tuple {db_op, table, [cols], [keys], [vals]} ei_x_encode_tuple_header(&argbuf, 5); ei_x_encode_atom(&argbuf,"insert_update"); ei_x_encode_atom_len(&argbuf,CON_TABLE(_h)->s,CON_TABLE(_h)->len); ei_x_encode_list_header(&argbuf, 0); //_c // ei_x_encode_list_header(&argbuf, 0); //_k srdb1_encode_k(_k, NULL, _v, _n, &argbuf); //_k srdb1_encode_v(_k, _v, _n, &argbuf); //_v retcode=erl_bind.do_erlang_call(&(CON_ERLANG(_h)->con),&(CON_ERLANG(_h)->regname), &argbuf, NULL /*&retbuf*/); ei_x_free(&argbuf); if (retcode<0) { // if(retbuf.buff) shm_free(retbuf.buff); return retcode; } return 0; }
/* * 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; }
/** * Insert a row into a specified table, update on duplicate key. * \param _h structure representing database connection * \param _k key names * \param _v values of the keys * \param _n number of key=value pairs */ int db_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { int off, ret; static str sql_str; static char sql_buf[SQL_BUF_LEN]; if ((!_h) || (!_k) || (!_v) || (!_n)) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, db_mysql_val2str); if (ret < 0) return -1; off += ret; *(sql_buf + off++) = ')'; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " on duplicate key update "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _v, _n, db_mysql_val2str); if (ret < 0) return -1; off += ret; sql_str.s = sql_buf; sql_str.len = off; if (db_mysql_submit_query(_h, &sql_str) < 0) { LM_ERR("error while submitting query\n"); return -2; } return 0; error: LM_ERR("error while preparing insert_update 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_insert_cmd(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n, int (*val2str) (const db1_con_t*, const db_val_t*, char*, int*), int (*submit_query)(const db1_con_t* _h, const str* _c), int mode) { int off, ret; if (!_h || !_k || !_v || !_n || !val2str || !submit_query) { LM_ERR("invalid parameter value\n"); return -1; } if(mode==1) ret = snprintf(sql_buf, sql_buffer_size, "insert delayed into %s%.*s%s (", CON_TQUOTESZ(_h), CON_TABLE(_h)->len, CON_TABLE(_h)->s, CON_TQUOTESZ(_h)); else ret = snprintf(sql_buf, sql_buffer_size, "insert into %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; ret = db_print_columns(sql_buf + off, sql_buffer_size - off, _k, _n, CON_TQUOTESZ(_h)); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, sql_buffer_size - off, ") values ("); if (ret < 0 || ret >= (sql_buffer_size - off)) goto error; off += ret; ret = db_print_values(_h, sql_buf + off, sql_buffer_size - off, _v, _n, val2str); if (ret < 0) return -1; off += ret; if (off + 2 > sql_buffer_size) goto error; sql_buf[off++] = ')'; 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 insert operation\n"); return -1; }
/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */ int db_oracle_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, int _n, int _un) { query_data_t cb; int rc; if (!_h || !CON_TABLE(_h)) { LM_ERR("invalid parameter value\n"); return -1; } cb._rs = NULL; cb._v = _uv; cb._n = _un; cb._w = _v; cb._nw = _n; CON_ORA(_h)->pqdata = &cb; CON_ORA(_h)->bindpos = 0; CON_RESET_CURR_PS(_h); /* no prepared statements support */ rc = db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_oracle_val2str, db_oracle_submit_query); CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */ return rc; }
/* * 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_oracle_query(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, int _n, int _nc, const db_key_t _o, db1_res_t** _r) { query_data_t cb; OCIStmt* reshp; int rc; if (!_h || !CON_TABLE(_h) || !_r) { LM_ERR("invalid parameter value\n"); return -1; } cb._rs = &reshp; cb._v = _v; cb._n = _n; cb._w = NULL; cb._nw = 0; CON_ORA(_h)->pqdata = &cb; CON_ORA(_h)->bindpos = 0; rc = db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r, db_oracle_val2str, db_oracle_submit_query, db_oracle_store_result); CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */ return rc; }
/* * Store name of table that will be used by * subsequent database functions */ int flat_use_table(db_con_t* h, const str* t) { struct flat_con* con; if (!h || !t || !t->s) { LM_ERR("invalid parameter value\n"); return -1; } if (!CON_TAIL(h) || !(CON_FILENAME(h).len == t->len && !memcmp(CON_FILENAME(h).s, t->s, t->len))) { if (CON_TAIL(h)) { /* Decrement the reference count * of the connection but do not remove * it from the connection pool */ con = (struct flat_con*)CON_TAIL(h); con->ref--; } CON_TAIL(h) = (unsigned long) flat_get_connection(CON_TABLE(h), t); if (!CON_TAIL(h)) { return -1; } } return 0; }
int bdb_use_table(db_con_t* _h, const char* _t) { if ((!_h) || (!_t)) return -1; CON_TABLE(_h) = _t; return 0; }
int db_do_replace( db_con_t* _h, db_key_t* _k, 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)) { int off, ret; str sql_str; char sql_buf[SQL_BUF_LEN]; if (!_h || !_k || !_v || !val2str|| !submit_query) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "replace %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, val2str); if (ret < 0) return -1; off += ret; if (off + 2 > SQL_BUF_LEN) goto error; sql_buf[off++] = ')'; 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"); return -2; } return 0; error: LM_ERR("error while preparing replace operation\n"); 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; }
/* * Affected Rows */ int dbt_affected_rows(db1_con_t* _h) { if (!_h || !CON_TABLE(_h)) { LM_ERR("invalid parameter\n"); return -1; } return ((dbt_con_p)_h->tail)->affected; }
int bdb_rrow_db2bdb(db_con_t* _h, db_key_t* _k, int _n, bdb_rrow_p *_r) { bdb_rrow_p r; bdb_table_p t; bdb_column_p c; int found; int i; int c_idx; *_r = NULL; if ((t = bdb_find_table(CON_TABLE(_h))) == NULL) { #ifdef BDB_EXTRA_DEBUG LOG(L_ERR, "BDB:bdb_rrow_db2bdb: no table in use\n"); #endif return -1; }; i = (_n == 0) ? BDB_CON_COL_NUM(_h) : _n; r = pkg_malloc(sizeof(*r) * i); memset(r, 0, sizeof(*r) * i); if (_n > 0) { for (i = 0; i < _n; i++) { found = 0; for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { if (!strcmp(_k[i], c->name.s)) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_rrow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif r[i] = c_idx; found = 1; break; } } if (!found) { LOG(L_ERR, "BDB:bdb_rrow_db2bdb: column '%s' does not exist\n", _k[i]); bdb_free_rrow(r); return -1; } } } else { /* return all columns */ for (c = t->cols, c_idx = 0; c != NULL; c = c->next, c_idx++) { #ifdef BDB_EXTRA_DEBUG LOG(L_NOTICE, "BDB:bdb_rrow_db2bdb: filling column '%.*s', c_idx = %0d\n", c->name.len, c->name.s, c_idx); #endif r[c_idx] = c_idx; } } *_r = r; return 0; };
/* * Store name of table that will be used by * subsequent database functions */ int use_table(db_con_t* _h, const char* _t) { char* ptr; int l; #ifdef PARANOID if ((!_h) || (!_t)) { LOG(L_ERR, "use_table(): Invalid parameter value\n"); return -1; } #endif l = strlen(_t) + 1; ptr = (char*)pkg_malloc(l); if (!ptr) { LOG(L_ERR, "use_table(): No memory left\n"); return -2; } memcpy(ptr, _t, l); if (CON_TABLE(_h)) pkg_free(CON_TABLE(_h)); CON_TABLE(_h) = ptr; return 0; }
int dbt_use_table(db_con_t* _h, const char* _t) { if ((!_h) || (!_t)) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_use_table: Invalid parameter value\n"); #endif return -1; } CON_TABLE(_h) = _t; return 0; }
/* * Insert a row into specified table, update on duplicate key * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_insert_update(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { int off, ret; if ((!_h) || (!_k) || (!_v) || (!_n)) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, val2str); if (ret < 0) return -1; off += ret; *(sql_buf + off++) = ')'; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " ON DUPLICATE KEY UPDATE "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _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; }
/* * 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_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "delete from %s", CON_TABLE(_h)); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_delete(): Error while deleting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Insert a row into specified table * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h)); off += print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); off += snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); off += print_values(sql_buf + off, SQL_BUF_LEN - off, _v, _n); *(sql_buf + off++) = ')'; *(sql_buf + off) = '\0'; if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_insert(): Error while inserting\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { int off, ret; if ((!_h) || (!_uk) || (!_uv) || (!_un)) { LOG(L_ERR, "db_update: Invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = print_set(CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un); if (ret < 0) return -1; 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 = print_where(CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); if (ret < 0) return -1; off += ret; *(sql_buf + off) = '\0'; } if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_update: Error while submitting query\n"); return -2; } return 0; error: LOG(L_ERR, "db_update: Error in snprintf\n"); return -1; }
/* * 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_oracle_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o, const db_val_t* _v, int _n) { query_data_t cb; int rc; if (!_h || !CON_TABLE(_h)) { LM_ERR("invalid parameter value\n"); return -1; } cb._rs = NULL; cb._v = _v; cb._n = _n; cb._w = NULL; cb._nw = 0; CON_ORA(_h)->pqdata = &cb; CON_ORA(_h)->bindpos = 0; rc = db_do_delete(_h, _k, _o, _v, _n, db_oracle_val2str, db_oracle_submit_query); CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */ return rc; }
/* * Update some rows in the specified table * _h: structure representing database connection * _k: key names * _o: operators * _v: values of the keys that must match * _uk: updated columns * _uv: updated values of the columns * _n: number of key=value pairs * _un: number of columns to update */ int db_update(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, db_key_t* _uk, db_val_t* _uv, int _n, int _un) { int off; off = snprintf(sql_buf, SQL_BUF_LEN, "update %s set ", CON_TABLE(_h)); off += print_set(sql_buf + off, SQL_BUF_LEN - off, _uk, _uv, _un); if (_n) { off += snprintf(sql_buf + off, SQL_BUF_LEN - off, " where "); off += print_where(sql_buf + off, SQL_BUF_LEN - off, _k, _o, _v, _n); *(sql_buf + off) = '\0'; } if(begin_transaction(_h, sql_buf)) return(-1); if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_update(): Error while updating\n"); return -2; } free_query(_h); commit_transaction(_h); return(0); }
/* * Insert a row into specified table * _h: structure representing database connection * _k: key names * _v: values of the keys * _n: number of key=value pairs */ int db_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { int off, ret; if ((!_h) || (!_k) || (!_v) || (!_n)) { LOG(L_ERR, "db_insert: Invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %s (", CON_TABLE(_h)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = print_values(CON_CONNECTION(_h), sql_buf + off, SQL_BUF_LEN - off, _v, _n); if (ret < 0) return -1; off += ret; *(sql_buf + off++) = ')'; *(sql_buf + off) = '\0'; if (submit_query(_h, sql_buf) < 0) { LOG(L_ERR, "db_insert: Error while submitting query\n"); return -2; } return 0; error: LOG(L_ERR, "db_insert: Error in snprintf\n"); return -1; }
/* * Just like insert, but replace the row if it exists */ int db_mysql_replace(db_con_t* handle, db_key_t* keys, db_val_t* vals, int n) { int off, ret; if (!handle || !keys || !vals) { LM_ERR("invalid parameter value\n"); return -1; } ret = snprintf(sql_buf, SQL_BUF_LEN, "replace %s (", CON_TABLE(handle)); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, keys, n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_values(handle, sql_buf + off, SQL_BUF_LEN - off, vals, n, val2str); if (ret < 0) return -1; off += ret; *(sql_buf + off++) = ')'; *(sql_buf + off) = '\0'; if (db_mysql_submit_query(handle, sql_buf) < 0) { LM_ERR("error while submitting query\n"); return -2; } return 0; error: LM_ERR("error in snprintf\n"); return -1; }
/** * Insert a row into a specified table, update on duplicate key. * \param _h structure representing database connection * \param _k key names * \param _v values of the keys * \param _n number of key=value pairs */ int db_insert_update(const db_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { #define SQL_BUF_LEN 65536 int off, ret; static str sql_str; static char sql_buf[SQL_BUF_LEN]; sqlite3_stmt* stmt; if ((!_h) || (!_k) || (!_v) || (!_n)) { LM_ERR("invalid parameter value\n"); return -1; } #ifdef SQLITE_BIND db_ps_t ps; CON_SET_CURR_PS(_h, &ps); #endif ret = snprintf(sql_buf, SQL_BUF_LEN, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s); if (ret < 0 || ret >= SQL_BUF_LEN) goto error; off = ret; ret = db_print_columns(sql_buf + off, SQL_BUF_LEN - off, _k, _n); if (ret < 0) return -1; off += ret; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, ") values ("); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_values(_h, sql_buf + off, SQL_BUF_LEN - off, _v, _n, db_sqlite_val2str); if (ret < 0) return -1; off += ret; *(sql_buf + off++) = ')'; ret = snprintf(sql_buf + off, SQL_BUF_LEN - off, " on duplicate key update "); if (ret < 0 || ret >= (SQL_BUF_LEN - off)) goto error; off += ret; ret = db_print_set(_h, sql_buf + off, SQL_BUF_LEN - off, _k, _v, _n, db_sqlite_val2str); if (ret < 0) return -1; off += ret; sql_str.s = sql_buf; sql_str.len = off; again: ret=sqlite3_prepare_v2(CON_CONNECTION(_h), sql_str.s, sql_str.len, &stmt, NULL); if (ret==SQLITE_BUSY) goto again; if (ret!=SQLITE_OK) LM_ERR("failed to prepare: (%s)\n", sqlite3_errmsg(CON_CONNECTION(_h))); #ifdef SQLITE_BIND if (db_sqlite_bind_values(stmt, _v, _n) != SQLITE_OK) { LM_ERR("failed to bind values\n"); return -1; } #endif again2: ret = sqlite3_step(stmt); if (ret==SQLITE_BUSY) goto again2; if (ret != SQLITE_DONE) { LM_ERR("insert query failed %s\n", sqlite3_errmsg(CON_CONNECTION(_h))); return -1; } sqlite3_finalize(stmt); return 0; #undef SQL_BUF_LEN error: LM_ERR("error while preparing insert_update operation\n"); return -1; }