Ver código fonte

db_text Add support for empty strings as a replacement for NULL strings

Some modules, like dialplan, doesn't accept NULL strings from databases.
db_text had no way to define a string as empty - all empty strings was NULL.
Adding a new option to change this behaviour. With "emptystring" set to 1
db_text will not set an empty string to NULL any more.
Olle E. Johansson 10 anos atrás
pai
commit
935f11ab8d

+ 39 - 15
modules/db_text/README

@@ -16,6 +16,12 @@ Daniel-Constantin Mierla
 
    <[email protected]>
 
+Edited by
+
+Olle E. Johansson
+
+   <[email protected]>
+
    Copyright © 2003, 2004 FhG FOKUS
      __________________________________________________________________
 
@@ -37,6 +43,7 @@ Daniel-Constantin Mierla
         3. Parameters
 
               3.1. db_mode (integer)
+              3.2. emptystring (integer)
 
         4. Exported RPC Functions
 
@@ -54,11 +61,12 @@ Daniel-Constantin Mierla
    1.2. Minimal Kamailio location dbtext table definition
    1.3. Minimal Kamailio subscriber dbtext table example
    1.4. Set db_mode parameter
-   1.5. Load the dbtext module
-   1.6. Definition of 'subscriber' table (one line)
-   1.7. Definition of 'location' and 'aliases' tables (one line)
-   1.8. Definition of 'version' table and sample records
-   1.9. Configuration file
+   1.5. Set emptystring parameter
+   1.6. Load the dbtext module
+   1.7. Definition of 'subscriber' table (one line)
+   1.8. Definition of 'location' and 'aliases' tables (one line)
+   1.9. Definition of 'version' table and sample records
+   1.10. Configuration file
 
 Chapter 1. Admin Guide
 
@@ -78,6 +86,7 @@ Chapter 1. Admin Guide
    3. Parameters
 
         3.1. db_mode (integer)
+        3.2. emptystring (integer)
 
    4. Exported RPC Functions
 
@@ -111,7 +120,8 @@ Chapter 1. Admin Guide
    NOTE: even when db_text is in non-caching mode, the module does not
    write back to hard drive after changes. In this mode, the module checks
    if the corresponding file on disk has changed, and reloads it. The
-   write to disk happens at Kamailio shut down.
+   write to disk happens at Kamailio shut down. If db_text is in caching
+   mode, many "reload" functions in various modules will not work.
 
 1.1. Design of dbtext engine
 
@@ -150,7 +160,8 @@ Chapter 1. Admin Guide
      * each other line is a row with data. The line ends with "\n".
      * the fields are separated by ":".
      * no value between two ':' (or between ':' and start/end of a row)
-       means "null" value.
+       means "null" value. If the parameter "emptystring" is enabled,
+       db_text sets a NULL string to an empty string value.
      * next characters must be escaped in strings: "\n", "\r", "\t", ":".
      * 0 -- the zero value must be escaped too.
 
@@ -199,8 +210,7 @@ suser:supasswd:xxx:alpha.org:xxx
 3. Parameters
 
    3.1. db_mode (integer)
-
-   None.
+   3.2. emptystring (integer)
 
 3.1. db_mode (integer)
 
@@ -216,11 +226,25 @@ suser:supasswd:xxx:alpha.org:xxx
 modparam("db_text", "db_mode", 1)
 ...
 
+3.2. emptystring (integer)
+
+   db_text by default handles an empty string as a NULL value. Some
+   modules, like the dialplan module, does not accept NULL strings. If you
+   enable emptystring an empty string will not be NULL, but an empty
+   string.
+
+   Default value is "0" (off).
+
+   Example 1.5. Set emptystring parameter
+...
+modparam("db_text", "emptystring", 1)
+...
+
 4. Exported RPC Functions
 
    4.1. db_text.dump
 
-4.1.  db_text.dump
+4.1. db_text.dump
 
    Write back to hard drive all modified tables.
 
