Thu Apr 28 2011 17:15:25

Asterisk developer's documentation


res_realtime.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Anthony Minessale <anthmct@yahoo.com>
00007  * Mark Spencer <markster@digium.com>
00008  *
00009  * See http://www.asterisk.org for more information about
00010  * the Asterisk project. Please do not directly contact
00011  * any of the maintainers of this project for assistance;
00012  * the project provides a web site, mailing lists and IRC
00013  * channels for your use.
00014  *
00015  * This program is free software, distributed under the terms of
00016  * the GNU General Public License Version 2. See the LICENSE file
00017  * at the top of the source tree.
00018  */
00019 
00020 /*! \file
00021  *
00022  * \brief RealTime CLI
00023  *
00024  * \author Anthony Minessale <anthmct@yahoo.com>
00025  * \author Mark Spencer <markster@digium.com>
00026  * 
00027  * \ingroup applications
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 208053 $")
00033 
00034 #include "asterisk/file.h"
00035 #include "asterisk/channel.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/config.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/lock.h"
00040 #include "asterisk/cli.h"
00041 
00042 
00043 static char *cli_realtime_load(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 
00044 {
00045 #define CRL_HEADER_FORMAT "%30s  %-30s\n"
00046    struct ast_variable *var = NULL, *orig_var = NULL;
00047 
00048    switch (cmd) {
00049    case CLI_INIT:
00050       e->command = "realtime load";
00051       e->usage =
00052          "Usage: realtime load <family> <colmatch> <value>\n"
00053          "       Prints out a list of variables using the RealTime driver.\n"
00054          "       You must supply a family name, a column to match on, and a value to match to.\n";
00055       return NULL;
00056    case CLI_GENERATE:
00057       return NULL;
00058    }
00059 
00060 
00061    if (a->argc < 5) 
00062       return CLI_SHOWUSAGE;
00063 
00064    var = ast_load_realtime_all(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00065 
00066    if (var) {
00067       ast_cli(a->fd, CRL_HEADER_FORMAT, "Column Name", "Column Value");
00068       ast_cli(a->fd, CRL_HEADER_FORMAT, "--------------------", "--------------------");
00069       orig_var = var;
00070       while (var) {
00071          ast_cli(a->fd, CRL_HEADER_FORMAT, var->name, var->value);
00072          var = var->next;
00073       }
00074    } else {
00075       ast_cli(a->fd, "No rows found matching search criteria.\n");
00076    }
00077    ast_variables_destroy(orig_var);
00078    return CLI_SUCCESS;
00079 }
00080 
00081 static char *cli_realtime_update(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00082 {
00083    int res = 0;
00084 
00085    switch (cmd) {
00086    case CLI_INIT:
00087       e->command = "realtime update";
00088       e->usage =
00089          "Usage: realtime update <family> <colmatch> <valuematch> <colupdate> <newvalue>\n"
00090          "       Update a single variable using the RealTime driver.\n"
00091          "       You must supply a family name, a column to update on, a new value, column to match, and value to match.\n"
00092          "       Ex: realtime update sipfriends name bobsphone port 4343\n"
00093          "       will execute SQL as UPDATE sipfriends SET port = 4343 WHERE name = bobsphone\n";
00094       return NULL;
00095    case CLI_GENERATE:
00096       return NULL;
00097    }
00098 
00099    if (a->argc < 7) 
00100       return CLI_SHOWUSAGE;
00101 
00102    res = ast_update_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00103 
00104    if (res < 0) {
00105       ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00106       return CLI_FAILURE;
00107    }
00108 
00109    ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
00110 
00111    return CLI_SUCCESS;
00112 }
00113 
00114 static char *cli_realtime_update2(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00115 {
00116    int res = -1;
00117 
00118    switch (cmd) {
00119    case CLI_INIT:
00120       e->command = "realtime update2";
00121       e->usage =
00122          "Usage: realtime update2 <family> <colmatch> <valuematch> [... <colmatch5> <valuematch5>] NULL <colupdate> <newvalue>\n"
00123          "   Update a single variable, requiring one or more fields to match using the\n"
00124          "   RealTime driver.  You must supply a family name, a column to update, a new\n"
00125          "   value, and at least one column and value to match.\n"
00126          "   Ex: realtime update sipfriends name bobsphone ipaddr 127.0.0.1 NULL port 4343\n"
00127          "   will execute SQL as\n"
00128          "   UPDATE sipfriends SET port='4343' WHERE name='bobsphone' and ipaddr='127.0.0.1'\n";
00129       return NULL;
00130    case CLI_GENERATE:
00131       return NULL;
00132    }
00133 
00134    if (a->argc < 7) 
00135       return CLI_SHOWUSAGE;
00136 
00137    if (a->argc == 7) {
00138       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL, a->argv[5], a->argv[6], SENTINEL);
00139    } else if (a->argc == 9) {
00140       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL, a->argv[7], a->argv[8], SENTINEL);
00141    } else if (a->argc == 11) {
00142       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL, a->argv[9], a->argv[10], SENTINEL);
00143    } else if (a->argc == 13) {
00144       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL, a->argv[11], a->argv[12], SENTINEL);
00145    } else if (a->argc == 15) {
00146       res = ast_update2_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL, a->argv[13], a->argv[14], SENTINEL);
00147    } else {
00148       return CLI_SHOWUSAGE;
00149    }
00150 
00151    if (res < 0) {
00152       ast_cli(a->fd, "Failed to update. Check the debug log for possible SQL related entries.\n");
00153       return CLI_FAILURE;
00154    }
00155 
00156    ast_cli(a->fd, "Updated %d RealTime record%s.\n", res, ESS(res));
00157 
00158    return CLI_SUCCESS;
00159 }
00160 
00161 static char *cli_realtime_store(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00162 {
00163    int res = -1;
00164 
00165    switch (cmd) {
00166    case CLI_INIT:
00167       e->command = "realtime store";
00168       e->usage =
00169          "Usage: realtime store <family> <colname1> <value1> [<colname2> <value2> [... <colname5> <value5>]]\n"
00170          "       Create a stored row using the RealTime driver.\n"
00171          "       You must supply a family name and name/value pairs (up to 5).  If\n"
00172          "       you need to store more than 5 key/value pairs, start with the first\n"
00173          "       five, then use 'realtime update' or 'realtime update2' to add\n"
00174          "       additional columns.\n";
00175       return NULL;
00176    case CLI_GENERATE:
00177       return NULL;
00178    }
00179 
00180    if (a->argc < 5) {
00181       return CLI_SHOWUSAGE;
00182    } else if (a->argc == 5) {
00183       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00184    } else if (a->argc == 7) {
00185       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00186    } else if (a->argc == 9) {
00187       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
00188    } else if (a->argc == 11) {
00189       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
00190    } else if (a->argc == 13) {
00191       res = ast_store_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
00192    } else {
00193       return CLI_SHOWUSAGE;
00194    }
00195 
00196    if (res < 0) {
00197       ast_cli(a->fd, "Failed to store record. Check the debug log for possible SQL related entries.\n");
00198       return CLI_FAILURE;
00199    }
00200 
00201    ast_cli(a->fd, "Stored RealTime record.\n");
00202 
00203    return CLI_SUCCESS;
00204 }
00205 
00206 static char *cli_realtime_destroy(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00207 {
00208    int res = -1;
00209 
00210    switch (cmd) {
00211    case CLI_INIT:
00212       e->command = "realtime destroy";
00213       e->usage =
00214          "Usage: realtime destroy <family> <colmatch1> <valuematch1> [<colmatch2> <valuematch2> [... <colmatch5> <valuematch5>]]\n"
00215          "       Remove a stored row using the RealTime driver.\n"
00216          "       You must supply a family name and name/value pairs (up to 5).\n";
00217       return NULL;
00218    case CLI_GENERATE:
00219       return NULL;
00220    }
00221 
00222    if (a->argc < 5) {
00223       return CLI_SHOWUSAGE;
00224    } else if (a->argc == 5) {
00225       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], SENTINEL);
00226    } else if (a->argc == 7) {
00227       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], SENTINEL);
00228    } else if (a->argc == 9) {
00229       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], SENTINEL);
00230    } else if (a->argc == 11) {
00231       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], SENTINEL);
00232    } else if (a->argc == 13) {
00233       res = ast_destroy_realtime(a->argv[2], a->argv[3], a->argv[4], a->argv[5], a->argv[6], a->argv[7], a->argv[8], a->argv[9], a->argv[10], a->argv[11], a->argv[12], SENTINEL);
00234    } else {
00235       return CLI_SHOWUSAGE;
00236    }
00237 
00238    if (res < 0) {
00239       ast_cli(a->fd, "Failed to remove record. Check the debug log for possible SQL related entries.\n");
00240       return CLI_FAILURE;
00241    }
00242 
00243    ast_cli(a->fd, "Removed %d RealTime record%s.\n", res, ESS(res));
00244 
00245    return CLI_SUCCESS;
00246 }
00247 
00248 static struct ast_cli_entry cli_realtime[] = {
00249    AST_CLI_DEFINE(cli_realtime_load, "Used to print out RealTime variables."),
00250    AST_CLI_DEFINE(cli_realtime_update, "Used to update RealTime variables."),
00251    AST_CLI_DEFINE(cli_realtime_update2, "Used to test the RealTime update2 method"),
00252    AST_CLI_DEFINE(cli_realtime_store, "Store a new row into a RealTime database"),
00253    AST_CLI_DEFINE(cli_realtime_destroy, "Delete a row from a RealTime database"),
00254 };
00255 
00256 static int unload_module(void)
00257 {
00258    ast_cli_unregister_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
00259    return 0;
00260 }
00261 
00262 static int load_module(void)
00263 {
00264    ast_cli_register_multiple(cli_realtime, ARRAY_LEN(cli_realtime));
00265    return AST_MODULE_LOAD_SUCCESS;
00266 }
00267 
00268 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Realtime Data Lookup/Rewrite");