[sr-dev] git:master: modules_k/sqlops: new PV $sqlrows return affected rows of previous query

Alex Hermann alex at speakup.nl
Thu Aug 18 09:50:52 CEST 2011


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

Author: Alex Hermann <alex at speakup.nl>
Committer: Alex Hermann <alex at speakup.nl>
Date:   Tue Aug  9 12:31:48 2011 +0200

modules_k/sqlops: new PV $sqlrows return affected rows of previous query

The PV $sqlrows(<con>) returns the number of affected rows of the
previous UPDATE, INSERT or DELETE query on the specified connection.

---

 modules_k/sqlops/README               |   20 +++++++++++++++
 modules_k/sqlops/doc/sqlops_admin.xml |   25 ++++++++++++++++++-
 modules_k/sqlops/sql_api.c            |   43 +++++++++++++++++++++++++++++++++
 modules_k/sqlops/sql_api.h            |    4 +++
 modules_k/sqlops/sqlops.c             |    2 +
 5 files changed, 93 insertions(+), 1 deletions(-)

diff --git a/modules_k/sqlops/README b/modules_k/sqlops/README
index 75379b9..d0de106 100644
--- a/modules_k/sqlops/README
+++ b/modules_k/sqlops/README
@@ -38,6 +38,7 @@ Daniel-Constantin Mierla
         5. Exported pseudo-variables
 
               5.1. $dbr(result=>key)
+              5.2. $sqlrows(con)
 
    List of Examples
 
@@ -47,6 +48,7 @@ Daniel-Constantin Mierla
    1.4. sql_xquery() usage
    1.5. sql_result_free() usage
    1.6. $dbr(result=>key) usage
+   1.7. $sqlrows(con) usage
 
 Chapter 1. Admin Guide
 
@@ -72,6 +74,7 @@ Chapter 1. Admin Guide
    5. Exported pseudo-variables
 
         5.1. $dbr(result=>key)
+        5.2. $sqlrows(con)
 
 1. Overview
 
@@ -224,6 +227,7 @@ sql_result_free("ra");
 5. Exported pseudo-variables
 
    5.1. $dbr(result=>key)
+   5.2. $sqlrows(con)
 
 5.1. $dbr(result=>key)
 
@@ -290,3 +294,19 @@ if (sql_xquery("ca", "select * from domain", "ra") == 1)
     }
 }
 ...
+
+5.2. $sqlrows(con)
+
+   Number of affected rows of the previous query on the specified
+   connection. Its primary use is to get the number of rows affected by
+   UPDATE, INSERT and DELETE queries.
+
+   “con” must be the name identifying an SQL connection.
+
+   Example 1.7. $sqlrows(con) usage
+...
+modparam("sqlops","sqlcon","ca=>dbdriver://username:password@dbhost/dbname")
+...
+sql_query("ca", "update domain set domain='mydomain' where id=5");
+xlog("Affected rows: $sqlrows(ca)\n");
+...
diff --git a/modules_k/sqlops/doc/sqlops_admin.xml b/modules_k/sqlops/doc/sqlops_admin.xml
index a0a8512..f8bd3e6 100644
--- a/modules_k/sqlops/doc/sqlops_admin.xml
+++ b/modules_k/sqlops/doc/sqlops_admin.xml
@@ -379,7 +379,30 @@ if (sql_xquery("ca", "select * from domain", "ra") == 1)
 ...
 				</programlisting>
 			</example>
-	</section>
+		</section>
+		<section>
+			<title><varname>$sqlrows(con)</varname></title>
+			<para>
+				Number of affected rows of the previous query on the
+				specified connection. Its primary use is to get the number
+				of rows affected by UPDATE, INSERT and DELETE queries.
+			</para>
+			<para>
+				<quote>con</quote> must be the name identifying an SQL
+				connection.
+			</para>
+			<example>
+				<title><function moreinfo="none">$sqlrows(con)</function> usage</title>
+				<programlisting format="linespecific">
+...
+modparam("sqlops","sqlcon","ca=&gt;&exampledb;")
+...
+sql_query("ca", "update domain set domain='mydomain' where id=5");
+xlog("Affected rows: $sqlrows(ca)\n");
+...
+				</programlisting>
+			</example>
+		</section>
 	</section>
 </chapter>
 
