From a96606b32934c1b756364fda6e1fb2c4825e54d7 Mon Sep 17 00:00:00 2001
From: Russell Bryant <russell@russellbryant.com>
Date: Thu, 25 May 2006 15:40:38 +0000
Subject: [PATCH] add DB_DELETE function for the common case of retrieving and
 deleting a key in a single operation (issue #7214, twilson)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@30241 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_db.c   | 10 +++++++++-
 funcs/func_db.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/apps/app_db.c b/apps/app_db.c
index 4c5f5c6fa0..55fe25dcc0 100644
--- a/apps/app_db.c
+++ b/apps/app_db.c
@@ -48,9 +48,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/lock.h"
 #include "asterisk/options.h"
 
+/*! \todo XXX Remove this application after 1.4 is relased */
 static char *d_descrip =
 "  DBdel(family/key): This applicaiton will delete a key from the Asterisk\n"
-"database.\n";
+"database.\n"
+"  This application has been DEPRECATED in favor of the DB_DELETE function.\n";
 
 static char *dt_descrip =
 "  DBdeltree(family[/keytree]): This application will delete a family or keytree\n"
@@ -109,9 +111,15 @@ static int del_exec(struct ast_channel *chan, void *data)
 {
 	char *argv, *family, *key;
 	struct localuser *u;
+	static int deprecation_warning = 0;
 
 	LOCAL_USER_ADD(u);
 
+	if (!deprecation_warning) {
+		deprecation_warning = 1;
+		ast_log(LOG_WARNING, "The DBdel application has been deprecated in favor of the DB_DELETE dialplan function!\n");
+	}
+
 	argv = ast_strdupa(data);
 
 	if (strchr(argv, '/')) {
diff --git a/funcs/func_db.c b/funcs/func_db.c
index f7439302bf..e35859d1a0 100644
--- a/funcs/func_db.c
+++ b/funcs/func_db.c
@@ -159,6 +159,53 @@ static struct ast_custom_function db_exists_function = {
 	.read = function_db_exists,
 };
 
+static int function_db_delete(struct ast_channel *chan, char* cmd,
+			      char *parse, char *buf, size_t len)
+{
+	AST_DECLARE_APP_ARGS(args,
+			     AST_APP_ARG(family);
+			     AST_APP_ARG(key);
+	);
+
+	buf[0] = '\0';
+
+	if (ast_strlen_zero(parse)) {
+		ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
+		return -1;
+	}
+
+	AST_NONSTANDARD_APP_ARGS(args, parse, '/');
+
+	if (args.argc < 2) {
+		ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
+		return -1;
+	}
+
+	if (ast_db_get(args.family, args.key, buf, len - 1)) {
+		ast_log(LOG_DEBUG, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
+	} else {
+		if (ast_db_del(args.family, args.key)) {
+			ast_log(LOG_DEBUG, "DB_DELETE: %s/%s could not be deleted from the database\n", 
+				args.family, args.key);
+		}
+	}
+	pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
+
+	return 0;
+}
+
+
+static struct ast_custom_function db_delete_function = {
+	.name = "DB_DELETE",
+	.synopsis = "Return a value from the database and delete it",
+	.syntax = "DB_DELETE(<family>/<key>)",
+	.desc =
+		"This function will retrieve a value from the Asterisk database\n"
+		" and then remove that key from the database.  DB_RESULT\n"
+		"will be set to the key's value if it exists.\n",
+	.read = function_db_delete,
+};
+
 static char *tdesc = "Database (astdb) related dialplan functions";
 
 static int unload_module(void *mod)
@@ -167,6 +214,7 @@ static int unload_module(void *mod)
 
 	res |= ast_custom_function_unregister(&db_function);
 	res |= ast_custom_function_unregister(&db_exists_function);
+	res |= ast_custom_function_unregister(&db_delete_function);
 
 	return res;
 }
@@ -177,6 +225,7 @@ static int load_module(void *mod)
 
 	res |= ast_custom_function_register(&db_function);
 	res |= ast_custom_function_register(&db_exists_function);
+	res |= ast_custom_function_register(&db_delete_function);
 
 	return res;
 }
-- 
GitLab