[sr-dev] git:master: modules/db_postgres: Fixes

Peter Dunkley peter.dunkley at crocodile-rcs.com
Sun Jan 20 15:24:27 CET 2013


Module: sip-router
Branch: master
Commit: d934a2acd31408ae6afdf31007f2ebe4527d1f3d
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d934a2acd31408ae6afdf31007f2ebe4527d1f3d

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Sun Jan 20 14:23:17 2013 +0000

modules/db_postgres: Fixes

- Fixed submit_query() retry mechanism - connection errors now detected and retried.
- Fixed memory leak when raw queries contain > 1 SQL command.
- Fixes by Hugh Waite @ Crocodile RCS

---

 modules/db_postgres/km_dbase.c |   26 +++++++-------------------
 1 files changed, 7 insertions(+), 19 deletions(-)

diff --git a/modules/db_postgres/km_dbase.c b/modules/db_postgres/km_dbase.c
index bf393ed..21c9cea 100644
--- a/modules/db_postgres/km_dbase.c
+++ b/modules/db_postgres/km_dbase.c
@@ -166,6 +166,7 @@ static int db_postgres_submit_query(const db1_con_t* _con, const str* _s)
 	char *s=NULL;
 	int i, retries;
 	ExecStatusType pqresult;
+	PGresult *res = NULL;
 
 	if(! _con || !_s || !_s->s)
 	{
@@ -216,9 +217,14 @@ static int db_postgres_submit_query(const db1_con_t* _con, const str* _s)
 		/* exec the query */
 
 		if (PQsendQuery(CON_CONNECTION(_con), s)) {
+			/* Get the result of the query */
+			while ((res = PQgetResult(CON_CONNECTION(_con))) != NULL) {
+				db_postgres_free_query(_con);
+				CON_RESULT(_con) = res;
+			}
 			pqresult = PQresultStatus(CON_RESULT(_con));
 			if((pqresult!=PGRES_FATAL_ERROR)
-					|| (PQstatus(CON_CONNECTION(_con))==CONNECTION_OK))
+					&& (PQstatus(CON_CONNECTION(_con))==CONNECTION_OK))
 			{
 				LM_DBG("sending query ok: %p (%d) - [%.*s]\n",
 						_con, pqresult, _s->len, _s->s);
@@ -262,7 +268,6 @@ static int db_postgres_submit_query(const db1_con_t* _con, const str* _s)
 int db_postgres_fetch_result(const db1_con_t* _con, db1_res_t** _res, const int nrows)
 {
 	int rows;
-	PGresult *res = NULL;
 	ExecStatusType pqresult;
 
 	if (!_con || !_res || nrows < 0) {
@@ -283,14 +288,6 @@ int db_postgres_fetch_result(const db1_con_t* _con, db1_res_t** _res, const int
 		/* Allocate a new result structure */
 		*_res = db_new_result();
 
-		/* Get the result of the previous query */
-		while (1) {
-			if ((res = PQgetResult(CON_CONNECTION(_con)))) {
-				CON_RESULT(_con) = res;
-			} else {
-				break;
-			}
-		}
 		pqresult = PQresultStatus(CON_RESULT(_con));
 		LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con,
 			PQresStatus(pqresult), CON_RESULT(_con));
@@ -496,7 +493,6 @@ int db_postgres_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
  */
 int db_postgres_store_result(const db1_con_t* _con, db1_res_t** _r)
 {
-	PGresult *res = NULL;
 	ExecStatusType pqresult;
 	int rc = 0;
 
@@ -507,14 +503,6 @@ int db_postgres_store_result(const db1_con_t* _con, db1_res_t** _r)
 		goto done;
 	}
 
-	while (1) {
-		if ((res = PQgetResult(CON_CONNECTION(_con)))) {
-			CON_RESULT(_con) = res;
-		} else {
-			break;
-		}
-	}
-
 	pqresult = PQresultStatus(CON_RESULT(_con));
 	
 	LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con,




More information about the sr-dev mailing list