diff --git a/modules_k/sqlops/sql_api.c b/modules_k/sqlops/sql_api.c
index ef69071..a61b00a 100644
--- a/modules_k/sqlops/sql_api.c
+++ b/modules_k/sqlops/sql_api.c
@@ -96,6 +96,49 @@ int sql_init_con(str *name, str *url)
 	return 0;
 }
 
+int pv_parse_con_name(pv_spec_p sp, str *in)
+{
+	sql_con_t *con;
+
+	if(sp==NULL || in==NULL || in->len<=0)
+		return -1;
+
+	con = sql_get_connection(in);
+	if (con==NULL) {
+		LM_ERR("invalid connection [%.*s]\n", in->len, in->s);
+		return -1;
+	}
+
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
+	sp->pvp.pvn.u.isname.type = AVP_VAL_STR;
+	sp->pvp.pvn.u.isname.name.s = *in;
+	return 0;
+}
+
+int pv_get_sqlrows(struct sip_msg *msg,  pv_param_t *param,
+		pv_value_t *res)
+{
+	sql_con_t *con;
+	str* sc;
+
+	sc = &param->pvn.u.isname.name.s;
+	con = sql_get_connection(sc);
+	if(con==NULL)
+	{
+		LM_ERR("invalid connection [%.*s]\n", sc->len, sc->s);
+		return -1;
+	}
+
+	if (!DB_CAPABILITY(con->dbf, DB_CAP_AFFECTED_ROWS))
+	{
+		LM_ERR("con: %p database module does not have DB_CAP_AFFECTED_ROWS [%.*s]\n",
+		       con, sc->len, sc->s);
+		return -1;
+	}
+
+	return pv_get_sintval(msg, param, res, con->dbf.affected_rows(con->dbh));
+}
+
 int sql_connect(void)
 {
 	sql_con_t *sc;
diff --git a/modules_k/sqlops/sql_api.h b/modules_k/sqlops/sql_api.h
index ce00672..c198eee 100644
--- a/modules_k/sqlops/sql_api.h
+++ b/modules_k/sqlops/sql_api.h
@@ -77,6 +77,10 @@ int sql_do_query(sql_con_t *con, str *query, sql_result_t *res);
 int sql_do_xquery(struct sip_msg *msg, sql_con_t *con, pv_elem_t *query,
 		pv_elem_t *res);
 #endif
+int pv_get_sqlrows(struct sip_msg *msg,  pv_param_t *param,
+		pv_value_t *res);
+int pv_parse_con_name(pv_spec_p sp, str *in);
+
 sql_con_t* sql_get_connection(str *name);
 sql_result_t* sql_get_result(str *name);
 
diff --git a/modules_k/sqlops/sqlops.c b/modules_k/sqlops/sqlops.c
index a34a7d9..5bb4b2f 100644
--- a/modules_k/sqlops/sqlops.c
+++ b/modules_k/sqlops/sqlops.c
@@ -80,6 +80,8 @@ static int sql_res_param(modparam_t type, void* val);
 static pv_export_t mod_pvs[] = {
 	{ {"dbr", sizeof("dbr")-1}, PVT_OTHER, pv_get_dbr, 0,
 		pv_parse_dbr_name, 0, 0, 0 },
+	{ {"sqlrows", sizeof("sqlrows")-1}, PVT_OTHER, pv_get_sqlrows, 0,
+		pv_parse_con_name, 0, 0, 0 },
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
 };
 




More information about the sr-dev mailing list