Let's also include aclocal.m4
[asterisk-bristuff.git] / res / res_odbc.c
blobb0fed02c99218fd3d4b4395e3e37007c59db7ed3
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * res_odbc.c <ODBC resource manager>
9 * Copyright (C) 2004 - 2005 Anthony Minessale II <anthmct@yahoo.com>
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.
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.
22 /*! \file
24 * \brief ODBC resource manager
26 * \author Mark Spencer <markster@digium.com>
27 * \author Anthony Minessale II <anthmct@yahoo.com>
29 * \arg See also: \ref cdr_odbc
32 /*** MODULEINFO
33 <depend>unixodbc</depend>
34 <depend>ltdl</depend>
35 ***/
37 #include "asterisk.h"
39 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <string.h>
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 #include "asterisk/time.h"
58 struct odbc_class
60 AST_LIST_ENTRY(odbc_class) list;
61 char name[80];
62 char dsn[80];
63 char username[80];
64 char password[80];
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 unsigned int backslash_is_escape:1; /* On this database, the backslash is a native escape sequence */
71 unsigned int idlecheck; /* Recheck the connection if it is idle for this long */
72 AST_LIST_HEAD(, odbc_obj) odbc_obj;
75 AST_LIST_HEAD_STATIC(odbc_list, odbc_class);
77 static odbc_status odbc_obj_connect(struct odbc_obj *obj);
78 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
79 static int odbc_register_class(struct odbc_class *class, int connect);
82 SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
84 int res = 0, i, attempt;
85 SQLINTEGER nativeerror=0, numfields=0;
86 SQLSMALLINT diagbytes=0;
87 unsigned char state[10], diagnostic[256];
88 SQLHSTMT stmt;
90 for (attempt = 0; attempt < 2; attempt++) {
91 /* This prepare callback may do more than just prepare -- it may also
92 * bind parameters, bind results, etc. The real key, here, is that
93 * when we disconnect, all handles become invalid for most databases.
94 * We must therefore redo everything when we establish a new
95 * connection. */
96 stmt = prepare_cb(obj, data);
98 if (stmt) {
99 res = SQLExecute(stmt);
100 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
101 if (res == SQL_ERROR) {
102 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
103 for (i = 0; i < numfields; i++) {
104 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
105 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
106 if (i > 10) {
107 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
108 break;
113 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
114 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
115 stmt = NULL;
117 obj->up = 0;
119 * While this isn't the best way to try to correct an error, this won't automatically
120 * fail when the statement handle invalidates.
122 /* XXX Actually, it might, if we're using a non-pooled connection. Possible race here. XXX */
123 odbc_obj_disconnect(obj);
124 odbc_obj_connect(obj);
125 continue;
126 } else
127 obj->last_used = ast_tvnow();
128 break;
129 } else {
130 ast_log(LOG_WARNING, "SQL Prepare failed. Attempting a reconnect...\n");
131 odbc_obj_disconnect(obj);
132 odbc_obj_connect(obj);
136 return stmt;
139 int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
141 int res = 0, i;
142 SQLINTEGER nativeerror=0, numfields=0;
143 SQLSMALLINT diagbytes=0;
144 unsigned char state[10], diagnostic[256];
146 res = SQLExecute(stmt);
147 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
148 if (res == SQL_ERROR) {
149 SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
150 for (i = 0; i < numfields; i++) {
151 SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
152 ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
153 if (i > 10) {
154 ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
155 break;
159 #if 0
160 /* This is a really bad method of trying to correct a dead connection. It
161 * only ever really worked with MySQL. It will not work with any other
162 * database, since most databases prepare their statements on the server,
163 * and if you disconnect, you invalidate the statement handle. Hence, if
164 * you disconnect, you're going to fail anyway, whether you try to execute
165 * a second time or not.
167 ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
168 ast_mutex_lock(&obj->lock);
169 obj->up = 0;
170 ast_mutex_unlock(&obj->lock);
171 odbc_obj_disconnect(obj);
172 odbc_obj_connect(obj);
173 res = SQLExecute(stmt);
174 #endif
175 } else
176 obj->last_used = ast_tvnow();
178 return res;
182 int ast_odbc_sanity_check(struct odbc_obj *obj)
184 char *test_sql = "select 1";
185 SQLHSTMT stmt;
186 int res = 0;
188 if (obj->up) {
189 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
190 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
191 obj->up = 0;
192 } else {
193 res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
194 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
195 obj->up = 0;
196 } else {
197 res = SQLExecute(stmt);
198 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
199 obj->up = 0;
203 SQLFreeHandle (SQL_HANDLE_STMT, stmt);
206 if (!obj->up) { /* Try to reconnect! */
207 ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
208 odbc_obj_disconnect(obj);
209 odbc_obj_connect(obj);
211 return obj->up;
214 static int load_odbc_config(void)
216 static char *cfg = "res_odbc.conf";
217 struct ast_config *config;
218 struct ast_variable *v;
219 char *cat, *dsn, *username, *password;
220 int enabled, pooling, limit, bse;
221 unsigned int idlecheck;
222 int connect = 0, res = 0;
224 struct odbc_class *new;
226 config = ast_config_load(cfg);
227 if (!config) {
228 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
229 return -1;
231 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
232 if (!strcasecmp(cat, "ENV")) {
233 for (v = ast_variable_browse(config, cat); v; v = v->next) {
234 setenv(v->name, v->value, 1);
235 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
237 } else {
238 /* Reset all to defaults for each class of odbc connections */
239 dsn = username = password = NULL;
240 enabled = 1;
241 connect = idlecheck = 0;
242 pooling = 0;
243 limit = 0;
244 bse = 1;
245 for (v = ast_variable_browse(config, cat); v; v = v->next) {
246 if (!strcasecmp(v->name, "pooling")) {
247 if (ast_true(v->value))
248 pooling = 1;
249 } else if (!strcasecmp(v->name, "limit")) {
250 sscanf(v->value, "%d", &limit);
251 if (ast_true(v->value) && !limit) {
252 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);
253 limit = 1023;
254 } else if (ast_false(v->value)) {
255 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'. Disabling ODBC class '%s'.\n", v->value, cat);
256 enabled = 0;
257 break;
259 } else if (!strcasecmp(v->name, "idlecheck")) {
260 sscanf(v->value, "%d", &idlecheck);
261 } else if (!strcasecmp(v->name, "enabled")) {
262 enabled = ast_true(v->value);
263 } else if (!strcasecmp(v->name, "pre-connect")) {
264 connect = ast_true(v->value);
265 } else if (!strcasecmp(v->name, "dsn")) {
266 dsn = v->value;
267 } else if (!strcasecmp(v->name, "username")) {
268 username = v->value;
269 } else if (!strcasecmp(v->name, "password")) {
270 password = v->value;
271 } else if (!strcasecmp(v->name, "backslash_is_escape")) {
272 bse = ast_true(v->value);
276 if (enabled && !ast_strlen_zero(dsn)) {
277 new = ast_calloc(1, sizeof(*new));
279 if (!new) {
280 res = -1;
281 break;
284 if (cat)
285 ast_copy_string(new->name, cat, sizeof(new->name));
286 if (dsn)
287 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
288 if (username)
289 ast_copy_string(new->username, username, sizeof(new->username));
290 if (password)
291 ast_copy_string(new->password, password, sizeof(new->password));
293 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
294 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
296 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
297 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
298 SQLFreeHandle(SQL_HANDLE_ENV, new->env);
299 return res;
302 if (pooling) {
303 new->haspool = pooling;
304 if (limit) {
305 new->limit = limit;
306 } else {
307 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless. Changing limit from 0 to 5.\n");
308 new->limit = 5;
312 new->backslash_is_escape = bse ? 1 : 0;
313 new->idlecheck = idlecheck;
315 odbc_register_class(new, connect);
316 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
320 ast_config_destroy(config);
321 return res;
324 static int odbc_show_command(int fd, int argc, char **argv)
326 struct odbc_class *class;
327 struct odbc_obj *current;
329 AST_LIST_LOCK(&odbc_list);
330 AST_LIST_TRAVERSE(&odbc_list, class, list) {
331 if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
332 int count = 0;
333 ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
335 if (class->haspool) {
336 ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections in use: %d\n", class->limit, class->count);
338 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
339 ast_cli(fd, " Connection %d: %s\n", ++count, current->used ? "in use" : current->up && ast_odbc_sanity_check(current) ? "connected" : "disconnected");
341 } else {
342 /* Should only ever be one of these */
343 AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
344 ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && ast_odbc_sanity_check(current) ? "yes" : "no");
348 ast_cli(fd, "\n");
351 AST_LIST_UNLOCK(&odbc_list);
353 return 0;
356 static char show_usage[] =
357 "Usage: odbc show [<class>]\n"
358 " List settings of a particular ODBC class.\n"
359 " or, if not specified, all classes.\n";
361 static struct ast_cli_entry cli_odbc[] = {
362 { { "odbc", "show", NULL },
363 odbc_show_command, "List ODBC DSN(s)",
364 show_usage },
367 static int odbc_register_class(struct odbc_class *class, int connect)
369 struct odbc_obj *obj;
370 if (class) {
371 AST_LIST_LOCK(&odbc_list);
372 AST_LIST_INSERT_HEAD(&odbc_list, class, list);
373 AST_LIST_UNLOCK(&odbc_list);
375 if (connect) {
376 /* Request and release builds a connection */
377 obj = ast_odbc_request_obj(class->name, 0);
378 if (obj)
379 ast_odbc_release_obj(obj);
382 return 0;
383 } else {
384 ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
385 return -1;
389 void ast_odbc_release_obj(struct odbc_obj *obj)
391 /* For pooled connections, this frees the connection to be
392 * reused. For non-pooled connections, it does nothing. */
393 obj->used = 0;
396 int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
398 return obj->parent->backslash_is_escape;
401 struct odbc_obj *ast_odbc_request_obj(const char *name, int check)
403 struct odbc_obj *obj = NULL;
404 struct odbc_class *class;
406 AST_LIST_LOCK(&odbc_list);
407 AST_LIST_TRAVERSE(&odbc_list, class, list) {
408 if (!strcmp(class->name, name))
409 break;
411 AST_LIST_UNLOCK(&odbc_list);
413 if (!class)
414 return NULL;
416 AST_LIST_LOCK(&class->odbc_obj);
417 if (class->haspool) {
418 /* Recycle connections before building another */
419 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
420 if (! obj->used) {
421 obj->used = 1;
422 break;
426 if (!obj && (class->count < class->limit)) {
427 class->count++;
428 obj = ast_calloc(1, sizeof(*obj));
429 if (!obj) {
430 AST_LIST_UNLOCK(&class->odbc_obj);
431 return NULL;
433 ast_mutex_init(&obj->lock);
434 obj->parent = class;
435 if (odbc_obj_connect(obj) == ODBC_FAIL) {
436 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
437 ast_mutex_destroy(&obj->lock);
438 free(obj);
439 obj = NULL;
440 class->count--;
441 } else {
442 obj->used = 1;
443 AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
446 } else {
447 /* Non-pooled connection: multiple modules can use the same connection. */
448 AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
449 /* Non-pooled connection: if there is an entry, return it */
450 break;
453 if (!obj) {
454 /* No entry: build one */
455 obj = ast_calloc(1, sizeof(*obj));
456 if (!obj) {
457 AST_LIST_UNLOCK(&class->odbc_obj);
458 return NULL;
460 ast_mutex_init(&obj->lock);
461 obj->parent = class;
462 if (odbc_obj_connect(obj) == ODBC_FAIL) {
463 ast_log(LOG_WARNING, "Failed to connect to %s\n", name);
464 ast_mutex_destroy(&obj->lock);
465 free(obj);
466 obj = NULL;
467 } else {
468 AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
472 AST_LIST_UNLOCK(&class->odbc_obj);
474 if (obj && check) {
475 ast_odbc_sanity_check(obj);
476 } else if (obj && obj->parent->idlecheck > 0 && ast_tvdiff_ms(ast_tvnow(), obj->last_used) / 1000 > obj->parent->idlecheck)
477 odbc_obj_connect(obj);
479 return obj;
482 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
484 int res;
485 SQLINTEGER err;
486 short int mlen;
487 unsigned char msg[200], stat[10];
489 /* Nothing to disconnect */
490 if (!obj->con) {
491 return ODBC_SUCCESS;
494 ast_mutex_lock(&obj->lock);
496 res = SQLDisconnect(obj->con);
498 if (res == SQL_SUCCESS || res == SQL_SUCCESS_WITH_INFO) {
499 ast_log(LOG_DEBUG, "Disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
500 } else {
501 ast_log(LOG_DEBUG, "res_odbc: %s [%s] already disconnected\n", obj->parent->name, obj->parent->dsn);
504 if ((res = SQLFreeHandle(SQL_HANDLE_DBC, obj->con) == SQL_SUCCESS)) {
505 obj->con = NULL;
506 ast_log(LOG_DEBUG, "Database handle deallocated\n");
507 } else {
508 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
509 ast_log(LOG_WARNING, "Unable to deallocate database handle? %d errno=%d %s\n", res, (int)err, msg);
512 obj->up = 0;
513 ast_mutex_unlock(&obj->lock);
514 return ODBC_SUCCESS;
517 static odbc_status odbc_obj_connect(struct odbc_obj *obj)
519 int res;
520 SQLINTEGER err;
521 short int mlen;
522 unsigned char msg[200], stat[10];
523 #ifdef NEEDTRACE
524 SQLINTEGER enable = 1;
525 char *tracefile = "/tmp/odbc.trace";
526 #endif
527 ast_mutex_lock(&obj->lock);
529 if (obj->up) {
530 odbc_obj_disconnect(obj);
531 ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
532 } else {
533 ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
536 res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
538 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
539 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
540 ast_mutex_unlock(&obj->lock);
541 return ODBC_FAIL;
543 SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
544 SQLSetConnectAttr(obj->con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *) 10, 0);
545 #ifdef NEEDTRACE
546 SQLSetConnectAttr(obj->con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
547 SQLSetConnectAttr(obj->con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
548 #endif
550 res = SQLConnect(obj->con,
551 (SQLCHAR *) obj->parent->dsn, SQL_NTS,
552 (SQLCHAR *) obj->parent->username, SQL_NTS,
553 (SQLCHAR *) obj->parent->password, SQL_NTS);
555 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
556 SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
557 ast_mutex_unlock(&obj->lock);
558 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
559 return ODBC_FAIL;
560 } else {
561 ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
562 obj->up = 1;
563 obj->last_used = ast_tvnow();
566 ast_mutex_unlock(&obj->lock);
567 return ODBC_SUCCESS;
570 static int reload(void)
572 static char *cfg = "res_odbc.conf";
573 struct ast_config *config;
574 struct ast_variable *v;
575 char *cat, *dsn, *username, *password;
576 int enabled, pooling, limit, bse;
577 unsigned int idlecheck;
578 int connect = 0, res = 0;
580 struct odbc_class *new, *class;
581 struct odbc_obj *current;
583 /* First, mark all to be purged */
584 AST_LIST_LOCK(&odbc_list);
585 AST_LIST_TRAVERSE(&odbc_list, class, list) {
586 class->delme = 1;
589 config = ast_config_load(cfg);
590 if (config) {
591 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
592 if (!strcasecmp(cat, "ENV")) {
593 for (v = ast_variable_browse(config, cat); v; v = v->next) {
594 setenv(v->name, v->value, 1);
595 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
597 } else {
598 /* Reset all to defaults for each class of odbc connections */
599 dsn = username = password = NULL;
600 enabled = 1;
601 connect = idlecheck = 0;
602 pooling = 0;
603 limit = 0;
604 bse = 1;
605 for (v = ast_variable_browse(config, cat); v; v = v->next) {
606 if (!strcasecmp(v->name, "pooling")) {
607 pooling = 1;
608 } else if (!strcasecmp(v->name, "limit")) {
609 sscanf(v->value, "%d", &limit);
610 if (ast_true(v->value) && !limit) {
611 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);
612 limit = 1023;
613 } else if (ast_false(v->value)) {
614 ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'. Disabling ODBC class '%s'.\n", v->value, cat);
615 enabled = 0;
616 break;
618 } else if (!strcasecmp(v->name, "idlecheck")) {
619 sscanf(v->value, "%ud", &idlecheck);
620 } else if (!strcasecmp(v->name, "enabled")) {
621 enabled = ast_true(v->value);
622 } else if (!strcasecmp(v->name, "pre-connect")) {
623 connect = ast_true(v->value);
624 } else if (!strcasecmp(v->name, "dsn")) {
625 dsn = v->value;
626 } else if (!strcasecmp(v->name, "username")) {
627 username = v->value;
628 } else if (!strcasecmp(v->name, "password")) {
629 password = v->value;
630 } else if (!strcasecmp(v->name, "backslash_is_escape")) {
631 bse = ast_true(v->value);
635 if (enabled && !ast_strlen_zero(dsn)) {
636 /* First, check the list to see if it already exists */
637 AST_LIST_TRAVERSE(&odbc_list, class, list) {
638 if (!strcmp(class->name, cat)) {
639 class->delme = 0;
640 break;
644 if (class) {
645 new = class;
646 } else {
647 new = ast_calloc(1, sizeof(*new));
650 if (!new) {
651 res = -1;
652 break;
655 if (cat)
656 ast_copy_string(new->name, cat, sizeof(new->name));
657 if (dsn)
658 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
659 if (username)
660 ast_copy_string(new->username, username, sizeof(new->username));
661 if (password)
662 ast_copy_string(new->password, password, sizeof(new->password));
664 if (!class) {
665 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
666 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
668 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
669 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
670 SQLFreeHandle(SQL_HANDLE_ENV, new->env);
671 AST_LIST_UNLOCK(&odbc_list);
672 return res;
676 if (pooling) {
677 new->haspool = pooling;
678 if (limit) {
679 new->limit = limit;
680 } else {
681 ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless. Changing limit from 0 to 5.\n");
682 new->limit = 5;
686 new->backslash_is_escape = bse;
687 new->idlecheck = idlecheck;
689 if (class) {
690 ast_log(LOG_NOTICE, "Refreshing ODBC class '%s' dsn->[%s]\n", cat, dsn);
691 } else {
692 odbc_register_class(new, connect);
693 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
698 ast_config_destroy(config);
701 /* Purge classes that we know can go away (pooled with 0, only) */
702 AST_LIST_TRAVERSE_SAFE_BEGIN(&odbc_list, class, list) {
703 if (class->delme && class->haspool && class->count == 0) {
704 AST_LIST_TRAVERSE_SAFE_BEGIN(&(class->odbc_obj), current, list) {
705 AST_LIST_REMOVE_CURRENT(&(class->odbc_obj), list);
706 odbc_obj_disconnect(current);
707 ast_mutex_destroy(&current->lock);
708 free(current);
710 AST_LIST_TRAVERSE_SAFE_END;
712 AST_LIST_REMOVE_CURRENT(&odbc_list, list);
713 free(class);
716 AST_LIST_TRAVERSE_SAFE_END;
717 AST_LIST_UNLOCK(&odbc_list);
719 return 0;
722 static int unload_module(void)
724 /* Prohibit unloading */
725 return -1;
728 static int load_module(void)
730 if(load_odbc_config() == -1)
731 return AST_MODULE_LOAD_DECLINE;
732 ast_cli_register_multiple(cli_odbc, sizeof(cli_odbc) / sizeof(struct ast_cli_entry));
733 ast_log(LOG_NOTICE, "res_odbc loaded.\n");
734 return 0;
737 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "ODBC Resource",
738 .load = load_module,
739 .unload = unload_module,
740 .reload = reload,