[sr-dev] git:master: modules/db_mysql: added parameter to change affected rows value for UPDATE queries
Jason Penton
jason.penton at gmail.com
Thu Oct 9 17:03:51 CEST 2014
Module: sip-router
Branch: master
Commit: bb30c83e4ed3594852c6251558b4c441bb5b6e19
URL: http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=bb30c83e4ed3594852c6251558b4c441bb5b6e19
Author: Jason Penton <jason.penton at gmail.com>
Committer: Jason Penton <jason.penton at gmail.com>
Date: Thu Oct 9 17:02:57 2014 +0200
modules/db_mysql: added parameter to change affected rows value for UPDATE queries
- enabled will return the number of matched/foudn rows as opposed to the number of updated rows
---
modules/db_mysql/doc/db_mysql_admin.xml | 19 +++++++++++++++++++
modules/db_mysql/km_db_mysql.c | 1 +
modules/db_mysql/km_db_mysql.h | 1 +
modules/db_mysql/km_my_con.c | 9 +++++++--
modules/db_mysql/mysql_mod.c | 1 +
5 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/modules/db_mysql/doc/db_mysql_admin.xml b/modules/db_mysql/doc/db_mysql_admin.xml
index 738c464..fdc1a5c 100644
--- a/modules/db_mysql/doc/db_mysql_admin.xml
+++ b/modules/db_mysql/doc/db_mysql_admin.xml
@@ -141,6 +141,25 @@ modparam("db_mysql", "insert_delayed", 1)
</programlisting>
</example>
</section>
+ <section id="db_mysql.p.update_affected_found">
+ <title><varname>update_affected_found</varname> (integer)</title>
+ <para>
+ If set to 1, all UPDATE SQL queries will return the number of matched rows instead of the number of "updated" rows.
+ </para>
+ <para>
+ <emphasis>
+ Default value is 0 (1 - on / 0 - off).
+ </emphasis>
+ </para>
+ <example>
+ <title>Set <varname>update_affected_found</varname> parameter</title>
+ <programlisting format="linespecific">
+...
+modparam("db_mysql", "update_affected_found", 1)
+...
+</programlisting>
+ </example>
+ </section>
</section>
<section>
<title>Functions</title>
diff --git a/modules/db_mysql/km_db_mysql.c b/modules/db_mysql/km_db_mysql.c
index ab39ff0..d360650 100644
--- a/modules/db_mysql/km_db_mysql.c
+++ b/modules/db_mysql/km_db_mysql.c
@@ -51,6 +51,7 @@
unsigned int db_mysql_timeout_interval = 2; /* Default is 6 seconds */
unsigned int db_mysql_auto_reconnect = 1; /* Default is enabled */
unsigned int db_mysql_insert_all_delayed = 0; /* Default is off */
+unsigned int db_mysql_update_affected_found = 0; /* Default is off */
/* MODULE_VERSION */
diff --git a/modules/db_mysql/km_db_mysql.h b/modules/db_mysql/km_db_mysql.h
index f817765..cb51b55 100644
--- a/modules/db_mysql/km_db_mysql.h
+++ b/modules/db_mysql/km_db_mysql.h
@@ -43,6 +43,7 @@
extern unsigned int db_mysql_timeout_interval;
extern unsigned int db_mysql_auto_reconnect;
extern unsigned int db_mysql_insert_all_delayed;
+extern unsigned int db_mysql_update_affected_found;
int db_mysql_bind_api(db_func_t *dbb);
diff --git a/modules/db_mysql/km_my_con.c b/modules/db_mysql/km_my_con.c
index 4d29c6c..d0338a4 100644
--- a/modules/db_mysql/km_my_con.c
+++ b/modules/db_mysql/km_my_con.c
@@ -44,6 +44,7 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
{
struct my_con* ptr;
char *host, *grp;
+ unsigned int connection_flag = 0;
if (!id) {
LM_ERR("invalid parameter value\n");
@@ -99,12 +100,16 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
+ if (db_mysql_update_affected_found) {
+ connection_flag |= CLIENT_FOUND_ROWS;
+ }
+
#if (MYSQL_VERSION_ID >= 40100)
if (!mysql_real_connect(ptr->con, host, id->username, id->password,
- id->database, id->port, 0, CLIENT_MULTI_STATEMENTS)) {
+ id->database, id->port, 0, connection_flag|CLIENT_MULTI_STATEMENTS)) {
#else
if (!mysql_real_connect(ptr->con, host, id->username, id->password,
- id->database, id->port, 0, 0)) {
+ id->database, id->port, 0, connection_flag)) {
#endif
LM_ERR("driver error: %s\n", mysql_error(ptr->con));
/* increase error counter */
diff --git a/modules/db_mysql/mysql_mod.c b/modules/db_mysql/mysql_mod.c
index 8d369c7..664f258 100644
--- a/modules/db_mysql/mysql_mod.c
+++ b/modules/db_mysql/mysql_mod.c
@@ -109,6 +109,7 @@ static param_export_t params[] = {
{"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
{"auto_reconnect", INT_PARAM, &db_mysql_auto_reconnect},
{"insert_delayed", INT_PARAM, &db_mysql_insert_all_delayed},
+ {"update_affected_found", INT_PARAM, &db_mysql_update_affected_found},
{0, 0, 0}
};
More information about the sr-dev
mailing list