| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  |  * Copyright (c) 2005, 2006 Tilghman Lesher | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Tilghman Lesher <func_odbc__200508@the-tilghman.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  | /*!
 | 
					
						
							|  |  |  |  * \file | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \brief ODBC lookups | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \author Tilghman Lesher <func_odbc__200508@the-tilghman.com> | 
					
						
							| 
									
										
										
										
											2007-01-24 09:05:29 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \ingroup functions | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<depend>unixodbc</depend> | 
					
						
							| 
									
										
										
										
											2007-06-18 16:35:51 +00:00
										 |  |  | 	<depend>ltdl</depend> | 
					
						
							| 
									
										
										
										
											2007-02-09 23:53:51 +00:00
										 |  |  | 	<depend>res_odbc</depend> | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/config.h"
 | 
					
						
							|  |  |  | #include "asterisk/res_odbc.h"
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char *config = "func_odbc.conf"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-22 03:40:31 +00:00
										 |  |  | enum { | 
					
						
							|  |  |  | 	OPT_ESCAPECOMMAS =	(1 << 0), | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	OPT_MULTIROW     =	(1 << 1), | 
					
						
							| 
									
										
										
										
											2006-05-22 03:40:31 +00:00
										 |  |  | } odbc_option_flags; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | struct acf_odbc_query { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_ENTRY(acf_odbc_query) list; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	char readhandle[5][30]; | 
					
						
							|  |  |  | 	char writehandle[5][30]; | 
					
						
							| 
									
										
										
										
											2006-02-23 19:26:22 +00:00
										 |  |  | 	char sql_read[2048]; | 
					
						
							|  |  |  | 	char sql_write[2048]; | 
					
						
							| 
									
										
										
											
												After some study, thought, comparing, etc. I've backed out the previous universal mod to make ast_flags a 64 bit thing. Instead, I added a 64-bit version of ast_flags (ast_flags64), and 64-bit versions of the test-flag, set-flag, etc. macros, and an app_parse_options64 routine, and I use these in app_dial alone, to eliminate the 30-option limit it had grown to meet. There is room now for 32 more options and flags. I was heavily tempted to implement some of the other ideas that were presented, but this solution does not intro any new versions of dial, doesn't have a different API, has a minimal/zero impact on code outside of dial, and doesn't seriously (I hope) affect the code structure of dial. It's the best I can think of right now. My goal was NOT to rewrite dial. I leave that to a future, coordinated effort.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@75983 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-07-19 23:24:27 +00:00
										 |  |  | 	unsigned int flags; | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	int rowlimit; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	struct ast_custom_function *acf; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | static void odbc_datastore_free(void *data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct ast_datastore_info odbc_info = { | 
					
						
							|  |  |  | 	.type = "FUNC_ODBC", | 
					
						
							|  |  |  | 	.destroy = odbc_datastore_free, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* For storing each result row */ | 
					
						
							|  |  |  | struct odbc_datastore_row { | 
					
						
							|  |  |  | 	AST_LIST_ENTRY(odbc_datastore_row) list; | 
					
						
							|  |  |  | 	char data[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* For storing each result set */ | 
					
						
							|  |  |  | struct odbc_datastore { | 
					
						
							|  |  |  | 	AST_LIST_HEAD(, odbc_datastore_row); | 
					
						
							|  |  |  | 	char names[0]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | AST_LIST_HEAD_STATIC(queries, acf_odbc_query); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | static int resultcount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void odbc_datastore_free(void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct odbc_datastore *result = data; | 
					
						
							|  |  |  | 	struct odbc_datastore_row *row; | 
					
						
							|  |  |  | 	AST_LIST_LOCK(result); | 
					
						
							|  |  |  | 	while ((row = AST_LIST_REMOVE_HEAD(result, list))) { | 
					
						
							|  |  |  | 		ast_free(row); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(result); | 
					
						
							|  |  |  | 	AST_LIST_HEAD_DESTROY(result); | 
					
						
							|  |  |  | 	ast_free(result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	char *sql = data; | 
					
						
							|  |  |  | 	SQLHSTMT stmt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt); | 
					
						
							|  |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS); | 
					
						
							|  |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql); | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 		SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 		SQLFreeHandle (SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return stmt; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Master control routine | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2007-01-06 00:13:33 +00:00
										 |  |  | static int acf_odbc_write(struct ast_channel *chan, const char *cmd, char *s, const char *value) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-12-30 03:42:20 +00:00
										 |  |  | 	struct odbc_obj *obj = NULL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	struct acf_odbc_query *query; | 
					
						
							| 
									
										
										
											
												This commits the performance mods that give the priority processing engine in the pbx, a 25-30% speed boost. The two updates used, are, first, to merge the ast_exists_extension() and the ast_spawn_extension() where they are called sequentially in a loop in the code, into a slightly upgraded version of ast_spawn_extension(), with a few extra args; and, second, I modified the substitute_variables_helper_full, so it zeroes out the byte after the evaluated string instead of demanding you pre-zero the buffer; I also went thru the code and removed the code that zeroed this buffer before every call to the substitute_variables_helper_full. The first fix provides about a 9% speedup, and the second the rest. These figures come from the 'PIPS' benchmark I describe in blogs, conf. reports, etc.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@88166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-11-01 22:26:51 +00:00
										 |  |  | 	char *t, buf[2048], varname[15]; | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	int i, dsn, bogus_chan = 0; | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	AST_DECLARE_APP_ARGS(values, | 
					
						
							|  |  |  | 		AST_APP_ARG(field)[100]; | 
					
						
							|  |  |  | 	); | 
					
						
							|  |  |  | 	AST_DECLARE_APP_ARGS(args, | 
					
						
							|  |  |  | 		AST_APP_ARG(field)[100]; | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2007-02-03 22:04:09 +00:00
										 |  |  | 	SQLHSTMT stmt = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-19 14:57:45 +00:00
										 |  |  | 	SQLLEN rows=0; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&queries, query, list) { | 
					
						
							|  |  |  | 		if (!strcmp(query->acf->name, cmd)) { | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!query) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No such function '%s'\n", cmd); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	if (!chan) { | 
					
						
							|  |  |  | 		if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc"))) | 
					
						
							|  |  |  | 			bogus_chan = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (chan) | 
					
						
							|  |  |  | 		ast_autoservice_start(chan); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	/* Parse our arguments */ | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	t = value ? ast_strdupa(value) : ""; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-21 17:50:04 +00:00
										 |  |  | 	if (!s || !t) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "Out of memory\n"); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 		if (chan) | 
					
						
							|  |  |  | 			ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 		if (bogus_chan) | 
					
						
							|  |  |  | 			ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2006-01-21 17:50:04 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	AST_STANDARD_APP_ARGS(args, s); | 
					
						
							|  |  |  | 	for (i = 0; i < args.argc; i++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "ARG%d", i + 1); | 
					
						
							|  |  |  | 		pbx_builtin_pushvar_helper(chan, varname, args.field[i]); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Parse values, just like arguments */ | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	AST_STANDARD_APP_ARGS(values, t); | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	for (i = 0; i < values.argc; i++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "VAL%d", i + 1); | 
					
						
							|  |  |  | 		pbx_builtin_pushvar_helper(chan, varname, values.field[i]); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	/* Additionally set the value as a whole (but push an empty string if value is NULL) */ | 
					
						
							|  |  |  | 	pbx_builtin_pushvar_helper(chan, "VALUE", value ? value : ""); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	pbx_substitute_variables_helper(chan, query->sql_write, buf, sizeof(buf) - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Restore prior values */ | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	for (i = 0; i < args.argc; i++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "ARG%d", i + 1); | 
					
						
							|  |  |  | 		pbx_builtin_setvar_helper(chan, varname, NULL); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	for (i = 0; i < values.argc; i++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "VAL%d", i + 1); | 
					
						
							|  |  |  | 		pbx_builtin_setvar_helper(chan, varname, NULL); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	pbx_builtin_setvar_helper(chan, "VALUE", NULL); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	for (dsn = 0; dsn < 5; dsn++) { | 
					
						
							|  |  |  | 		if (!ast_strlen_zero(query->writehandle[dsn])) { | 
					
						
							|  |  |  | 			obj = ast_odbc_request_obj(query->writehandle[dsn], 0); | 
					
						
							|  |  |  | 			if (obj) | 
					
						
							|  |  |  | 				stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, buf); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (stmt) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 	if (stmt) { | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		/* Rows affected */ | 
					
						
							|  |  |  | 		SQLRowCount(stmt, &rows); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Output the affected rows, for all cases.  In the event of failure, we
 | 
					
						
							|  |  |  | 	 * flag this as -1 rows.  Note that this is different from 0 affected rows | 
					
						
							|  |  |  | 	 * which would be the case if we succeeded in our query, but the values did | 
					
						
							|  |  |  | 	 * not change. */ | 
					
						
							|  |  |  | 	snprintf(varname, sizeof(varname), "%d", (int)rows); | 
					
						
							|  |  |  | 	pbx_builtin_setvar_helper(chan, "ODBCROWS", varname); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 	if (stmt) { | 
					
						
							|  |  |  | 		SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 		SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 	if (obj) | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 		ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	if (chan) | 
					
						
							|  |  |  | 		ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 	if (bogus_chan) | 
					
						
							|  |  |  | 		ast_channel_free(chan); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-06 00:13:33 +00:00
										 |  |  | static int acf_odbc_read(struct ast_channel *chan, const char *cmd, char *s, char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-12-30 03:42:20 +00:00
										 |  |  | 	struct odbc_obj *obj = NULL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	struct acf_odbc_query *query; | 
					
						
							| 
									
										
										
											
												This commits the performance mods that give the priority processing engine in the pbx, a 25-30% speed boost. The two updates used, are, first, to merge the ast_exists_extension() and the ast_spawn_extension() where they are called sequentially in a loop in the code, into a slightly upgraded version of ast_spawn_extension(), with a few extra args; and, second, I modified the substitute_variables_helper_full, so it zeroes out the byte after the evaluated string instead of demanding you pre-zero the buffer; I also went thru the code and removed the code that zeroed this buffer before every call to the substitute_variables_helper_full. The first fix provides about a 9% speedup, and the second the rest. These figures come from the 'PIPS' benchmark I describe in blogs, conf. reports, etc.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@88166 65c4cc65-6c06-0410-ace0-fbb531ad65f3
											
										 
											2007-11-01 22:26:51 +00:00
										 |  |  | 	char sql[2048], varname[15], colnames[2048] = "", rowcount[12] = "-1"; | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	int res, x, y, buflen = 0, escapecommas, rowlimit = 1, dsn, bogus_chan = 0; | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	AST_DECLARE_APP_ARGS(args, | 
					
						
							|  |  |  | 		AST_APP_ARG(field)[100]; | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2007-02-03 22:04:09 +00:00
										 |  |  | 	SQLHSTMT stmt = NULL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	SQLSMALLINT colcount=0; | 
					
						
							| 
									
										
										
										
											2006-12-19 14:57:45 +00:00
										 |  |  | 	SQLLEN indicator; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	SQLSMALLINT collength; | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	struct odbc_datastore *resultset = NULL; | 
					
						
							|  |  |  | 	struct odbc_datastore_row *row = NULL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							|  |  |  | 	AST_LIST_TRAVERSE(&queries, query, list) { | 
					
						
							|  |  |  | 		if (!strcmp(query->acf->name, cmd)) { | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!query) { | 
					
						
							|  |  |  | 		ast_log(LOG_ERROR, "No such function '%s'\n", cmd); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	if (!chan) { | 
					
						
							|  |  |  | 		if ((chan = ast_channel_alloc(0, 0, "", "", "", "", "", 0, "Bogus/func_odbc"))) | 
					
						
							|  |  |  | 			bogus_chan = 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (chan) | 
					
						
							|  |  |  | 		ast_autoservice_start(chan); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	AST_STANDARD_APP_ARGS(args, s); | 
					
						
							|  |  |  | 	for (x = 0; x < args.argc; x++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "ARG%d", x + 1); | 
					
						
							|  |  |  | 		pbx_builtin_pushvar_helper(chan, varname, args.field[x]); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pbx_substitute_variables_helper(chan, query->sql_read, sql, sizeof(sql) - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Restore prior values */ | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 	for (x = 0; x < args.argc; x++) { | 
					
						
							|  |  |  | 		snprintf(varname, sizeof(varname), "ARG%d", x + 1); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, varname, NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	/* Save these flags, so we can release the lock */ | 
					
						
							| 
									
										
										
										
											2006-05-22 03:40:31 +00:00
										 |  |  | 	escapecommas = ast_test_flag(query, OPT_ESCAPECOMMAS); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	if (ast_test_flag(query, OPT_MULTIROW)) { | 
					
						
							|  |  |  | 		resultset = ast_calloc(1, sizeof(*resultset)); | 
					
						
							|  |  |  | 		AST_LIST_HEAD_INIT(resultset); | 
					
						
							|  |  |  | 		if (query->rowlimit) | 
					
						
							|  |  |  | 			rowlimit = query->rowlimit; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			rowlimit = INT_MAX; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	for (dsn = 0; dsn < 5; dsn++) { | 
					
						
							|  |  |  | 		if (!ast_strlen_zero(query->writehandle[dsn])) { | 
					
						
							|  |  |  | 			obj = ast_odbc_request_obj(query->writehandle[dsn], 0); | 
					
						
							|  |  |  | 			if (obj) | 
					
						
							|  |  |  | 				stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, sql); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (stmt) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 	if (!stmt) { | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "Unable to execute query [%s]\n", sql); | 
					
						
							|  |  |  | 		if (obj) | 
					
						
							|  |  |  | 			ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 		if (chan) | 
					
						
							|  |  |  | 			ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 		if (bogus_chan) | 
					
						
							|  |  |  | 			ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = SQLNumResultCols(stmt, &colcount); | 
					
						
							|  |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql); | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 		SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		SQLFreeHandle (SQL_HANDLE_STMT, stmt); | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 		ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 		if (chan) | 
					
						
							|  |  |  | 			ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 		if (bogus_chan) | 
					
						
							|  |  |  | 			ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	res = SQLFetch(stmt); | 
					
						
							|  |  |  | 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 		int res1 = -1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		if (res == SQL_NO_DATA) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 			ast_verb(4, "Found no rows [%s]\n", sql); | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 			res1 = 0; | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			ast_copy_string(rowcount, "0", sizeof(rowcount)); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 		SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2006-05-16 21:14:01 +00:00
										 |  |  | 		SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 		ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 		if (chan) | 
					
						
							|  |  |  | 			ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 		if (bogus_chan) | 
					
						
							|  |  |  | 			ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2006-08-20 15:46:00 +00:00
										 |  |  | 		return res1; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	for (y = 0; y < rowlimit; y++) { | 
					
						
							|  |  |  | 		*buf = '\0'; | 
					
						
							|  |  |  | 		for (x = 0; x < colcount; x++) { | 
					
						
							|  |  |  | 			int i; | 
					
						
							|  |  |  | 			char coldata[256]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (y == 0) { | 
					
						
							|  |  |  | 				char colname[256]; | 
					
						
							|  |  |  | 				int namelen; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				res = SQLDescribeCol(stmt, x + 1, (unsigned char *)colname, sizeof(colname), &collength, NULL, NULL, NULL, NULL); | 
					
						
							|  |  |  | 				if (((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) || collength == 0) { | 
					
						
							|  |  |  | 					snprintf(colname, sizeof(colname), "field%d", x); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (!ast_strlen_zero(colnames)) | 
					
						
							|  |  |  | 					strncat(colnames, ",", sizeof(colnames) - 1); | 
					
						
							|  |  |  | 				namelen = strlen(colnames); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/* Copy data, encoding '\' and ',' for the argument parser */ | 
					
						
							|  |  |  | 				for (i = 0; i < sizeof(colname); i++) { | 
					
						
							|  |  |  | 					if (escapecommas && (colname[i] == '\\' || colname[i] == ',')) { | 
					
						
							|  |  |  | 						colnames[namelen++] = '\\'; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					colnames[namelen++] = colname[i]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (namelen >= sizeof(colnames) - 2) { | 
					
						
							|  |  |  | 						colnames[namelen >= sizeof(colnames) ? sizeof(colnames) - 1 : namelen] = '\0'; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (colname[i] == '\0') | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (resultset) { | 
					
						
							|  |  |  | 					void *tmp = ast_realloc(resultset, sizeof(*resultset) + strlen(colnames) + 1); | 
					
						
							|  |  |  | 					if (!tmp) { | 
					
						
							|  |  |  | 						ast_log(LOG_ERROR, "No space for a new resultset?\n"); | 
					
						
							|  |  |  | 						ast_free(resultset); | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 						SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 						SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 						ast_odbc_release_obj(obj); | 
					
						
							|  |  |  | 						pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 						if (chan) | 
					
						
							|  |  |  | 							ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 						if (bogus_chan) | 
					
						
							|  |  |  | 							ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 						return -1; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					resultset = tmp; | 
					
						
							|  |  |  | 					strcpy((char *)resultset + sizeof(*resultset), colnames); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			buflen = strlen(buf); | 
					
						
							|  |  |  | 			res = SQLGetData(stmt, x + 1, SQL_CHAR, coldata, sizeof(coldata), &indicator); | 
					
						
							|  |  |  | 			if (indicator == SQL_NULL_DATA) { | 
					
						
							|  |  |  | 				coldata[0] = '\0'; | 
					
						
							|  |  |  | 				res = SQL_SUCCESS; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql); | 
					
						
							|  |  |  | 				y = -1; | 
					
						
							|  |  |  | 				goto end_acf_read; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			/* Copy data, encoding '\' and ',' for the argument parser */ | 
					
						
							|  |  |  | 			for (i = 0; i < sizeof(coldata); i++) { | 
					
						
							|  |  |  | 				if (escapecommas && (coldata[i] == '\\' || coldata[i] == ',')) { | 
					
						
							|  |  |  | 					buf[buflen++] = '\\'; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				buf[buflen++] = coldata[i]; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 				if (buflen >= len - 2) | 
					
						
							|  |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 				if (coldata[i] == '\0') | 
					
						
							|  |  |  | 					break; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			buf[buflen - 1] = ','; | 
					
						
							|  |  |  | 			buf[buflen] = '\0'; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		/* Trim trailing comma */ | 
					
						
							|  |  |  | 		buf[buflen - 1] = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (resultset) { | 
					
						
							|  |  |  | 			row = ast_calloc(1, sizeof(*row) + buflen); | 
					
						
							|  |  |  | 			if (!row) { | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Unable to allocate space for more rows in this resultset.\n"); | 
					
						
							|  |  |  | 				goto end_acf_read; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			strcpy((char *)row + sizeof(*row), buf); | 
					
						
							|  |  |  | 			AST_LIST_INSERT_TAIL(resultset, row, list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			/* Get next row */ | 
					
						
							|  |  |  | 			res = SQLFetch(stmt); | 
					
						
							|  |  |  | 			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) { | 
					
						
							|  |  |  | 				if (res != SQL_NO_DATA) | 
					
						
							|  |  |  | 					ast_log(LOG_WARNING, "Error %d in FETCH [%s]\n", res, sql); | 
					
						
							|  |  |  | 				y++; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 				break; | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | end_acf_read: | 
					
						
							|  |  |  | 	snprintf(rowcount, sizeof(rowcount), "%d", y); | 
					
						
							|  |  |  | 	pbx_builtin_setvar_helper(chan, "ODBCROWS", rowcount); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", colnames); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	if (resultset) { | 
					
						
							|  |  |  | 		int uid; | 
					
						
							|  |  |  | 		struct ast_datastore *odbc_store; | 
					
						
							| 
									
										
										
										
											2007-08-21 21:58:29 +00:00
										 |  |  | 		uid = ast_atomic_fetchadd_int(&resultcount, +1) + 1; | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 		snprintf(buf, len, "%d", uid); | 
					
						
							|  |  |  | 		odbc_store = ast_channel_datastore_alloc(&odbc_info, buf); | 
					
						
							|  |  |  | 		if (!odbc_store) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "Rows retrieved, but unable to store it in the channel.  Results fail.\n"); | 
					
						
							|  |  |  | 			odbc_datastore_free(resultset); | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 			SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							|  |  |  | 			ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 			if (chan) | 
					
						
							|  |  |  | 				ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 			if (bogus_chan) | 
					
						
							|  |  |  | 				ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		odbc_store->data = resultset; | 
					
						
							|  |  |  | 		ast_channel_datastore_add(chan, odbc_store); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-06-01 21:49:34 +00:00
										 |  |  | 	SQLCloseCursor(stmt); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	SQLFreeHandle(SQL_HANDLE_STMT, stmt); | 
					
						
							| 
									
										
										
										
											2006-09-20 04:57:20 +00:00
										 |  |  | 	ast_odbc_release_obj(obj); | 
					
						
							| 
									
										
										
										
											2007-10-28 14:11:01 +00:00
										 |  |  | 	if (chan) | 
					
						
							|  |  |  | 		ast_autoservice_stop(chan); | 
					
						
							|  |  |  | 	if (bogus_chan) | 
					
						
							|  |  |  | 		ast_channel_free(chan); | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-06 00:13:33 +00:00
										 |  |  | static int acf_escape(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 	char *out = buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (; *data && out - buf < len; data++) { | 
					
						
							|  |  |  | 		if (*data == '\'') { | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | 			*out = '\''; | 
					
						
							|  |  |  | 			out++; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 		*out++ = *data; | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	*out = '\0'; | 
					
						
							| 
									
										
										
										
											2006-02-12 04:28:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | static struct ast_custom_function escape_function = { | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | 	.name = "SQL_ESC", | 
					
						
							|  |  |  | 	.synopsis = "Escapes single ticks for use in SQL statements", | 
					
						
							|  |  |  | 	.syntax = "SQL_ESC(<string>)", | 
					
						
							|  |  |  | 	.desc = | 
					
						
							|  |  |  | "Used in SQL templates to escape data which may contain single ticks (') which\n" | 
					
						
							|  |  |  | "are otherwise used to delimit data.  For example:\n" | 
					
						
							|  |  |  | "SELECT foo FROM bar WHERE baz='${SQL_ESC(${ARG1})}'\n", | 
					
						
							|  |  |  | 	.read = acf_escape, | 
					
						
							|  |  |  | 	.write = NULL, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | static int acf_fetch(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_datastore *store; | 
					
						
							|  |  |  | 	struct odbc_datastore *resultset; | 
					
						
							|  |  |  | 	struct odbc_datastore_row *row; | 
					
						
							|  |  |  | 	store = ast_channel_datastore_find(chan, &odbc_info, data); | 
					
						
							|  |  |  | 	if (!store) { | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	resultset = store->data; | 
					
						
							|  |  |  | 	AST_LIST_LOCK(resultset); | 
					
						
							|  |  |  | 	row = AST_LIST_REMOVE_HEAD(resultset, list); | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(resultset); | 
					
						
							|  |  |  | 	if (!row) { | 
					
						
							|  |  |  | 		/* Cleanup datastore */ | 
					
						
							|  |  |  | 		ast_channel_datastore_remove(chan, store); | 
					
						
							|  |  |  | 		ast_channel_datastore_free(store); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", resultset->names); | 
					
						
							|  |  |  | 	ast_copy_string(buf, row->data, len); | 
					
						
							|  |  |  | 	ast_free(row); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct ast_custom_function fetch_function = { | 
					
						
							|  |  |  | 	.name = "ODBC_FETCH", | 
					
						
							|  |  |  | 	.synopsis = "Fetch a row from a multirow query", | 
					
						
							|  |  |  | 	.syntax = "ODBC_FETCH(<result-id>)", | 
					
						
							|  |  |  | 	.desc = | 
					
						
							|  |  |  | "For queries which are marked as mode=multirow, the original query returns a\n" | 
					
						
							|  |  |  | "result-id from which results may be fetched.  This function implements the\n" | 
					
						
							|  |  |  | "actual fetch of the results.\n", | 
					
						
							|  |  |  | 	.read = acf_fetch, | 
					
						
							|  |  |  | 	.write = NULL, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *app_odbcfinish = "ODBCFinish"; | 
					
						
							|  |  |  | static char *syn_odbcfinish = "Clear the resultset of a successful multirow query"; | 
					
						
							|  |  |  | static char *desc_odbcfinish = | 
					
						
							|  |  |  | "ODBCFinish(<result-id>)\n" | 
					
						
							|  |  |  | "  Clears any remaining rows of the specified resultset\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int exec_odbcfinish(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_datastore *store = ast_channel_datastore_find(chan, &odbc_info, data); | 
					
						
							|  |  |  | 	if (!store) /* Already freed; no big deal. */ | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	ast_channel_datastore_remove(chan, store); | 
					
						
							|  |  |  | 	ast_channel_datastore_free(store); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | static int init_acf_query(struct ast_config *cfg, char *catg, struct acf_odbc_query **query) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-09-20 20:40:39 +00:00
										 |  |  | 	const char *tmp; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!cfg || !catg) { | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return EINVAL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	*query = ast_calloc(1, sizeof(struct acf_odbc_query)); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	if (! (*query)) | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return ENOMEM; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (((tmp = ast_variable_retrieve(cfg, catg, "writehandle"))) || ((tmp = ast_variable_retrieve(cfg, catg, "dsn")))) { | 
					
						
							|  |  |  | 		char *tmp2 = ast_strdupa(tmp); | 
					
						
							|  |  |  | 		AST_DECLARE_APP_ARGS(write, | 
					
						
							|  |  |  | 			AST_APP_ARG(dsn)[5]; | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2008-01-02 21:08:33 +00:00
										 |  |  | 		AST_STANDARD_APP_ARGS(write, tmp2); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		for (i = 0; i < 5; i++) { | 
					
						
							|  |  |  | 			if (!ast_strlen_zero(write.dsn[i])) | 
					
						
							|  |  |  | 				ast_copy_string((*query)->writehandle[i], write.dsn[i], sizeof((*query)->writehandle[i])); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "readhandle"))) { | 
					
						
							|  |  |  | 		char *tmp2 = ast_strdupa(tmp); | 
					
						
							|  |  |  | 		AST_DECLARE_APP_ARGS(read, | 
					
						
							|  |  |  | 			AST_APP_ARG(dsn)[5]; | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2008-01-02 21:08:33 +00:00
										 |  |  | 		AST_STANDARD_APP_ARGS(read, tmp2); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		for (i = 0; i < 5; i++) { | 
					
						
							|  |  |  | 			if (!ast_strlen_zero(read.dsn[i])) | 
					
						
							|  |  |  | 				ast_copy_string((*query)->readhandle[i], read.dsn[i], sizeof((*query)->readhandle[i])); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		/* If no separate readhandle, then use the writehandle for reading */ | 
					
						
							|  |  |  | 		for (i = 0; i < 5; i++) { | 
					
						
							|  |  |  | 			if (!ast_strlen_zero((*query)->writehandle[i])) | 
					
						
							|  |  |  | 				ast_copy_string((*query)->readhandle[i], (*query)->writehandle[i], sizeof((*query)->readhandle[i])); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  |  	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "readsql"))) | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read)); | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	else if ((tmp = ast_variable_retrieve(cfg, catg, "read"))) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Parameter 'read' is deprecated for category %s.  Please use 'readsql' instead.\n", catg); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		ast_copy_string((*query)->sql_read, tmp, sizeof((*query)->sql_read)); | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!ast_strlen_zero((*query)->sql_read) && ast_strlen_zero((*query)->readhandle[0])) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for reading: %s\n", catg); | 
					
						
							|  |  |  | 		return EINVAL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "writesql"))) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write)); | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	else if ((tmp = ast_variable_retrieve(cfg, catg, "write"))) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Parameter 'write' is deprecated for category %s.  Please use 'writesql' instead.\n", catg); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_copy_string((*query)->sql_write, tmp, sizeof((*query)->sql_write)); | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (!ast_strlen_zero((*query)->sql_write) && ast_strlen_zero((*query)->writehandle[0])) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "There is SQL, but no ODBC class to be used for writing: %s\n", catg); | 
					
						
							|  |  |  | 		return EINVAL; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-22 03:40:31 +00:00
										 |  |  | 	/* Allow escaping of embedded commas in fields to be turned off */ | 
					
						
							|  |  |  | 	ast_set_flag((*query), OPT_ESCAPECOMMAS); | 
					
						
							|  |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "escapecommas"))) { | 
					
						
							|  |  |  | 		if (ast_false(tmp)) | 
					
						
							|  |  |  | 			ast_clear_flag((*query), OPT_ESCAPECOMMAS); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "mode"))) { | 
					
						
							|  |  |  | 		if (strcasecmp(tmp, "multirow") == 0) | 
					
						
							|  |  |  | 			ast_set_flag((*query), OPT_MULTIROW); | 
					
						
							|  |  |  | 		if ((tmp = ast_variable_retrieve(cfg, catg, "rowlimit"))) | 
					
						
							|  |  |  | 			sscanf(tmp, "%d", &((*query)->rowlimit)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	(*query)->acf = ast_calloc(1, sizeof(struct ast_custom_function)); | 
					
						
							|  |  |  | 	if (! (*query)->acf) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return ENOMEM; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((tmp = ast_variable_retrieve(cfg, catg, "prefix")) && !ast_strlen_zero(tmp)) { | 
					
						
							|  |  |  | 		asprintf((char **)&((*query)->acf->name), "%s_%s", tmp, catg); | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2006-04-11 05:37:05 +00:00
										 |  |  | 		asprintf((char **)&((*query)->acf->name), "ODBC_%s", catg); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	if (!((*query)->acf->name)) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free((*query)->acf); | 
					
						
							|  |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return ENOMEM; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	asprintf((char **)&((*query)->acf->syntax), "%s(<arg1>[...[,<argN>]])", (*query)->acf->name); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	if (!((*query)->acf->syntax)) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free((char *)(*query)->acf->name); | 
					
						
							|  |  |  | 		ast_free((*query)->acf); | 
					
						
							|  |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return ENOMEM; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	(*query)->acf->synopsis = "Runs the referenced query with the specified arguments"; | 
					
						
							|  |  |  | 	if (!ast_strlen_zero((*query)->sql_read) && !ast_strlen_zero((*query)->sql_write)) { | 
					
						
							|  |  |  | 		asprintf((char **)&((*query)->acf->desc), | 
					
						
							|  |  |  | 					"Runs the following query, as defined in func_odbc.conf, performing\n" | 
					
						
							|  |  |  | 				   	"substitution of the arguments into the query as specified by ${ARG1},\n" | 
					
						
							|  |  |  | 					"${ARG2}, ... ${ARGn}.  When setting the function, the values are provided\n" | 
					
						
							|  |  |  | 					"either in whole as ${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n" | 
					
						
							|  |  |  | 					"\nRead:\n%s\n\nWrite:\n%s\n", | 
					
						
							|  |  |  | 					(*query)->sql_read, | 
					
						
							|  |  |  | 					(*query)->sql_write); | 
					
						
							|  |  |  | 	} else if (!ast_strlen_zero((*query)->sql_read)) { | 
					
						
							|  |  |  | 		asprintf((char **)&((*query)->acf->desc), | 
					
						
							|  |  |  | 					"Runs the following query, as defined in func_odbc.conf, performing\n" | 
					
						
							|  |  |  | 				   	"substitution of the arguments into the query as specified by ${ARG1},\n" | 
					
						
							|  |  |  | 					"${ARG2}, ... ${ARGn}.  This function may only be read, not set.\n\nSQL:\n%s\n", | 
					
						
							|  |  |  | 					(*query)->sql_read); | 
					
						
							|  |  |  | 	} else if (!ast_strlen_zero((*query)->sql_write)) { | 
					
						
							|  |  |  | 		asprintf((char **)&((*query)->acf->desc), | 
					
						
							|  |  |  | 					"Runs the following query, as defined in func_odbc.conf, performing\n" | 
					
						
							|  |  |  | 				   	"substitution of the arguments into the query as specified by ${ARG1},\n" | 
					
						
							|  |  |  | 					"${ARG2}, ... ${ARGn}.  The values are provided either in whole as\n" | 
					
						
							|  |  |  | 					"${VALUE} or parsed as ${VAL1}, ${VAL2}, ... ${VALn}.\n" | 
					
						
							|  |  |  | 					"This function may only be set.\nSQL:\n%s\n", | 
					
						
							|  |  |  | 					(*query)->sql_write); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free((char *)(*query)->acf->syntax); | 
					
						
							|  |  |  | 		ast_free((char *)(*query)->acf->name); | 
					
						
							|  |  |  | 		ast_free((*query)->acf); | 
					
						
							|  |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "Section %s was found, but there was no SQL to execute.  Ignoring.\n", catg); | 
					
						
							|  |  |  | 		return EINVAL; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (! ((*query)->acf->desc)) { | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free((char *)(*query)->acf->syntax); | 
					
						
							|  |  |  | 		ast_free((char *)(*query)->acf->name); | 
					
						
							|  |  |  | 		ast_free((*query)->acf); | 
					
						
							|  |  |  | 		ast_free(*query); | 
					
						
							| 
									
										
										
										
											2007-01-17 23:35:43 +00:00
										 |  |  | 		*query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		return ENOMEM; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero((*query)->sql_read)) { | 
					
						
							|  |  |  | 		(*query)->acf->read = NULL; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		(*query)->acf->read = acf_odbc_read; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero((*query)->sql_write)) { | 
					
						
							|  |  |  | 		(*query)->acf->write = NULL; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		(*query)->acf->write = acf_odbc_write; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int free_acf_query(struct acf_odbc_query *query) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (query) { | 
					
						
							|  |  |  | 		if (query->acf) { | 
					
						
							|  |  |  | 			if (query->acf->name) | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 				ast_free((char *)query->acf->name); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			if (query->acf->syntax) | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 				ast_free((char *)query->acf->syntax); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			if (query->acf->desc) | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 				ast_free((char *)query->acf->desc); | 
					
						
							|  |  |  | 			ast_free(query->acf); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-06-06 21:20:11 +00:00
										 |  |  | 		ast_free(query); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	struct ast_config *cfg; | 
					
						
							|  |  |  | 	char *catg; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	struct ast_flags config_flags = { 0 }; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	res |= ast_custom_function_register(&fetch_function); | 
					
						
							|  |  |  | 	res |= ast_register_application(app_odbcfinish, exec_odbcfinish, syn_odbcfinish, desc_odbcfinish); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	cfg = ast_config_load(config, config_flags); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	if (!cfg) { | 
					
						
							| 
									
										
										
										
											2006-05-26 03:08:15 +00:00
										 |  |  | 		ast_log(LOG_NOTICE, "Unable to load config for func_odbc: %s\n", config); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2006-08-31 21:00:20 +00:00
										 |  |  | 		return AST_MODULE_LOAD_DECLINE; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (catg = ast_category_browse(cfg, NULL); | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | 	     catg; | 
					
						
							|  |  |  | 	     catg = ast_category_browse(cfg, catg)) { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 		struct acf_odbc_query *query = NULL; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 		int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((err = init_acf_query(cfg, catg, &query))) { | 
					
						
							|  |  |  | 			if (err == ENOMEM) | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Out of memory\n"); | 
					
						
							|  |  |  | 			else if (err == EINVAL) | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Invalid parameters for category %s\n", catg); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "%s (%d)\n", strerror(err), err); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 			AST_LIST_INSERT_HEAD(&queries, query, list); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 			ast_custom_function_register(query->acf); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_config_destroy(cfg); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	res |= ast_custom_function_register(&escape_function); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	struct acf_odbc_query *query; | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	int res = 0; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							|  |  |  | 	while (!AST_LIST_EMPTY(&queries)) { | 
					
						
							|  |  |  | 		query = AST_LIST_REMOVE_HEAD(&queries, list); | 
					
						
							|  |  |  | 		ast_custom_function_unregister(query->acf); | 
					
						
							|  |  |  | 		free_acf_query(query); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	res |= ast_custom_function_unregister(&escape_function); | 
					
						
							| 
									
										
										
										
											2007-05-31 15:05:56 +00:00
										 |  |  | 	res |= ast_custom_function_unregister(&fetch_function); | 
					
						
							|  |  |  | 	res |= ast_unregister_application(app_odbcfinish); | 
					
						
							| 
									
										
										
										
											2005-12-27 02:02:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	/* Allow any threads waiting for this lock to pass (avoids a race) */ | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2006-12-28 20:13:00 +00:00
										 |  |  | 	usleep(1); | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int reload(void) | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int res = 0; | 
					
						
							|  |  |  | 	struct ast_config *cfg; | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	struct acf_odbc_query *oldquery; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	char *catg; | 
					
						
							| 
									
										
										
										
											2007-08-16 21:09:46 +00:00
										 |  |  | 	struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg = ast_config_load(config, config_flags); | 
					
						
							|  |  |  | 	if (cfg == CONFIG_STATUS_FILEUNCHANGED) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_LOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	while (!AST_LIST_EMPTY(&queries)) { | 
					
						
							|  |  |  | 		oldquery = AST_LIST_REMOVE_HEAD(&queries, list); | 
					
						
							|  |  |  | 		ast_custom_function_unregister(oldquery->acf); | 
					
						
							|  |  |  | 		free_acf_query(oldquery); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!cfg) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Unable to load config for func_odbc: %s\n", config); | 
					
						
							|  |  |  | 		goto reload_out; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (catg = ast_category_browse(cfg, NULL); | 
					
						
							| 
									
										
										
										
											2006-02-11 03:14:05 +00:00
										 |  |  | 	     catg; | 
					
						
							|  |  |  | 	     catg = ast_category_browse(cfg, catg)) { | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		struct acf_odbc_query *query = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (init_acf_query(cfg, catg, &query)) { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 			ast_log(LOG_ERROR, "Cannot initialize query %s\n", catg); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 			AST_LIST_INSERT_HEAD(&queries, query, list); | 
					
						
							|  |  |  | 			ast_custom_function_register(query->acf); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_config_destroy(cfg); | 
					
						
							|  |  |  | reload_out: | 
					
						
							| 
									
										
										
										
											2006-05-06 13:36:29 +00:00
										 |  |  | 	AST_LIST_UNLOCK(&queries); | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-14 14:08:19 +00:00
										 |  |  | /* XXX need to revise usecount - set if query_lock is set */ | 
					
						
							| 
									
										
										
										
											2005-12-21 18:43:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "ODBC lookups", | 
					
						
							|  |  |  | 		.load = load_module, | 
					
						
							|  |  |  | 		.unload = unload_module, | 
					
						
							|  |  |  | 		.reload = reload, | 
					
						
							|  |  |  | 	       ); | 
					
						
							| 
									
										
										
										
											2006-04-24 17:11:45 +00:00
										 |  |  | 
 |