2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * res_config_odbc.c <odbc+odbc plugin for portable configuration engine >
9 * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/config.h"
41 #include "asterisk/module.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/options.h"
44 #include "asterisk/res_odbc.h"
45 #include "asterisk/utils.h"
47 static char *tdesc = "ODBC Configuration";
52 static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
60 const char *newparam, *newval;
66 struct ast_variable *var=NULL, *prev=NULL;
69 SQLSMALLINT colcount=0;
71 SQLSMALLINT decimaldigits;
82 obj = fetch_odbc_obj(database, 0);
86 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
87 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
88 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
92 newparam = va_arg(aq, const char *);
94 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
97 newval = va_arg(aq, const char *);
98 if (!strchr(newparam, ' ')) op = " ="; else op = "";
99 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
100 while((newparam = va_arg(aq, const char *))) {
101 if (!strchr(newparam, ' ')) op = " ="; else op = "";
102 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
103 newval = va_arg(aq, const char *);
106 res = SQLPrepare(stmt, sql, SQL_NTS);
107 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
108 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
109 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
113 /* Now bind the parameters */
116 while((newparam = va_arg(ap, const char *))) {
117 newval = va_arg(ap, const char *);
118 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
121 res = odbc_smart_execute(obj, stmt);
123 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
124 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
125 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
129 res = SQLRowCount(stmt, &rowcount);
130 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
131 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
132 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
136 res = SQLNumResultCols(stmt, &colcount);
137 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
138 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
139 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
144 res = SQLFetch(stmt);
145 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
146 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
147 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
150 for (x=0;x<colcount;x++) {
152 collen = sizeof(coltitle);
153 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
154 &datatype, &colsize, &decimaldigits, &nullable);
155 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
156 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
158 ast_variables_destroy(var);
163 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
164 if (indicator == SQL_NULL_DATA)
167 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
168 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
170 ast_variables_destroy(var);
175 chunk = strsep(&stringp, ";");
176 if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
178 prev->next = ast_variable_new(coltitle, chunk);
182 prev = var = ast_variable_new(coltitle, chunk);
190 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
194 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
201 const char *initfield=NULL;
203 const char *newparam, *newval;
209 struct ast_variable *var=NULL;
210 struct ast_config *cfg=NULL;
211 struct ast_category *cat=NULL;
212 struct ast_realloca ra;
215 SQLSMALLINT colcount=0;
216 SQLSMALLINT datatype;
217 SQLSMALLINT decimaldigits;
218 SQLSMALLINT nullable;
219 SQLINTEGER indicator;
227 memset(&ra, 0, sizeof(ra));
229 obj = fetch_odbc_obj(database, 0);
233 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
234 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
235 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
239 newparam = va_arg(aq, const char *);
241 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
244 initfield = ast_strdupa(newparam);
245 if (initfield && (op = strchr(initfield, ' ')))
247 newval = va_arg(aq, const char *);
248 if (!strchr(newparam, ' ')) op = " ="; else op = "";
249 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
250 while((newparam = va_arg(aq, const char *))) {
251 if (!strchr(newparam, ' ')) op = " ="; else op = "";
252 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
253 newval = va_arg(aq, const char *);
256 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
258 res = SQLPrepare(stmt, sql, SQL_NTS);
259 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
260 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
261 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
265 /* Now bind the parameters */
268 while((newparam = va_arg(ap, const char *))) {
269 newval = va_arg(ap, const char *);
270 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
273 res = odbc_smart_execute(obj, stmt);
275 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
276 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
277 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
281 res = SQLRowCount(stmt, &rowcount);
282 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
283 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
284 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
288 res = SQLNumResultCols(stmt, &colcount);
289 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
290 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
291 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
295 cfg = ast_config_new();
297 ast_log(LOG_WARNING, "Out of memory!\n");
298 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
304 res = SQLFetch(stmt);
305 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
306 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
309 cat = ast_category_new("");
311 ast_log(LOG_WARNING, "Out of memory!\n");
314 for (x=0;x<colcount;x++) {
316 collen = sizeof(coltitle);
317 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
318 &datatype, &colsize, &decimaldigits, &nullable);
319 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
320 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
321 ast_category_destroy(cat);
326 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
327 if (indicator == SQL_NULL_DATA)
330 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
331 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
332 ast_category_destroy(cat);
337 chunk = strsep(&stringp, ";");
338 if (chunk && !ast_strlen_zero(ast_strip(chunk))) {
339 if (initfield && !strcmp(initfield, coltitle))
340 ast_category_rename(cat, chunk);
341 var = ast_variable_new(coltitle, chunk);
342 ast_variable_append(cat, var);
346 ast_category_append(cfg, cat);
349 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
353 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
359 const char *newparam, *newval;
369 obj = fetch_odbc_obj (database, 0);
373 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
374 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
375 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
379 newparam = va_arg(aq, const char *);
381 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
384 newval = va_arg(aq, const char *);
385 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam);
386 while((newparam = va_arg(aq, const char *))) {
387 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam);
388 newval = va_arg(aq, const char *);
391 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield);
393 res = SQLPrepare(stmt, sql, SQL_NTS);
394 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
395 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
396 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
400 /* Now bind the parameters */
403 while((newparam = va_arg(ap, const char *))) {
404 newval = va_arg(ap, const char *);
405 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
408 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(lookup), 0, (void *)lookup, 0, NULL);
410 res = odbc_smart_execute(obj, stmt);
412 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
413 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
414 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
418 res = SQLRowCount(stmt, &rowcount);
419 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
421 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
422 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
427 return (int)rowcount;
432 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg)
434 struct ast_variable *new_v;
435 struct ast_category *cur_cat;
438 SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
440 char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
441 SQLSMALLINT rowcount=0;
445 if (!file || !strcmp (file, "res_config_odbc.conf"))
446 return NULL; /* cant configure myself with myself ! */
448 obj = fetch_odbc_obj(database, 0);
452 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
454 SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
455 SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
456 SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
457 SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
458 SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
459 SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
460 SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
461 SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
463 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);
465 res = odbc_smart_direct_execute(obj, stmt, sql);
467 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
468 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
469 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
473 res = SQLNumResultCols (stmt, &rowcount);
475 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
476 ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
477 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
482 ast_log (LOG_NOTICE, "found nothing\n");
486 cur_cat = ast_config_get_current_category(cfg);
488 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
489 if (!strcmp (var_name, "#include")) {
490 if (!ast_config_internal_load(var_val, cfg)) {
491 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
496 if (strcmp(last, category) || last_cat_metric != cat_metric) {
497 cur_cat = ast_category_new(category);
499 ast_log(LOG_WARNING, "Out of memory!\n");
502 strcpy(last, category);
503 last_cat_metric = cat_metric;
504 ast_category_append(cfg, cur_cat);
507 new_v = ast_variable_new(var_name, var_val);
508 ast_variable_append(cur_cat, new_v);
511 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
515 static struct ast_config_engine odbc_engine = {
517 .load_func = config_odbc,
518 .realtime_func = realtime_odbc,
519 .realtime_multi_func = realtime_multi_odbc,
520 .update_func = update_odbc
523 int unload_module (void)
525 ast_config_engine_deregister(&odbc_engine);
527 ast_verbose("res_config_odbc unloaded.\n");
528 STANDARD_HANGUP_LOCALUSERS;
532 int load_module (void)
534 ast_config_engine_register(&odbc_engine);
536 ast_verbose("res_config_odbc loaded.\n");
540 char *description (void)
547 /* never unload a config module */
553 return ASTERISK_GPL_KEY;