int dbt_cache_print(int _f) { dbt_cache_p _dc=NULL; tbl_cache_p _tbc = NULL; if(!_cachesem) return -1; lock_get(_cachesem); _dc = *_cachedb; while(_dc) { lock_get(&_dc->sem); if(_dc->dbp) { if(_f) fprintf(stdout, "\n--- Database [%.*s]\n", _dc->dbp->name.len, _dc->dbp->name.s); _tbc = _dc->dbp->tables; while(_tbc) { lock_get(&_tbc->sem); if(_tbc->dtp) { if(_f) { fprintf(stdout, "\n----- Table [%.*s]\n", _tbc->dtp->name.len, _tbc->dtp->name.s); fprintf(stdout, "------- LA=<%d> FL=<%x> AC=<%d>" " AV=<%d>\n", _tbc->dtp->mark, _tbc->dtp->flag, _tbc->dtp->auto_col, _tbc->dtp->auto_val); dbt_print_table(_tbc->dtp, NULL); } else { if(_tbc->dtp->flag & DBT_TBFL_MODI) { dbt_print_table(_tbc->dtp, &(_dc->dbp->name)); dbt_table_update_flags(_tbc->dtp,DBT_TBFL_MODI, DBT_FL_UNSET, 0); } } } lock_release(&_tbc->sem); _tbc = _tbc->next; } } lock_release(&_dc->sem); _dc = _dc->next; } lock_release(_cachesem); return 0; }
int dbt_cache_print(int _f) { int i; dbt_table_p _tbc; if(!_dbt_cachetbl) return -1; for(i=0; i< DBT_CACHETBL_SIZE; i++) { lock_get(&_dbt_cachetbl[i].sem); _tbc = _dbt_cachetbl[i].dtp; while(_tbc) { if(_f) fprintf(stdout, "\n--- Database [%.*s]\n", _tbc->dbname.len, _tbc->dbname.s); if(_f) { fprintf(stdout, "\n----- Table [%.*s]\n", _tbc->name.len, _tbc->name.s); fprintf(stdout, "------- LA=<%d> FL=<%x> AC=<%d>" " AV=<%d>\n", _tbc->mark, _tbc->flag, _tbc->auto_col, _tbc->auto_val); dbt_print_table(_tbc, NULL); } else { if(_tbc->flag & DBT_TBFL_MODI) { dbt_print_table(_tbc, &(_tbc->dbname)); dbt_table_update_flags(_tbc,DBT_TBFL_MODI, DBT_FL_UNSET, 0); } } _tbc = _tbc->next; } lock_release(&_dbt_cachetbl[i].sem); } return 0; }
tbl_cache_p dbt_db_get_table(dbt_cache_p _dc, str *_s) { // dbt_db_p _dbp = NULL; tbl_cache_p _tbc = NULL; dbt_table_p _dtp = NULL; if(!_dc || !_s || !_s->s || _s->len<=0) return NULL; lock_get(&_dc->sem); if(!_dc->dbp) { lock_release(&_dc->sem); return NULL; } _tbc = _dc->dbp->tables; while(_tbc) { if(_tbc->dtp) { lock_get(&_tbc->sem); if(_tbc->dtp->name.len == _s->len && !strncasecmp(_tbc->dtp->name.s, _s->s, _s->len )) { lock_release(&_tbc->sem); lock_release(&_dc->sem); return _tbc; } lock_release(&_tbc->sem); } _tbc = _tbc->next; } // new table _tbc = tbl_cache_new(); if(!_tbc) { lock_release(&_dc->sem); return NULL; } _dtp = dbt_load_file(_s, &(_dc->dbp->name)); #ifdef DBT_EXTRA_DEBUG DBG("DTB:dbt_db_get_table: ---------------\n"); dbt_print_table(_dtp, NULL); #endif if(!_dtp) { lock_release(&_dc->sem); return NULL; } _tbc->dtp = _dtp; if(_dc->dbp->tables) (_dc->dbp->tables)->prev = _tbc; _tbc->next = _dc->dbp->tables; _dc->dbp->tables = _tbc; lock_release(&_dc->sem); return _tbc; }
dbt_table_p dbt_db_get_table(dbt_cache_p _dc, str *_s) { dbt_table_p _tbc = NULL; int hash; int hashidx; if(!_dbt_cachetbl || !_dc || !_s || !_s->s || _s->len<=0) return NULL; hash = core_hash(&_dc->name, _s, DBT_CACHETBL_SIZE); hashidx = hash % DBT_CACHETBL_SIZE; lock_get(&_dbt_cachetbl[hashidx].sem); _tbc = _dbt_cachetbl[hashidx].dtp; while(_tbc) { if(_tbc->hash==hash && _tbc->dbname.len == _dc->name.len && _tbc->name.len == _s->len && !strncasecmp(_tbc->dbname.s, _dc->name.s, _dc->name.len) && !strncasecmp(_tbc->name.s, _s->s, _s->len)) { /* found - if cache mode or no-change, return */ if(db_mode==0 || dbt_check_mtime(_s, &(_dc->name), &(_tbc->mt))!=1) { LM_DBG("cache or mtime succeeded\n"); return _tbc; } break; } _tbc = _tbc->next; } /* new table */ if(_tbc) /* free old one */ { lock_release(&_dbt_cachetbl[hashidx].sem); dbt_db_del_table(_dc, _s); lock_get(&_dbt_cachetbl[hashidx].sem); } _tbc = dbt_load_file(_s, &(_dc->name)); #ifdef DBT_EXTRA_DEBUG LM_DBG("%.*s\n", _s->len, _s->s); dbt_print_table(_tbc, NULL); #endif if(!_tbc) { lock_release(&_dbt_cachetbl[hashidx].sem); return NULL; } _tbc->hash = hash; _tbc->next = _dbt_cachetbl[hashidx].dtp; if(_dbt_cachetbl[hashidx].dtp) _dbt_cachetbl[hashidx].dtp->prev = _tbc; _dbt_cachetbl[hashidx].dtp = _tbc; /* table is locked */ return _tbc; }
/* * 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; }
/* * Delete a row from table */ int dbt_delete(db_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { tbl_cache_p _tbc = NULL; dbt_table_p _dtp = NULL; dbt_row_p _drp = NULL, _drp0 = NULL; int *lkey = NULL; str stbl; if (!_h || !CON_TABLE(_h)) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_delete: 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_delete: error loading table <%s>!\n", CON_TABLE(_h)); return -1; } lock_get(&_tbc->sem); _dtp = _tbc->dtp; if(!_dtp) { DBG("DBT:dbt_delete: table does not exist!!\n"); goto error; } if(!_k || !_v || _n<=0) { #ifdef DBT_EXTRA_DEBUG LOG(L_ERR, "DBT:dbt_delete: delete all values\n"); #endif dbt_table_free_rows(_dtp); lock_release(&_tbc->sem); return 0; } lkey = dbt_get_refs(_dtp, _k, _n); if(!lkey) goto error; _drp = _dtp->rows; while(_drp) { _drp0 = _drp->next; if(dbt_row_match(_dtp, _drp, lkey, _o, _v, _n)) { // delete row DBG("DBT:dbt_delete: deleting a row!\n"); if(_drp->prev) (_drp->prev)->next = _drp->next; else _dtp->rows = _drp->next; if(_drp->next) (_drp->next)->prev = _drp->prev; _dtp->nrrows--; // free row dbt_row_free(_dtp, _drp); } _drp = _drp0; } 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); return 0; error: lock_release(&_tbc->sem); DBG("DBT:dbt_delete: error deleting from 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; }