2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005-2006, Russell Bryant <russelb@clemson.edu>
6 * func_db.c adapted from the old app_db.c, copyright by the following people
7 * Copyright (C) 2005, Mark Spencer <markster@digium.com>
8 * Copyright (C) 2003, Jefferson Noxon <jeff@debian.org>
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief Functions for interaction with the Asterisk database
25 * \author Russell Bryant <russelb@clemson.edu>
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/module.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/app.h"
45 #include "asterisk/astdb.h"
48 <function name="DB" language="en_US">
50 Read from or write to the Asterisk database.
53 <parameter name="family" required="true" />
54 <parameter name="key" required="true" />
57 <para>This function will read from or write a value to the Asterisk database. On a
58 read, this function returns the corresponding value from the database, or blank
59 if it does not exist. Reading a database value will also set the variable
60 DB_RESULT. If you wish to find out if an entry exists, use the DB_EXISTS
64 <ref type="application">DBdel</ref>
65 <ref type="function">DB_DELETE</ref>
66 <ref type="application">DBdeltree</ref>
67 <ref type="function">DB_EXISTS</ref>
70 <function name="DB_EXISTS" language="en_US">
72 Check to see if a key exists in the Asterisk database.
75 <parameter name="family" required="true" />
76 <parameter name="key" required="true" />
79 <para>This function will check to see if a key exists in the Asterisk
80 database. If it exists, the function will return <literal>1</literal>. If not,
81 it will return <literal>0</literal>. Checking for existence of a database key will
82 also set the variable DB_RESULT to the key's value if it exists.</para>
85 <ref type="function">DB</ref>
88 <function name="DB_KEYS" language="en_US">
90 Obtain a list of keys within the Asterisk database.
93 <parameter name="prefix" />
96 <para>This function will return a comma-separated list of keys existing
97 at the prefix specified within the Asterisk database. If no argument is
98 provided, then a list of key families will be returned.</para>
101 <function name="DB_DELETE" language="en_US">
103 Return a value from the database and delete it.
106 <parameter name="family" required="true" />
107 <parameter name="key" required="true" />
110 <para>This function will retrieve a value from the Asterisk database
111 and then remove that key from the database. <variable>DB_RESULT</variable>
112 will be set to the key's value if it exists.</para>
115 <ref type="application">DBdel</ref>
116 <ref type="function">DB</ref>
117 <ref type="application">DBdeltree</ref>
122 static int function_db_read(struct ast_channel *chan, const char *cmd,
123 char *parse, char *buf, size_t len)
125 AST_DECLARE_APP_ARGS(args,
132 if (ast_strlen_zero(parse)) {
133 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
137 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
140 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)\n");
144 if (ast_db_get(args.family, args.key, buf, len - 1)) {
145 ast_debug(1, "DB: %s/%s not found in database.\n", args.family, args.key);
147 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
153 static int function_db_write(struct ast_channel *chan, const char *cmd, char *parse,
156 AST_DECLARE_APP_ARGS(args,
161 if (ast_strlen_zero(parse)) {
162 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=<value>\n");
166 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
169 ast_log(LOG_WARNING, "DB requires an argument, DB(<family>/<key>)=value\n");
173 if (ast_db_put(args.family, args.key, value)) {
174 ast_log(LOG_WARNING, "DB: Error writing value to database.\n");
180 static struct ast_custom_function db_function = {
182 .read = function_db_read,
183 .write = function_db_write,
186 static int function_db_exists(struct ast_channel *chan, const char *cmd,
187 char *parse, char *buf, size_t len)
189 AST_DECLARE_APP_ARGS(args,
196 if (ast_strlen_zero(parse)) {
197 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
201 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
204 ast_log(LOG_WARNING, "DB_EXISTS requires an argument, DB(<family>/<key>)\n");
208 if (ast_db_get(args.family, args.key, buf, len - 1)) {
211 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
218 static struct ast_custom_function db_exists_function = {
220 .read = function_db_exists,
224 static int function_db_keys(struct ast_channel *chan, const char *cmd, char *parse, struct ast_str **result, ssize_t maxlen)
226 size_t parselen = strlen(parse);
227 struct ast_db_entry *dbe, *orig_dbe;
228 struct ast_str *escape_buf = NULL;
229 const char *last = "";
231 /* Remove leading and trailing slashes */
232 while (parse[0] == '/') {
236 while (parse[parselen - 1] == '/') {
237 parse[--parselen] = '\0';
240 ast_str_reset(*result);
242 /* Nothing within the database at that prefix? */
243 if (!(orig_dbe = dbe = ast_db_gettree(parse, NULL))) {
247 for (; dbe; dbe = dbe->next) {
248 /* Find the current component */
249 char *curkey = &dbe->key[parselen + 1], *slash;
250 if (*curkey == '/') {
253 /* Remove everything after the current component */
254 if ((slash = strchr(curkey, '/'))) {
258 /* Skip duplicates */
259 if (!strcasecmp(last, curkey)) {
264 if (orig_dbe != dbe) {
265 ast_str_append(result, maxlen, ",");
267 ast_str_append_escapecommas(result, maxlen, curkey, strlen(curkey));
269 ast_db_freetree(orig_dbe);
270 ast_free(escape_buf);
274 static struct ast_custom_function db_keys_function = {
276 .read2 = function_db_keys,
279 static int function_db_delete(struct ast_channel *chan, const char *cmd,
280 char *parse, char *buf, size_t len)
282 AST_DECLARE_APP_ARGS(args,
289 if (ast_strlen_zero(parse)) {
290 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
294 AST_NONSTANDARD_APP_ARGS(args, parse, '/');
297 ast_log(LOG_WARNING, "DB_DELETE requires an argument, DB_DELETE(<family>/<key>)\n");
301 if (ast_db_get(args.family, args.key, buf, len - 1)) {
302 ast_debug(1, "DB_DELETE: %s/%s not found in database.\n", args.family, args.key);
304 if (ast_db_del(args.family, args.key)) {
305 ast_debug(1, "DB_DELETE: %s/%s could not be deleted from the database\n", args.family, args.key);
309 pbx_builtin_setvar_helper(chan, "DB_RESULT", buf);
315 static struct ast_custom_function db_delete_function = {
317 .read = function_db_delete,
320 static int unload_module(void)
324 res |= ast_custom_function_unregister(&db_function);
325 res |= ast_custom_function_unregister(&db_exists_function);
326 res |= ast_custom_function_unregister(&db_delete_function);
327 res |= ast_custom_function_unregister(&db_keys_function);
332 static int load_module(void)
336 res |= ast_custom_function_register(&db_function);
337 res |= ast_custom_function_register(&db_exists_function);
338 res |= ast_custom_function_register(&db_delete_function);
339 res |= ast_custom_function_register(&db_keys_function);
344 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Database (astdb) related dialplan functions");