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 * http://www.unixodbc.org
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;
68 SQLSMALLINT colcount=0;
70 SQLSMALLINT decimaldigits;
81 obj = fetch_odbc_obj(database, 0);
85 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
86 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
87 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
91 newparam = va_arg(aq, const char *);
93 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
96 newval = va_arg(aq, const char *);
97 if (!strchr(newparam, ' ')) op = " ="; else op = "";
98 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
99 while((newparam = va_arg(aq, const char *))) {
100 if (!strchr(newparam, ' ')) op = " ="; else op = "";
101 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
102 newval = va_arg(aq, const char *);
105 res = SQLPrepare(stmt, sql, SQL_NTS);
106 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
107 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
108 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
112 /* Now bind the parameters */
115 while((newparam = va_arg(ap, const char *))) {
116 newval = va_arg(ap, const char *);
117 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
120 res = odbc_smart_execute(obj, stmt);
122 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
123 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
124 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
128 res = SQLNumResultCols(stmt, &colcount);
129 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
130 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
131 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
135 res = SQLFetch(stmt);
136 if (res == SQL_NO_DATA) {
137 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
140 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
141 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
142 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
145 for (x=0;x<colcount;x++) {
147 collen = sizeof(coltitle);
148 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
149 &datatype, &colsize, &decimaldigits, &nullable);
150 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
151 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
153 ast_variables_destroy(var);
158 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
159 if (indicator == SQL_NULL_DATA)
162 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
163 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
165 ast_variables_destroy(var);
170 chunk = strsep(&stringp, ";");
171 if (!ast_strlen_zero(ast_strip(chunk))) {
173 prev->next = ast_variable_new(coltitle, chunk);
177 prev = var = ast_variable_new(coltitle, chunk);
184 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
188 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
195 const char *initfield=NULL;
197 const char *newparam, *newval;
203 struct ast_variable *var=NULL;
204 struct ast_config *cfg=NULL;
205 struct ast_category *cat=NULL;
206 struct ast_realloca ra;
208 SQLSMALLINT colcount=0;
209 SQLSMALLINT datatype;
210 SQLSMALLINT decimaldigits;
211 SQLSMALLINT nullable;
212 SQLINTEGER indicator;
220 memset(&ra, 0, sizeof(ra));
222 obj = fetch_odbc_obj(database, 0);
226 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
227 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
228 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
232 newparam = va_arg(aq, const char *);
234 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
237 initfield = ast_strdupa(newparam);
238 if (initfield && (op = strchr(initfield, ' ')))
240 newval = va_arg(aq, const char *);
241 if (!strchr(newparam, ' ')) op = " ="; else op = "";
242 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
243 while((newparam = va_arg(aq, const char *))) {
244 if (!strchr(newparam, ' ')) op = " ="; else op = "";
245 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
246 newval = va_arg(aq, const char *);
249 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
251 res = SQLPrepare(stmt, sql, SQL_NTS);
252 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
253 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
254 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
258 /* Now bind the parameters */
261 while((newparam = va_arg(ap, const char *))) {
262 newval = va_arg(ap, const char *);
263 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
266 res = odbc_smart_execute(obj, stmt);
268 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
269 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
270 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
274 res = SQLNumResultCols(stmt, &colcount);
275 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
276 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
277 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
281 cfg = ast_config_new();
283 ast_log(LOG_WARNING, "Out of memory!\n");
284 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
288 while ((res=SQLFetch(stmt)) != SQL_NO_DATA) {
290 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
291 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
294 cat = ast_category_new("");
296 ast_log(LOG_WARNING, "Out of memory!\n");
299 for (x=0;x<colcount;x++) {
301 collen = sizeof(coltitle);
302 res = SQLDescribeCol(stmt, x + 1, coltitle, sizeof(coltitle), &collen,
303 &datatype, &colsize, &decimaldigits, &nullable);
304 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
305 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
306 ast_category_destroy(cat);
311 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
312 if (indicator == SQL_NULL_DATA)
315 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
316 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
317 ast_category_destroy(cat);
322 chunk = strsep(&stringp, ";");
323 if (!ast_strlen_zero(ast_strip(chunk))) {
324 if (initfield && !strcmp(initfield, coltitle))
325 ast_category_rename(cat, chunk);
326 var = ast_variable_new(coltitle, chunk);
327 ast_variable_append(cat, var);
331 ast_category_append(cfg, cat);
334 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
338 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
344 const char *newparam, *newval;
354 obj = fetch_odbc_obj (database, 0);
358 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
359 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
360 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
364 newparam = va_arg(aq, const char *);
366 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
369 newval = va_arg(aq, const char *);
370 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam);
371 while((newparam = va_arg(aq, const char *))) {
372 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam);
373 newval = va_arg(aq, const char *);
376 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield);
378 res = SQLPrepare(stmt, sql, SQL_NTS);
379 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
380 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
381 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
385 /* Now bind the parameters */
388 while((newparam = va_arg(ap, const char *))) {
389 newval = va_arg(ap, const char *);
390 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
393 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(lookup), 0, (void *)lookup, 0, NULL);
395 res = odbc_smart_execute(obj, stmt);
397 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
398 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
399 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
403 res = SQLRowCount(stmt, &rowcount);
404 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
406 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
407 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
412 return (int)rowcount;
417 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg)
419 struct ast_variable *new_v;
420 struct ast_category *cur_cat;
423 SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
425 char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
426 SQLSMALLINT rowcount=0;
430 if (!file || !strcmp (file, "res_config_odbc.conf"))
431 return NULL; /* cant configure myself with myself ! */
433 obj = fetch_odbc_obj(database, 0);
437 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
439 SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
440 SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
441 SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
442 SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
443 SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
444 SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
445 SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
446 SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
448 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);
450 res = odbc_smart_direct_execute(obj, stmt, sql);
452 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
453 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
454 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
458 res = SQLNumResultCols (stmt, &rowcount);
460 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
461 ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
462 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
467 ast_log (LOG_NOTICE, "found nothing\n");
471 cur_cat = ast_config_get_current_category(cfg);
473 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
474 if (!strcmp (var_name, "#include")) {
475 if (!ast_config_internal_load(var_val, cfg)) {
476 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
481 if (strcmp(last, category) || last_cat_metric != cat_metric) {
482 cur_cat = ast_category_new(category);
484 ast_log(LOG_WARNING, "Out of memory!\n");
487 strcpy(last, category);
488 last_cat_metric = cat_metric;
489 ast_category_append(cfg, cur_cat);
492 new_v = ast_variable_new(var_name, var_val);
493 ast_variable_append(cur_cat, new_v);
496 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
500 static struct ast_config_engine odbc_engine = {
502 .load_func = config_odbc,
503 .realtime_func = realtime_odbc,
504 .realtime_multi_func = realtime_multi_odbc,
505 .update_func = update_odbc
508 int unload_module (void)
510 ast_config_engine_deregister(&odbc_engine);
512 ast_verbose("res_config_odbc unloaded.\n");
513 STANDARD_HANGUP_LOCALUSERS;
517 int load_module (void)
519 ast_config_engine_register(&odbc_engine);
521 ast_verbose("res_config_odbc loaded.\n");
525 char *description (void)
532 /* never unload a config module */
538 return ASTERISK_GPL_KEY;