[svn-commits] espiceland: branch 1.4 r1133 - in /branches/1.4: apps/ cdr/ configs/ res/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Fri Oct 8 09:43:01 CDT 2010


Author: espiceland
Date: Fri Oct  8 09:42:56 2010
New Revision: 1133

URL: http://svnview.digium.com/svn/asterisk-addons?view=rev&rev=1133
Log:
Add option to specify a character set that MySQL should use.
 (closes issue #17948)
 Reported by qmax.

Modified:
    branches/1.4/apps/app_addon_sql_mysql.c
    branches/1.4/cdr/cdr_addon_mysql.c
    branches/1.4/configs/cdr_mysql.conf.sample
    branches/1.4/configs/res_mysql.conf.sample
    branches/1.4/res/res_config_mysql.c

Modified: branches/1.4/apps/app_addon_sql_mysql.c
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.4/apps/app_addon_sql_mysql.c?view=diff&rev=1133&r1=1132&r2=1133
==============================================================================
--- branches/1.4/apps/app_addon_sql_mysql.c (original)
+++ branches/1.4/apps/app_addon_sql_mysql.c Fri Oct  8 09:42:56 2010
@@ -46,10 +46,10 @@
 static char *descrip = 
 "MYSQL():  Do several mySQLy things\n"
 "Syntax:\n"
-"  MYSQL(Connect connid dhhost dbuser dbpass dbname)\n"
+"  MYSQL(Connect connid dhhost dbuser dbpass dbname [dbcharset])\n"
 "    Connects to a database.  Arguments contain standard MySQL parameters\n"
-"    passed to function mysql_real_connect.  Connection identifer returned\n"
-"    in ${var}\n"
+"    passed to function mysql_real_connect.  Optional parameter dbcharset\n"
+"    defaults to 'latin1'.  Connection identifer returned in ${var}\n"
 "  MYSQL(Query resultid ${connid} query-string)\n"
 "    Executes standard MySQL query contained in query-string using established\n"
 "    connection identified by ${connection_identifier}. Result of query is\n"
@@ -67,7 +67,7 @@
 /*	
 EXAMPLES OF USE : 
 
-exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit)
+exten => s,2,MYSQL(Connect connid localhost asterisk mypass credit utf8)
 exten => s,3,MYSQL(Query resultid ${connid} SELECT username\,credit FROM credit WHERE callerid=${CALLERIDNUM})
 exten => s,4,MYSQL(Fetch fetchid ${resultid} datavar1 datavar2)
 exten => s,5,GotoIf(${fetchid}?6:8)
@@ -281,6 +281,7 @@
 	char *dbuser;
 	char *dbpass;
 	char *dbname;
+	char *dbcharset;
 	 
 	strsep(&data," "); // eat the first token, we already know it :P 
 
@@ -288,11 +289,21 @@
 	dbhost=strsep(&data," ");
 	dbuser=strsep(&data," ");
 	dbpass=strsep(&data," ");
-	dbname=strsep(&data,"\n");
-	
-	if( connid_var && dbhost && dbuser && dbpass && dbname ) {
+	dbname = strsep(&data, " ");
+	dbcharset = strsep(&data, "\n");
+
+	if (connid_var && dbhost && dbuser && dbpass && dbname ) {
 		mysql = mysql_init(NULL);
 		if (mysql) {
+			if(dbcharset){
+				char set_names[255];
+				char statement[512];
+				snprintf(set_names, sizeof(set_names), "SET NAMES %s", dbcharset);
+				mysql_real_escape_string(mysql, statement, set_names, sizeof(set_names));
+				mysql_options(mysql, MYSQL_INIT_COMMAND, set_names);
+				mysql_options(mysql, MYSQL_SET_CHARSET_NAME, dbcharset);
+			}
+
 			if (mysql_real_connect(mysql,dbhost,dbuser,dbpass,dbname,0,NULL,0)) {
 				add_identifier_and_set_asterisk_int(chan,connid_var,AST_MYSQL_ID_CONNID,mysql);
 				return 0;
@@ -302,7 +313,7 @@
 			}
 		}
 		else {
-			ast_log(LOG_WARNING,"myslq_init returned NULL\n");
+			ast_log(LOG_WARNING,"mysql_init returned NULL\n");
 		}
  	}
 	else {

Modified: branches/1.4/cdr/cdr_addon_mysql.c
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.4/cdr/cdr_addon_mysql.c?view=diff&rev=1133&r1=1132&r2=1133
==============================================================================
--- branches/1.4/cdr/cdr_addon_mysql.c (original)
+++ branches/1.4/cdr/cdr_addon_mysql.c Fri Oct  8 09:42:56 2010
@@ -53,8 +53,8 @@
 static char *desc = "MySQL CDR Backend";
 static char *name = "mysql";
 static char *config = "cdr_mysql.conf";
-static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL;
-static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0;
+static char *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL, *charset = NULL;
+static int hostname_alloc = 0, dbname_alloc = 0, dbuser_alloc = 0, password_alloc = 0, dbsock_alloc = 0, dbtable_alloc = 0, charset_alloc = 0;
 static int dbport = 0;
 static int connected = 0;
 static time_t connect_time = 0;
@@ -151,6 +151,17 @@
 			ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
 		}
 #endif
+		/* Add option to use a specific character set */
+		if(charset && strlen(charset) > 2){
+			char set_names[255];
+			char statement[512];
+			snprintf(set_names, sizeof(set_names), "SET NAMES %s", charset);
+			mysql_real_escape_string(&mysql, statement, set_names, sizeof(set_names));
+			mysql_options(&mysql, MYSQL_INIT_COMMAND, set_names);
+			mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, charset);
+			ast_log(LOG_WARNING, "cdr_mysql: using charset %s", charset);
+		}
+
 		if (mysql_real_connect(&mysql, hostname, dbuser, password, dbname, dbport, dbsock, 0)) {
 			connected = 1;
 			connect_time = time(NULL);
@@ -296,6 +307,11 @@
 		password = NULL;
 		password_alloc = 0;
 	}
