[sr-dev] git:master: modules/db_postgres: Added affect_rows() API to db_postgres

Peter Dunkley peter.dunkley at crocodile-rcs.com
Mon Mar 12 23:01:47 CET 2012


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

Author: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Committer: Peter Dunkley <peter.dunkley at crocodile-rcs.com>
Date:   Mon Mar 12 21:31:51 2012 +0000

modules/db_postgres: Added affect_rows() API to db_postgres

---

 modules/db_postgres/km_db_postgres.c |    1 +
 modules/db_postgres/km_dbase.c       |   19 +++++++++++++++++++
 modules/db_postgres/km_dbase.h       |    4 ++++
 modules/db_postgres/km_pg_con.h      |    3 ++-
 4 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/modules/db_postgres/km_db_postgres.c b/modules/db_postgres/km_db_postgres.c
index 9b0bd8b..5914e5f 100644
--- a/modules/db_postgres/km_db_postgres.c
+++ b/modules/db_postgres/km_db_postgres.c
@@ -93,6 +93,7 @@ int db_postgres_bind_api(db_func_t *dbb)
 	dbb->insert           = db_postgres_insert;
 	dbb->delete           = db_postgres_delete; 
 	dbb->update           = db_postgres_update;
+	dbb->affected_rows    = db_postgres_affected_rows;
 
 	return 0;
 }
diff --git a/modules/db_postgres/km_dbase.c b/modules/db_postgres/km_dbase.c
index c01d37a..a7b9546 100644
--- a/modules/db_postgres/km_dbase.c
+++ b/modules/db_postgres/km_dbase.c
@@ -69,6 +69,7 @@
 
 #include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include "../../dprint.h"
 #include "../../mem/mem.h"
 #include "../../lib/srdb1/db.h"
@@ -425,11 +426,14 @@ int db_postgres_store_result(const db1_con_t* _con, db1_res_t** _r)
 	LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con,
 		PQresStatus(pqresult), CON_RESULT(_con));
 
+	CON_AFFECTED(_con) = 0;
+
 	switch(pqresult) {
 		case PGRES_COMMAND_OK:
 		/* Successful completion of a command returning no data
 		 * (such as INSERT or UPDATE). */
 		rc = 0;
+		CON_AFFECTED(_con) = atoi(PQcmdTuples(CON_RESULT(_con)));
 		break;
 
 		case PGRES_TUPLES_OK:
@@ -444,6 +448,7 @@ int db_postgres_store_result(const db1_con_t* _con, db1_res_t** _r)
 				break;
 			}
 			rc =  0;
+			CON_AFFECTED(_con) = atoi(PQcmdTuples(CON_RESULT(_con)));
 			break;
 		/* query failed */
 		case PGRES_FATAL_ERROR:
@@ -557,6 +562,20 @@ int db_postgres_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _
 	return tmp;
 }
 
+/**
+ * Returns the affected rows of the last query.
+ * \param _h database handle
+ * \return returns the affected rows as integer or -1 on error.
+ */
+int db_postgres_affected_rows(const db1_con_t* _h)
+{
+	if (!_h) {
+		LM_ERR("invalid parameter value\n");
+		return -1;
+	}
+	return CON_AFFECTED(_h);
+}
+
 
 /*!
  * Store name of table that will be used by subsequent database functions
diff --git a/modules/db_postgres/km_dbase.h b/modules/db_postgres/km_dbase.h
index 0e8a20b..7c15e8d 100644
--- a/modules/db_postgres/km_dbase.h
+++ b/modules/db_postgres/km_dbase.h
@@ -102,6 +102,10 @@ int db_postgres_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _
  */
 int db_postgres_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows);
 
+/*
+ * number of rows affected by the last DB query/statement
+ */
+int db_postgres_affected_rows(const db1_con_t* _h);
 
 /*
  * Store name of table that will be used by
diff --git a/modules/db_postgres/km_pg_con.h b/modules/db_postgres/km_pg_con.h
index 798a4af..b0f92c6 100644
--- a/modules/db_postgres/km_pg_con.h
+++ b/modules/db_postgres/km_pg_con.h
@@ -53,7 +53,7 @@ struct pg_con {
 	PGresult *res;		/*!< this is the current result */
 	char**  row;		/*!< Actual row in the result */
 	time_t timestamp;	/*!< Timestamp of last query */
-
+	int affected_rows;	/*!< Number of rows affected by the last statement */
 };
 
 #define CON_SQLURL(db_con)     (((struct pg_con*)((db_con)->tail))->sqlurl)
@@ -63,6 +63,7 @@ struct pg_con {
 #define CON_ROW(db_con)	       (((struct pg_con*)((db_con)->tail))->row)
 #define CON_TIMESTAMP(db_con)  (((struct pg_con*)((db_con)->tail))->timestamp)
 #define CON_ID(db_con) 	       (((struct pg_con*)((db_con)->tail))->id)
+#define CON_AFFECTED(db_con)   (((struct pg_con*)((db_con)->tail))->affected_rows)
 
 /*
  * Create a new connection structure,




More information about the sr-dev mailing list