int dbt_raw_query_replace(db1_con_t* _h, str* _s, db1_res_t** _r) { int res = -1; int i, len; char* table_ptr = NULL; char* fields_end_ptr = NULL; char* fields_start_ptr = NULL; char* fields_ptr = NULL; char* where_ptr = NULL; char* table_start_ptr = NULL; str table; dbt_table_p _tbc = NULL; int cols; int n = 0; int ncols = 0; int nkeys = 0; db_key_t* _k = NULL; db_op_t* _op1 = NULL; db_val_t* _kv = NULL; db_key_t* _c = NULL; db_op_t* _op2 = NULL; db_val_t* _cv = NULL; db_key_t* _f = NULL; db_val_t* _v = NULL; LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s); table_start_ptr = _s->s + 7; fields_start_ptr = strcasestr(_s->s, " set "); if(fields_start_ptr == NULL) return res; len = fields_start_ptr - table_start_ptr; table_ptr = pkg_malloc(len); strncpy(table_ptr, table_start_ptr, len); table_ptr[len] = '\0'; dbt_trim(table_ptr); table.s = table_ptr; table.len = len; where_ptr = strcasestr(_s->s, " where "); fields_end_ptr = where_ptr; len = fields_end_ptr - ( fields_start_ptr + 4) + 1; fields_ptr = pkg_malloc(len); strncpy(fields_ptr, fields_start_ptr + 4, len); fields_ptr[len] = '\0'; fields_ptr = dbt_trim(fields_ptr); ncols = dbt_build_where(fields_ptr, &_c, &_op2, &_cv); if(ncols <0) { LM_ERR("unexpected error buuilding fields\n"); goto error; } if(where_ptr == NULL) { LM_ERR("specify where clause to determine keys\n"); goto error; } nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv); if(nkeys < 1) { LM_ERR("needsa at least one key\n"); goto error; } LM_DBG("using table '%.*s'\n", table.len, table.s); if(dbt_use_table(_h, &table) != 0) { LM_ERR("use table is invalid %.*s\n", table.len, table.s); goto error; } _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); goto error; } cols = nkeys + ncols; _f = pkg_malloc(sizeof(db_key_t) * cols); _v = pkg_malloc(sizeof(db_val_t) * cols); for(n=0; n < nkeys; n++) { _f[n] = _k[n]; _v[n] = _kv[n]; } for(i=n; i < cols; i++) { _f[i] = _c[i-n]; _v[i] = _cv[i-n]; } dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); _tbc = NULL; res = dbt_replace(_h, _f, _v, cols, nkeys, 0); error: if(_tbc) dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(fields_ptr) pkg_free(fields_ptr); if(table_ptr) pkg_free(table_ptr); dbt_clean_where(nkeys, _k, _op1, _kv); dbt_clean_where(ncols, _c, _op2, _cv); if(_f) pkg_free(_f); if(_v) pkg_free(_v); return res; }
int dbt_raw_query_select(db1_con_t* _h, str* _s, db1_res_t** _r) { int res = -1; int i, len; char* table_ptr = NULL; char* fields_end_ptr = NULL; char* fields_ptr = NULL; char* where_ptr = NULL; char* order_start_ptr = NULL; char** tokens = NULL; str table; dbt_table_p _tbc = NULL; int cols; int n = 0; int ncols = 0; int nc = 0; db_key_t *result_cols = NULL; db_key_t* _k = NULL; db_op_t* _op = NULL; db_val_t* _v = NULL; str order; db_key_t k_order = NULL; LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s); fields_end_ptr = strcasestr(_s->s, " from "); if(fields_end_ptr == NULL) return res; len = fields_end_ptr - (_s->s + 6) + 1; fields_ptr = pkg_malloc(len); strncpy(fields_ptr, _s->s + 6, len); fields_ptr[len] = '\0'; fields_ptr = dbt_trim(fields_ptr); order_start_ptr = strcasestr(_s->s, " order by "); if(order_start_ptr != NULL) { *order_start_ptr = '\0'; order_start_ptr += 10; } where_ptr = strcasestr(_s->s, " where "); if(where_ptr == NULL) { len = strlen(fields_end_ptr + 6); } else { len = where_ptr - (fields_end_ptr + 6); nc = dbt_build_where(where_ptr + 7, &_k, &_op, &_v); } table_ptr = pkg_malloc(len); strncpy(table_ptr, fields_end_ptr + 6, len); table_ptr[len] = '\0'; dbt_trim(table_ptr); table.s = table_ptr; table.len = len; LM_DBG("using table '%.*s'\n", table.len, table.s); if(dbt_use_table(_h, &table) != 0) { LM_ERR("use table is invalid %.*s\n", table.len, table.s); goto error; } _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); goto error; } tokens = dbt_str_split(fields_ptr, ',', &ncols); pkg_free(fields_ptr); fields_ptr = NULL; if (!tokens) { LM_ERR("error extracting tokens\n"); goto error; } if(ncols == 1 && strncmp(*tokens, "*", 1) == 0) { cols = _tbc->nrcols; result_cols = pkg_malloc(sizeof(db_key_t) * cols); memset(result_cols, 0, sizeof(db_key_t) * cols); for(n=0; n < cols; n++) { result_cols[n] = &_tbc->colv[n]->name; } } else { cols = ncols; result_cols = pkg_malloc(sizeof(db_key_t) * cols); memset(result_cols, 0, sizeof(db_key_t) * cols); for(n=0; *(tokens + n); n++) { result_cols[n]->s = *(tokens + n); result_cols[n]->len = strlen(*(tokens + n)); } } dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); _tbc = NULL; if(order_start_ptr != NULL) { order.s = order_start_ptr; order.len = strlen(order_start_ptr); k_order = ℴ } res = dbt_query(_h, _k, _op, _v, result_cols, nc, cols, k_order, _r); error: if(_tbc) dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(tokens) { for (i = 0; *(tokens + i); i++) { pkg_free(*(tokens + i)); } pkg_free(tokens); } if(fields_ptr) pkg_free(fields_ptr); if(table_ptr) pkg_free(table_ptr); dbt_clean_where(nc, _k, _op, _v); if(result_cols) { pkg_free(result_cols); } return res; }
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; }
int dbt_raw_query_delete(db1_con_t* _h, str* _s, db1_res_t** _r) { int res = -1; int len; char* table_ptr = NULL; char* fields_end_ptr = NULL; char* fields_ptr = NULL; char* where_ptr = NULL; str table; dbt_table_p _tbc = NULL; int nkeys = 0; db_key_t* _k = NULL; db_op_t* _op1 = NULL; db_val_t* _kv = NULL; LM_DBG("SQLRAW : %.*s\n", _s->len, _s->s); fields_end_ptr = strcasestr(_s->s, " from "); if(fields_end_ptr == NULL) return res; where_ptr = strcasestr(_s->s, " where "); if(where_ptr == NULL) { len = strlen(fields_end_ptr + 6); } else { len = where_ptr - (fields_end_ptr + 6); nkeys = dbt_build_where(where_ptr + 7, &_k, &_op1, &_kv); } table_ptr = pkg_malloc(len); strncpy(table_ptr, fields_end_ptr + 6, len); table_ptr[len] = '\0'; dbt_trim(table_ptr); table.s = table_ptr; table.len = len; LM_DBG("using table '%.*s'\n", table.len, table.s); if(dbt_use_table(_h, &table) != 0) { LM_ERR("use table is invalid %.*s\n", table.len, table.s); goto error; } _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); goto error; } dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); _tbc = NULL; res = dbt_delete(_h, _k, _op1, _kv, nkeys); error: if(_tbc) dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(fields_ptr) pkg_free(fields_ptr); if(table_ptr) pkg_free(table_ptr); dbt_clean_where(nkeys, _k, _op1, _kv); return res; }
/* * 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; }
/* * Delete a row from table */ int dbt_delete(db1_con_t* _h, db_key_t* _k, db_op_t* _o, db_val_t* _v, int _n) { dbt_table_p _tbc = NULL; dbt_row_p _drp = NULL, _drp0 = NULL; int *lkey = NULL; if (!_h || !CON_TABLE(_h)) { 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("failed to load table <%.*s>!\n", CON_TABLE(_h)->len, CON_TABLE(_h)->s); return -1; } if(!_k || !_v || _n<=0) { LM_DBG("deleting all records\n"); ((dbt_con_p)_h->tail)->affected = _tbc->nrrows; dbt_table_free_rows(_tbc); /* unlock databse */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); return 0; } lkey = dbt_get_refs(_tbc, _k, _n); if(!lkey) goto error; _drp = _tbc->rows; while(_drp) { _drp0 = _drp->next; if(dbt_row_match(_tbc, _drp, lkey, _o, _v, _n)) { // delete row if(_drp->prev) (_drp->prev)->next = _drp->next; else _tbc->rows = _drp->next; if(_drp->next) (_drp->next)->prev = _drp->prev; _tbc->nrrows--; // free row dbt_row_free(_tbc, _drp); ((dbt_con_p)_h->tail)->affected++; } _drp = _drp0; } 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); return 0; error: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); LM_ERR("failed to delete from 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_query(db1_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, db1_res_t** _r) { dbt_table_p _tbc = NULL; dbt_table_p _tbc_temp = NULL; dbt_row_p _drp = NULL; dbt_row_p *_res = NULL; // dbt_result_p _dres = NULL; int result = 0; int counter = 0; int i=0; int *lkey=NULL, *lres=NULL; db_key_t *_o_k=NULL; /* columns in order-by */ char *_o_op=NULL; /* operators for oder-by */ int _o_n; /* no of elements in order-by */ int *_o_l=NULL; /* column selection for order-by */ // int _o_nc; /* no of elements in _o_l but not lres */ if(_r) *_r = NULL; if ((!_h) || !CON_TABLE(_h)) { LM_ERR("invalid parameters\n"); return -1; } if (_o) { if (dbt_parse_orderbyclause(&_o_k, &_o_op, &_o_n, _o) < 0) return -1; } _tbc_temp = dbt_db_get_temp_table(DBT_CON_CONNECTION(_h)); if(!_tbc_temp) { LM_ERR("unable to allocate temp table\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); dbt_db_del_table(DBT_CON_CONNECTION(_h), &_tbc_temp->name, 0); return -1; } if(!_tbc || _tbc->nrcols < _nc) { LM_ERR("table %s not loaded! (too few columns)\n", CON_TABLE(_h)->s); goto error; } if(_k) { lkey = dbt_get_refs(_tbc, _k, _n); if(!lkey) goto error; } if(_c) { lres = dbt_get_refs(_tbc, _c, _nc); if(!lres) goto error; } if(_o_k) { _o_l = dbt_get_refs(_tbc, _o_k, _o_n); if (!_o_l) goto error; /* enlarge select-columns lres by all order-by columns, _o_nc is how many */ // if (dbt_mangle_columnselection(&lres, &_nc, &_o_nc, _o_l, _o_n) < 0) // goto error; } /* LM_DBG("new res with %d cols\n", _nc); _dres = dbt_result_new(_tbc, lres, _nc); if(!_dres) goto error; */ dbt_column_p pPrevCol = NULL; _tbc_temp->colv = (dbt_column_p*) shm_malloc(_nc*sizeof(dbt_column_p)); for(i=0; i < _nc; i++) { dbt_column_p pCol = dbt_column_new(_tbc->colv[ lres[i] ]->name.s, _tbc->colv[ lres[i] ]->name.len); pCol->type = _tbc->colv[ lres[i] ]->type; pCol->flag = _tbc->colv[ lres[i] ]->flag; if(pPrevCol) { pCol->prev = pPrevCol; pPrevCol->next = pCol; } else _tbc_temp->cols = pCol; _tbc_temp->colv[i] = pCol; pPrevCol = pCol; _tbc_temp->nrcols++; } _res = (dbt_row_p*) pkg_malloc(_db_text_max_result_rows * sizeof(dbt_row_p)); if(!_res) { LM_ERR("no more space to allocate for query rows\n"); goto error; } _drp = _tbc->rows; while(_drp && counter < _db_text_max_result_rows) { if(dbt_row_match(_tbc, _drp, lkey, _op, _v, _n)) { _res[counter] = _drp; // if(dbt_result_extract_fields(_tbc, _drp, lres, _dres)) // { // LM_ERR("failed to extract result fields!\n"); // goto clean; // } counter++; } _drp = _drp->next; } if (_o_l) { if (counter > 1) { if (dbt_sort_result_temp(_res, counter, _o_l, _o_op, _o_n) < 0) goto error; } /* last but not least, remove surplus columns */ // if (_o_nc) // dbt_project_result(_dres, _o_nc); } // copy results to temp table _tbc_temp->rows = dbt_result_extract_results(_tbc, _res, counter, lres, _nc); _tbc_temp->nrrows = (_tbc_temp->rows == NULL ? 0 : counter); dbt_table_update_flags(_tbc, DBT_TBFL_ZERO, DBT_FL_IGN, 1); /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); // DBT_CON_TEMP_TABLE(_h) = _tbc_temp; last_temp_table = _tbc_temp; // dbt_release_table(DBT_CON_CONNECTION(_h), &_tbc_temp->name); // dbt_result_print(_tbc_temp); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); if(_res) pkg_free(_res); if(_r) { result = dbt_get_result(_r, _tbc_temp); // dbt_db_del_table(DBT_CON_CONNECTION(_h), &_tbc_temp->name, 1); if(result != 0) dbt_result_free(_h, _tbc_temp); } return result; error: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); /* delete temp table */ dbt_db_del_table(DBT_CON_CONNECTION(_h), &_tbc_temp->name, 1); if(_res) pkg_free(_res); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); LM_ERR("failed to query the table!\n"); return -1; /* clean: dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); return -1; */ }
int dbt_query(db1_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, db1_res_t** _r) { dbt_table_p _tbc = NULL; dbt_row_p _drp = NULL; dbt_result_p _dres = NULL; int result = 0; int *lkey=NULL, *lres=NULL; db_key_t *_o_k=NULL; /* columns in order-by */ char *_o_op=NULL; /* operators for oder-by */ int _o_n; /* no of elements in order-by */ int *_o_l=NULL; /* column selection for order-by */ int _o_nc; /* no of elements in _o_l but not lres */ if ((!_h) || (!_r) || !CON_TABLE(_h)) { LM_ERR("invalid parameters\n"); return -1; } *_r = NULL; if (_o) { if (dbt_parse_orderbyclause(&_o_k, &_o_op, &_o_n, _o) < 0) 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 || _tbc->nrcols < _nc) { LM_ERR("table %s not loaded! (too few columns)\n", CON_TABLE(_h)->s); goto error; } if(_k) { lkey = dbt_get_refs(_tbc, _k, _n); if(!lkey) goto error; } if(_c) { lres = dbt_get_refs(_tbc, _c, _nc); if(!lres) goto error; } if(_o_k) { _o_l = dbt_get_refs(_tbc, _o_k, _o_n); if (!_o_l) goto error; /* enlarge select-columns lres by all order-by columns, _o_nc is how many */ if (dbt_mangle_columnselection(&lres, &_nc, &_o_nc, _o_l, _o_n) < 0) goto error; } LM_DBG("new res with %d cols\n", _nc); _dres = dbt_result_new(_tbc, lres, _nc); if(!_dres) goto error; _drp = _tbc->rows; while(_drp) { if(dbt_row_match(_tbc, _drp, lkey, _op, _v, _n)) { if(dbt_result_extract_fields(_tbc, _drp, lres, _dres)) { LM_ERR("failed to extract result fields!\n"); goto clean; } } _drp = _drp->next; } dbt_table_update_flags(_tbc, DBT_TBFL_ZERO, DBT_FL_IGN, 1); /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if (_o_l) { if (_dres->nrrows > 1) { if (dbt_sort_result(_dres, _o_l, _o_op, _o_n, lres, _nc) < 0) goto error_nounlock; } /* last but not least, remove surplus columns */ if (_o_nc) dbt_project_result(_dres, _o_nc); } /* dbt_result_print(_dres); */ if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); result = dbt_get_result(_r, _dres); if(result != 0) dbt_result_free(_dres); return result; error: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); error_nounlock: if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); if(_dres) dbt_result_free(_dres); LM_ERR("failed to query the table!\n"); return -1; clean: /* unlock database */ dbt_release_table(DBT_CON_CONNECTION(_h), CON_TABLE(_h)); if(lkey) pkg_free(lkey); if(lres) pkg_free(lres); if(_o_k) pkg_free(_o_k); if(_o_op) pkg_free(_o_op); if(_o_l) pkg_free(_o_l); if(_dres) dbt_result_free(_dres); return -1; }