فهرست منبع

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.
Alex Hermann 14 سال پیش
والد
کامیت
d947e7528f
5فایلهای تغییر یافته به همراه93 افزوده شده و 1 حذف شده
  1. 20 0
      modules_k/sqlops/README
  2. 24 1
      modules_k/sqlops/doc/sqlops_admin.xml
  3. 43 0
      modules_k/sqlops/sql_api.c
  4. 4 0
      modules_k/sqlops/sql_api.h
  5. 2 0
      modules_k/sqlops/sqlops.c

+ 20 - 0
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");
+...

+ 24 - 1
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>
 

+ 43 - 0
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;

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

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