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"
53 static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
61 const char *newparam, *newval;
67 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, (unsigned char *)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 = SQLNumResultCols(stmt, &colcount);
130 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
131 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
132 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
136 res = SQLFetch(stmt);
137 if (res == SQL_NO_DATA) {
138 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
141 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
142 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
143 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
146 for (x=0;x<colcount;x++) {
148 collen = sizeof(coltitle);
149 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
150 &datatype, &colsize, &decimaldigits, &nullable);
151 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
152 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
154 ast_variables_destroy(var);
159 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
160 if (indicator == SQL_NULL_DATA)
163 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
164 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
166 ast_variables_destroy(var);
171 chunk = strsep(&stringp, ";");
172 if (!ast_strlen_zero(ast_strip(chunk))) {
174 prev->next = ast_variable_new(coltitle, chunk);
178 prev = var = ast_variable_new(coltitle, chunk);
185 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
189 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
196 const char *initfield=NULL;
198 const char *newparam, *newval;
204 struct ast_variable *var=NULL;
205 struct ast_config *cfg=NULL;
206 struct ast_category *cat=NULL;
207 struct ast_realloca ra;
209 SQLSMALLINT colcount=0;
210 SQLSMALLINT datatype;
211 SQLSMALLINT decimaldigits;
212 SQLSMALLINT nullable;
213 SQLINTEGER indicator;
221 memset(&ra, 0, sizeof(ra));
223 obj = fetch_odbc_obj(database, 0);
227 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
228 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
229 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
233 newparam = va_arg(aq, const char *);
235 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
238 initfield = ast_strdupa(newparam);
239 if (initfield && (op = strchr(initfield, ' ')))
241 newval = va_arg(aq, const char *);
242 if (!strchr(newparam, ' ')) op = " ="; else op = "";
243 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
244 while((newparam = va_arg(aq, const char *))) {
245 if (!strchr(newparam, ' ')) op = " ="; else op = "";
246 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
247 newval = va_arg(aq, const char *);
250 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
252 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
253 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
254 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
255 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
259 /* Now bind the parameters */
262 while((newparam = va_arg(ap, const char *))) {
263 newval = va_arg(ap, const char *);
264 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
267 res = odbc_smart_execute(obj, stmt);
269 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
270 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
271 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
275 res = SQLNumResultCols(stmt, &colcount);
276 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
277 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
278 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
282 cfg = ast_config_new();
284 ast_log(LOG_WARNING, "Out of memory!\n");
285 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
289 while ((res=SQLFetch(stmt)) != SQL_NO_DATA) {
291 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
292 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
295 cat = ast_category_new("");
297 ast_log(LOG_WARNING, "Out of memory!\n");
300 for (x=0;x<colcount;x++) {
302 collen = sizeof(coltitle);
303 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
304 &datatype, &colsize, &decimaldigits, &nullable);
305 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
306 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
307 ast_category_destroy(cat);
312 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
313 if (indicator == SQL_NULL_DATA)
316 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
317 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
318 ast_category_destroy(cat);
323 chunk = strsep(&stringp, ";");
324 if (!ast_strlen_zero(ast_strip(chunk))) {
325 if (initfield && !strcmp(initfield, coltitle))
326 ast_category_rename(cat, chunk);
327 var = ast_variable_new(coltitle, chunk);
328 ast_variable_append(cat, var);
332 ast_category_append(cfg, cat);
335 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
339 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
345 const char *newparam, *newval;
355 obj = fetch_odbc_obj (database, 0);
359 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
360 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
361 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
365 newparam = va_arg(aq, const char *);
367 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
370 newval = va_arg(aq, const char *);
371 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam);
372 while((newparam = va_arg(aq, const char *))) {
373 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam);
374 newval = va_arg(aq, const char *);
377 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield);
379 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
380 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
381 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
382 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
386 /* Now bind the parameters */
389 while((newparam = va_arg(ap, const char *))) {
390 newval = va_arg(ap, const char *);
391 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
394 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(lookup), 0, (void *)lookup, 0, NULL);
396 res = odbc_smart_execute(obj, stmt);
398 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
399 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
400 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
404 res = SQLRowCount(stmt, &rowcount);
405 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
407 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
408 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
413 return (int)rowcount;
418 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg)
420 struct ast_variable *new_v;
421 struct ast_category *cur_cat;
424 SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
426 char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
427 SQLSMALLINT rowcount=0;
431 if (!file || !strcmp (file, "res_config_odbc.conf"))
432 return NULL; /* cant configure myself with myself ! */
434 obj = fetch_odbc_obj(database, 0);
438 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
440 SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
441 SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
442 SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
443 SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
444 SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
445 SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
446 SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
447 SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
449 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);
451 res = odbc_smart_direct_execute(obj, stmt, sql);
453 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
454 ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
455 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
459 res = SQLNumResultCols (stmt, &rowcount);
461 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
462 ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
463 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
468 ast_log (LOG_NOTICE, "found nothing\n");
472 cur_cat = ast_config_get_current_category(cfg);
474 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
475 if (!strcmp (var_name, "#include")) {
476 if (!ast_config_internal_load(var_val, cfg)) {
477 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
482 if (strcmp(last, category) || last_cat_metric != cat_metric) {
483 cur_cat = ast_category_new(category);
485 ast_log(LOG_WARNING, "Out of memory!\n");
488 strcpy(last, category);
489 last_cat_metric = cat_metric;
490 ast_category_append(cfg, cur_cat);
493 new_v = ast_variable_new(var_name, var_val);
494 ast_variable_append(cur_cat, new_v);
497 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
501 static struct ast_config_engine odbc_engine = {
503 .load_func = config_odbc,
504 .realtime_func = realtime_odbc,
505 .realtime_multi_func = realtime_multi_odbc,
506 .update_func = update_odbc
509 static int unload_module (void *mod)
511 ast_hangup_localusers(mod);
512 ast_config_engine_deregister(&odbc_engine);
514 ast_verbose("res_config_odbc unloaded.\n");
518 static int load_module (void *mod)
520 ast_config_engine_register(&odbc_engine);
522 ast_verbose("res_config_odbc loaded.\n");
526 static const char *description(void)
528 return "ODBC Configuration";
531 static const char *key(void)
533 return ASTERISK_GPL_KEY;
536 STD_MOD(MOD_0 | NO_USECOUNT | NO_UNLOAD, NULL, NULL, NULL);