Asterisk - The Open Source Telephony Project GIT-master-2de1a68
res_odbc.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2012, 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 * \author Tilghman Lesher <tilghman@digium.com>
29 *
30 * \arg See also: \ref cdr_odbc.c
31 */
32
33/*! \li \ref res_odbc.c uses the configuration file \ref res_odbc.conf
34 * \addtogroup configuration_file Configuration Files
35 */
36
37/*!
38 * \page res_odbc.conf res_odbc.conf
39 * \verbinclude res_odbc.conf.sample
40 */
41
42/*** MODULEINFO
43 <depend>generic_odbc</depend>
44 <depend>res_odbc_transaction</depend>
45 <support_level>core</support_level>
46 ***/
47
48#include "asterisk.h"
49
50#include "asterisk/file.h"
51#include "asterisk/channel.h"
52#include "asterisk/config.h"
53#include "asterisk/pbx.h"
54#include "asterisk/module.h"
55#include "asterisk/cli.h"
56#include "asterisk/lock.h"
57#include "asterisk/res_odbc.h"
58#include "asterisk/time.h"
59#include "asterisk/astobj2.h"
60#include "asterisk/app.h"
61#include "asterisk/strings.h"
63
65{
67 char name[80];
68 char dsn[80];
69 char *username;
70 char *password;
71 char *sanitysql;
72 SQLHENV env;
73 unsigned int delme:1; /*!< Purge the class */
74 unsigned int backslash_is_escape:1; /*!< On this database, the backslash is a native escape sequence */
75 unsigned int forcecommit:1; /*!< Should uncommitted transactions be auto-committed on handle release? */
76 unsigned int isolation; /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
77 unsigned int conntimeout; /*!< Maximum time the connection process should take */
78 unsigned int maxconnections; /*!< Maximum number of allowed connections */
79 /*! When a connection fails, cache that failure for how long? */
81 /*! When a connection fails, when did that last occur? */
82 struct timeval last_negative_connect;
83 /*! A pool of available connections */
85 /*! Lock to protect the connections */
87 /*! Condition to notify any pending connection requesters */
89 /*! The total number of current connections */
91 /*! Whether logging is enabled on this class or not */
92 unsigned int logging;
93 /*! The number of prepares executed on this class (total from all connections */
95 /*! The number of queries executed on this class (total from all connections) */
97 /*! The longest execution time for a query executed on this class */
99 /*! The SQL query that took the longest to execute */
100 char *sql_text;
101 /*! Slow query limit (in milliseconds) */
102 unsigned int slowquerylimit;
103};
104
106
108
109static odbc_status odbc_obj_connect(struct odbc_obj *obj);
110static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
111static void odbc_register_class(struct odbc_class *class, int connect);
112
114
118 struct odbc_obj *obj; /*!< Database handle within which transacted statements are run */
119 /*!\brief Is this record the current active transaction within the channel?
120 * Note that the active flag is really only necessary for statements which
121 * are triggered from the dialplan, as there isn't a direct correlation
122 * between multiple statements. Applications wishing to use transactions
123 * may simply perform each statement on the same odbc_obj, which keeps the
124 * transaction persistent.
125 */
126 unsigned int active:1;
127 unsigned int forcecommit:1; /*!< Should uncommitted transactions be auto-committed on handle release? */
128 unsigned int isolation; /*!< Flags for how the DB should deal with data in other, uncommitted transactions */
129 char name[0]; /*!< Name of this transaction ID */
130};
131
132const char *ast_odbc_isolation2text(int iso)
133{
134 if (iso == SQL_TXN_READ_COMMITTED) {
135 return "read_committed";
136 } else if (iso == SQL_TXN_READ_UNCOMMITTED) {
137 return "read_uncommitted";
138 } else if (iso == SQL_TXN_SERIALIZABLE) {
139 return "serializable";
140 } else if (iso == SQL_TXN_REPEATABLE_READ) {
141 return "repeatable_read";
142 } else {
143 return "unknown";
144 }
145}
146
147int ast_odbc_text2isolation(const char *txt)
148{
149 if (strncasecmp(txt, "read_", 5) == 0) {
150 if (strncasecmp(txt + 5, "c", 1) == 0) {
151 return SQL_TXN_READ_COMMITTED;
152 } else if (strncasecmp(txt + 5, "u", 1) == 0) {
153 return SQL_TXN_READ_UNCOMMITTED;
154 } else {
155 return 0;
156 }
157 } else if (strncasecmp(txt, "ser", 3) == 0) {
158 return SQL_TXN_SERIALIZABLE;
159 } else if (strncasecmp(txt, "rep", 3) == 0) {
160 return SQL_TXN_REPEATABLE_READ;
161 } else {
162 return 0;
163 }
164}
165
166static void odbc_class_destructor(void *data)
167{
168 struct odbc_class *class = data;
169 struct odbc_obj *obj;
170
171 /* Due to refcounts, we can safely assume that any objects with a reference
172 * to us will prevent our destruction, so we don't need to worry about them.
173 */
174 if (class->username) {
175 ast_free(class->username);
176 }
177 if (class->password) {
178 ast_free(class->password);
179 }
180 if (class->sanitysql) {
181 ast_free(class->sanitysql);
182 }
183
184 while ((obj = AST_LIST_REMOVE_HEAD(&class->connections, list))) {
185 ao2_ref(obj, -1);
186 }
187
188 SQLFreeHandle(SQL_HANDLE_ENV, class->env);
189 ast_mutex_destroy(&class->lock);
190 ast_cond_destroy(&class->cond);
191 ast_free(class->sql_text);
192}
193
194static void odbc_obj_destructor(void *data)
195{
196 struct odbc_obj *obj = data;
197
199}
200
202{
203 struct odbc_cache_columns *col;
204
205 ast_debug(1, "Destroying table cache for %s\n", table->table);
206
207 AST_RWLIST_WRLOCK(&table->columns);
208 while ((col = AST_RWLIST_REMOVE_HEAD(&table->columns, list))) {
209 ast_free(col);
210 }
211 AST_RWLIST_UNLOCK(&table->columns);
213
215}
216
217/*!
218 * XXX This creates a connection and disconnects it. In some situations, the caller of
219 * this function has its own connection and could donate it to this function instead of
220 * needing to create another one.
221 *
222 * XXX The automatic readlock of the columns is awkward. It's done because it's possible for
223 * multiple threads to have references to the table, and the table is not refcounted. Possible
224 * changes here would be
225 * * Eliminate the table cache entirely. The use of ast_odbc_find_table() is generally
226 * questionable. The only real good use right now is from ast_realtime_require_field() in
227 * order to make sure the DB has the expected columns in it. Since that is only used sparingly,
228 * the need to cache tables is questionable. Instead, the table structure can be fetched from
229 * the DB directly each time, resulting in a single owner of the data.
230 * * Make odbc_cache_tables a refcounted object.
231 */
232struct odbc_cache_tables *ast_odbc_find_table(const char *database, const char *tablename)
233{
234 struct odbc_cache_tables *tableptr;
236 char columnname[80];
237 SQLLEN sqlptr;
238 SQLHSTMT stmt = NULL;
239 int res = 0, error = 0;
240 struct odbc_obj *obj;
241
244 if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
245 break;
246 }
247 }
248 if (tableptr) {
249 AST_RWLIST_RDLOCK(&tableptr->columns);
251 return tableptr;
252 }
253
254 if (!(obj = ast_odbc_request_obj(database, 0))) {
255 ast_log(LOG_WARNING, "Unable to retrieve database handle for table description '%s@%s'\n", tablename, database);
257 return NULL;
258 }
259
260 /* Table structure not already cached; build it now. */
261 do {
262 res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
263 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
264 ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", database);
265 break;
266 }
267
268 res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)tablename, SQL_NTS, (unsigned char *)"%", SQL_NTS);
269 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
270 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
271 ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'.\n", database);
272 break;
273 }
274
275 if (!(tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + strlen(database) + 1 + strlen(tablename) + 1))) {
276 ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", tablename, database);
277 break;
278 }
279
280 tableptr->connection = (char *)tableptr + sizeof(*tableptr);
281 tableptr->table = (char *)tableptr + sizeof(*tableptr) + strlen(database) + 1;
282 strcpy(tableptr->connection, database); /* SAFE */
283 strcpy(tableptr->table, tablename); /* SAFE */
284 AST_RWLIST_HEAD_INIT(&(tableptr->columns));
285
286 while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
287 SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
288
289 if (!(entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1))) {
290 ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, tablename, database);
291 error = 1;
292 break;
293 }
294 entry->name = (char *)entry + sizeof(*entry);
295 strcpy(entry->name, columnname);
296
297 SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
298 SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
299 SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
300 SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
301 SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
302 SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
303
304 /* Specification states that the octenlen should be the maximum number of bytes
305 * returned in a char or binary column, but it seems that some drivers just set
306 * it to NULL. (Bad Postgres! No biscuit!) */
307 if (entry->octetlen == 0) {
308 entry->octetlen = entry->size;
309 }
310
311 ast_debug(3, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
312 /* Insert column info into column list */
313 AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
314 }
315 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
316
318 AST_RWLIST_RDLOCK(&(tableptr->columns));
319 break;
320 } while (1);
321
323
324 if (error) {
325 destroy_table_cache(tableptr);
326 tableptr = NULL;
327 }
329 return tableptr;
330}
331
333{
334 struct odbc_cache_columns *col;
335 AST_RWLIST_TRAVERSE(&table->columns, col, list) {
336 if (strcasecmp(col->name, colname) == 0) {
337 return col;
338 }
339 }
340 return NULL;
341}
342
343int ast_odbc_clear_cache(const char *database, const char *tablename)
344{
345 struct odbc_cache_tables *tableptr;
346
349 if (strcmp(tableptr->connection, database) == 0 && strcmp(tableptr->table, tablename) == 0) {
351 destroy_table_cache(tableptr);
352 break;
353 }
354 }
357 return tableptr ? 0 : -1;
358}
359
360SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT (*exec_cb)(struct odbc_obj *obj, void *data), void *data)
361{
362 struct timeval start;
363 SQLHSTMT stmt;
364
365 if (obj->parent->logging) {
366 start = ast_tvnow();
367 }
368
369 stmt = exec_cb(obj, data);
370
371 if (obj->parent->logging) {
372 long execution_time = ast_tvdiff_ms(ast_tvnow(), start);
373
374 if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) {
375 ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n",
376 obj->sql_text, execution_time, obj->parent->name);
377 }
378
379 ast_mutex_lock(&obj->parent->lock);
380 if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) {
381 obj->parent->longest_query_execution_time = execution_time;
382 /* Due to the callback nature of the res_odbc API it's not possible to ensure that
383 * the SQL text is removed from the connection in all cases, so only if it becomes the
384 * new longest executing query do we steal the SQL text. In other cases what will happen
385 * is that the SQL text will be freed if the connection is released back to the class or
386 * if a new query is done on the connection.
387 */
388 ast_free(obj->parent->sql_text);
389 obj->parent->sql_text = obj->sql_text;
390 obj->sql_text = NULL;
391 }
393 }
394
395 return stmt;
396}
397
398SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
399{
400 struct timeval start;
401 int res = 0;
402 SQLHSTMT stmt;
403
404 if (obj->parent->logging) {
405 start = ast_tvnow();
406 }
407
408 /* This prepare callback may do more than just prepare -- it may also
409 * bind parameters, bind results, etc. The real key, here, is that
410 * when we disconnect, all handles become invalid for most databases.
411 * We must therefore redo everything when we establish a new
412 * connection. */
413 stmt = prepare_cb(obj, data);
414 if (!stmt) {
415 return NULL;
416 }
417
418 res = SQLExecute(stmt);
419 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
420 if (res == SQL_ERROR) {
421 ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute");
422 }
423
424 ast_log(LOG_WARNING, "SQL Execute error %d!\n", res);
425 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
426 stmt = NULL;
427 } else if (obj->parent->logging) {
428 long execution_time = ast_tvdiff_ms(ast_tvnow(), start);
429
430 if (obj->parent->slowquerylimit && execution_time > obj->parent->slowquerylimit) {
431 ast_log(LOG_WARNING, "SQL query '%s' took %ld milliseconds to execute on class '%s', this may indicate a database problem\n",
432 obj->sql_text, execution_time, obj->parent->name);
433 }
434
435 ast_mutex_lock(&obj->parent->lock);
436
437 /* If this takes the record on longest query execution time, update the parent class
438 * with the information.
439 */
440 if (execution_time > obj->parent->longest_query_execution_time || !obj->parent->sql_text) {
441 obj->parent->longest_query_execution_time = execution_time;
442 ast_free(obj->parent->sql_text);
443 obj->parent->sql_text = obj->sql_text;
444 obj->sql_text = NULL;
445 }
447
449 }
450
451 return stmt;
452}
453
454int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
455{
456 if (obj->parent->logging) {
457 /* It is possible for this connection to be reused without being
458 * released back to the class, so we free what may already exist
459 * and place the new SQL in.
460 */
461 ast_free(obj->sql_text);
462 obj->sql_text = ast_strdup(sql);
464 }
465
466 return SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
467}
468
469SQLRETURN ast_odbc_execute_sql(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
470{
471 if (obj->parent->logging) {
472 ast_free(obj->sql_text);
473 obj->sql_text = ast_strdup(sql);
475 }
476
477 return SQLExecDirect(stmt, (unsigned char *)sql, SQL_NTS);
478}
479
480int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
481{
482 int res = 0;
483
484 res = SQLExecute(stmt);
485 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO) && (res != SQL_NO_DATA)) {
486 if (res == SQL_ERROR) {
487 ast_odbc_print_errors(SQL_HANDLE_STMT, stmt, "SQL Execute");
488 }
489 }
490
491 if (obj->parent->logging) {
493 }
494
495 return res;
496}
497
498SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
499{
500 SQLRETURN res;
501
502 if (pmaxlen == 0) {
503 if (SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), 0, StrLen_or_Ind) == SQL_SUCCESS_WITH_INFO) {
504 ast_str_make_space(buf, *StrLen_or_Ind + 1);
505 }
506 } else if (pmaxlen > 0) {
507 ast_str_make_space(buf, pmaxlen);
508 }
509 res = SQLGetData(StatementHandle, ColumnNumber, TargetType, ast_str_buffer(*buf), ast_str_size(*buf), StrLen_or_Ind);
511
512 return res;
513}
514
515struct ast_str *ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation)
516{
517 struct ast_str *errors = ast_str_thread_get(&errors_buf, 16);
518 SQLINTEGER nativeerror = 0;
519 SQLSMALLINT diagbytes = 0;
520 SQLSMALLINT i;
521 unsigned char state[10];
522 unsigned char diagnostic[256];
523
524 ast_str_reset(errors);
525 i = 0;
526 while (SQLGetDiagRec(handle_type, handle, ++i, state, &nativeerror,
527 diagnostic, sizeof(diagnostic), &diagbytes) == SQL_SUCCESS) {
528 ast_str_append(&errors, 0, "%s%s", ast_str_strlen(errors) ? "," : "", state);
529 ast_log(LOG_WARNING, "%s returned an error: %s: %s\n", operation, state, diagnostic);
530 /* XXX Why is this here? */
531 if (i > 10) {
532 ast_log(LOG_WARNING, "There are more than 10 diagnostic records! Ignore the rest.\n");
533 break;
534 }
535 }
536
537 return errors;
538}
539
540unsigned int ast_odbc_class_get_isolation(struct odbc_class *class)
541{
542 return class->isolation;
543}
544
546{
547 return class->forcecommit;
548}
549
550const char *ast_odbc_class_get_name(struct odbc_class *class)
551{
552 return class->name;
553}
554
555static int load_odbc_config(void)
556{
557 static char *cfg = "res_odbc.conf";
558 struct ast_config *config;
559 struct ast_variable *v;
560 char *cat;
561 const char *dsn, *username, *password, *sanitysql;
562 int enabled, bse, conntimeout, forcecommit, isolation, maxconnections, logging, slowquerylimit;
563 struct timeval ncache = { 0, 0 };
564 int preconnect = 0, res = 0;
565 struct ast_flags config_flags = { 0 };
566
567 struct odbc_class *new;
568
569 config = ast_config_load(cfg, config_flags);
571 ast_log(LOG_WARNING, "Unable to load config file res_odbc.conf\n");
572 return -1;
573 }
574 for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
575 if (!strcasecmp(cat, "ENV")) {
576 for (v = ast_variable_browse(config, cat); v; v = v->next) {
577 setenv(v->name, v->value, 1);
578 ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
579 }
580 } else {
581 /* Reset all to defaults for each class of odbc connections */
583 enabled = 1;
584 preconnect = 0;
585 bse = 1;
586 conntimeout = 10;
587 forcecommit = 0;
588 isolation = SQL_TXN_READ_COMMITTED;
589 maxconnections = 1;
590 logging = 0;
591 slowquerylimit = 5000;
592 for (v = ast_variable_browse(config, cat); v; v = v->next) {
593 if (!strcasecmp(v->name, "pooling") ||
594 !strncasecmp(v->name, "share", 5) ||
595 !strcasecmp(v->name, "limit") ||
596 !strcasecmp(v->name, "idlecheck")) {
597 ast_log(LOG_WARNING, "The 'pooling', 'shared_connections', 'limit', and 'idlecheck' options were replaced by 'max_connections'. See res_odbc.conf.sample.\n");
598 } else if (!strcasecmp(v->name, "enabled")) {
599 enabled = ast_true(v->value);
600 } else if (!strcasecmp(v->name, "pre-connect")) {
601 preconnect = ast_true(v->value);
602 } else if (!strcasecmp(v->name, "dsn")) {
603 dsn = v->value;
604 } else if (!strcasecmp(v->name, "username")) {
605 username = v->value;
606 } else if (!strcasecmp(v->name, "password")) {
607 password = v->value;
608 } else if (!strcasecmp(v->name, "sanitysql")) {
609 sanitysql = v->value;
610 } else if (!strcasecmp(v->name, "backslash_is_escape")) {
611 bse = ast_true(v->value);
612 } else if (!strcasecmp(v->name, "connect_timeout")) {
613 if (sscanf(v->value, "%d", &conntimeout) != 1 || conntimeout < 1) {
614 ast_log(LOG_WARNING, "connect_timeout must be a positive integer\n");
615 conntimeout = 10;
616 }
617 } else if (!strcasecmp(v->name, "negative_connection_cache")) {
618 double dncache;
619 if (sscanf(v->value, "%lf", &dncache) != 1 || dncache < 0) {
620 ast_log(LOG_WARNING, "negative_connection_cache must be a non-negative integer\n");
621 /* 5 minutes sounds like a reasonable default */
622 ncache.tv_sec = 300;
623 ncache.tv_usec = 0;
624 } else {
625 ncache.tv_sec = (int)dncache;
626 ncache.tv_usec = (dncache - ncache.tv_sec) * 1000000;
627 }
628 } else if (!strcasecmp(v->name, "forcecommit")) {
630 } else if (!strcasecmp(v->name, "isolation")) {
631 if ((isolation = ast_odbc_text2isolation(v->value)) == 0) {
632 ast_log(LOG_ERROR, "Unrecognized value for 'isolation': '%s' in section '%s'\n", v->value, cat);
633 isolation = SQL_TXN_READ_COMMITTED;
634 }
635 } else if (!strcasecmp(v->name, "max_connections")) {
636 if (sscanf(v->value, "%30d", &maxconnections) != 1 || maxconnections < 1) {
637 ast_log(LOG_WARNING, "max_connections must be a positive integer\n");
638 maxconnections = 1;
639 }
640 } else if (!strcasecmp(v->name, "logging")) {
641 logging = ast_true(v->value);
642 } else if (!strcasecmp(v->name, "slow_query_limit")) {
643 if (sscanf(v->value, "%30d", &slowquerylimit) != 1) {
644 ast_log(LOG_WARNING, "slow_query_limit must be a positive integer\n");
645 slowquerylimit = 5000;
646 }
647 }
648 }
649
650 if (enabled && !ast_strlen_zero(dsn)) {
651 new = ao2_alloc(sizeof(*new), odbc_class_destructor);
652
653 if (!new) {
654 res = -1;
655 break;
656 }
657
658 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
659 res = SQLSetEnvAttr(new->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
660
661 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
662 ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
663 ao2_ref(new, -1);
664 return res;
665 }
666
667 new->backslash_is_escape = bse ? 1 : 0;
668 new->forcecommit = forcecommit ? 1 : 0;
669 new->isolation = isolation;
670 new->conntimeout = conntimeout;
671 new->negative_connection_cache = ncache;
672 new->maxconnections = maxconnections;
673 new->logging = logging;
674 new->slowquerylimit = slowquerylimit;
675
676 if (cat)
677 ast_copy_string(new->name, cat, sizeof(new->name));
678 if (dsn)
679 ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
680 if (username && !(new->username = ast_strdup(username))) {
681 ao2_ref(new, -1);
682 break;
683 }
684 if (password && !(new->password = ast_strdup(password))) {
685 ao2_ref(new, -1);
686 break;
687 }
688 if (sanitysql && !(new->sanitysql = ast_strdup(sanitysql))) {
689 ao2_ref(new, -1);
690 break;
691 }
692
693 ast_mutex_init(&new->lock);
694 ast_cond_init(&new->cond, NULL);
695
696 odbc_register_class(new, preconnect);
697 ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
698 ao2_ref(new, -1);
699 new = NULL;
700 }
701 }
702 }
704 return res;
705}
706
707static char *handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
708{
709 struct ao2_iterator aoi;
710 struct odbc_class *class;
711 int length = 0;
712 int which = 0;
713 char *ret = NULL;
714
715 switch (cmd) {
716 case CLI_INIT:
717 e->command = "odbc show";
718 e->usage =
719 "Usage: odbc show [class]\n"
720 " List settings of a particular ODBC class or,\n"
721 " if not specified, all classes.\n";
722 return NULL;
723 case CLI_GENERATE:
724 if (a->pos != 2)
725 return NULL;
726 length = strlen(a->word);
728 while ((class = ao2_iterator_next(&aoi))) {
729 if (!strncasecmp(a->word, class->name, length) && ++which > a->n) {
730 ret = ast_strdup(class->name);
731 }
732 ao2_ref(class, -1);
733 if (ret) {
734 break;
735 }
736 }
738 if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
739 ret = ast_strdup("all");
740 }
741 return ret;
742 }
743
744 ast_cli(a->fd, "\nODBC DSN Settings\n");
745 ast_cli(a->fd, "-----------------\n\n");
747 while ((class = ao2_iterator_next(&aoi))) {
748 if ((a->argc == 2) || (a->argc == 3 && !strcmp(a->argv[2], "all")) || (!strcmp(a->argv[2], class->name))) {
749 char timestr[80];
750 struct ast_tm tm;
751
752 ast_cli(a->fd, " Name: %s\n DSN: %s\n", class->name, class->dsn);
753
754 if (class->last_negative_connect.tv_sec > 0) {
755 ast_localtime(&class->last_negative_connect, &tm, NULL);
756 ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
757 ast_cli(a->fd, " Last fail connection attempt: %s\n", timestr);
758 }
759
760 ast_cli(a->fd, " Number of active connections: %zd (out of %d)\n", class->connection_cnt, class->maxconnections);
761 ast_cli(a->fd, " Logging: %s\n", class->logging ? "Enabled" : "Disabled");
762 if (class->logging) {
763 ast_cli(a->fd, " Number of prepares executed: %d\n", class->prepares_executed);
764 ast_cli(a->fd, " Number of queries executed: %d\n", class->queries_executed);
765 ast_mutex_lock(&class->lock);
766 if (class->sql_text) {
767 ast_cli(a->fd, " Longest running SQL query: %s (%ld milliseconds)\n", class->sql_text, class->longest_query_execution_time);
768 }
769 ast_mutex_unlock(&class->lock);
770 }
771 ast_cli(a->fd, "\n");
772 }
773 ao2_ref(class, -1);
774 }
776
777 return CLI_SUCCESS;
778}
779
780static struct ast_cli_entry cli_odbc[] = {
781 AST_CLI_DEFINE(handle_cli_odbc_show, "List ODBC DSN(s)")
782};
783
784static void odbc_register_class(struct odbc_class *class, int preconnect)
785{
786 struct odbc_obj *obj;
787
789 /* I still have a reference in the caller, so a deref is NOT missing here. */
790
791 if (!preconnect) {
792 return;
793 }
794
795 /* Request and release builds a connection */
796 obj = ast_odbc_request_obj(class->name, 0);
797 if (obj) {
799 }
800
801 return;
802}
803
805{
806 struct odbc_class *class = obj->parent;
807
808 ast_debug(2, "Releasing ODBC handle %p into pool\n", obj);
809
810 /* The odbc_obj only holds a reference to the class when it is
811 * actively being used. This guarantees no circular reference
812 * between odbc_class and odbc_obj. Since it is being released
813 * we also release our class reference. If a reload occurred before
814 * the class will go away automatically once all odbc_obj are
815 * released back.
816 */
817 obj->parent = NULL;
818
819 /* Free the SQL text so that the next user of this connection has
820 * a fresh start.
821 */
822 ast_free(obj->sql_text);
823 obj->sql_text = NULL;
824
825 ast_mutex_lock(&class->lock);
826 AST_LIST_INSERT_HEAD(&class->connections, obj, list);
827 ast_cond_signal(&class->cond);
828 ast_mutex_unlock(&class->lock);
829
830 ao2_ref(class, -1);
831}
832
834{
835 return obj->parent->backslash_is_escape;
836}
837
838static int aoro2_class_cb(void *obj, void *arg, int flags)
839{
840 struct odbc_class *class = obj;
841 char *name = arg;
842 if (!strcmp(class->name, name) && !class->delme) {
843 return CMP_MATCH | CMP_STOP;
844 }
845 return 0;
846}
847
848unsigned int ast_odbc_get_max_connections(const char *name)
849{
850 struct odbc_class *class;
851 unsigned int max_connections;
852
853 class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name);
854 if (!class) {
855 return 0;
856 }
857
858 max_connections = class->maxconnections;
859 ao2_ref(class, -1);
860
861 return max_connections;
862}
863
864/*!
865 * \brief Determine if the connection has died.
866 *
867 * \param connection The connection to check
868 * \param class The ODBC class
869 * \retval 1 Yep, it's dead
870 * \retval 0 It's alive and well
871 */
872static int connection_dead(struct odbc_obj *connection, struct odbc_class *class)
873{
874 char *test_sql = "select 1";
875 SQLINTEGER dead;
876 SQLRETURN res;
877 SQLHSTMT stmt;
878
879 res = SQLGetConnectAttr(connection->con, SQL_ATTR_CONNECTION_DEAD, &dead, 0, 0);
880 if (SQL_SUCCEEDED(res)) {
881 return dead == SQL_CD_TRUE ? 1 : 0;
882 }
883
884 /* If the Driver doesn't support SQL_ATTR_CONNECTION_DEAD do a
885 * probing query instead
886 */
887 res = SQLAllocHandle(SQL_HANDLE_STMT, connection->con, &stmt);
888 if (!SQL_SUCCEEDED(res)) {
889 return 1;
890 }
891
892 if (!ast_strlen_zero(class->sanitysql)) {
893 test_sql = class->sanitysql;
894 }
895
896 res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
897 if (!SQL_SUCCEEDED(res)) {
898 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
899 return 1;
900 }
901
902 res = SQLExecute(stmt);
903 SQLFreeHandle(SQL_HANDLE_STMT, stmt);
904
905 return SQL_SUCCEEDED(res) ? 0 : 1;
906}
907
908struct odbc_obj *_ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
909{
910 struct odbc_obj *obj = NULL;
911 struct odbc_class *class;
912
913 if (!(class = ao2_callback(class_container, 0, aoro2_class_cb, (char *) name))) {
914 ast_debug(1, "Class '%s' not found!\n", name);
915 return NULL;
916 }
917
918 while (!obj) {
919 ast_mutex_lock(&class->lock);
920
921 obj = AST_LIST_REMOVE_HEAD(&class->connections, list);
922
923 ast_mutex_unlock(&class->lock);
924
925 if (!obj) {
926 ast_mutex_lock(&class->lock);
927
928 if (class->connection_cnt < class->maxconnections) {
929 /* If no connection is immediately available establish a new
930 * one if allowed. If we try and fail we give up completely as
931 * we could go into an infinite loop otherwise.
932 */
933 obj = ao2_alloc(sizeof(*obj), odbc_obj_destructor);
934 if (!obj) {
935 ast_mutex_unlock(&class->lock);
936 break;
937 }
938
939 obj->parent = ao2_bump(class);
940
941 class->connection_cnt++;
942
943 ast_mutex_unlock(&class->lock);
944
945 if (odbc_obj_connect(obj) == ODBC_FAIL) {
946 ast_mutex_lock(&class->lock);
947 class->connection_cnt--;
948 ast_mutex_unlock(&class->lock);
949 ao2_ref(obj->parent, -1);
950 ao2_ref(obj, -1);
951 obj = NULL;
952 break;
953 }
954
955 ast_mutex_lock(&class->lock);
956
957 ast_debug(2, "Created ODBC handle %p on class '%s', new count is %zd\n", obj,
958 name, class->connection_cnt);
959
960 } else {
961 /* Otherwise if we're not allowed to create a new one we
962 * wait for another thread to give up the connection they
963 * own.
964 */
965 ast_cond_wait(&class->cond, &class->lock);
966 }
967
968 ast_mutex_unlock(&class->lock);
969
970 } else if (connection_dead(obj, class)) {
971 /* If the connection is dead try to grab another functional one from the
972 * pool instead of trying to resurrect this one.
973 */
974 ao2_ref(obj, -1);
975 obj = NULL;
976
977 ast_mutex_lock(&class->lock);
978
979 class->connection_cnt--;
980 ast_debug(2, "ODBC handle %p dead - removing from class '%s', new count is %zd\n",
981 obj, name, class->connection_cnt);
982
983 ast_mutex_unlock(&class->lock);
984
985 } else {
986 /* We successfully grabbed a connection from the pool and all is well!
987 */
988 obj->parent = ao2_bump(class);
989 ast_debug(2, "Reusing ODBC handle %p from class '%s'\n", obj, name);
990 }
991 }
992
993 ao2_ref(class, -1);
994
995 return obj;
996}
997
998struct odbc_obj *_ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
999{
1000 struct ast_flags flags = { check ? RES_ODBC_SANITY_CHECK : 0 };
1001 /* XXX New flow means that the "check" parameter doesn't do anything. We're requesting
1002 * a connection from ODBC. We'll either get a new one, which obviously is already connected, or
1003 * we'll get one from the ODBC connection pool. In that case, it will ensure to only give us a
1004 * live connection
1005 */
1006 return _ast_odbc_request_obj2(name, flags, file, function, lineno);
1007}
1008
1010{
1011 int res;
1012 SQLINTEGER err;
1013 short int mlen;
1014 unsigned char msg[200], state[10];
1015 SQLHDBC con;
1016
1017 /* Nothing to disconnect */
1018 if (!obj->con) {
1019 return ODBC_SUCCESS;
1020 }
1021
1022 con = obj->con;
1023 obj->con = NULL;
1024 res = SQLDisconnect(con);
1025
1026 if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) == SQL_SUCCESS) {
1027 ast_debug(3, "Database handle %p (connection %p) deallocated\n", obj, con);
1028 } else {
1029 SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1030 ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg);
1031 }
1032
1033 return ODBC_SUCCESS;
1034}
1035
1037{
1038 int res;
1039 SQLINTEGER err;
1040 short int mlen;
1041 unsigned char msg[200], state[10];
1042#ifdef NEEDTRACE
1043 SQLINTEGER enable = 1;
1044 char *tracefile = "/tmp/odbc.trace";
1045#endif
1046 SQLHDBC con;
1047 long int negative_cache_expiration;
1048
1049 ast_assert(obj->con == NULL);
1050 ast_debug(3, "Connecting %s(%p)\n", obj->parent->name, obj);
1051
1052 /* Dont connect while server is marked as unreachable via negative_connection_cache */
1053 negative_cache_expiration = obj->parent->last_negative_connect.tv_sec + obj->parent->negative_connection_cache.tv_sec;
1054 if (time(NULL) < negative_cache_expiration) {
1055 char secs[AST_TIME_T_LEN];
1056 ast_time_t_to_string(negative_cache_expiration - time(NULL), secs, sizeof(secs));
1057 ast_log(LOG_WARNING, "Not connecting to %s. Negative connection cache for %s seconds\n", obj->parent->name, secs);
1058 return ODBC_FAIL;
1059 }
1060
1061 res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &con);
1062
1063 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1064 ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
1066 return ODBC_FAIL;
1067 }
1068 SQLSetConnectAttr(con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1069 SQLSetConnectAttr(con, SQL_ATTR_CONNECTION_TIMEOUT, (SQLPOINTER *)(long) obj->parent->conntimeout, 0);
1070#ifdef NEEDTRACE
1071 SQLSetConnectAttr(con, SQL_ATTR_TRACE, &enable, SQL_IS_INTEGER);
1072 SQLSetConnectAttr(con, SQL_ATTR_TRACEFILE, tracefile, strlen(tracefile));
1073#endif
1074
1075 res = SQLConnect(con,
1076 (SQLCHAR *) obj->parent->dsn, SQL_NTS,
1077 (SQLCHAR *) obj->parent->username, SQL_NTS,
1078 (SQLCHAR *) obj->parent->password, SQL_NTS);
1079
1080 if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
1081 SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1083 ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
1084 if ((res = SQLFreeHandle(SQL_HANDLE_DBC, con)) != SQL_SUCCESS) {
1085 SQLGetDiagRec(SQL_HANDLE_DBC, con, 1, state, &err, msg, 100, &mlen);
1086 ast_log(LOG_WARNING, "Unable to deallocate database handle %p? %d errno=%d %s\n", con, res, (int)err, msg);
1087 }
1088 return ODBC_FAIL;
1089 } else {
1090 ast_debug(3, "res_odbc: Connected to %s [%s (%p)]\n", obj->parent->name, obj->parent->dsn, obj);
1091 }
1092
1093 obj->con = con;
1094 return ODBC_SUCCESS;
1095}
1096
1097static int reload(void)
1098{
1099 struct odbc_cache_tables *table;
1100 struct odbc_class *class;
1102
1103 /* First, mark all to be purged */
1104 while ((class = ao2_iterator_next(&aoi))) {
1105 class->delme = 1;
1106 ao2_ref(class, -1);
1107 }
1109
1111
1113 while ((class = ao2_iterator_next(&aoi))) {
1114 if (class->delme) {
1116 }
1117 ao2_ref(class, -1);
1118 }
1120
1121 /* Empty the cache; it will get rebuilt the next time the tables are needed. */
1123 while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
1125 }
1127
1128 return 0;
1129}
1130
1131static int unload_module(void)
1132{
1135
1136 return 0;
1137}
1138
1139static int load_module(void)
1140{
1142 if (!class_container) {
1144 }
1145
1146 if (load_odbc_config() == -1) {
1148 }
1149
1152
1154}
1155
1157 .support_level = AST_MODULE_SUPPORT_CORE,
1158 .load = load_module,
1159 .unload = unload_module,
1160 .reload = reload,
1161 .load_pri = AST_MODPRI_REALTIME_DEPEND,
1162 .requires = "res_odbc_transaction",
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
int ao2_match_by_addr(void *obj, void *arg, int flags)
A common ao2_callback is one that matches by address.
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_unlink(container, obj)
Remove an object from a container.
Definition: astobj2.h:1578
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Allocate and initialize a list container.
Definition: astobj2.h:1327
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
static char * dsn
Definition: cdr_odbc.c:55
static char * table
Definition: cdr_odbc.c:55
static const char config[]
Definition: chan_ooh323.c:111
General Asterisk PBX channel definitions.
Standard Command Line Interface.
#define CLI_SUCCESS
Definition: cli.h:44
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:197
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
static int enabled
Definition: dnsmgr.c:91
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
Generic File Format Support. Should be included by clients of the file handling routines....
static const char name[]
Definition: format_mp3.c:68
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
int setenv(const char *name, const char *value, int overwrite)
Configuration File Parser.
#define ast_config_load(filename, flags)
Load a config file.
char * ast_category_browse(struct ast_config *config, const char *prev_name)
Browse categories.
Definition: extconf.c:3326
#define CONFIG_STATUS_FILEMISSING
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_NOTICE
#define LOG_WARNING
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:225
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_HEAD_INIT(head)
Initializes an rwlist head structure.
Definition: linkedlists.h:639
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized.
Definition: linkedlists.h:333
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:844
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:410
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:494
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
Definition: linkedlists.h:711
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_RWLIST_HEAD_DESTROY(head)
Destroys an rwlist head structure.
Definition: linkedlists.h:667
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2524
Asterisk locking-related definitions:
#define ast_cond_destroy(cond)
Definition: lock.h:202
#define ast_cond_wait(cond, mutex)
Definition: lock.h:205
#define ast_cond_init(cond, attr)
Definition: lock.h:201
#define ast_mutex_init(pmutex)
Definition: lock.h:186
#define ast_mutex_unlock(a)
Definition: lock.h:190
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
pthread_cond_t ast_cond_t
Definition: lock.h:178
#define ast_mutex_destroy(a)
Definition: lock.h:188
#define ast_mutex_lock(a)
Definition: lock.h:189
#define ast_cond_signal(cond)
Definition: lock.h:203
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:316
#define ast_module_shutdown_ref(mod)
Prevent unload of the module before shutdown.
Definition: module.h:464
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_REALTIME_DEPEND
Definition: module.h:321
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Core PBX routines and definitions.
static char * handle_cli_odbc_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: res_odbc.c:707
static struct ast_threadstorage errors_buf
Definition: res_odbc.c:113
static struct ast_cli_entry cli_odbc[]
Definition: res_odbc.c:780
static void odbc_register_class(struct odbc_class *class, int connect)
Definition: res_odbc.c:784
static int load_odbc_config(void)
Definition: res_odbc.c:555
static int aoro2_class_cb(void *obj, void *arg, int flags)
Definition: res_odbc.c:838
static void odbc_obj_destructor(void *data)
Definition: res_odbc.c:194
static odbc_status odbc_obj_connect(struct odbc_obj *obj)
Definition: res_odbc.c:1036
int ast_odbc_prepare(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
Prepares a SQL query on a statement.
Definition: res_odbc.c:454
unsigned int ast_odbc_class_get_isolation(struct odbc_class *class)
Get the transaction isolation setting for an ODBC class.
Definition: res_odbc.c:540
void ast_odbc_release_obj(struct odbc_obj *obj)
Releases an ODBC object previously allocated by ast_odbc_request_obj()
Definition: res_odbc.c:804
struct odbc_cache_tables * ast_odbc_find_table(const char *database, const char *tablename)
Find or create an entry describing the table specified.
Definition: res_odbc.c:232
unsigned int ast_odbc_get_max_connections(const char *name)
Return the current configured maximum number of connections for a class.
Definition: res_odbc.c:848
int ast_odbc_clear_cache(const char *database, const char *tablename)
Remove a cache entry from memory This function may be called to clear entries created and cached by t...
Definition: res_odbc.c:343
struct odbc_cache_columns * ast_odbc_find_column(struct odbc_cache_tables *table, const char *colname)
Find a column entry within a cached table structure.
Definition: res_odbc.c:332
static void odbc_class_destructor(void *data)
Definition: res_odbc.c:166
struct odbc_obj * _ast_odbc_request_obj(const char *name, int check, const char *file, const char *function, int lineno)
Definition: res_odbc.c:998
static struct ao2_container * class_container
Definition: res_odbc.c:105
struct ast_str * ast_odbc_print_errors(SQLSMALLINT handle_type, SQLHANDLE handle, const char *operation)
Shortcut for printing errors to logs after a failed SQL operation.
Definition: res_odbc.c:515
static void destroy_table_cache(struct odbc_cache_tables *table)
Definition: res_odbc.c:201
int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
Checks if the database natively supports backslash as an escape character.
Definition: res_odbc.c:833
const char * ast_odbc_isolation2text(int iso)
Convert from numeric transaction isolation values to their textual counterparts.
Definition: res_odbc.c:132
const char * ast_odbc_class_get_name(struct odbc_class *class)
Get the name of an ODBC class.
Definition: res_odbc.c:550
SQLRETURN ast_odbc_ast_str_SQLGetData(struct ast_str **buf, int pmaxlen, SQLHSTMT StatementHandle, SQLUSMALLINT ColumnNumber, SQLSMALLINT TargetType, SQLLEN *StrLen_or_Ind)
Wrapper for SQLGetData to use with dynamic strings.
Definition: res_odbc.c:498
SQLHSTMT ast_odbc_direct_execute(struct odbc_obj *obj, SQLHSTMT(*exec_cb)(struct odbc_obj *obj, void *data), void *data)
Executes an non prepared statement and returns the resulting statement handle.
Definition: res_odbc.c:360
unsigned int ast_odbc_class_get_forcecommit(struct odbc_class *class)
Get the transaction forcecommit setting for an ODBC class.
Definition: res_odbc.c:545
static int load_module(void)
Definition: res_odbc.c:1139
SQLRETURN ast_odbc_execute_sql(struct odbc_obj *obj, SQLHSTMT *stmt, const char *sql)
Execute a unprepared SQL query.
Definition: res_odbc.c:469
static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
Definition: res_odbc.c:1009
static int unload_module(void)
Definition: res_odbc.c:1131
static int reload(void)
Definition: res_odbc.c:1097
SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT(*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
Prepares, executes, and returns the resulting statement handle.
Definition: res_odbc.c:398
int ast_odbc_text2isolation(const char *txt)
Convert from textual transaction isolation values to their numeric constants.
Definition: res_odbc.c:147
static int connection_dead(struct odbc_obj *connection, struct odbc_class *class)
Determine if the connection has died.
Definition: res_odbc.c:872
struct odbc_obj * _ast_odbc_request_obj2(const char *name, struct ast_flags flags, const char *file, const char *function, int lineno)
Definition: res_odbc.c:908
int ast_odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
Executes a prepared statement handle.
Definition: res_odbc.c:480
ODBC resource manager.
odbc_status
Definition: res_odbc.h:36
@ ODBC_FAIL
Definition: res_odbc.h:36
@ ODBC_SUCCESS
Definition: res_odbc.h:36
@ RES_ODBC_SANITY_CHECK
Definition: res_odbc.h:40
#define ast_odbc_request_obj(name, check)
Get a ODBC connection object.
Definition: res_odbc.h:120
#define NULL
Definition: resample.c:96
String manipulation functions.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true"....
Definition: utils.c:2199
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
Definition: strings.h:693
#define ast_str_make_space(buf, new_len)
Definition: strings.h:828
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:703
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:730
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:742
struct ast_str * ast_str_thread_get(struct ast_threadstorage *ts, size_t init_len)
Retrieve a thread locally stored dynamic string.
Definition: strings.h:909
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Main Channel structure associated with a channel.
descriptor for a cli entry.
Definition: cli.h:171
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Structure used to handle boolean flags.
Definition: utils.h:199
unsigned int flags
Definition: utils.h:200
struct ast_module * self
Definition: module.h:342
Structure for mutex and tracking information.
Definition: lock.h:135
Support for dynamic strings.
Definition: strings.h:623
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Data source name.
Definition: func_odbc.c:167
Definition: search.h:40
These structures are used for adaptive capabilities.
Definition: res_odbc.h:59
char * connection
Definition: res_odbc.h:71
struct odbc_cache_tables::_columns columns
unsigned int slowquerylimit
Definition: res_odbc.c:102
char dsn[80]
Definition: res_odbc.c:68
ast_cond_t cond
Definition: res_odbc.c:88
char * sql_text
Definition: res_odbc.c:100
unsigned int delme
Definition: res_odbc.c:73
int queries_executed
Definition: res_odbc.c:96
char name[80]
Definition: res_odbc.c:67
size_t connection_cnt
Definition: res_odbc.c:90
unsigned int logging
Definition: res_odbc.c:92
char * password
Definition: res_odbc.c:70
unsigned int maxconnections
Definition: res_odbc.c:78
int prepares_executed
Definition: res_odbc.c:94
struct timeval negative_connection_cache
Definition: res_odbc.c:80
unsigned int isolation
Definition: res_odbc.c:76
unsigned int backslash_is_escape
Definition: res_odbc.c:74
char * username
Definition: res_odbc.c:69
SQLHENV env
Definition: res_odbc.c:72
long longest_query_execution_time
Definition: res_odbc.c:98
unsigned int conntimeout
Definition: res_odbc.c:77
struct odbc_class::@442 connections
struct odbc_class::@441 list
struct timeval last_negative_connect
Definition: res_odbc.c:82
unsigned int forcecommit
Definition: res_odbc.c:75
ast_mutex_t lock
Definition: res_odbc.c:86
char * sanitysql
Definition: res_odbc.c:71
ODBC container.
Definition: res_odbc.h:46
char * sql_text
Definition: res_odbc.h:54
struct odbc_class * parent
Definition: res_odbc.h:48
struct odbc_obj::@255 list
SQLHDBC con
Definition: res_odbc.h:47
char name[0]
Definition: res_odbc.c:129
unsigned int isolation
Definition: res_odbc.c:128
struct ast_channel * owner
Definition: res_odbc.c:117
unsigned int active
Is this record the current active transaction within the channel? Note that the active flag is really...
Definition: res_odbc.c:126
unsigned int forcecommit
Definition: res_odbc.c:127
struct odbc_txn_frame::@443 list
struct odbc_obj * obj
Definition: res_odbc.c:118
const char * name
static struct test_val a
Definitions to aid in the use of thread local storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:86
Time-related functions and macros.
int ast_time_t_to_string(time_t time, char *buf, size_t length)
Converts to a string representation of a time_t as decimal seconds since the epoch....
Definition: time.c:152
#define AST_TIME_T_LEN
Definition: time.h:45
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int error(const char *format,...)
Definition: utils/frame.c:999
#define ast_assert(a)
Definition: utils.h:739
#define ARRAY_LEN(a)
Definition: utils.h:666