+	if (charset && charset_alloc) {
+		free(charset);
+		charset = NULL;
+		charset_alloc = 0;
+	}
 	dbport = 0;
 	ast_cdr_unregister(name);
 	return 0;
@@ -411,6 +427,18 @@
 	} else {
 		ast_log(LOG_WARNING, "MySQL database password not specified.  Assuming blank\n");
 		password = "";
+	}
+
+	tmp = ast_variable_retrieve(cfg, "global", "charset");
+	if (tmp) {
+		charset = malloc(strlen(tmp) + 1);
+		if (charset != NULL) {
+			charset_alloc= 1;
+			strcpy(charset, tmp);
+		} else {
+			ast_log(LOG_ERROR, "Out of memory error.\n");
+			return -1;
+		}
 	}
 
 	tmp = ast_variable_retrieve(cfg, "global", "port");

Modified: branches/1.4/configs/cdr_mysql.conf.sample
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.4/configs/cdr_mysql.conf.sample?view=diff&rev=1133&r1=1132&r2=1133
==============================================================================
--- branches/1.4/configs/cdr_mysql.conf.sample (original)
+++ branches/1.4/configs/cdr_mysql.conf.sample Fri Oct  8 09:42:56 2010
@@ -8,7 +8,9 @@
 ; port specified or use the default port.  If hostname is not specified
 ; or if hostname is "localhost", then cdr_mysql will attempt to connect
 ; to the socket file specified by sock or otherwise use the default socket
-; file.
+; file.  If no dbcharset is specified, the connection is made with no extra
+; charset configurations sent to MySQL, leaving all configured MySQL charset
+; options and defaults untouched.
 ;
 ;[global]
 ;hostname=database.host.name
@@ -19,3 +21,4 @@
 ;port=3306
 ;sock=/tmp/mysql.sock
 ;userfield=1
+;charset = latin1

Modified: branches/1.4/configs/res_mysql.conf.sample
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.4/configs/res_mysql.conf.sample?view=diff&rev=1133&r1=1132&r2=1133
==============================================================================
--- branches/1.4/configs/res_mysql.conf.sample (original)
+++ branches/1.4/configs/res_mysql.conf.sample Fri Oct  8 09:42:56 2010
@@ -4,7 +4,9 @@
 ; The value of dbhost may be either a hostname or an IP address.
 ; If dbhost is commented out or the string "localhost", a connection
 ; to the local host is assumed and dbsock is used instead of TCP/IP
-; to connect to the server.
+; to connect to the server. If no dbcharset is specified, the connection
+; is made with no extra charset configurations sent to MySQL, leaving all 
+; configured MySQL charset options and defaults untouched.
 ;
 [general]
 ;dbhost = 127.0.0.1
@@ -13,3 +15,4 @@
 ;dbpass = mypass
 ;dbport = 3306
 ;dbsock = /tmp/mysql.sock
+;dbcharset = latin1

Modified: branches/1.4/res/res_config_mysql.c
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.4/res/res_config_mysql.c?view=diff&rev=1133&r1=1132&r2=1133
==============================================================================
--- branches/1.4/res/res_config_mysql.c (original)
+++ branches/1.4/res/res_config_mysql.c Fri Oct  8 09:42:56 2010
@@ -68,6 +68,7 @@
 static char   dbpass[50];
 static char   dbname[50];
 static char   dbsock[50];
+static char   dbcharset[50];
 static int    dbport;
 static int    connected;
 static time_t connect_time;
@@ -579,6 +580,10 @@
                 } else {
                         strncpy(dbsock, s, sizeof(dbsock) - 1);
                 }
+
+		if (s = ast_variable_retrieve(config, "general", "dbcharset")) {
+			ast_copy_string(dbcharset, s, sizeof(dbcharset));
+		}
 	}
 	ast_config_destroy(config);
 
@@ -590,6 +595,8 @@
 	}
 	ast_log(LOG_DEBUG, "MySQL RealTime User: %s\n", dbuser);
 	ast_log(LOG_DEBUG, "MySQL RealTime Password: %s\n", dbpass);
+	if(dbcharset)
+		ast_log(LOG_DEBUG, "MySQL Charset: %s\n", dbcharset);
 
 	return 1;
 }
@@ -615,6 +622,15 @@
 			connected = 0;
 			return 0;
 		}
+		if(strlen(dbcharset) > 2){
+			char set_names[255];
+			char statement[512];
+			snprintf(set_names, sizeof(set_names), "SET NAMES %s", dbcharset);
+			mysql_real_escape_string(&mysql, statement, set_names, sizeof(set_names));
+			mysql_options(&mysql, MYSQL_INIT_COMMAND, set_names);
+			mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, dbcharset);
+		}
+
 		if(mysql_real_connect(&mysql, dbhost, dbuser, dbpass, my_database, dbport, dbsock, 0)) {
 #ifdef MYSQL_OPT_RECONNECT
 			/* The default is no longer to automatically reconnect on failure,




More information about the svn-commits mailing list