Bläddra i källkod

usrloc(k): option to check if contact DB UPDATE was successful

- use DB API affected_rows() when available to detect if the DB UPDATE
  operation for a contact was successful, if not, do an INSERT instead
- behaviour controlled by parameter db_check_update, default is 0 (no
  check for affected rows and no insert -- backward compatible)
- closes items FS#41 and FS#226
Daniel-Constantin Mierla 13 år sedan
förälder
incheckning
4b68b3ca0a

+ 25 - 6
modules_k/usrloc/README

@@ -64,7 +64,8 @@ Bogdan-Andrei Iancu
               3.26. hash_size (integer)
               3.27. preload (string)
               3.28. db_update_as_insert (string)
-              3.29. timer_procs (string)
+              3.29. db_check_update (string)
+              3.30. timer_procs (string)
 
         4. Functions
         5. MI Commands
@@ -137,7 +138,8 @@ Bogdan-Andrei Iancu
    1.26. Set hash_size parameter
    1.27. Set preload parameter
    1.28. Set db_update_as_insert parameter
-   1.29. Set timer_procs parameter
+   1.29. Set db_check_update parameter
+   1.30. Set timer_procs parameter
 
 Chapter 1. Admin Guide
 
@@ -182,7 +184,8 @@ Chapter 1. Admin Guide
         3.26. hash_size (integer)
         3.27. preload (string)
         3.28. db_update_as_insert (string)
-        3.29. timer_procs (string)
+        3.29. db_check_update (string)
+        3.30. timer_procs (string)
 
    4. Functions
    5. MI Commands
@@ -287,7 +290,8 @@ Chapter 1. Admin Guide
    3.26. hash_size (integer)
    3.27. preload (string)
    3.28. db_update_as_insert (string)
-   3.29. timer_procs (string)
+   3.29. db_check_update (string)
+   3.30. timer_procs (string)
 
 3.1. nat_bflag (integer)
 
@@ -667,7 +671,22 @@ modparam("usrloc", "preload", "location")
 modparam("usrloc", "db_update_as_insert", 1)
 ...
 
-3.29. timer_procs (string)
+3.29. db_check_update (string)
+
+   Set this parameter to 1 if you want to do DB INSERT if the number of
+   affected rows by contact DB UPDATE operation is 0. The database module
+   driver has to implement affected_rows() DB API function, otherwise this
+   parameter is ignored - e.g., MySQL and Postgres DB connectors offer
+   affected_rows().
+
+   Default value is "0" (no DB INSERT).
+
+   Example 1.29. Set db_check_update parameter
+...
+modparam("usrloc", "db_check_update", 1)
+...
+
+3.30. timer_procs (string)
 
    Number of timer processes to be started by module. Timer processes take
    care of checking expired records and syncronization with database. If
@@ -676,7 +695,7 @@ modparam("usrloc", "db_update_as_insert", 1)
 
    Default value is "0".
 
-   Example 1.29. Set timer_procs parameter
+   Example 1.30. Set timer_procs parameter
 ...
 modparam("usrloc", "timer_procs", 4)
 ...

+ 24 - 0
modules_k/usrloc/doc/usrloc_admin.xml

@@ -775,6 +775,30 @@ modparam("usrloc", "db_update_as_insert", 1)
 		</example>
 	</section>
 
+	<section id="db_check_update">
+		<title><varname>db_check_update</varname> (string)</title>
+		<para>
+			Set this parameter to 1 if you want to do DB INSERT if the number
+			of affected rows by contact DB UPDATE operation is 0. The
+			database module driver has to implement affected_rows() DB API
+			function, otherwise this parameter is ignored - e.g., MySQL and
+			Postgres DB connectors offer affected_rows().
+		</para>
+		<para>
+		<emphasis>
+			Default value is <quote>0</quote> (no DB INSERT).
+		</emphasis>
+		</para>
+		<example>
+		<title>Set <varname>db_check_update</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("usrloc", "db_check_update", 1)
+...
+</programlisting>
+		</example>
+	</section>
+
 	<section id="timer_procs">
 		<title><varname>timer_procs</varname> (string)</title>
 		<para>

+ 9 - 0
modules_k/usrloc/ucontact.c

@@ -728,6 +728,15 @@ int db_update_ucontact(ucontact_t* _c)
 		return -1;
 	}
 
+	if (ul_db_check_update==1 && ul_dbf.affected_rows) {
+		/* supposed to be an UPDATE, but if affected rows is 0, then try
+		 * to do an INSERT */
+		if(ul_dbf.affected_rows(ul_dbh)==0) {
+			LM_DBG("affected rows by UPDATE was 0, doing an INSERT\n");
+			return db_insert_ucontact(_c);
+		}
+	}
+
 	return 0;
 }
 

+ 2 - 0
modules_k/usrloc/ul_mod.c

@@ -107,6 +107,7 @@ extern int bind_usrloc(usrloc_api_t* api);
 extern int ul_locks_no;
 int ul_db_update_as_insert = 0;
 int ul_timer_procs = 0;
+int ul_db_check_update = 0;
 
 /* sruid to get internal uid for mi/rpc commands */
 sruid_t _ul_sruid;
@@ -194,6 +195,7 @@ static param_export_t params[] = {
 	{"preload",             STR_PARAM|USE_FUNC_PARAM, (void*)ul_preload_param},
 	{"db_update_as_insert", INT_PARAM, &ul_db_update_as_insert},
 	{"timer_procs",         INT_PARAM, &ul_timer_procs},
+	{"db_check_update",     INT_PARAM, &ul_db_check_update},
 	{0, 0, 0}
 };
 

+ 1 - 0
modules_k/usrloc/ul_mod.h

@@ -73,6 +73,7 @@ extern int cseq_delay;
 extern int ul_fetch_rows;
 extern int ul_hash_size;
 extern int ul_db_update_as_insert;
+extern int ul_db_check_update;
 
 extern db1_con_t* ul_dbh;   /* Database connection handle */
 extern db_func_t ul_dbf;