[asterisk-addons-commits] espiceland: branch 1.6.2 r1134 - in /branches/1.6.2: apps/ configs/ res/

SVN commits to the Asterisk addons project asterisk-addons-commits at lists.digium.com
Fri Oct 8 09:58:00 CDT 2010


Author: espiceland
Date: Fri Oct  8 09:57:58 2010
New Revision: 1134

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


Modified:
    branches/1.6.2/apps/app_addon_sql_mysql.c
    branches/1.6.2/configs/res_mysql.conf.sample
    branches/1.6.2/res/res_config_mysql.c

Modified: branches/1.6.2/apps/app_addon_sql_mysql.c
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.6.2/apps/app_addon_sql_mysql.c?view=diff&rev=1134&r1=1133&r2=1134
==============================================================================
--- branches/1.6.2/apps/app_addon_sql_mysql.c (original)
+++ branches/1.6.2/apps/app_addon_sql_mysql.c Fri Oct  8 09:57:58 2010
@@ -50,10 +50,10 @@
 "Syntax:\n"
 "  MYSQL(Set timeout <num>)\n"
 "    Set the connection timeout, in seconds.\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 ${connid}\n"
+"    passed to function mysql_real_connect.  Optional parameter dbcharset\n"
+"    defaults to 'latin1'.  Connection identifer returned in ${connid}\n"
 "  MYSQL(Query resultid ${connid} query-string)\n"
 "    Executes standard MySQL query contained in query-string using established\n"
 "    connection identified by ${connid}. Result of query is stored in ${resultid}.\n"
@@ -73,7 +73,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)
@@ -305,6 +305,7 @@
 		AST_APP_ARG(dbuser);
 		AST_APP_ARG(dbpass);
 		AST_APP_ARG(dbname);
+		AST_APP_ARG(dbcharset);
 	);
 	MYSQL *mysql;
 	int timeout;
@@ -312,7 +313,7 @@
 
 	AST_NONSTANDARD_APP_ARGS(args, data, ' ');
 
-	if (args.argc != 6) {
+	if (args.argc < 6) {
 		ast_log(LOG_WARNING, "MYSQL_connect is missing some arguments\n");
 		return -1;
 	}
@@ -325,6 +326,14 @@
 	ctimeout = pbx_builtin_getvar_helper(chan, "MYSQL_TIMEOUT");
 	if (ctimeout && sscanf(ctimeout, "%d", &timeout) == 1) {
 		mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, (void *)&timeout);
+	}
+	if(args.dbcharset && strlen(args.dbcharset) > 2){
+		char set_names[255];
+		char statement[512];
+		snprintf(set_names, sizeof(set_names), "SET NAMES %s", args.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, args.dbcharset);
 	}
 
 	if (! mysql_real_connect(mysql, args.dbhost, args.dbuser, args.dbpass, args.dbname, 0, NULL,

Modified: branches/1.6.2/configs/res_mysql.conf.sample
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.6.2/configs/res_mysql.conf.sample?view=diff&rev=1134&r1=1133&r2=1134
==============================================================================
--- branches/1.6.2/configs/res_mysql.conf.sample (original)
+++ branches/1.6.2/configs/res_mysql.conf.sample Fri Oct  8 09:57:58 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.
 ;
 ; Multiple database contexts may be configured, with the caveat that
 ; all context names should be unique and must not contain the slash ('/')
@@ -37,4 +39,5 @@
 ;dbpass = mypass
 ;dbport = 3306
 ;dbsock = /tmp/mysql.sock
+;dbcharset = latin1
 ;requirements=warn ; or createclose or createchar

Modified: branches/1.6.2/res/res_config_mysql.c
URL: http://svnview.digium.com/svn/asterisk-addons/branches/1.6.2/res/res_config_mysql.c?view=diff&rev=1134&r1=1133&r2=1134
==============================================================================
--- branches/1.6.2/res/res_config_mysql.c (original)
+++ branches/1.6.2/res/res_config_mysql.c Fri Oct  8 09:57:58 2010
@@ -95,6 +95,7 @@
 	char        user[50];
 	char        pass[50];
 	char        sock[50];
+	char        charset[50];
 	int         port;
 	int         connected;
 	time_t      connect_time;
@@ -1520,6 +1521,10 @@
 	} else
 		ast_copy_string(conn->sock, s, sizeof(conn->sock));
 
+	if (s = ast_variable_retrieve(config, category, "dbcharset")) {
+		ast_copy_string(conn->charset, s, sizeof(conn->charset));
+	}
+
 	if (!(s = ast_variable_retrieve(config, category, "requirements"))) {
 		ast_log(LOG_WARNING, "MySQL realtime: no requirements setting found, using 'warn' as default.\n");
 		conn->requirements = RQ_WARN;
@@ -1540,6 +1545,8 @@
 	} else
 		ast_debug(1, "MySQL RealTime socket: %s\n", conn->sock);
 	ast_debug(1, "MySQL RealTime database name: %s\n", conn->name);
+	if(conn->charset)
+		ast_debug(1, "MySQL RealTime charset: %s\n", conn->charset);
 	ast_debug(1, "MySQL RealTime user: %s\n", conn->user);
 	ast_debug(1, "MySQL RealTime password: %s\n", conn->pass);
 
@@ -1561,6 +1568,15 @@
 			conn->connected = 0;
 			return 0;
 		}
+		if(conn->charset && strlen(conn->charset) > 2){
+			char set_names[255];
+			char statement[512];
+			snprintf(set_names, sizeof(set_names), "SET NAMES %s", conn->charset);
+			mysql_real_escape_string(&conn->handle, statement, set_names, sizeof(set_names));
+			mysql_options(&conn->handle, MYSQL_INIT_COMMAND, set_names);
+			mysql_options(&conn->handle, MYSQL_SET_CHARSET_NAME, conn->charset);
+		}
+
 		if (mysql_real_connect(&conn->handle, conn->host, conn->user, conn->pass, conn->name, conn->port, conn->sock, 0)) {
 #ifdef MYSQL_OPT_RECONNECT
 			/* The default is no longer to automatically reconnect on failure,




More information about the asterisk-addons-commits mailing list