Browse Source

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

Jason Penton 11 years ago
parent
commit
bb30c83e4e

+ 19 - 0
modules/db_mysql/doc/db_mysql_admin.xml

@@ -138,6 +138,25 @@ modparam("db_mysql", "auto_reconnect", 0)
 ...
 ...
 modparam("db_mysql", "insert_delayed", 1)
 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>
 </programlisting>
 		</example>
 		</example>
 	</section>
 	</section>

+ 1 - 0
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_timeout_interval = 2;   /* Default is 6 seconds */
 unsigned int db_mysql_auto_reconnect = 1;     /* Default is enabled   */
 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_insert_all_delayed = 0; /* Default is off */
+unsigned int db_mysql_update_affected_found = 0; /* Default is off */
 
 
 /* MODULE_VERSION */
 /* MODULE_VERSION */
 
 

+ 1 - 0
modules/db_mysql/km_db_mysql.h

@@ -43,6 +43,7 @@
 extern unsigned int db_mysql_timeout_interval;
 extern unsigned int db_mysql_timeout_interval;
 extern unsigned int db_mysql_auto_reconnect;
 extern unsigned int db_mysql_auto_reconnect;
 extern unsigned int db_mysql_insert_all_delayed;
 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);
 int db_mysql_bind_api(db_func_t *dbb);
 
 

+ 7 - 2
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;
 	struct my_con* ptr;
 	char *host, *grp;
 	char *host, *grp;
+	unsigned int connection_flag = 0;
 
 
 	if (!id) {
 	if (!id) {
 		LM_ERR("invalid parameter value\n");
 		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_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
 	mysql_options(ptr->con, MYSQL_OPT_WRITE_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_VERSION_ID >= 40100)
 	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
 	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
 #else
 	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
 	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
 #endif
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
 		/* increase error counter */
 		/* increase error counter */

+ 1 - 0
modules/db_mysql/mysql_mod.c

@@ -109,6 +109,7 @@ static param_export_t params[] = {
 	{"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
 	{"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
 	{"auto_reconnect",   INT_PARAM, &db_mysql_auto_reconnect},
 	{"auto_reconnect",   INT_PARAM, &db_mysql_auto_reconnect},
 	{"insert_delayed",   INT_PARAM, &db_mysql_insert_all_delayed},
 	{"insert_delayed",   INT_PARAM, &db_mysql_insert_all_delayed},
+	{"update_affected_found", INT_PARAM, &db_mysql_update_affected_found},
 	{0, 0, 0}
 	{0, 0, 0}
 };
 };