Example #1
0
/*
 * 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;
}
Example #2
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;
}
Example #3
0
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;
*/
}
Example #4
0
/*
 * 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;
}
Example #5
0
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;
}
Example #6
0
/*
 * 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;
}
Example #7
0
/*
 * 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;
}
Example #8
0
int dbt_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)
{
	tbl_cache_p _tbc = NULL;
	dbt_table_p _dtp = NULL;
	dbt_row_p _drp = NULL;
	dbt_result_p _dres = NULL;
	
	str stbl;
	int *lkey=NULL, *lres=NULL;
	
	if ((!_h) || (!_r) || !CON_TABLE(_h))
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_query: 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_query: table does not exist!\n");
		return -1;
	}

	lock_get(&_tbc->sem);
	_dtp = _tbc->dtp;

	if(!_dtp || _dtp->nrcols < _nc)
	{
		DBG("DBT:dbt_query: table not loaded!\n");
		goto error;
	}
	if(_k)
	{
		lkey = dbt_get_refs(_dtp, _k, _n);
		if(!lkey)
			goto error;
	}
	if(_c)
	{
		lres = dbt_get_refs(_dtp, _c, _nc);
		if(!lres)
			goto error;
	}

	DBG("DBT:dbt_query: new res with %d cols\n", _nc);
	_dres = dbt_result_new(_dtp, lres, _nc);
	
	if(!_dres)
		goto error;
	
	_drp = _dtp->rows;
	while(_drp)
	{
		if(dbt_row_match(_dtp, _drp, lkey, _op, _v, _n))
		{
			if(dbt_result_extract_fields(_dtp, _drp, lres, _dres))
			{
				DBG("DBT:dbt_query: error extracting result fields!\n");
				goto clean;
			}
		}
		_drp = _drp->next;
	}

	dbt_table_update_flags(_dtp, DBT_TBFL_ZERO, DBT_FL_IGN, 1);
	
	lock_release(&_tbc->sem);

#ifdef DBT_EXTRA_DEBUG
	dbt_result_print(_dres);
#endif
	
	DBT_CON_RESULT(_h) = _dres;
	
	if(lkey)
		pkg_free(lkey);
	if(lres)
		pkg_free(lres);

	return dbt_get_result(_h, _r);

error:
	lock_release(&_tbc->sem);
	if(lkey)
		pkg_free(lkey);
	if(lres)
		pkg_free(lres);
	DBG("DBT:dbt_query: error while querying table!\n");
    
	return -1;

clean:
	lock_release(&_tbc->sem);
	if(lkey)
		pkg_free(lkey);
	if(lres)
		pkg_free(lres);
	if(_dres)
		dbt_result_free(_dres);
	DBG("DBT:dbt_query: make clean\n");

	return -1;
}