コード例 #1
0
ファイル: dbt_tb.c プロジェクト: bluemutedwisdom/OpenSIPS
int dbt_table_free_rows(dbt_table_p _dtp)
{
    dbt_row_p _rp=NULL, _rp0=NULL;

    if(!_dtp || !_dtp->rows || !_dtp->colv)
        return -1;
    _rp = _dtp->rows;
    while(_rp)
    {
        _rp0=_rp;
        _rp=_rp->next;
        dbt_row_free(_dtp, _rp0);
    }

    dbt_table_update_flags(_dtp, DBT_TBFL_MODI, DBT_FL_SET, 1);

    _dtp->rows = NULL;
    _dtp->nrrows = 0;

    return 0;
}
コード例 #2
0
ファイル: dbt_base.c プロジェクト: TheGrandWazoo/kamailio
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;
}
コード例 #3
0
ファイル: dbt_base.c プロジェクト: TheGrandWazoo/kamailio
/*
 * 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;
}
コード例 #4
0
ファイル: dbt_base.c プロジェクト: TheGrandWazoo/kamailio
/*
 * 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;
}
コード例 #5
0
ファイル: dbt_base.c プロジェクト: Gaoithe/openimscore_ims
/*
 * 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;
}
コード例 #6
0
ファイル: dbt_base.c プロジェクト: Gaoithe/openimscore_ims
/*
 * 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;
}