int dbt_table_check_row(dbt_table_p _dtp, dbt_row_p _drp) { int i; if(!_dtp || _dtp->nrcols <= 0 || !_drp) return -1; for(i=0; i<_dtp->nrcols; i++) { if(!_drp->fields[i].nul && dbt_is_neq_type(_dtp->colv[i]->type, _drp->fields[i].type)) { LM_DBG("incompatible types - field %d [%d/%d]\n",i, _dtp->colv[i]->type, _drp->fields[i].type); return -1; } if(_dtp->colv[i]->flag & DBT_FLAG_NULL) continue; if(!_drp->fields[i].nul) continue; if(_dtp->colv[i]->type==DB_INT && (_dtp->colv[i]->flag & DBT_FLAG_AUTO) && i==_dtp->auto_col) { _drp->fields[i].nul = 0; _drp->fields[i].val.int_val = ++_dtp->auto_val; continue; } LM_DBG("null value not allowed - field %d\n",i); return -1; } return 0; }
int dbt_replace(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n, int _nk, int _m) { dbt_table_p _tbc = NULL; dbt_row_p _drp = NULL; int i, j; int *lkey=NULL, *lres=NULL; if (!_h || !CON_TABLE(_h) || _nk <= 0) { LM_ERR("invalid parameters\n"); return -1; } ((dbt_con_p)_h->tail)->affected = 0; /* lock database */ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(!_tbc) { LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s); return -1; } if(_k) { lkey = dbt_get_refs(_tbc, _k, _nk); if(!lkey) goto error; } lres = dbt_get_refs(_tbc, _k, _n); if(!lres) goto error; _drp = _tbc->rows; while(_drp) { if(dbt_row_match(_tbc, _drp, lkey, NULL, _v, _nk)) { // update fields for(i=0; i<_n; i++) { if(dbt_is_neq_type(_tbc->colv[lres[i]]->type, _v[i].type)) { LM_ERR("incompatible types!\n"); goto error; } if(dbt_row_update_val(_drp, &(_v[i]), _tbc->colv[lres[i]]->type, lres[i])) { LM_ERR("cannot set v[%d] in c[%d]!\n", i, lres[i]); goto error; } } ((dbt_con_p)_h->tail)->affected++; break; } _drp = _drp->next; } if(((dbt_con_p)_h->tail)->affected == 0) { _drp = dbt_row_new(_tbc->nrcols); if(!_drp) { LM_ERR("no shm memory for a new row!!\n"); goto error; } for(i=0; i<_n; i++) { j = lres[i]; if(dbt_is_neq_type(_tbc->colv[j]->type, _v[i].type)) { LM_ERR("incompatible types v[%d] - c[%d]!\n", i, j); goto error; } if(_v[i].type == DB1_STRING && !_v[i].nul) _v[i].val.str_val.len = strlen(_v[i].val.string_val); if(dbt_row_set_val(_drp, &(_v[i]), _tbc->colv[j]->type, j)) { LM_ERR("cannot set v[%d] in c[%d]!\n", i, j); goto error; } } if(dbt_table_add_row(_tbc, _drp)) { LM_ERR("cannot insert the new row!!\n"); goto error; } ((dbt_con_p)_h->tail)->affected = 1; } if( ((dbt_con_p)_h->tail)->affected ) dbt_table_update_flags(_tbc, DBT_TBFL_MODI, DBT_FL_SET, 1); /* dbt_print_table(_tbc, NULL); */ /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); return 0; error: if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_drp) // free row dbt_row_free(_tbc, _drp); /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); LM_ERR("failed to update the table!\n"); return -1; }
/* * Update a row in table */ int dbt_update(db1_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) { dbt_table_p _tbc = NULL; dbt_row_p _drp = NULL; int i; int *lkey=NULL, *lres=NULL; if (!_h || !CON_TABLE(_h) || !_uk || !_uv || _un <= 0) { LM_ERR("invalid parameters\n"); return -1; } ((dbt_con_p)_h->tail)->affected = 0; /* lock database */ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(!_tbc) { LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s); return -1; } if(_k) { lkey = dbt_get_refs(_tbc, _k, _n); if(!lkey) goto error; } lres = dbt_get_refs(_tbc, _uk, _un); if(!lres) goto error; _drp = _tbc->rows; while(_drp) { if(dbt_row_match(_tbc, _drp, lkey, _o, _v, _n)) { // update fields for(i=0; i<_un; i++) { if(dbt_is_neq_type(_tbc->colv[lres[i]]->type, _uv[i].type)) { LM_ERR("incompatible types!\n"); goto error; } if(dbt_row_update_val(_drp, &(_uv[i]), _tbc->colv[lres[i]]->type, lres[i])) { LM_ERR("cannot set v[%d] in c[%d]!\n", i, lres[i]); goto error; } } ((dbt_con_p)_h->tail)->affected++; } _drp = _drp->next; } if( ((dbt_con_p)_h->tail)->affected ) dbt_table_update_flags(_tbc, DBT_TBFL_MODI, DBT_FL_SET, 1); /* dbt_print_table(_tbc, NULL); */ /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); return 0; error: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); LM_ERR("failed to update the table!\n"); return -1; }
/* * Insert a row into table */ int dbt_insert(db1_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { dbt_table_p _tbc = NULL; dbt_row_p _drp = NULL; int *lkey=NULL, i, j; if (!_h || !CON_TABLE(_h)) { LM_ERR("invalid parameter\n"); return -1; } ((dbt_con_p)_h->tail)->affected = 0; if(!_k || !_v || _n<=0) { LM_ERR("no key-value to insert\n"); return -1; } /* lock database */ _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(!_tbc) { LM_ERR("table %.*s does not exist!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s); return -1; } if(_tbc->nrcols<_n) { LM_ERR("more values than columns!!\n"); goto error; } if(_k) { lkey = dbt_get_refs(_tbc, _k, _n); if(!lkey) goto error; } _drp = dbt_row_new(_tbc->nrcols); if(!_drp) { LM_ERR("no shm memory for a new row!!\n"); goto error; } for(i=0; i<_n; i++) { j = (lkey)?lkey[i]:i; if(dbt_is_neq_type(_tbc->colv[j]->type, _v[i].type)) { LM_ERR("incompatible types v[%d] - c[%d]!\n", i, j); goto clean; } if(_v[i].type == DB1_STRING && !_v[i].nul) _v[i].val.str_val.len = strlen(_v[i].val.string_val); if(dbt_row_set_val(_drp, &(_v[i]), _tbc->colv[j]->type, j)) { LM_ERR("cannot set v[%d] in c[%d]!\n", i, j); goto clean; } } if(dbt_table_add_row(_tbc, _drp)) { LM_ERR("cannot insert the new row!!\n"); goto clean; } ((dbt_con_p)_h->tail)->affected = 1; /* dbt_print_table(_tbc, NULL); */ /* unlock databse */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); return 0; error: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); LM_ERR("failed to insert row in table!\n"); return -1; clean: if(lkey) pkg_free(lkey); if(_drp) // free row dbt_row_free(_tbc, _drp); /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); return -1; }
int dbt_result_extract_fields(dbt_table_p _dtp, dbt_row_p _drp, int* _lres, dbt_result_p _dres) { dbt_row_p _rp=NULL; int i, n; if(!_dtp || !_drp || !_dres || _dres->nrcols<=0) return -1; _rp = dbt_result_new_row(_dres); if(!_rp) return -1; for(i=0; i<_dres->nrcols; i++) { n = (_lres)?_lres[i]:i; if(dbt_is_neq_type(_dres->colv[i].type, _dtp->colv[n]->type)) { LM_DBG("wrong types!\n"); goto clean; } _rp->fields[i].nul = _drp->fields[n].nul; if(_rp->fields[i].nul) { memset(&(_rp->fields[i].val), 0, sizeof(_rp->fields[i].val)); continue; } switch(_dres->colv[i].type) { case DB_INT: case DB_DATETIME: case DB_BITMAP: _rp->fields[i].type = _dres->colv[i].type; _rp->fields[i].val.int_val = _drp->fields[n].val.int_val; break; case DB_BIGINT: _rp->fields[i].type = _dres->colv[i].type; _rp->fields[i].val.bigint_val = _drp->fields[n].val.bigint_val; break; case DB_DOUBLE: _rp->fields[i].type = DB_DOUBLE; _rp->fields[i].val.double_val=_drp->fields[n].val.double_val; break; case DB_STRING: case DB_STR: case DB_BLOB: _rp->fields[i].type = _dres->colv[i].type; _rp->fields[i].val.str_val.len = _drp->fields[n].val.str_val.len; _rp->fields[i].val.str_val.s =(char*)pkg_malloc(sizeof(char)* (_drp->fields[n].val.str_val.len+1)); if(!_rp->fields[i].val.str_val.s) goto clean; memcpy(_rp->fields[i].val.str_val.s, _drp->fields[n].val.str_val.s, _rp->fields[i].val.str_val.len); _rp->fields[i].val.str_val.s[_rp->fields[i].val.str_val.len]=0; break; default: goto clean; } } _rp->next = NULL; if (_dres->last) { _dres->last->next = _rp; _rp->prev = _dres->last; } else { _dres->rows = _rp; } _dres->last = _rp; _dres->nrrows++; return 0; clean: LM_DBG("make clean!\n"); while(i>=0) { if((_rp->fields[i].type == DB_STRING || _rp->fields[i].type == DB_STR || _rp->fields[i].type == DB_BLOB) && !_rp->fields[i].nul && _rp->fields[i].val.str_val.s) pkg_free(_rp->fields[i].val.str_val.s); i--; } pkg_free(_rp->fields); pkg_free(_rp); return -1; }
/* * Update a row in table */ int dbt_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) { tbl_cache_p _tbc = NULL; dbt_table_p _dtp = NULL; dbt_row_p _drp = NULL; int i; str stbl; int *lkey=NULL, *lres=NULL; if (!_h || !CON_TABLE(_h) || !_uk || !_uv || _un <= 0) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_update: Invalid parameter value\n"); #endif return -1; } stbl.s = (char*)CON_TABLE(_h); stbl.len = strlen(CON_TABLE(_h)); _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), &stbl); if(!_tbc) { DBG("DBT:dbt_update: table does not exist!\n"); return -1; } lock_get(&_tbc->sem); _dtp = _tbc->dtp; if(!_dtp || _dtp->nrcols < _un) { DBG("DBT:dbt_update: table not loaded or more values" " to update than columns!\n"); goto error; } if(_k) { lkey = dbt_get_refs(_dtp, _k, _n); if(!lkey) goto error; } lres = dbt_get_refs(_dtp, _uk, _un); if(!lres) goto error; DBG("DBT:dbt_update: ---- \n"); _drp = _dtp->rows; while(_drp) { if(dbt_row_match(_dtp, _drp, lkey, _o, _v, _n)) { // update fields for(i=0; i<_un; i++) { if(dbt_is_neq_type(_dtp->colv[lres[i]]->type, _uv[i].type)) { DBG("DBT:dbt_update: incompatible types!\n"); goto error; } if(dbt_row_update_val(_drp, &(_uv[i]), _uv[i].type, lres[i])) { DBG("DBT:dbt_update: cannot set v[%d] in c[%d]!\n", i, lres[i]); goto error; } } } _drp = _drp->next; } dbt_table_update_flags(_dtp, DBT_TBFL_MODI, DBT_FL_SET, 1); #ifdef DBT_EXTRA_DEBUG dbt_print_table(_dtp, NULL); #endif lock_release(&_tbc->sem); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); return 0; error: lock_release(&_tbc->sem); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); DBG("DBT:dbt_update: error while updating table!\n"); return -1; }
/* * Insert a row into table */ int dbt_insert(db_con_t* _h, db_key_t* _k, db_val_t* _v, int _n) { tbl_cache_p _tbc = NULL; dbt_table_p _dtp = NULL; dbt_row_p _drp = NULL; str stbl; int *lkey=NULL, i, j; if (!_h || !CON_TABLE(_h)) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_insert: Invalid parameter value\n"); #endif return -1; } if(!_k || !_v || _n<=0) { #ifdef DBT_EXTRA_DEBUG DBG("DBT:dbt_insert: no key-value to insert\n"); #endif return -1; } stbl.s = (char*)CON_TABLE(_h); stbl.len = strlen(CON_TABLE(_h)); _tbc = dbt_db_get_table(DBT_CON_CONNECTION(_h), &stbl); if(!_tbc) { DBG("DBT:db_insert: table does not exist!\n"); return -1; } lock_get(&_tbc->sem); _dtp = _tbc->dtp; if(!_dtp) { DBG("DBT:db_insert: table does not exist!!\n"); goto error; } if(_dtp->nrcols<_n) { DBG("DBT:db_insert: more values than columns!!\n"); goto error; } if(_k) { lkey = dbt_get_refs(_dtp, _k, _n); if(!lkey) goto error; } _drp = dbt_row_new(_dtp->nrcols); if(!_drp) { DBG("DBT:db_insert: no memory for a new row!!\n"); goto error; } for(i=0; i<_n; i++) { j = (lkey)?lkey[i]:i; if(dbt_is_neq_type(_dtp->colv[j]->type, _v[i].type)) { DBG("DBT:db_insert: incompatible types v[%d] - c[%d]!\n", i, j); goto clean; } if(dbt_row_set_val(_drp, &(_v[i]), _v[i].type, j)) { DBG("DBT:db_insert: cannot set v[%d] in c[%d]!\n", i, j); goto clean; } } if(dbt_table_add_row(_dtp, _drp)) { DBG("DBT:db_insert: cannot insert the new row!!\n"); goto clean; } #ifdef DBT_EXTRA_DEBUG dbt_print_table(_dtp, NULL); #endif lock_release(&_tbc->sem); if(lkey) pkg_free(lkey); DBG("DBT:db_insert: done!\n"); return 0; error: lock_release(&_tbc->sem); if(lkey) pkg_free(lkey); DBG("DBT:db_insert: error inserting row in table!\n"); return -1; clean: lock_release(&_tbc->sem); if(lkey) pkg_free(lkey); if(_drp) // free row dbt_row_free(_dtp, _drp); DBG("DBT:db_insert: make clean!\n"); return -1; }