@@ -244,7 +268,7 @@ modparam("db_text", "db_mode", 1)
    database path. So, either you provide an absolute path to database
    directory or a relative one to "CFG_DIR" directory.
 
-   Example 1.5. Load the dbtext module
+   Example 1.6. Load the dbtext module
 ...
 loadmodule "/path/to/kamailio/modules_k/db_text.so"
 ...
@@ -263,7 +287,7 @@ modparam("module_name", "db_url", "text:///path/to/dbtext/database")
    in order to have authentication. To use with the given configuration
    file, the table files must be placed in the '/tmp/serdb' directory.
 
-   Example 1.6. Definition of 'subscriber' table (one line)
+   Example 1.7. Definition of 'subscriber' table (one line)
 ...
 username(str) domain(str) password(str) first_name(str) last_name(str) phone(str
 ) email_address(str) datetime_created(int) datetime_modified(int) confirmation(s
@@ -271,14 +295,14 @@ tr) flag(str) sendnotification(str) greeting(str) ha1(str) ha1b(str) perms(str)
 allow_find(str) timezone(str,null) rpid(str,null)
 ...
 
-   Example 1.7. Definition of 'location' and 'aliases' tables (one line)
+   Example 1.8. Definition of 'location' and 'aliases' tables (one line)
 ...
 username(str) domain(str,null) contact(str,null) received(str) expires(int,null)
  q(double,null) callid(str,null) cseq(int,null) last_modified(str) flags(int) us
 er_agent(str) socket(str)
 ...
 
-   Example 1.8. Definition of 'version' table and sample records
+   Example 1.9. Definition of 'version' table and sample records
 ...
 table_name(str) table_version(int)
 subscriber:3
@@ -286,7 +310,7 @@ location:6
 aliases:6
 ...
 
-   Example 1.9. Configuration file
+   Example 1.10. Configuration file
 ...
 #
 # $Id$

+ 10 - 4
modules/db_text/dbt_file.c

