Пример #1
0
/*
 * Insert a row into specified table
 * _con: structure representing database connection
 * _k: key names
 * _v: values of the keys
 * _n: number of key=value pairs
 */
int db_postgres_insert(const db_con_t* _h, const db_key_t* _k, 
		const db_val_t* _v, const int _n)
{
	db_res_t* _r = NULL;

	CON_RESET_CURR_PS(_h); /* no prepared statements support */
	int tmp = db_do_insert(_h, _k, _v, _n, db_postgres_val2str,
		db_postgres_submit_query);

	if (submit_func_called)
	{
		/* finish the async query, 
		 * otherwise the next query will not complete */

		/* only call this if the DB API has effectively called
		 * our submit_query function
		 *
		 * in case of insert queueing,
		 * it may postpone calling the insert func until
		 * enough rows have piled up */
		if (db_postgres_store_result(_h, &_r) != 0)
			LM_WARN("unexpected result returned\n");

		submit_func_called = 0;
	}
	
	if (_r)
		db_free_result(_r);

	if (CON_HAS_INSLIST(_h))
		CON_RESET_INSLIST(_h);

	return tmp;
}
Пример #2
0
/*
 * Insert a row into specified table
 * _con: structure representing database connection
 * _k: key names
 * _v: values of the keys
 * _n: number of key=value pairs
 */
int db_postgres_insert(const db_con_t* _h, const db_key_t* _k,
		const db_val_t* _v, const int _n)
{
	db_res_t* _r = NULL;

	CON_RESET_CURR_PS(_h); /* no prepared statements support */

	/* This needs to be reset before each call to db_do_insert.
	   This is only used by inserts, but as a side effect delete and updates
	   will set it to 1 without resetting it. */
	submit_func_called = 0;

	int tmp = db_do_insert(_h, _k, _v, _n, db_postgres_val2str,
		db_postgres_submit_query);

	/* For bulk queries the insert may not be submitted until enough rows are queued */
	if (submit_func_called)
	{
		/* Query was submitted.
		   Result must be handled. */
		if (db_postgres_store_result(_h, &_r) != 0)
			LM_WARN("unexpected result returned\n");
	}

	if (_r)
		db_free_result(_r);

	if (CON_HAS_INSLIST(_h))
		CON_RESET_INSLIST(_h);

	return tmp;
}
Пример #3
0
int db_postgres_async_resume(db_con_t *_h, int fd, db_res_t **_r, void *_priv)
{
	struct pool_con *con = (struct pool_con *)_priv;
	PGresult *res = NULL;

#ifdef EXTRA_DEBUG
	if (!db_match_async_con(fd, _h)) {
		LM_BUG("no conn match for fd %d", fd);
		abort();
	}
#endif


	db_switch_to_async(_h, con);

	if( PQconsumeInput(CON_CONNECTION(_h)) == 0) {
		LM_ERR("Unable to consume input\n");
		db_switch_to_sync(_h);
		db_store_async_con(_h, con);
		return -1;
	}

	if(PQisBusy(CON_CONNECTION(_h))) {
		async_status = ASYNC_CONTINUE;

		db_switch_to_sync(_h);
		return 1;
	}

	while (1) {
		if ((res = PQgetResult(CON_CONNECTION(_h)))) {
			CON_RESULT(_h) = res;
		} else {
			break;
		}
	}

	if (_r) {
		if (db_postgres_store_result(_h, _r) != 0) {
			LM_ERR("failed to store result\n");
			db_switch_to_sync(_h);
			db_store_async_con(_h, con);
			return -2;
		}
	}

	db_switch_to_sync(_h);
	db_store_async_con(_h, con);

	return 0;
}
Пример #4
0
/*!
 * \brief Delete a row from the specified table
 * \param _h structure representing database connection
 * \param _k key names
 * \param _o operators
 * \param _v values of the keys that must match
 * \param _n number of key=value pairs
 * \return 0 on success, negative on failure
 */
int db_postgres_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
		const db_val_t* _v, const int _n)
{
	db1_res_t* _r = NULL;
	int tmp = db_do_delete(_h, _k, _o, _v, _n, db_postgres_val2str,
		db_postgres_submit_query);

	if (db_postgres_store_result(_h, &_r) != 0)
		LM_WARN("unexpected result returned");
	
	if (_r)
		db_free_result(_r);

	return tmp;
}
Пример #5
0
/*!
 * \brief Insert a row into specified table
 * \param _h structure representing database connection
 * \param _k key names
 * \param _v values of the keys
 * \param _n number of key=value pairs
 * \return 0 on success, negative on failure
 */
int db_postgres_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
		const int _n)
{
	db1_res_t* _r = NULL;

	int tmp = db_do_insert(_h, _k, _v, _n, db_postgres_val2str, db_postgres_submit_query);
	// finish the async query, otherwise the next query will not complete
	if (db_postgres_store_result(_h, &_r) != 0)
		LM_WARN("unexpected result returned");
	
	if (_r)
		db_free_result(_r);

	return tmp;
}
Пример #6
0
/*
 * Delete a row from the specified table
 * _con: structure representing database connection
 * _k: key names
 * _o: operators
 * _v: values of the keys that must match
 * _n: number of key=value pairs
 */
int db_postgres_delete(const db_con_t* _h, const db_key_t* _k,
		const db_op_t* _o, const db_val_t* _v, const int _n)
{
	db_res_t* _r = NULL;

	CON_RESET_CURR_PS(_h); /* no prepared statements support */
	int tmp = db_do_delete(_h, _k, _o, _v, _n, db_postgres_val2str,
		db_postgres_submit_query);

	if (db_postgres_store_result(_h, &_r) != 0)
		LM_WARN("unexpected result returned");
	
	if (_r)
		db_free_result(_r);

	return tmp;
}
Пример #7
0
/*!
 * Update some rows in the specified table
 * \param _h structure representing database connection
 * \param _k key names
 * \param _o operators
 * \param _v values of the keys that must match
 * \param _uk updated columns
 * \param _uv updated values of the columns
 * \param _n number of key=value pairs
 * \param _un number of columns to update
 * \return 0 on success, negative on failure
 */
int db_postgres_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
		const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
		const int _un)
{
	db1_res_t* _r = NULL;
	int ret = db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_postgres_val2str,
		db_postgres_submit_query);
	int tmp = db_postgres_store_result(_h, &_r);

	if (tmp < 0) {
		LM_WARN("unexpected result returned");
		ret = tmp;
	}
	
	if (_r)
		db_free_result(_r);

	return ret;
}