[asterisk-commits] tilghman: trunk r157870 - /trunk/funcs/func_realtime.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Nov 19 15:54:39 CST 2008


Author: tilghman
Date: Wed Nov 19 15:54:39 2008
New Revision: 157870

URL: http://svn.digium.com/view/asterisk?view=rev&rev=157870
Log:
Two new functions, REALTIME_FIELD, and REALTIME_HASH, which should make
querying realtime from the dialplan a little more consistent and easy to use.
The original REALTIME function is preserved, for those who are already
accustomed to that interface.
(closes issue #13651)
 Reported by: Corydon76
 Patches: 
       20081119__bug13651__2.diff.txt uploaded by Corydon76 (license 14)
 Tested by: blitzrage, Corydon76

Modified:
    trunk/funcs/func_realtime.c

Modified: trunk/funcs/func_realtime.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_realtime.c?view=diff&rev=157870&r1=157869&r2=157870
==============================================================================
--- trunk/funcs/func_realtime.c (original)
+++ trunk/funcs/func_realtime.c Wed Nov 19 15:54:39 2008
@@ -98,10 +98,66 @@
 		</syntax>
 		<description>
 			<para>This function acts in the same way as REALTIME(....) does, except that
-			it destroys matched record in RT engine.</para>
+			it destroys the matched record in the RT engine.</para>
+		</description>
+	</function>
+	<function name="REALTIME_FIELD" language="en_US">
+		<synopsis>
+			RealTime query function.
+		</synopsis>
+		<syntax>
+			<parameter name="family" required="true" />
+			<parameter name="fieldmatch" required="true" />
+			<parameter name="value" required="true" />
+			<parameter name="fieldname" required="true" />
+		</syntax>
+		<description>
+			<para>This function retrieves a single item, <replaceable>fieldname</replaceable>
+			from the RT engine, where <replaceable>fieldmatch</replaceable> contains the value
+			<replaceable>value</replaceable>.  When written to, the REALTIME_FIELD() function
+			performs identically to the REALTIME() function.</para>
+		</description>
+	</function>
+	<function name="REALTIME_HASH" language="en_US">
+		<synopsis>
+			RealTime query function.
+		</synopsis>
+		<syntax>
+			<parameter name="family" required="true" />
+			<parameter name="fieldmatch" required="true" />
+			<parameter name="value" required="true" />
+		</syntax>
+		<description>
+			<para>This function retrieves a single record from the RT engine, where
+			<replaceable>fieldmatch</replaceable> contains the value
+			<replaceable>value</replaceable> and formats the output suitably, such that
+			it can be assigned to the HASH() function.  The HASH() function then provides
+			a suitable method for retrieving each field value of the record.</para>
 		</description>
 	</function>
  ***/
+
+AST_THREADSTORAGE(buf1);
+AST_THREADSTORAGE(buf2);
+AST_THREADSTORAGE(buf3);
+
+static char *hash_escape(struct ast_str **str, const char *value)
+{
+	int len;
+	ast_str_reset(*str);
+
+	if ((*str)->len < (len = strlen(value) * 2 + 1)) {
+		ast_str_make_space(str, len);
+	}
+	for (; *value; value++) {
+		if (*value == ',' || *value == '\\') {
+			(*str)->str[(*str)->used++] = '\\';
+		}
+		(*str)->str[(*str)->used++] = *value;
+	}
+	(*str)->str[(*str)->used] = '\0';
+	return (*str)->str;
+}
 
 static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) 
 {
@@ -169,7 +225,7 @@
 	);
 
 	if (ast_strlen_zero(data)) {
-		ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch,value,newcol) - missing argument!\n");
+		ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value,newcol) - missing argument!\n", cmd);
 		return -1;
 	}
 
@@ -186,6 +242,82 @@
 
 	if (chan)
 		ast_autoservice_stop(chan);
+
+	return 0;
+}
+
+static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) 
+{
+	struct ast_variable *var, *head;
+	struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16);
+	struct ast_str *fields = ast_str_thread_get(&buf2, 16);
+	struct ast_str *values = ast_str_thread_get(&buf3, 16);
+	int first;
+	enum { rtfield, rthash } which;
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(family);
+		AST_APP_ARG(fieldmatch);
+		AST_APP_ARG(value);
+		AST_APP_ARG(fieldname);
+	);
+
+	if (!strcmp(cmd, "REALTIME_FIELD")) {
+		which = rtfield;
+	} else {
+		which = rthash;
+	}
+
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, data);
+
+	if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) {
+		ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,value%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : "");
+		return -1;
+	}
+
+	if (chan) {
+		ast_autoservice_start(chan);
+	}
+
+	if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) {
+		if (chan) {
+			ast_autoservice_stop(chan);
+		}
+		return -1;
+	}
+
+	ast_str_reset(fields);
+	ast_str_reset(values);
+
+	for (var = head; var; var = var->next) {
+		if (which == rtfield) {
+			ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname);
+			if (!strcasecmp(var->name, args.fieldname)) {
+				ast_debug(1, "Match! Value is %s\n", var->value);
+				ast_copy_string(buf, var->value, len);
+				break;
+			}
+		} else if (which == rthash) {
+			ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value);
+			ast_str_append(&fields, 0, "%s%s", first ? "" : ",", hash_escape(&escapebuf, var->name));
+			ast_str_append(&values, 0, "%s%s", first ? "" : ",", hash_escape(&escapebuf, var->value));
+			first = 0;
+		}
+	}
+	ast_variables_destroy(head);
+
+	if (which == rthash) {
+		pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", fields->str);
+		ast_copy_string(buf, values->str, len);
+	}
+
+	if (chan) {
+		ast_autoservice_stop(chan);
+	}
 
 	return 0;
 }
@@ -302,6 +434,17 @@
 	.write = function_realtime_write,
 };
 
+struct ast_custom_function realtimefield_function = {
+	.name = "REALTIME_FIELD",
+	.read = realtimefield_read,
+	.write = function_realtime_write,
+};
+
+struct ast_custom_function realtimehash_function = {
+	.name = "REALTIME_HASH",
+	.read = realtimefield_read,
+};
+
 struct ast_custom_function realtime_store_function = {
 	.name = "REALTIME_STORE",
 	.write = function_realtime_store,
@@ -318,6 +461,8 @@
 	res |= ast_custom_function_unregister(&realtime_function);
 	res |= ast_custom_function_unregister(&realtime_store_function);
 	res |= ast_custom_function_unregister(&realtime_destroy_function);
+	res |= ast_custom_function_unregister(&realtimefield_function);
+	res |= ast_custom_function_unregister(&realtimehash_function);
 	return res;
 }
 
@@ -327,6 +472,8 @@
 	res |= ast_custom_function_register(&realtime_function);
 	res |= ast_custom_function_register(&realtime_store_function);
 	res |= ast_custom_function_register(&realtime_destroy_function);
+	res |= ast_custom_function_register(&realtimefield_function);
+	res |= ast_custom_function_register(&realtimehash_function);
 	return res;
 }
 




More information about the asterisk-commits mailing list