Add a direct execute method to res_odbc (closes issue #10722)
[asterisk/asterisk.git] / res / res_odbc.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * res_odbc.c <ODBC resource manager>
9  * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
10  *
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.
16  *
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.
20  */
21
22 /*! \file
23  *
24  * \brief ODBC resource manager
25  * 
26  * \author Mark Spencer <markster@digium.com>
27  * \author Anthony Minessale II <anthmct@yahoo.com>
28  *
29  * \arg See also: \ref cdr_odbc
30  */
31
32 /*** MODULEINFO
33         <depend>unixodbc</depend>
34         <depend>ltdl</depend>
35  ***/
36
37 #include "asterisk.h"
38
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
45
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/config.h"
50 #include "asterisk/options.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/module.h"
53 #include "asterisk/cli.h"
54 #include "asterisk/lock.h"
55 #include "asterisk/res_odbc.h"
56
57 struct odbc_class
58 {
59         AST_LIST_ENTRY(odbc_class) list;
60         char name[80];
61         char dsn[80];
62         char username[80];
63         char password[80];
64         char sanitysql[256];
65         SQLHENV env;
66         unsigned int haspool:1;         /* Boolean - TDS databases need this */
67         unsigned int limit:10;          /* Gives a limit of 1023 maximum */
68         unsigned int count:10;          /* Running count of pooled connections */
69         unsigned int delme:1;                   /* Purge the class */
70         AST_LIST_HEAD(, odbc_obj) odbc_obj;
71 };
72
73 AST_LIST_HEAD_STATIC(odbc_list, odbc_class);
74
75 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
76 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
77 static int odbc_register_class(struct odbc_class *class, int connect);
78
79
80 SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data)
81 {
82         int attempt;
83         SQLHSTMT stmt;
84
85         for (attempt = 0; attempt < 2; attempt++) {
86                 stmt = exec_cb(obj, data);
87
88                 if (stmt) {
89                         break;
90                 } else {
91                         obj->up = 0;
92                         ast_log(LOG_WARNING, "SQL Exec Direct failed.  Attempting a reconnect...\n");
93
94                         odbc_obj_disconnect(obj);
95                         odbc_obj_connect(obj);
96                 }
97         }
98
99         return stmt;
100 }
101
102 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
103 {
104         int res = 0, i, attempt;
105         SQLINTEGER nativeerror=0, numfields=0;
106         SQLSMALLINT diagbytes=0;
107         unsigned char state[10], diagnostic[256];
108         SQLHSTMT stmt;
109
110         for (attempt = 0; attempt < 2; attempt++) {
111                 /* This prepare callback may do more than just prepare -- it may also
112                  * bind parameters, bind results, etc.  The real key, here, is that
113                  * when we disconnect, all handles become invalid for most databases.
114                  * We must therefore redo everything when we establish a new
115                  * connection. */
116                 stmt = prepare_cb(obj, data);
117
118                 if (stmt) {
119                         res = SQLExecute(stmt);
120                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
121                                 if (res == SQL_ERROR) {
122                                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
123                                         for (i = 0; i < numfields; i++) {
124                                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
125                                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
126                                                 if (i > 10) {
127                                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
128                                                         break;
129                                                 }
130                                         }
131                                 }
132
133                                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
134                                 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
135                                 stmt = NULL;
136
137                                 obj->up = 0;
138                                 /*
139                                  * While this isn't the best way to try to correct an error, this won't automatically
140                                  * fail when the statement handle invalidates.
141                                  */
142                                 ast_odbc_sanity_check(obj);
143                                 continue;
144                         }
145                         break;
146                 } else if (attempt == 0)
147                         ast_odbc_sanity_check(obj);
148         }
149
150         return stmt;
151 }
152
153 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt) 
154 {
155         int res = 0, i;
156         SQLINTEGER nativeerror=0, numfields=0;
157         SQLSMALLINT diagbytes=0;
158         unsigned char state[10], diagnostic[256];
159
160         res = SQLExecute(stmt);
161         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
162                 if (res == SQL_ERROR) {
163                         SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
164                         for (i = 0; i < numfields; i++) {
165                                 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
166                                 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
167                                 if (i > 10) {
168                                         ast_log(LOG_WARNING, "Oh, that was good.  There are really %d diagnostics?\n", (int)numfields);
169                                         break;
170                                 }
171                         }
172                 }
173 #if 0
174                 /* This is a really bad method of trying to correct a dead connection.  It
175                  * only ever really worked with MySQL.  It will not work with any other
176                  * database, since most databases prepare their statements on the server,
177                  * and if you disconnect, you invalidate the statement handle.  Hence, if
178                  * you disconnect, you're going to fail anyway, whether you try to execute
179                  * a second time or not.
180                  */
181                 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
182                 ast_mutex_lock(&obj->lock);
183                 obj->up = 0;
184                 ast_mutex_unlock(&obj->lock);
185                 odbc_obj_disconnect(obj);
186                 odbc_obj_connect(obj);
187                 res = SQLExecute(stmt);
188 #endif
189         }
190         
191         return res;
192 }
193
194
195 int ast_odbc_sanity_check(struct odbc_obj *obj) 
196 {
197         char *test_sql = "select 1";
198         SQLHSTMT stmt;
199         int res = 0;
200
201         if (!ast_strlen_zero(obj->parent->sanitysql))
202                 test_sql = obj->parent->sanitysql;
203
204         if (obj->up) {
205                 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
206                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
207                         obj->up = 0;
208                 } else {
209                         res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
210                         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
211                                 obj->up = 0;
212                         } else {
213                                 res = SQLExecute(stmt);
214                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
215                                         obj->up = 0;
216                                 }
217                         }
218                 }
219                 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
220         }
221
222         if (!obj->up) { /* Try to reconnect! */
223                 ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
224                 odbc_obj_disconnect(obj);
225                 odbc_obj_connect(obj);
226         }
227         return obj->up;
228 }
229
230 static int load_odbc_config(void)
231 {
232         static char *cfg = "res_odbc.conf";
233         struct ast_config *config;
234         struct ast_variable *v;
235         char *cat, *dsn, *username, *password, *sanitysql;
236         int enabled, pooling, limit;
237         int connect = 0, res = 0;
238         struct ast_flags config_flags = { 0 };
239
240         struct odbc_class *new;
241
242         config = ast_config_load(cfg, config_flags);
243         if (!config) {
244                 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
245                 return -1;
246         }
247         for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
248                 if (!strcasecmp(cat, "ENV")) {
249                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
250                                 setenv(v->name, v->value, 1);
251                                 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
252                         }
253                 } else {
254                         /* Reset all to defaults for each class of odbc connections */
255                         dsn = username = password = sanitysql = NULL;
256                         enabled = 1;
257                         connect = 0;
258                         pooling = 0;
259                         limit = 0;
260                         for (v = ast_variable_browse(config, cat); v; v = v->next) {
261                                 if (!strcasecmp(v->name, "pooling")) {
262                                         if (ast_true(v->value))
263                                                 pooling = 1;
264                                 } else if (!strcasecmp(v->name, "limit")) {
265                                         sscanf(v->value, "%d", &limit);
266                                         if (ast_true(v->value) && !limit) {
267                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
268                                                 limit = 1023;
269                                         } else if (ast_false(v->value)) {
270                                                 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
271                                                 enabled = 0;
272                                                 break;
273                                         }
274                                 } else if (!strcasecmp(v->name, "enabled")) {
275                                         enabled = ast_true(v->value);
276                                 } else if (!strcasecmp(v->name, "pre-connect")) {
277                                         connect = ast_true(v->value);
278                                 } else if (!strcasecmp(v->name, "dsn")) {
279                                         dsn = v->value;
280                                 } else if (!strcasecmp(v->name, "username")) {
281                                         username = v->value;
282                                 } else if (!strcasecmp(v->name, "password")) {
283                                         password = v->value;
284                                 } else if (!strcasecmp(v->name, "sanitysql")) {
285                                         sanitysql = v->value;
286                                 }
287                         }
288
289                         if (enabled && !ast_strlen_zero(dsn)) {
290                                 new = ast_calloc(1, sizeof(*new));
291
292                                 if (!new) {
293                                         res = -1;
294                                         break;
295                                 }
296
297                                 if (cat)
298                                         ast_copy_string(new->name, cat, sizeof(new->name));
299                                 if (dsn)
300                                         ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
301                                 if (username)
302                                         ast_copy_string(new->username, username, sizeof(new->username));
303                                 if (password)
304                                         ast_copy_string(new->password, password, sizeof(new->password));
305                                 if (sanitysql)
306                                         ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
307
308                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
309                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
310
311                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
312                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
313                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
314                                         return res;
315                                 }
316
317                                 if (pooling) {
318                                         new->haspool = pooling;
319                                         if (limit) {
320                                                 new->limit = limit;
321                                         } else {
322                                                 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
323                                                 new->limit = 5;
324                                         }
325                                 }
326
327                                 odbc_register_class(new, connect);
328                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
329                         }
330                 }
331         }
332         ast_config_destroy(config);
333         return res;
334 }
335
336 static int odbc_show_command(int fd, int argc, char **argv)
337 {
338         struct odbc_class *class;
339         struct odbc_obj *current;
340
341         AST_LIST_LOCK(&odbc_list);
342         AST_LIST_TRAVERSE(&odbc_list, class, list) {
343                 if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
344                         int count = 0;
345                         ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
346
347                         if (class->haspool) {
348                                 ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count);
349
350                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
351                                         ast_cli(fd, "  Connection %d: %s\n", ++count, current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
352                                 }
353                         } else {
354                                 /* Should only ever be one of these */
355                                 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
356                                         ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && ast_odbc_sanity_check(current) ? "yes" : "no");
357                                 }
358                         }
359
360                                 ast_cli(fd, "\n");
361                 }
362         }
363         AST_LIST_UNLOCK(&odbc_list);
364
365         return 0;
366 }
367
368 static const char show_usage[] =
369 "Usage: odbc show [<class>]\n"
370 "       List settings of a particular ODBC class.\n"
371 "       or, if not specified, all classes.\n";
372
373 static struct ast_cli_entry cli_odbc[] = {
374         { { "odbc", "show", NULL },
375         odbc_show_command, "List ODBC DSN(s)",
376         show_usage },
377 };
378
379 static int odbc_register_class(struct odbc_class *class, int connect)
380 {
381         struct odbc_obj *obj;
382         if (class) {
383                 AST_LIST_LOCK(&odbc_list);
384                 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
385                 AST_LIST_UNLOCK(&odbc_list);
386
387                 if (connect) {
388                         /* Request and release builds a connection */
389                         obj = ast_odbc_request_obj(class->name, 0);
390                         if (obj)
391                                 ast_odbc_release_obj(obj);
392                 }
393
394                 return 0;
395         } else {
396                 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
397                 return -1;
398         }
399 }
400
401 void ast_odbc_release_obj(struct odbc_obj *obj)
402 {
403         /* For pooled connections, this frees the connection to be
404          * reused.  For non-pooled connections, it does nothing. */
405         obj->used = 0;
406 }
407
408 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
409 {
410         struct odbc_obj *obj = NULL;
411         struct odbc_class *class;
412
413         AST_LIST_LOCK(&odbc_list);
414         AST_LIST_TRAVERSE(&odbc_list, class, list) {
415                 if (!strcmp(class->name, name))
416                         break;
417         }
418         AST_LIST_UNLOCK(&odbc_list);
419
420         if (!class)
421                 return NULL;
422
423         AST_LIST_LOCK(&class->odbc_obj);
424         if (class->haspool) {
425                 /* Recycle connections before building another */
426                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
427                         if (! obj->used) {
428                                 obj->used = 1;
429                                 break;
430                         }
431                 }
432
433                 if (!obj && (class->count < class->limit)) {
434                         class->count++;
435                         obj = ast_calloc(1, sizeof(*obj));
436                         if (!obj) {
437                                 AST_LIST_UNLOCK(&class->odbc_obj);
438                                 return NULL;
439                         }
440                         ast_mutex_init(&obj->lock);
441                         obj->parent = class;
442                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
443                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
444                                 ast_mutex_destroy(&obj->lock);
445                                 free(obj);
446                                 obj = NULL;
447                                 class->count--;
448                         } else {
449                                 obj->used = 1;
450                                 AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
451                         }
452                 }
453         } else {
454                 /* Non-pooled connection: multiple modules can use the same connection. */
455                 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
456                         /* Non-pooled connection: if there is an entry, return it */
457                         break;
458                 }
459
460                 if (!obj) {
461                         /* No entry: build one */
462                         obj = ast_calloc(1, sizeof(*obj));
463                         if (!obj) {
464                                 AST_LIST_UNLOCK(&class->odbc_obj);
465                                 return NULL;
466                         }
467                         ast_mutex_init(&obj->lock);
468                         obj->parent = class;
469                         if (odbc_obj_connect(obj) == ODBC_FAIL) {
470                                 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
471                                 ast_mutex_destroy(&obj->lock);
472                                 ast_free(obj);
473                                 obj = NULL;
474                         } else {
475                                 AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
476                         }
477                 }
478         }
479         AST_LIST_UNLOCK(&class->odbc_obj);
480
481         if (obj && check) {
482                 ast_odbc_sanity_check(obj);
483         }
484         return obj;
485 }
486
487 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
488 {
489         int res;
490         ast_mutex_lock(&obj->lock);
491
492         res = SQLDisconnect(obj->con);
493
494         if (res == ODBC_SUCCESS) {
495                 ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
496         } else {
497                 ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
498                 obj->parent->name, obj->parent->dsn);
499         }
500         obj->up = 0;
501         ast_mutex_unlock(&obj->lock);
502         return ODBC_SUCCESS;
503 }
504
505 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
506 {
507         int res;
508         SQLINTEGER err;
509         short int mlen;
510         unsigned char msg[200], stat[10];
511 #ifdef NEEDTRACE
512         SQLINTEGER enable = 1;
513         char *tracefile = "/tmp/odbc.trace";
514 #endif
515         ast_mutex_lock(&obj->lock);
516
517         res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
518
519         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
520                 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
521                 ast_mutex_unlock(&obj->lock);
522                 return ODBC_FAIL;
523         }
524         SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
525 #ifdef NEEDTRACE
526         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
527         SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
528 #endif
529
530         if (obj->up) {
531                 odbc_obj_disconnect(obj);
532                 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
533         } else {
534                 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
535         }
536
537         res = SQLConnect(obj->con,
538                    (SQLCHAR *) obj->parent->dsn, SQL_NTS,
539                    (SQLCHAR *) obj->parent->username, SQL_NTS,
540                    (SQLCHAR *) obj->parent->password, SQL_NTS);
541
542         if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
543                 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
544                 ast_mutex_unlock(&obj->lock);
545                 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
546                 return ODBC_FAIL;
547         } else {
548                 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
549                 obj->up = 1;
550         }
551
552         ast_mutex_unlock(&obj->lock);
553         return ODBC_SUCCESS;
554 }
555
556 static int reload(void)
557 {
558         static char *cfg = "res_odbc.conf";
559         struct ast_config *config;
560         struct ast_variable *v;
561         char *cat, *dsn, *username, *password, *sanitysql;
562         int enabled, pooling, limit;
563         int connect = 0, res = 0;
564         struct ast_flags config_flags = { CONFIG_FLAG_FILEUNCHANGED };
565
566         struct odbc_class *new, *class;
567         struct odbc_obj *current;
568
569         /* First, mark all to be purged */
570         AST_LIST_LOCK(&odbc_list);
571         AST_LIST_TRAVERSE(&odbc_list, class, list) {
572                 class->delme = 1;
573         }
574
575         config = ast_config_load(cfg, config_flags);
576         if (config != NULL && config != CONFIG_STATUS_FILEUNCHANGED) {
577                 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
578                         if (!strcasecmp(cat, "ENV")) {
579                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
580                                         setenv(v->name, v->value, 1);
581                                         ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
582                                 }
583                         } else {
584                                 /* Reset all to defaults for each class of odbc connections */
585                                 dsn = username = password = sanitysql = NULL;
586                                 enabled = 1;
587                                 connect = 0;
588                                 pooling = 0;
589                                 limit = 0;
590                                 for (v = ast_variable_browse(config, cat); v; v = v->next) {
591                                         if (!strcasecmp(v->name, "pooling")) {
592                                                 pooling = 1;
593                                         } else if (!strcasecmp(v->name, "limit")) {
594                                                 sscanf(v->value, "%d", &limit);
595                                                 if (ast_true(v->value) && !limit) {
596                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Setting limit to 1023 for ODBC class '%s'.\n", v->value, cat);
597                                                         limit = 1023;
598                                                 } else if (ast_false(v->value)) {
599                                                         ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'.  Disabling ODBC class '%s'.\n", v->value, cat);
600                                                         enabled = 0;
601                                                         break;
602                                                 }
603                                         } else if (!strcasecmp(v->name, "enabled")) {
604                                                 enabled = ast_true(v->value);
605                                         } else if (!strcasecmp(v->name, "pre-connect")) {
606                                                 connect = ast_true(v->value);
607                                         } else if (!strcasecmp(v->name, "dsn")) {
608                                                 dsn = v->value;
609                                         } else if (!strcasecmp(v->name, "username")) {
610                                                 username = v->value;
611                                         } else if (!strcasecmp(v->name, "password")) {
612                                                 password = v->value;
613                                         } else if (!strcasecmp(v->name, "sanitysql")) {
614                                                 sanitysql = v->value;
615                                         }
616                                 }
617
618                                 if (enabled && !ast_strlen_zero(dsn)) {
619                                         /* First, check the list to see if it already exists */
620                                         AST_LIST_TRAVERSE(&odbc_list, class, list) {
621                                                 if (!strcmp(class->name, cat)) {
622                                                         class->delme = 0;
623                                                         break;
624                                                 }
625                                         }
626
627                                         if (class) {
628                                                 new = class;
629                                         } else {
630                                                 new = ast_calloc(1, sizeof(*new));
631                                         }
632
633                                         if (!new) {
634                                                 res = -1;
635                                                 break;
636                                         }
637
638                                         if (cat)
639                                                 ast_copy_string(new->name, cat, sizeof(new->name));
640                                         if (dsn)
641                                                 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
642                                         if (username)
643                                                 ast_copy_string(new->username, username, sizeof(new->username));
644                                         if (password)
645                                                 ast_copy_string(new->password, password, sizeof(new->password));
646                                         if (sanitysql)
647                                                 ast_copy_string(new->sanitysql, sanitysql, sizeof(new->sanitysql));
648
649                                         if (!class) {
650                                                 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
651                                                 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
652
653                                                 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
654                                                         ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
655                                                         SQLFreeHandle(SQL_HANDLE_ENV, new->env);
656                                                         AST_LIST_UNLOCK(&odbc_list);
657                                                         return res;
658                                                 }
659                                         }
660
661                                         if (pooling) {
662                                                 new->haspool = pooling;
663                                                 if (limit) {
664                                                         new->limit = limit;
665                                                 } else {
666                                                         ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless.  Changing limit from 0 to 5.\n");
667                                                         new->limit = 5;
668                                                 }
669                                         }
670
671                                         if (class) {
672                                                 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
673                                         } else {
674                                                 odbc_register_class(new, connect);
675                                                 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
676                                         }
677                                 }
678                         }
679                 }
680                 ast_config_destroy(config);
681         }
682
683         /* Purge classes that we know can go away (pooled with 0, only) */
684         AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
685                 if (class->delme && class->haspool && class->count == 0) {
686                         AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
687                                 AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
688                                 odbc_obj_disconnect(current);
689                                 ast_mutex_destroy(&current->lock);
690                                 ast_free(current);
691                         }
692                         AST_LIST_TRAVERSE_SAFE_END;
693
694                         AST_LIST_REMOVE_CURRENT(&odbc_list, list);
695                         ast_free(class);
696                 }
697         }
698         AST_LIST_TRAVERSE_SAFE_END;
699         AST_LIST_UNLOCK(&odbc_list);
700
701         return 0;
702 }
703
704 static int unload_module(void)
705 {
706         /* Prohibit unloading */
707         return -1;
708 }
709
710 static int load_module(void)
711 {
712         if(load_odbc_config() == -1)
713                 return AST_MODULE_LOAD_DECLINE;
714         ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry));
715         ast_log(LOG_NOTICE, "res_odbc loaded.\n");
716         return 0;
717 }
718
719 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC resource",
720                 .load = load_module,
721                 .unload = unload_module,
722                 .reload = reload,
723                );