Browse code

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 authored on 09/10/2014 15:02:57
Showing 5 changed files
... ...
@@ -138,6 +138,25 @@ modparam("db_mysql", "auto_reconnect", 0)
138 138
 ...
139 139
 modparam("db_mysql", "insert_delayed", 1)
140 140
 ...
141
+</programlisting>
142
+		</example>
143
+	</section>
144
+        <section id="db_mysql.p.update_affected_found">
145
+		<title><varname>update_affected_found</varname> (integer)</title>
146
+		<para>
147
+		If set to 1, all UPDATE SQL queries will return the number of matched rows instead of the number of "updated" rows.
148
+		</para>
149
+		<para>
150
+		<emphasis>
151
+			Default value is 0 (1 - on / 0 - off).
152
+		</emphasis>
153
+		</para>
154
+		<example>
155
+		<title>Set <varname>update_affected_found</varname> parameter</title>
156
+		<programlisting format="linespecific">
157
+...
158
+modparam("db_mysql", "update_affected_found", 1)
159
+...
141 160
 </programlisting>
142 161
 		</example>
143 162
 	</section>
... ...
@@ -51,6 +51,7 @@
51 51
 unsigned int db_mysql_timeout_interval = 2;   /* Default is 6 seconds */
52 52
 unsigned int db_mysql_auto_reconnect = 1;     /* Default is enabled   */
53 53
 unsigned int db_mysql_insert_all_delayed = 0; /* Default is off */
54
+unsigned int db_mysql_update_affected_found = 0; /* Default is off */
54 55
 
55 56
 /* MODULE_VERSION */
56 57
 
... ...
@@ -43,6 +43,7 @@
43 43
 extern unsigned int db_mysql_timeout_interval;
44 44
 extern unsigned int db_mysql_auto_reconnect;
45 45
 extern unsigned int db_mysql_insert_all_delayed;
46
+extern unsigned int db_mysql_update_affected_found;
46 47
 
47 48
 int db_mysql_bind_api(db_func_t *dbb);
48 49
 
... ...
@@ -44,6 +44,7 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
44 44
 {
45 45
 	struct my_con* ptr;
46 46
 	char *host, *grp;
47
+	unsigned int connection_flag = 0;
47 48
 
48 49
 	if (!id) {
49 50
 		LM_ERR("invalid parameter value\n");
... ...
@@ -99,12 +100,16 @@ struct my_con* db_mysql_new_connection(const struct db_id* id)
99 100
 	mysql_options(ptr->con, MYSQL_OPT_READ_TIMEOUT, (const char *)&db_mysql_timeout_interval);
100 101
 	mysql_options(ptr->con, MYSQL_OPT_WRITE_TIMEOUT, (const char *)&db_mysql_timeout_interval);
101 102
 
103
+	if (db_mysql_update_affected_found) { 
104
+	    connection_flag |= CLIENT_FOUND_ROWS;
105
+	}
106
+	
102 107
 #if (MYSQL_VERSION_ID >= 40100)
103 108
 	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
104
-				id->database, id->port, 0, CLIENT_MULTI_STATEMENTS)) {
109
+				id->database, id->port, 0, connection_flag|CLIENT_MULTI_STATEMENTS)) {
105 110
 #else
106 111
 	if (!mysql_real_connect(ptr->con, host, id->username, id->password,
107
-				id->database, id->port, 0, 0)) {
112
+				id->database, id->port, 0, connection_flag)) {
108 113
 #endif
109 114
 		LM_ERR("driver error: %s\n", mysql_error(ptr->con));
110 115
 		/* increase error counter */
... ...
@@ -109,6 +109,7 @@ static param_export_t params[] = {
109 109
 	{"timeout_interval", INT_PARAM, &db_mysql_timeout_interval},
110 110
 	{"auto_reconnect",   INT_PARAM, &db_mysql_auto_reconnect},
111 111
 	{"insert_delayed",   INT_PARAM, &db_mysql_insert_all_delayed},
112
+	{"update_affected_found", INT_PARAM, &db_mysql_update_affected_found},
112 113
 	{0, 0, 0}
113 114
 };
114 115