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
32 <depend>unixodbc</depend>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/file.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/pbx.h"
48 #include "asterisk/config.h"
49 #include "asterisk/module.h"
50 #include "asterisk/lock.h"
51 #include "asterisk/options.h"
52 #include "asterisk/res_odbc.h"
53 #include "asterisk/utils.h"
57 static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
65 const char *newparam, *newval;
71 struct ast_variable *var=NULL, *prev=NULL;
73 SQLSMALLINT colcount=0;
75 SQLSMALLINT decimaldigits;
86 obj = odbc_request_obj(database, 0);
90 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
91 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
92 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
93 odbc_release_obj(obj);
97 newparam = va_arg(aq, const char *);
99 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
100 odbc_release_obj(obj);
103 newval = va_arg(aq, const char *);
104 if (!strchr(newparam, ' ')) op = " ="; else op = "";
105 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
106 while((newparam = va_arg(aq, const char *))) {
107 if (!strchr(newparam, ' ')) op = " ="; else op = "";
108 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
109 newval = va_arg(aq, const char *);
112 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
113 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
114 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
115 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
116 odbc_release_obj(obj);
120 /* Now bind the parameters */
123 while((newparam = va_arg(ap, const char *))) {
124 newval = va_arg(ap, const char *);
125 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
128 res = odbc_smart_execute(obj, stmt);
130 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
131 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
132 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
133 odbc_release_obj(obj);
137 res = SQLNumResultCols(stmt, &colcount);
138 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
139 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
140 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
141 odbc_release_obj(obj);
145 res = SQLFetch(stmt);
146 if (res == SQL_NO_DATA) {
147 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
148 odbc_release_obj(obj);
151 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
152 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
153 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
154 odbc_release_obj(obj);
157 for (x = 0; x < colcount; x++) {
159 collen = sizeof(coltitle);
160 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
161 &datatype, &colsize, &decimaldigits, &nullable);
162 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
163 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
165 ast_variables_destroy(var);
166 odbc_release_obj(obj);
171 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
172 if (indicator == SQL_NULL_DATA)
175 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
176 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
178 ast_variables_destroy(var);
179 odbc_release_obj(obj);
184 chunk = strsep(&stringp, ";");
185 if (!ast_strlen_zero(ast_strip(chunk))) {
187 prev->next = ast_variable_new(coltitle, chunk);
191 prev = var = ast_variable_new(coltitle, chunk);
198 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
199 odbc_release_obj(obj);
203 static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
205 struct odbc_obj *obj;
210 const char *initfield=NULL;
212 const char *newparam, *newval;
218 struct ast_variable *var=NULL;
219 struct ast_config *cfg=NULL;
220 struct ast_category *cat=NULL;
221 struct ast_realloca ra;
223 SQLSMALLINT colcount=0;
224 SQLSMALLINT datatype;
225 SQLSMALLINT decimaldigits;
226 SQLSMALLINT nullable;
227 SQLINTEGER indicator;
235 memset(&ra, 0, sizeof(ra));
237 obj = odbc_request_obj(database, 0);
241 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
242 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
243 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
244 odbc_release_obj(obj);
248 newparam = va_arg(aq, const char *);
250 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
251 odbc_release_obj(obj);
254 initfield = ast_strdupa(newparam);
255 if ((op = strchr(initfield, ' ')))
257 newval = va_arg(aq, const char *);
258 if (!strchr(newparam, ' ')) op = " ="; else op = "";
259 snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s%s ?", table, newparam, op);
260 while((newparam = va_arg(aq, const char *))) {
261 if (!strchr(newparam, ' ')) op = " ="; else op = "";
262 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " AND %s%s ?", newparam, op);
263 newval = va_arg(aq, const char *);
266 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " ORDER BY %s", initfield);
268 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
269 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
270 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
271 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
272 odbc_release_obj(obj);
276 /* Now bind the parameters */
279 while((newparam = va_arg(ap, const char *))) {
280 newval = va_arg(ap, const char *);
281 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
284 res = odbc_smart_execute(obj, stmt);
286 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
287 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
288 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
289 odbc_release_obj(obj);
293 res = SQLNumResultCols(stmt, &colcount);
294 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
295 ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
296 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
297 odbc_release_obj(obj);
301 cfg = ast_config_new();
303 ast_log(LOG_WARNING, "Out of memory!\n");
304 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
305 odbc_release_obj(obj);
309 while ((res=SQLFetch(stmt)) != SQL_NO_DATA) {
311 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
312 ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
315 cat = ast_category_new("");
317 ast_log(LOG_WARNING, "Out of memory!\n");
320 for (x=0;x<colcount;x++) {
322 collen = sizeof(coltitle);
323 res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen,
324 &datatype, &colsize, &decimaldigits, &nullable);
325 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
326 ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
327 ast_category_destroy(cat);
332 res = SQLGetData(stmt, x + 1, SQL_CHAR, rowdata, sizeof(rowdata), &indicator);
333 if (indicator == SQL_NULL_DATA)
336 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
337 ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
338 ast_category_destroy(cat);
343 chunk = strsep(&stringp, ";");
344 if (!ast_strlen_zero(ast_strip(chunk))) {
345 if (initfield && !strcmp(initfield, coltitle))
346 ast_category_rename(cat, chunk);
347 var = ast_variable_new(coltitle, chunk);
348 ast_variable_append(cat, var);
352 ast_category_append(cfg, cat);
355 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
356 odbc_release_obj(obj);
360 static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
362 struct odbc_obj *obj;
366 const char *newparam, *newval;
376 obj = odbc_request_obj(database, 0);
380 res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
381 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
382 ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
383 odbc_release_obj(obj);
387 newparam = va_arg(aq, const char *);
389 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
390 odbc_release_obj(obj);
393 newval = va_arg(aq, const char *);
394 snprintf(sql, sizeof(sql), "UPDATE %s SET %s=?", table, newparam);
395 while((newparam = va_arg(aq, const char *))) {
396 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), ", %s=?", newparam);
397 newval = va_arg(aq, const char *);
400 snprintf(sql + strlen(sql), sizeof(sql) - strlen(sql), " WHERE %s=?", keyfield);
402 res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
403 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
404 ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
405 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
406 odbc_release_obj(obj);
410 /* Now bind the parameters */
413 while((newparam = va_arg(ap, const char *))) {
414 newval = va_arg(ap, const char *);
415 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(newval), 0, (void *)newval, 0, NULL);
418 SQLBindParameter(stmt, x++, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(lookup), 0, (void *)lookup, 0, NULL);
420 res = odbc_smart_execute(obj, stmt);
422 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
423 ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
424 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
425 odbc_release_obj(obj);
429 res = SQLRowCount(stmt, &rowcount);
430 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
431 odbc_release_obj(obj);
433 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
434 ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
439 return (int)rowcount;
444 struct config_odbc_obj {
447 unsigned long cat_metric;
448 unsigned long var_metric;
449 unsigned long commented;
457 static SQLHSTMT config_odbc_prepare(struct odbc_obj *obj, void *data)
459 struct config_odbc_obj *q = data;
463 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth);
464 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
465 if (option_verbose > 3)
466 ast_verbose( VERBOSE_PREFIX_4 "Failure in AllocStatement %d\n", res);
470 res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS);
471 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
472 if (option_verbose > 3)
473 ast_verbose( VERBOSE_PREFIX_4 "Error in PREPARE %d\n", res);
474 SQLFreeHandle(SQL_HANDLE_STMT, sth);
478 SQLBindCol(sth, 1, SQL_C_ULONG, &q->id, sizeof(q->id), &q->err);
479 SQLBindCol(sth, 2, SQL_C_ULONG, &q->cat_metric, sizeof(q->cat_metric), &q->err);
480 SQLBindCol(sth, 3, SQL_C_ULONG, &q->var_metric, sizeof(q->var_metric), &q->err);
481 SQLBindCol(sth, 4, SQL_C_ULONG, &q->commented, sizeof(q->commented), &q->err);
482 SQLBindCol(sth, 5, SQL_C_CHAR, q->filename, sizeof(q->filename), &q->err);
483 SQLBindCol(sth, 6, SQL_C_CHAR, q->category, sizeof(q->category), &q->err);
484 SQLBindCol(sth, 7, SQL_C_CHAR, q->var_name, sizeof(q->var_name), &q->err);
485 SQLBindCol(sth, 8, SQL_C_CHAR, q->var_val, sizeof(q->var_val), &q->err);
490 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg, int withcomments)
492 struct ast_variable *new_v;
493 struct ast_category *cur_cat;
495 struct odbc_obj *obj;
497 unsigned int last_cat_metric = 0;
498 SQLSMALLINT rowcount=0;
501 struct config_odbc_obj q;
503 memset(&q, 0, sizeof(q));
505 if (!file || !strcmp (file, "res_config_odbc.conf"))
506 return NULL; /* cant configure myself with myself ! */
508 obj = odbc_request_obj(database, 0);
512 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);
515 stmt = odbc_prepare_and_execute(obj, config_odbc_prepare, &q);
518 ast_log(LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
519 odbc_release_obj(obj);
523 res = SQLNumResultCols(stmt, &rowcount);
525 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
526 ast_log(LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
527 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
528 odbc_release_obj(obj);
533 ast_log(LOG_NOTICE, "found nothing\n");
534 odbc_release_obj(obj);
538 cur_cat = ast_config_get_current_category(cfg);
540 while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
541 if (!strcmp (q.var_name, "#include")) {
542 if (!ast_config_internal_load(q.var_val, cfg, 0)) {
543 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
544 odbc_release_obj(obj);
549 if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
550 cur_cat = ast_category_new(q.category);
552 ast_log(LOG_WARNING, "Out of memory!\n");
555 strcpy(last, q.category);
556 last_cat_metric = q.cat_metric;
557 ast_category_append(cfg, cur_cat);
560 new_v = ast_variable_new(q.var_name, q.var_val);
561 ast_variable_append(cur_cat, new_v);
564 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
565 odbc_release_obj(obj);
569 static struct ast_config_engine odbc_engine = {
571 .load_func = config_odbc,
572 .realtime_func = realtime_odbc,
573 .realtime_multi_func = realtime_multi_odbc,
574 .update_func = update_odbc
577 static int unload_module (void *mod)
579 ast_hangup_localusers(mod);
580 ast_config_engine_deregister(&odbc_engine);
582 ast_verbose("res_config_odbc unloaded.\n");
586 static int load_module (void *mod)
588 ast_config_engine_register(&odbc_engine);
590 ast_verbose("res_config_odbc loaded.\n");
594 static const char *description(void)
596 return "ODBC Configuration";
599 static const char *key(void)
601 return ASTERISK_GPL_KEY;
604 STD_MOD(MOD_0 | NO_USECOUNT | NO_UNLOAD, NULL, NULL, NULL);