2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
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 odbc+odbc plugin for portable configuration engine
25 * \author Mark Spencer <markster@digium.com>
26 * \author Anthony Minessale II <anthmct@yahoo.com>
28 * \arg http://www.unixodbc.org
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/config.h"
45 #include "asterisk/module.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/options.h"
48 #include "asterisk/res_odbc.h"
49 #include "asterisk/utils.h"
51 static char *tdesc = "ODBC Configuration";
55 static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
63 const char *newparam, *newval;
69 struct ast_variable *var=NULL, *prev=NULL;
71 SQLSMALLINT colcount=0;
73 SQLSMALLINT decimaldigits;
84 obj = fetch_odbc_obj(database, 0);
88 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
89 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
90 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
94 newparam = va_arg(aq, const char *);
96 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
99 newval = va_arg(aq, const char *);
100 if (!strchr(newparam, ' ')) op = " ="; else op = "";
101 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
102 while((newparam = va_arg(aq, const char *))) {
103 if (!strchr(newparam, ' ')) op = " ="; else op = "";
104 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
105 newval = va_arg(aq, const char *);
108 res = SQLPrepare(stmt, sql, SQL_NTS);
109 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
110 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
111 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
115 /* Now bind the parameters */
118 while((newparam = va_arg(ap, const char *))) {
119 newval = va_arg(ap, const char *);
120 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
123 res = odbc_smart_execute(obj, stmt);
125 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
126 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
127 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
131 res = SQLNumResultCols(stmt, &colcount);
132 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
133 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
134 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
138 res = SQLFetch(stmt);
139 if (res == SQL_NO_DATA) {
140 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
143 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
144 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
145 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
148 for (x=0;x<colcount;x++) {
150 collen = sizeof(coltitle);
151 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
152 &datatype, &colsize, &decimaldigits, &nullable);
153 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
154 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
156 ast_variables_destroy(var);
161 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
162 if (indicator == SQL_NULL_DATA)
165 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
166 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
168 ast_variables_destroy(var);
173 chunk = strsep(&stringp, ";");
174 if (!ast_strlen_zero(ast_strip(chunk))) {
176 prev->next = ast_variable_new(coltitle, chunk);
180 prev = var = ast_variable_new(coltitle, chunk);
187 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
191 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
198 const char *initfield=NULL;
200 const char *newparam, *newval;
206 struct ast_variable *var=NULL;
207 struct ast_config *cfg=NULL;
208 struct ast_category *cat=NULL;
209 struct ast_realloca ra;
211 SQLSMALLINT colcount=0;
212 SQLSMALLINT datatype;
213 SQLSMALLINT decimaldigits;
214 SQLSMALLINT nullable;
215 SQLINTEGER indicator;
223 memset(&ra, 0, sizeof(ra));
225 obj = fetch_odbc_obj(database, 0);
229 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
230 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
231 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
235 newparam = va_arg(aq, const char *);
237 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
240 initfield = ast_strdupa(newparam);
241 if (initfield && (op = strchr(initfield, ' ')))
243 newval = va_arg(aq, const char *);
244 if (!strchr(newparam, ' ')) op = " ="; else op = "";
245 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
246 while((newparam = va_arg(aq, const char *))) {
247 if (!strchr(newparam, ' ')) op = " ="; else op = "";
248 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
249 newval = va_arg(aq, const char *);
252 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
254 res = SQLPrepare(stmt, sql, SQL_NTS);
255 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
256 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
257 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
261 /* Now bind the parameters */
264 while((newparam = va_arg(ap, const char *))) {
265 newval = va_arg(ap, const char *);
266 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
269 res = odbc_smart_execute(obj, stmt);
271 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
272 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
273 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
277 res = SQLNumResultCols(stmt, &colcount);
278 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
279 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
280 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
284 cfg = ast_config_new();
286 ast_log(LOG_WARNING, "Out of memory!\n");
287 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
291 while ((res=SQLFetch(stmt)) != SQL_NO_DATA) {
293 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
294 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
297 cat = ast_category_new("");
299 ast_log(LOG_WARNING, "Out of memory!\n");
302 for (x=0;x<colcount;x++) {
304 collen = sizeof(coltitle);
305 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
306 &datatype, &colsize, &decimaldigits, &nullable);
307 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
308 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
309 ast_category_destroy(cat);
314 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
315 if (indicator == SQL_NULL_DATA)
318 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
319 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
320 ast_category_destroy(cat);
325 chunk = strsep(&stringp, ";");
326 if (!ast_strlen_zero(ast_strip(chunk))) {
327 if (initfield && !strcmp(initfield, coltitle))
328 ast_category_rename(cat, chunk);
329 var = ast_variable_new(coltitle, chunk);
330 ast_variable_append(cat, var);
334 ast_category_append(cfg, cat);
337 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
341 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
347 const char *newparam, *newval;
357 obj = fetch_odbc_obj (database, 0);
361 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
362 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
363 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
367 newparam = va_arg(aq, const char *);
369 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
372 newval = va_arg(aq, const char *);
373 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam);
374 while((newparam = va_arg(aq, const char *))) {
375 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam);
376 newval = va_arg(aq, const char *);
379 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield);
381 res = SQLPrepare(stmt, sql, SQL_NTS);
382 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
383 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
384 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
388 /* Now bind the parameters */
391 while((newparam = va_arg(ap, const char *))) {
392 newval = va_arg(ap, const char *);
393 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
396 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(lookup), 0, (void *)lookup, 0, NULL);
398 res = odbc_smart_execute(obj, stmt);
400 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
401 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
402 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
406 res = SQLRowCount(stmt, &rowcount);
407 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
409 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
410 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
415 return (int)rowcount;
420 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg)
422 struct ast_variable *new_v;
423 struct ast_category *cur_cat;
426 SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
428 char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
429 SQLSMALLINT rowcount=0;
433 if (!file || !strcmp (file, "res_config_odbc.conf"))
434 return NULL; /* cant configure myself with myself ! */
436 obj = fetch_odbc_obj(database, 0);
440 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
442 SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
443 SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
444 SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
445 SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
446 SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
447 SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
448 SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
449 SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
451 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE filename='%s' and commented=0 ORDER BY filename,cat_metric desc,var_metric asc,category,var_name,var_val,id", table, file);
453 res = odbc_smart_direct_execute(obj, stmt, sql);
455 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
456 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
457 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
461 res = SQLNumResultCols (stmt, &rowcount);
463 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
464 ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
465 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
470 ast_log (LOG_NOTICE, "found nothing\n");
474 cur_cat = ast_config_get_current_category(cfg);
476 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
477 if (!strcmp (var_name, "#include")) {
478 if (!ast_config_internal_load(var_val, cfg)) {
479 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
484 if (strcmp(last, category) || last_cat_metric != cat_metric) {
485 cur_cat = ast_category_new(category);
487 ast_log(LOG_WARNING, "Out of memory!\n");
490 strcpy(last, category);
491 last_cat_metric = cat_metric;
492 ast_category_append(cfg, cur_cat);
495 new_v = ast_variable_new(var_name, var_val);
496 ast_variable_append(cur_cat, new_v);
499 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
503 static struct ast_config_engine odbc_engine = {
505 .load_func = config_odbc,
506 .realtime_func = realtime_odbc,
507 .realtime_multi_func = realtime_multi_odbc,
508 .update_func = update_odbc
511 int unload_module (void)
513 ast_config_engine_deregister(&odbc_engine);
515 ast_verbose("res_config_odbc unloaded.\n");
516 STANDARD_HANGUP_LOCALUSERS;
520 int load_module (void)
522 ast_config_engine_register(&odbc_engine);
524 ast_verbose("res_config_odbc loaded.\n");
528 char *description (void)
535 /* never unload a config module */
541 return ASTERISK_GPL_KEY;