Merged revisions 134814 via svnmerge from
[asterisk-bristuff.git] / res / res_realtime.c
blob5ecff8b94e1162a2721a9c05d221b9f14ec06bdd
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Anthony Minessale <anthmct@yahoo.com>
7 * Mark Spencer <markster@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
20 /*! \file
22 * \brief RealTime CLI
24 * \author Anthony Minessale <anthmct@yahoo.com>
25 * \author Mark Spencer <markster@digium.com>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/file.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/config.h"
38 #include "asterisk/module.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/cli.h"
43 static char *cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
45 #define CRL_HEADER_FORMAT "%30s %-30s\n"
46 struct ast_variable *var=NULL;
48 switch (cmd) {
49 case CLI_INIT:
50 e->command = "realtime load";
51 e->usage =
52 "Usage: realtime load <family> <colmatch> <value>\n"
53 " Prints out a list of variables using the RealTime driver.\n"
54 " You must supply a family name, a column to match on, and a value to match to.\n";
55 return NULL;
56 case CLI_GENERATE:
57 return NULL;
61 if (a->argc < 5)
62 return CLI_SHOWUSAGE;
64 var = ast_load_realtime_all(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
66 if (var) {
67 ast_cli(a->fd, CRL_HEADER_FORMAT, "Column Name", "Column Value");
68 ast_cli(a->fd, CRL_HEADER_FORMAT, "--------------------", "--------------------");
69 while (var) {
70 ast_cli(a->fd, CRL_HEADER_FORMAT, var->name, var->value);
71 var = var->next;
73 } else {
74 ast_cli(a->fd, "No rows found matching search criteria.\n");
76 return CLI_SUCCESS;
79 static char *cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) {
80 int res = 0;
82 switch (cmd) {
83 case CLI_INIT:
84 e->command = "realtime update";
85 e->usage =
86 "Usage: realtime update <family> <colupdate> <newvalue> <colmatch> <valuematch>\n"
87 " Update a single variable using the RealTime driver.\n"
88 " You must supply a family name, a column to update on, a new value, column to match, and value to match.\n"
89 " Ex: realtime update sipfriends name bobsphone port 4343\n"
90 " will execute SQL as UPDATE sipfriends SET port = 4343 WHERE name = bobsphone\n";
91 return NULL;
92 case CLI_GENERATE:
93 return NULL;
97 if (a->argc < 7)
98 return CLI_SHOWUSAGE;
100 res = ast_update_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
102 if(res < 0) {
103 ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
104 return CLI_FAILURE;
107 ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
109 return CLI_SUCCESS;
112 static struct ast_cli_entry cli_realtime[] = {
113 AST_CLI_DEFINE(cli_realtime_load, "Used to print out RealTime variables."),
114 AST_CLI_DEFINE(cli_realtime_update, "Used to update RealTime variables."),
117 static int unload_module(void)
119 ast_cli_unregister_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
120 return 0;
123 static int load_module(void)
125 ast_cli_register_multiple(cli_realtime, sizeof(cli_realtime) / sizeof(struct ast_cli_entry));
126 return AST_MODULE_LOAD_SUCCESS;
129 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");