@@ -408,10 +408,16 @@ dbt_table_p dbt_load_file(const str *tbn, const str *dbn)
 						
 						bp = 0;
 						if(c==DBT_DELIM || 
-								(ccol == dtp->nrcols-1
-								 && (c == DBT_DELIM_R || c==EOF)))
-							dtval.nul = 1;
-						else
+							(ccol == dtp->nrcols-1
+							 && (c == DBT_DELIM_R || c==EOF))) {
+							/* If empty_string is enabled, we'll just return
+							 * an empty string and avoid NULL
+							 */
+							if (empty_string == 0) {
+								/* Default - NULL */
+								dtval.nul = 1;
+							}
+						} else
 						{
 							dtval.nul = 0;
 							while(c!=DBT_DELIM && c!=DBT_DELIM_R && c!=EOF)

+ 1 - 0
modules/db_text/dbt_lib.h

@@ -54,6 +54,7 @@
  *  * Module parameters variables
  *   */
 extern int db_mode; /* Database usage mode: 0 = no cache, 1 = cache */
+extern int empty_string; /* If TRUE, an empty string is an empty string, otherwise NULL */
 
 typedef db_val_t dbt_val_t, *dbt_val_p;
 

+ 4 - 0
modules/db_text/dbtext.c

@@ -4,6 +4,7 @@
  * DBText module interface
  *
  * Copyright (C) 2001-2003 FhG Fokus
+ * Copyright (C) 2014 Edvina AB, Olle E. Johansson
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -26,6 +27,7 @@
  * 2003-01-30 created by Daniel
  * 2003-03-11 New module interface (janakj)
  * 2003-03-16 flags export parameter added (janakj)
+ * 2014-12-10 added the null string handling (for compatibility with dialplan)
  * 
  */
 
@@ -48,6 +50,7 @@ static void destroy(void);
  * Module parameter variables
  */
 int db_mode = 0;  /* Database usage mode: 0 = cache, 1 = no cache */
+int empty_string = 0;  /* Treat empty string as "" = 0, 1 = NULL */
 
 int dbt_bind_api(db_func_t *dbb);
 
@@ -65,6 +68,7 @@ static cmd_export_t cmds[] = {
  */
 static param_export_t params[] = {
 	{"db_mode", INT_PARAM, &db_mode},
+	{"emptystring", INT_PARAM, &empty_string},
 	{0, 0, 0}
 };
 

+ 5 - 0
modules/db_text/doc/db_text.xml

@@ -28,6 +28,11 @@
 		<surname>Mierla</surname>
 		<email>[email protected]</email>
 	    </editor>
+	    <editor>
+		<firstname>Olle E.</firstname>
+		<surname>Johansson</surname>
+		<email>[email protected]</email>
+	    </editor>
 	</authorgroup>
 	<copyright>
 	    <year>2003</year>

+ 42 - 19
modules/db_text/doc/db_text_admin.xml

@@ -36,7 +36,8 @@
 		NOTE: even when db_text is in non-caching mode, the module does not write
 		back to hard drive after changes. In this mode, the module checks if
 		the corresponding file on disk has changed, and reloads it. The write
-		to disk happens at &kamailio; shut down.
+		to disk happens at &kamailio; shut down. If db_text is in caching
+		mode, many "reload" functions in various modules will not work.
 	</para>
 		<section>
 		<title>Design of dbtext engine</title>
@@ -159,7 +160,8 @@
 			<listitem>
 				<para>
 				no value between two ':' (or between ':' and start/end of a row)
-				means <quote>null</quote> value.
+				means <quote>null</quote> value. If the parameter "emptystring" is
+				enabled, db_text sets a NULL string to an empty string value.
 				</para>
 			</listitem>
 			<listitem>
@@ -245,30 +247,51 @@ suser:supasswd:xxx:alpha.org:xxx
 	</section>
 	<section>
 	<title>Parameters</title>
-		<para>
-			<emphasis>None</emphasis>.
-	   	</para>
 		<section>
 			<title><varname>db_mode</varname> (integer)</title>
-		<para>
-		Set caching mode (0) or non-caching mode (1). In caching mode, data
-		is loaded at startup. In non-caching mode, the module check every time
-		a table is requested whether the corresponding file on disk has
-		changed, and if yes, will re-load the table from file.
-		</para>
-		<para>
-		<emphasis>
-			Default value is <quote>0</quote>.
-		</emphasis>
-		</para>
-		<example>
-		<title>Set <varname>db_mode</varname> parameter</title>
+			<para>
+			Set caching mode (0) or non-caching mode (1). In caching mode, data
+			is loaded at startup. In non-caching mode, the module check every time
+			a table is requested whether the corresponding file on disk has
+			changed, and if yes, will re-load the table from file.
+			</para>
+
+			<para>
+			<emphasis>
+				Default value is <quote>0</quote>.
+			</emphasis>
+			</para>
+			<example>
+			<title>Set <varname>db_mode</varname> parameter</title>
 		<programlisting format="linespecific">
 ...
 modparam("db_text", "db_mode", 1)
 ...
 </programlisting>
-		</example>
+			</example>
+		</section>
+		<section>
+			<title><varname>emptystring</varname> (integer)</title>
+			<para>
+			db_text by default handles an empty string as a NULL value.
+			Some modules, like the dialplan module, does not accept
+			NULL strings. If you enable emptystring an empty string
+			will not be NULL, but an empty string.
+			</para>
+
+			<para>
+			<emphasis>
+				Default value is <quote>0</quote> (off).
+			</emphasis>
+			</para>
+			<example>
+			<title>Set <varname>emptystring</varname> parameter</title>
+		<programlisting format="linespecific">
+...
+modparam("db_text", "emptystring", 1)
+...
+</programlisting>
+			</example>
 		</section>
 	</section>
 	<section>