Просмотр исходного кода

modules/db_postgres: Added affect_rows() API to db_postgres

Peter Dunkley 13 лет назад
Родитель
Сommit
2bd922dba3

+ 1 - 0
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;
 }

+ 19 - 0
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

+ 4 - 0
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

+ 2 - 1
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,