Пример #1
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;
}
Пример #2
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;
}