Esempio n. 1
0
/*
 * Free all memory allocated by get_result
 */
int dbt_free_query(db_con_t* _h, db_res_t* _r)
{
	if ((!_h) || (!_r))
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_free_query: Invalid parameter value\n");
#endif
		return -1;
	}

	if(dbt_free_result(_r) < 0) 
	{
		LOG(L_ERR,"DBT:dbt_free_query:Unable to free result structure\n");
		return -1;
	}

	
	if(dbt_result_free(DBT_CON_RESULT(_h)) < 0) 
	{
		LOG(L_ERR, "DBT:dbt_free_query: Unable to free internal structure\n");
		return -1;
	}
	DBT_CON_RESULT(_h) = NULL;
	return 0;
}
Esempio n. 2
0
/*
 * Get and convert columns from a result
 */
int dbt_get_columns(db_con_t* _h, db_res_t* _r)
{
	int n, i;
	
	if ((!_h) || (!_r)) 
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_get_columns: Invalid parameter\n");
#endif
		return -1;
	}
	
	n = DBT_CON_RESULT(_h)->nrcols;
	if (!n) 
	{
		LOG(L_ERR, "DBT:get_columns: No columns\n");
		return -2;
	}
	
	RES_NAMES(_r) = (db_key_t*)pkg_malloc(sizeof(db_key_t) * n);
	if (!RES_NAMES(_r)) 
	{
		LOG(L_ERR, "DBT:get_columns: No memory left\n");
		return -3;
	}

	RES_TYPES(_r) = (db_type_t*)pkg_malloc(sizeof(db_type_t) * n);
	if (!RES_TYPES(_r)) 
	{
		LOG(L_ERR, "DBT:get_columns: No memory left\n");
		pkg_free(RES_NAMES(_r));
		return -4;
	}

	RES_COL_N(_r) = n;

	for(i = 0; i < n; i++) 
	{
		RES_NAMES(_r)[i] = DBT_CON_RESULT(_h)->colv[i].name.s;
		switch( DBT_CON_RESULT(_h)->colv[i].type) 
		{
			case DB_INT:
			case DB_DATETIME:
				RES_TYPES(_r)[i] = DB_INT;
			break;

			case DB_DOUBLE:
				RES_TYPES(_r)[i] = DB_DOUBLE;
			break;

			default:
				RES_TYPES(_r)[i] = DB_STR;
			break;
		}		
	}
	return 0;
}
Esempio n. 3
0
/*
 * Convert rows from internal to db API representation
 */
int dbt_convert_rows(db_con_t* _h, db_res_t* _r)
{
	int n, i;
	dbt_row_p _rp = NULL;
	if ((!_h) || (!_r)) 
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_convert_rows: Invalid parameter\n");
#endif
		return -1;
	}
	n = DBT_CON_RESULT(_h)->nrrows;
	RES_ROW_N(_r) = n;
	if (!n) 
	{
		RES_ROWS(_r) = 0;
		return 0;
	}
	RES_ROWS(_r) = (struct db_row*)pkg_malloc(sizeof(db_row_t) * n);
	if (!RES_ROWS(_r)) 
	{
		LOG(L_ERR, "DBT:dbt_convert_rows: No memory left\n");
		return -2;
	}
	i = 0;
	_rp = DBT_CON_RESULT(_h)->rows;
	while(_rp)
	{
		DBT_CON_ROW(_h) = _rp;
		if (!DBT_CON_ROW(_h)) 
		{
			LOG(L_ERR, "DBT:dbt_convert_rows: error getting current row\n");
			RES_ROW_N(_r) = i;
			dbt_free_rows(_r);
			return -3;
		}
		if (dbt_convert_row(_h, _r, &(RES_ROWS(_r)[i])) < 0) 
		{
			LOG(L_ERR, "DBT:dbt_convert_rows: Error while converting"
				" row #%d\n", i);
			RES_ROW_N(_r) = i;
			dbt_free_rows(_r);
			return -4;
		}
		i++;
		_rp = _rp->next;
	}
	return 0;
}
Esempio n. 4
0
/*
 * Retrieve result set
 */
int dbt_get_result(db_con_t* _h, db_res_t** _r)
{
	if (!_h || !_r) {
		LM_ERR("invalid parameter value\n");
		return -1;
	}

	if (!DBT_CON_RESULT(_h))
	{
		LM_ERR("failed to get result\n");
		*_r = 0;
		return -3;
	}

	*_r = db_new_result();
	if (*_r == 0)
	{
		LM_ERR("no private memory left\n");
		return -2;
	}

	if (dbt_convert_result(_h, *_r) < 0)
	{
		LM_ERR("failed to convert result\n");
		pkg_free(*_r);
		return -4;
	}

	return 0;
}
Esempio n. 5
0
/*
 * Retrieve result set
 */
