[asterisk-addons-commits] qwell: trunk r396 - /trunk/app_addon_sql_mysql.c

asterisk-addons-commits at lists.digium.com asterisk-addons-commits at lists.digium.com
Tue Jun 12 15:12:41 MST 2007


Author: qwell
Date: Tue Jun 12 17:12:40 2007
New Revision: 396

URL: http://svn.digium.com/view/asterisk-addons?view=rev&rev=396
Log:
Add the ability to get the next result in a resultset.
This let's you use a stored procedure with multiple results - see example(s) in bug report.

Also fixes up some documentation errors.

Issue 9931, patch by eserra.

Modified:
    trunk/app_addon_sql_mysql.c

Modified: trunk/app_addon_sql_mysql.c
URL: http://svn.digium.com/view/asterisk-addons/trunk/app_addon_sql_mysql.c?view=diff&rev=396&r1=395&r2=396
==============================================================================
--- trunk/app_addon_sql_mysql.c (original)
+++ trunk/app_addon_sql_mysql.c Tue Jun 12 17:12:40 2007
@@ -47,11 +47,13 @@
 "  MYSQL(Connect connid dhhost dbuser dbpass dbname)\n"
 "    Connects to a database.  Arguments contain standard MySQL parameters\n"
 "    passed to function mysql_real_connect.  Connection identifer returned\n"
-"    in ${var}\n"
+"    in ${connid}\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"
-"    is stored in ${var}.\n"
+"    connection identified by ${connid}. Result of query is stored in ${resultid}.\n"
+"  MYSQL(Nextresult resultid ${connid}\n"
+"    If last query returned more than one result set, it stores the next\n"
+"    result set in ${resultid}. It's useful with stored procedures\n"
 "  MYSQL(Fetch fetchid ${resultid} var1 var2 ... varN)\n"
 "    Fetches a single row from a result set contained in ${result_identifier}.\n"
 "    Assigns returned fields to ${var1} ... ${varn}.  ${fetchid} is set TRUE\n"
@@ -264,11 +266,12 @@
 
 	if (resultid_var && (connid >= 0) && querystring) {
 		if ((mysql=find_identifier(connid, AST_MYSQL_ID_CONNID))) {
-			mysql_query(mysql,querystring);
+			mysql_query_res = mysql_query(mysql,querystring);
+			if (mysql_query_res != 0) {
+				ast_log(LOG_WARNING, "aMYSQL_query: mysql_query failed. Error: %s\n", mysql_error(mysql));
+			}
 			if ((mysqlres=mysql_store_result(mysql))) {
 				add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
-				if (mysql_more_results(mysql))
-					mysql_next_result(mysql);
 				return 0;
 			} else if (!mysql_field_count(mysql)) {
 				return 0;
@@ -281,6 +284,42 @@
 
 	mysql_free_result(mysqlres);
 
+	return -1;
+}
+
+static int aMYSQL_nextresult(struct ast_channel *chan, char *data) {
+
+	MYSQL       *mysql;
+	MYSQL_RES   *mysqlres;
+	
+	char *resultid_var;
+	int connid;
+	
+	strsep(&data, " ");
+	
+	resultid_var = strsep(&data," ");
+	connid       = safe_scan_int(&data," ",-1);
+	
+	if (resultid_var && (connid >= 0)) {
+		if ((mysql=find_identifier(connid, AST_MYSQL_ID_CONNID))) {
+			if (mysql_more_results(mysql)) { 
+				mysql_next_result(mysql);
+				if ((mysqlres=mysql_store_result(mysql))) {
+					add_identifier_and_set_asterisk_int(chan,resultid_var,AST_MYSQL_ID_RESID,mysqlres);
+					return 0;
+				} else if (!mysql_field_count(mysql)) {
+					return 0;
+				} else
+					ast_log(LOG_WARNING,"mysql_store_result() failed on storing next_result");
+			} else
+				ast_log(LOG_WARNING,"mysql_more_results() result set has no more results\n");
+		} else
+			ast_log(LOG_WARNING,"Invalid connection identifier %d passed in aMYSQL_query\n",connid);
+	} else
+		ast_log(LOG_WARNING,"missing some arguments\n");
+	
+	mysql_free_result(mysqlres);
+	
 	return -1;
 }
 
@@ -393,6 +432,8 @@
 		result=aMYSQL_connect(chan,ast_strdupa(data));
 	} else 	if (strncasecmp("query",data,strlen("query"))==0) {
 		result=aMYSQL_query(chan,ast_strdupa(data));
+	} else  if (strncasecmp("nextresult",data,strlen("nextresult"))==0) {
+		result=aMYSQL_nextresult(chan,ast_strdupa(data));
 	} else 	if (strncasecmp("fetch",data,strlen("fetch"))==0) {
 		result=aMYSQL_fetch(chan,ast_strdupa(data));
 	} else 	if (strncasecmp("clear",data,strlen("clear"))==0) {



More information about the asterisk-addons-commits mailing list