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>
34 <depend>res_odbc</depend>
39 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/config.h"
51 #include "asterisk/module.h"
52 #include "asterisk/lock.h"
53 #include "asterisk/options.h"
54 #include "asterisk/res_odbc.h"
55 #include "asterisk/utils.h"
57 struct custom_prepare_struct
{
63 static SQLHSTMT
custom_prepare(struct odbc_obj
*obj
, void *data
)
66 struct custom_prepare_struct
*cps
= data
;
67 const char *newparam
, *newval
;
73 res
= SQLAllocHandle(SQL_HANDLE_STMT
, obj
->con
, &stmt
);
74 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
75 ast_log(LOG_WARNING
, "SQL Alloc Handle failed!\n");
79 res
= SQLPrepare(stmt
, (unsigned char *)cps
->sql
, SQL_NTS
);
80 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
81 ast_log(LOG_WARNING
, "SQL Prepare failed![%s]\n", cps
->sql
);
82 SQLFreeHandle (SQL_HANDLE_STMT
, stmt
);
86 while ((newparam
= va_arg(ap
, const char *))) {
87 newval
= va_arg(ap
, const char *);
88 SQLBindParameter(stmt
, x
++, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, strlen(newval
), 0, (void *)newval
, 0, NULL
);
92 if (!ast_strlen_zero(cps
->extra
))
93 SQLBindParameter(stmt
, x
++, SQL_PARAM_INPUT
, SQL_C_CHAR
, SQL_CHAR
, strlen(cps
->extra
), 0, (void *)cps
->extra
, 0, NULL
);
97 static struct ast_variable
*realtime_odbc(const char *database
, const char *table
, va_list ap
)
105 const char *newparam
, *newval
;
111 struct ast_variable
*var
=NULL
, *prev
=NULL
;
113 SQLSMALLINT colcount
=0;
114 SQLSMALLINT datatype
;
115 SQLSMALLINT decimaldigits
;
116 SQLSMALLINT nullable
;
119 struct custom_prepare_struct cps
= { .sql
= sql
};
127 obj
= ast_odbc_request_obj(database
, 0);
130 ast_log(LOG_ERROR
, "No database handle available with the name of '%s' (check res_odbc.conf)\n", database
);
134 newparam
= va_arg(aq
, const char *);
137 newval
= va_arg(aq
, const char *);
138 op
= !strchr(newparam
, ' ') ? " =" : "";
139 snprintf(sql
, sizeof(sql
), "SELECT * FROM %s WHERE %s%s ?%s", table
, newparam
, op
,
140 strcasestr(newparam
, "LIKE") && !ast_odbc_backslash_is_escape(obj
) ? " ESCAPE '\\'" : "");
141 while((newparam
= va_arg(aq
, const char *))) {
142 op
= !strchr(newparam
, ' ') ? " =" : "";
143 snprintf(sql
+ strlen(sql
), sizeof(sql
) - strlen(sql
), " AND %s%s ?%s", newparam
, op
,
144 strcasestr(newparam
, "LIKE") && !ast_odbc_backslash_is_escape(obj
) ? " ESCAPE '\\'" : "");
145 newval
= va_arg(aq
, const char *);
149 stmt
= ast_odbc_prepare_and_execute(obj
, custom_prepare
, &cps
);
152 ast_odbc_release_obj(obj
);
156 res
= SQLNumResultCols(stmt
, &colcount
);
157 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
158 ast_log(LOG_WARNING
, "SQL Column Count error!\n[%s]\n\n", sql
);
159 SQLFreeHandle (SQL_HANDLE_STMT
, stmt
);
160 ast_odbc_release_obj(obj
);
164 res
= SQLFetch(stmt
);
165 if (res
== SQL_NO_DATA
) {
166 SQLFreeHandle (SQL_HANDLE_STMT
, stmt
);
167 ast_odbc_release_obj(obj
);
170 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
171 ast_log(LOG_WARNING
, "SQL Fetch error!\n[%s]\n\n", sql
);
172 SQLFreeHandle (SQL_HANDLE_STMT
, stmt
);
173 ast_odbc_release_obj(obj
);
176 for (x
= 0; x
< colcount
; x
++) {
178 collen
= sizeof(coltitle
);
179 res
= SQLDescribeCol(stmt
, x
+ 1, (unsigned char *)coltitle
, sizeof(coltitle
), &collen
,
180 &datatype
, &colsize
, &decimaldigits
, &nullable
);
181 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
182 ast_log(LOG_WARNING
, "SQL Describe Column error!\n[%s]\n\n", sql
);
184 ast_variables_destroy(var
);
185 ast_odbc_release_obj(obj
);
190 res
= SQLGetData(stmt
, x
+ 1, SQL_CHAR
, rowdata
, sizeof(rowdata
), &indicator
);
191 if (indicator
== SQL_NULL_DATA
)
194 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
195 ast_log(LOG_WARNING
, "SQL Get Data error!\n[%s]\n\n", sql
);
197 ast_variables_destroy(var
);
198 ast_odbc_release_obj(obj
);
203 chunk
= strsep(&stringp
, ";");
204 if (!ast_strlen_zero(ast_strip(chunk
))) {
206 prev
->next
= ast_variable_new(coltitle
, chunk
);
210 prev
= var
= ast_variable_new(coltitle
, chunk
);
216 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
217 ast_odbc_release_obj(obj
);
221 static struct ast_config
*realtime_multi_odbc(const char *database
, const char *table
, va_list ap
)
223 struct odbc_obj
*obj
;
228 const char *initfield
=NULL
;
230 const char *newparam
, *newval
;
236 struct ast_variable
*var
=NULL
;
237 struct ast_config
*cfg
=NULL
;
238 struct ast_category
*cat
=NULL
;
239 struct ast_realloca ra
;
241 SQLSMALLINT colcount
=0;
242 SQLSMALLINT datatype
;
243 SQLSMALLINT decimaldigits
;
244 SQLSMALLINT nullable
;
246 struct custom_prepare_struct cps
= { .sql
= sql
};
254 memset(&ra
, 0, sizeof(ra
));
256 obj
= ast_odbc_request_obj(database
, 0);
260 newparam
= va_arg(aq
, const char *);
262 ast_odbc_release_obj(obj
);
265 initfield
= ast_strdupa(newparam
);
266 if ((op
= strchr(initfield
, ' ')))
268 newval
= va_arg(aq
, const char *);
269 op
= !strchr(newparam
, ' ') ? " =" : "";
270 snprintf(sql
, sizeof(sql
), "SELECT * FROM %s WHERE %s%s ?%s", table
, newparam
, op
,
271 strcasestr(newparam
, "LIKE") && !ast_odbc_backslash_is_escape(obj
) ? " ESCAPE '\\'" : "");
272 while((newparam
= va_arg(aq
, const char *))) {
273 op
= !strchr(newparam
, ' ') ? " =" : "";
274 snprintf(sql
+ strlen(sql
), sizeof(sql
) - strlen(sql
), " AND %s%s ?%s", newparam
, op
,
275 strcasestr(newparam
, "LIKE") && !ast_odbc_backslash_is_escape(obj
) ? " ESCAPE '\\'" : "");
276 newval
= va_arg(aq
, const char *);
279 snprintf(sql
+ strlen(sql
), sizeof(sql
) - strlen(sql
), " ORDER BY %s", initfield
);
282 stmt
= ast_odbc_prepare_and_execute(obj
, custom_prepare
, &cps
);
285 ast_odbc_release_obj(obj
);
289 res
= SQLNumResultCols(stmt
, &colcount
);
290 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
291 ast_log(LOG_WARNING
, "SQL Column Count error!\n[%s]\n\n", sql
);
292 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
293 ast_odbc_release_obj(obj
);
297 cfg
= ast_config_new();
299 ast_log(LOG_WARNING
, "Out of memory!\n");
300 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
301 ast_odbc_release_obj(obj
);
305 while ((res
=SQLFetch(stmt
)) != SQL_NO_DATA
) {
307 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
308 ast_log(LOG_WARNING
, "SQL Fetch error!\n[%s]\n\n", sql
);
311 cat
= ast_category_new("");
313 ast_log(LOG_WARNING
, "Out of memory!\n");
316 for (x
=0;x
<colcount
;x
++) {
318 collen
= sizeof(coltitle
);
319 res
= SQLDescribeCol(stmt
, x
+ 1, (unsigned char *)coltitle
, sizeof(coltitle
), &collen
,
320 &datatype
, &colsize
, &decimaldigits
, &nullable
);
321 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
322 ast_log(LOG_WARNING
, "SQL Describe Column error!\n[%s]\n\n", sql
);
323 ast_category_destroy(cat
);
328 res
= SQLGetData(stmt
, x
+ 1, SQL_CHAR
, rowdata
, sizeof(rowdata
), &indicator
);
329 if (indicator
== SQL_NULL_DATA
)
332 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
333 ast_log(LOG_WARNING
, "SQL Get Data error!\n[%s]\n\n", sql
);
334 ast_category_destroy(cat
);
339 chunk
= strsep(&stringp
, ";");
340 if (!ast_strlen_zero(ast_strip(chunk
))) {
341 if (initfield
&& !strcmp(initfield
, coltitle
))
342 ast_category_rename(cat
, chunk
);
343 var
= ast_variable_new(coltitle
, chunk
);
344 ast_variable_append(cat
, var
);
348 ast_category_append(cfg
, cat
);
351 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
352 ast_odbc_release_obj(obj
);
356 static int update_odbc(const char *database
, const char *table
, const char *keyfield
, const char *lookup
, va_list ap
)
358 struct odbc_obj
*obj
;
362 const char *newparam
, *newval
;
365 struct custom_prepare_struct cps
= { .sql
= sql
, .extra
= lookup
};
373 obj
= ast_odbc_request_obj(database
, 0);
377 newparam
= va_arg(aq
, const char *);
379 ast_odbc_release_obj(obj
);
382 newval
= va_arg(aq
, const char *);
383 snprintf(sql
, sizeof(sql
), "UPDATE %s SET %s=?", table
, newparam
);
384 while((newparam
= va_arg(aq
, const char *))) {
385 snprintf(sql
+ strlen(sql
), sizeof(sql
) - strlen(sql
), ", %s=?", newparam
);
386 newval
= va_arg(aq
, const char *);
389 snprintf(sql
+ strlen(sql
), sizeof(sql
) - strlen(sql
), " WHERE %s=?", keyfield
);
391 stmt
= ast_odbc_prepare_and_execute(obj
, custom_prepare
, &cps
);
394 ast_odbc_release_obj(obj
);
398 res
= SQLRowCount(stmt
, &rowcount
);
399 SQLFreeHandle (SQL_HANDLE_STMT
, stmt
);
400 ast_odbc_release_obj(obj
);
402 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
403 ast_log(LOG_WARNING
, "SQL Row Count error!\n[%s]\n\n", sql
);
408 return (int)rowcount
;
413 struct config_odbc_obj
{
415 unsigned long cat_metric
;
418 char var_val
[1024]; /* changed from 128 to 1024 via bug 8251 */
422 static SQLHSTMT
config_odbc_prepare(struct odbc_obj
*obj
, void *data
)
424 struct config_odbc_obj
*q
= data
;
428 res
= SQLAllocHandle(SQL_HANDLE_STMT
, obj
->con
, &sth
);
429 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
430 if (option_verbose
> 3)
431 ast_verbose( VERBOSE_PREFIX_4
"Failure in AllocStatement %d\n", res
);
435 res
= SQLPrepare(sth
, (unsigned char *)q
->sql
, SQL_NTS
);
436 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
437 if (option_verbose
> 3)
438 ast_verbose( VERBOSE_PREFIX_4
"Error in PREPARE %d\n", res
);
439 SQLFreeHandle(SQL_HANDLE_STMT
, sth
);
443 SQLBindCol(sth
, 1, SQL_C_ULONG
, &q
->cat_metric
, sizeof(q
->cat_metric
), &q
->err
);
444 SQLBindCol(sth
, 2, SQL_C_CHAR
, q
->category
, sizeof(q
->category
), &q
->err
);
445 SQLBindCol(sth
, 3, SQL_C_CHAR
, q
->var_name
, sizeof(q
->var_name
), &q
->err
);
446 SQLBindCol(sth
, 4, SQL_C_CHAR
, q
->var_val
, sizeof(q
->var_val
), &q
->err
);
451 static struct ast_config
*config_odbc(const char *database
, const char *table
, const char *file
, struct ast_config
*cfg
, int withcomments
)
453 struct ast_variable
*new_v
;
454 struct ast_category
*cur_cat
;
456 struct odbc_obj
*obj
;
457 char sqlbuf
[1024] = "";
459 size_t sqlleft
= sizeof(sqlbuf
);
460 unsigned int last_cat_metric
= 0;
461 SQLSMALLINT rowcount
= 0;
464 struct config_odbc_obj q
;
466 memset(&q
, 0, sizeof(q
));
468 if (!file
|| !strcmp (file
, "res_config_odbc.conf"))
469 return NULL
; /* cant configure myself with myself ! */
471 obj
= ast_odbc_request_obj(database
, 0);
475 ast_build_string(&sql
, &sqlleft
, "SELECT cat_metric, category, var_name, var_val FROM %s ", table
);
476 ast_build_string(&sql
, &sqlleft
, "WHERE filename='%s' AND commented=0 ", file
);
477 ast_build_string(&sql
, &sqlleft
, "ORDER BY cat_metric DESC, var_metric ASC, category, var_name ");
480 stmt
= ast_odbc_prepare_and_execute(obj
, config_odbc_prepare
, &q
);
483 ast_log(LOG_WARNING
, "SQL select error!\n[%s]\n\n", sql
);
484 ast_odbc_release_obj(obj
);
488 res
= SQLNumResultCols(stmt
, &rowcount
);
490 if ((res
!= SQL_SUCCESS
) && (res
!= SQL_SUCCESS_WITH_INFO
)) {
491 ast_log(LOG_WARNING
, "SQL NumResultCols error!\n[%s]\n\n", sql
);
492 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
493 ast_odbc_release_obj(obj
);
498 ast_log(LOG_NOTICE
, "found nothing\n");
499 ast_odbc_release_obj(obj
);
503 cur_cat
= ast_config_get_current_category(cfg
);
505 while ((res
= SQLFetch(stmt
)) != SQL_NO_DATA
) {
506 if (!strcmp (q
.var_name
, "#include")) {
507 if (!ast_config_internal_load(q
.var_val
, cfg
, 0)) {
508 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
509 ast_odbc_release_obj(obj
);
514 if (strcmp(last
, q
.category
) || last_cat_metric
!= q
.cat_metric
) {
515 cur_cat
= ast_category_new(q
.category
);
517 ast_log(LOG_WARNING
, "Out of memory!\n");
520 strcpy(last
, q
.category
);
521 last_cat_metric
= q
.cat_metric
;
522 ast_category_append(cfg
, cur_cat
);
525 new_v
= ast_variable_new(q
.var_name
, q
.var_val
);
526 ast_variable_append(cur_cat
, new_v
);
529 SQLFreeHandle(SQL_HANDLE_STMT
, stmt
);
530 ast_odbc_release_obj(obj
);
534 static struct ast_config_engine odbc_engine
= {
536 .load_func
= config_odbc
,
537 .realtime_func
= realtime_odbc
,
538 .realtime_multi_func
= realtime_multi_odbc
,
539 .update_func
= update_odbc
542 static int unload_module (void)
544 ast_module_user_hangup_all();
545 ast_config_engine_deregister(&odbc_engine
);
547 ast_verbose("res_config_odbc unloaded.\n");
551 static int load_module (void)
553 ast_config_engine_register(&odbc_engine
);
555 ast_verbose("res_config_odbc loaded.\n");
559 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_GLOBAL_SYMBOLS
, "ODBC Configuration",
561 .unload
= unload_module
,