int dbt_get_result(db_con_t* _h, db_res_t** _r)
{
	if ((!_h) || (!_r)) 
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_get_result: Invalid parameter value\n");
#endif
		return -1;
	}

	if (!DBT_CON_RESULT(_h))
	{
		LOG(L_ERR, "DBT:dbt_get_result: error getting result\n");
		*_r = 0;
		return -3;
	}

	*_r = dbt_new_result();
	if (*_r == 0) 
	{
		LOG(L_ERR, "DBT:dbt_get_result: No memory left\n");
		return -2;
	}

	if (dbt_convert_result(_h, *_r) < 0) 
	{
		LOG(L_ERR, "DBT:dbt_get_result: Error while converting result\n");
		pkg_free(*_r);
		return -4;
	}
	
	return 0;
}
Esempio n. 6
0
/*
 * Close a database connection
 */
void dbt_close(db_con_t* _h)
{
	if (!_h) 
	{
#ifdef DBT_EXTRA_DEBUG
		LOG(L_ERR, "DBT:dbt_close: Invalid parameter value\n");
#endif
		return;
	}
	
	if (DBT_CON_RESULT(_h)) 
		dbt_result_free(DBT_CON_RESULT(_h));
	
	pkg_free(_h);
    return;
}
Esempio n. 7
0
/*
 * Convert rows from internal to db API representation
 */
static int dbt_convert_rows(db_con_t* _h, db_res_t* _r)
{
	int col;
	dbt_row_p _rp = NULL;
	if (!_h || !_r) {
		LM_ERR("invalid parameter\n");
		return -1;
	}
	RES_ROW_N(_r) = DBT_CON_RESULT(_h)->nrrows;
	if (!RES_ROW_N(_r)) {
		return 0;
	}

	if (db_allocate_rows( _r, RES_ROW_N(_r))!=0) {
		LM_ERR("no private memory left\n");
		return -2;
	}

	col = 0;
	_rp = DBT_CON_RESULT(_h)->rows;
	while(_rp) {
		DBT_CON_ROW(_h) = _rp;
		if (!DBT_CON_ROW(_h)) {
			LM_ERR("failed to get current row\n");
			RES_ROW_N(_r) = col;
			db_free_rows(_r);
			return -3;
		}
		if (dbt_convert_row(_h, _r, &(RES_ROWS(_r)[col])) < 0) {
			LM_ERR("failed to convert row #%d\n", col);
			RES_ROW_N(_r) = col;
			db_free_rows(_r);
			return -4;
		}
		col++;
		_rp = _rp->next;
	}
	return 0;
}
Esempio n. 8
0
/*
 * Get and convert columns from a result
 */
static int dbt_get_columns(db_con_t* _h, db_res_t* _r)
{
	int col;

	if (!_h || !_r) {
		LM_ERR("invalid parameter\n");
		return -1;
	}

	RES_COL_N(_r) = DBT_CON_RESULT(_h)->nrcols;
	if (!RES_COL_N(_r)) {
		LM_ERR("no columns\n");
		return -2;
	}
	if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
		LM_ERR("could not allocate columns\n");
		return -3;
	}

	for(col = 0; col < RES_COL_N(_r); col++) {

		RES_NAMES(_r)[col]->s = DBT_CON_RESULT(_h)->colv[col].name.s;
		RES_NAMES(_r)[col]->len = DBT_CON_RESULT(_h)->colv[col].name.len;

		switch(DBT_CON_RESULT(_h)->colv[col].type)
		{
			case DB_STR:
			case DB_STRING:
			case DB_BLOB:
			case DB_INT:
			case DB_BIGINT:
			case DB_DATETIME:
			case DB_DOUBLE:
				RES_TYPES(_r)[col] = DBT_CON_RESULT(_h)->colv[col].type;
			break;
			default:
				LM_WARN("unhandled data type column (%.*s) type id (%d), "
						"use STR as default\n", RES_NAMES(_r)[col]->len,
						RES_NAMES(_r)[col]->s, DBT_CON_RESULT(_h)->colv[col].type);
				RES_TYPES(_r)[col] = DB_STR;
			break;
		}
	}
	return 0;
}
Esempio n. 9
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;
}