[asterisk-commits] branch tilghman/res_odbc_rewrite r9184 - in
/team/tilghman/res_odbc_rewrite: ...
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Mon Feb 6 17:41:42 MST 2006
Author: tilghman
Date: Mon Feb 6 18:41:41 2006
New Revision: 9184
URL: http://svn.digium.com/view/asterisk?rev=9184&view=rev
Log:
Lots more futzing around with the structures
Modified:
team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h
team/tilghman/res_odbc_rewrite/res/res_config_odbc.c
team/tilghman/res_odbc_rewrite/res/res_odbc.c
Modified: team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h?rev=9184&r1=9183&r2=9184&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h (original)
+++ team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h Mon Feb 6 18:41:41 2006
@@ -29,16 +29,15 @@
#include <sqlext.h>
#include <sqltypes.h>
-typedef struct odbc_obj odbc_obj;
-
typedef enum { ODBC_SUCCESS=0, ODBC_FAIL=-1} odbc_status;
struct odbc_obj {
+ ast_mutex_t lock;
SQLHDBC con; /* ODBC Connection Handle */
- struct odbc_list *parent; /* Information about the connection is protected */
- struct odbc_obj *next;
+ struct odbc_class *parent; /* Information about the connection is protected */
unsigned int used:1;
unsigned int up:1;
+ AST_LIST_ENTRY(odbc_obj) list;
};
/* functions */
@@ -50,13 +49,13 @@
void destroy_odbc_obj(odbc_obj **obj); /* STATIC */
int register_odbc_obj(char *name,odbc_obj *obj); /* STATIC */
#endif
-int odbc_smart_execute(odbc_obj *obj, SQLHSTMT stmt); /* DEPRECATE */
+int odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt); /* DEPRECATE */
-odbc_obj *fetch_odbc_obj(const char *name, int check);
-int release_odbc_obj(odbc_obj *obj);
-void odbc_dump_fd(int fd, odbc_obj *obj);
-int odbc_sanity_check(odbc_obj *obj);
-SQLHSTMT odbc_prepare_and_execute(odbc_obj *obj, SQLHSTMT (*prepare_cb)(odbc_obj *obj, void *data), void *data);
-int odbc_smart_direct_execute(odbc_obj *obj, SQLHSTMT stmt, char *sql);
+#define fetch_odbc_obj odbc_request_obj
+struct odbc_obj *odbc_request_obj(const char *name, int check);
+int release_odbc_obj(struct odbc_obj *obj);
+int odbc_sanity_check(struct odbc_obj *obj);
+SQLHSTMT odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data);
+int odbc_smart_direct_execute(struct odbc_obj *obj, SQLHSTMT stmt, char *sql);
#endif /* _ASTERISK_RES_ODBC_H */
Modified: team/tilghman/res_odbc_rewrite/res/res_config_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/res/res_config_odbc.c?rev=9184&r1=9183&r2=9184&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_config_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_config_odbc.c Mon Feb 6 18:41:41 2006
@@ -55,7 +55,7 @@
static struct ast_variable *realtime_odbc(const char *database, const char *table, va_list ap)
{
- odbc_obj *obj;
+ struct odbc_obj *obj;
SQLHSTMT stmt;
char sql[1024];
char coltitle[256];
@@ -191,7 +191,7 @@
static struct ast_config *realtime_multi_odbc(const char *database, const char *table, va_list ap)
{
- odbc_obj *obj;
+ struct odbc_obj *obj;
SQLHSTMT stmt;
char sql[1024];
char coltitle[256];
@@ -341,7 +341,7 @@
static int update_odbc(const char *database, const char *table, const char *keyfield, const char *lookup, va_list ap)
{
- odbc_obj *obj;
+ struct odbc_obj *obj;
SQLHSTMT stmt;
char sql[256];
SQLLEN rowcount=0;
@@ -423,7 +423,7 @@
struct ast_variable *new_v;
struct ast_category *cur_cat;
int res = 0;
- odbc_obj *obj;
+ struct odbc_obj *obj;
SQLINTEGER err=0, commented=0, cat_metric=0, var_metric=0, last_cat_metric=0;
SQLBIGINT id;
char sql[255] = "", filename[128], category[128], var_name[128], var_val[512];
Modified: team/tilghman/res_odbc_rewrite/res/res_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/res/res_odbc.c?rev=9184&r1=9183&r2=9184&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_odbc.c Mon Feb 6 18:41:41 2006
@@ -49,10 +49,10 @@
#include "asterisk/cli.h"
#include "asterisk/lock.h"
#include "asterisk/res_odbc.h"
-#define MAX_ODBC_HANDLES 25
-
-struct odbc_list
-{
+
+struct odbc_class
+{
+ AST_LIST_ENTRY(odbc_class) list;
char name[80];
char dsn[80];
char username[80];
@@ -61,40 +61,40 @@
unsigned int haspool:1; /* Boolean - TDS databases need this */
unsigned int limit:10; /* Gives a limit of 1023 maximum */
unsigned int count:10; /* Running count of pooled connections */
- ast_mutex_t lock; /* Protects the list of connections */
- odbc_obj *head;
- odbc_obj *tail;
+ AST_LIST_HEAD(, odbc_obj) odbc_obj;
};
-static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
-
+AST_LIST_HEAD_STATIC(odbc_list, odbc_class);
+
+static odbc_status odbc_obj_connect(struct odbc_obj *obj);
+static odbc_status odbc_obj_disconnect(struct odbc_obj *obj);
static void odbc_destroy(void)
{
- int x = 0;
+ /* int x = 0;
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
if (ODBC_REGISTRY[x].obj) {
destroy_odbc_obj(&ODBC_REGISTRY[x].obj);
ODBC_REGISTRY[x].obj = NULL;
}
- }
-}
-
-static odbc_obj *odbc_read(struct odbc_list *registry, const char *name)
-{
- int x = 0;
+ } */
+}
+
+static struct odbc_obj *odbc_read(struct odbc_class *registry, const char *name)
+{
+ /* int x = 0;
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
if (registry[x].used && !strcmp(registry[x].name, name)) {
return registry[x].obj;
}
- }
+ } */
return NULL;
}
-static int odbc_write(struct odbc_list *registry, char *name, odbc_obj *obj)
-{
- int x = 0;
+static int odbc_write(struct odbc_class *registry, char *name, struct odbc_obj *obj)
+{
+ /* int x = 0;
for (x = 0; x < MAX_ODBC_HANDLES; x++) {
if (!registry[x].used) {
ast_copy_string(registry[x].name, name, sizeof(registry[x].name));
@@ -102,22 +102,14 @@
registry[x].used = 1;
return 1;
}
- }
+ } */
return 0;
-}
-
-static void odbc_init(void)
-{
- int x = 0;
- for (x = 0; x < MAX_ODBC_HANDLES; x++) {
- memset(&ODBC_REGISTRY[x], 0, sizeof(struct odbc_list));
- }
}
static char *tdesc = "ODBC Resource";
/* internal stuff */
-SQLHSTMT odbc_prepare_and_execute(odbc_obj *obj, SQLHSTMT (*prepare_cb)(odbc_obj *obj, void *data), void *data)
+SQLHSTMT odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
{
int res = 0, i, attempt;
SQLINTEGER nativeerror=0, numfields=0;
@@ -152,6 +144,10 @@
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
obj->up = 0;
+ /*
+ * While this isn't the best way to try to correct an error, this won't automatically
+ * fail when the statement handle invalidates.
+ */
odbc_obj_disconnect(obj);
odbc_obj_connect(obj);
continue;
@@ -163,7 +159,7 @@
return stmt;
}
-int odbc_smart_execute(odbc_obj *obj, SQLHSTMT stmt)
+int odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt)
{
int res = 0, i;
SQLINTEGER nativeerror=0, numfields=0;
@@ -183,7 +179,14 @@
}
}
}
-/*
+#if 0
+ /* This is a really bad method of trying to correct a dead connection. It
+ * only ever really worked with MySQL. It will not work with any other
+ * database, since most databases prepare their statements on the server,
+ * and if you disconnect, you invalidate the statement handle. Hence, if
+ * you disconnect, you're going to fail anyway, whether you try to execute
+ * a second time or not.
+ */
ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
ast_mutex_lock(&obj->lock);
obj->up = 0;
@@ -191,30 +194,14 @@
odbc_obj_disconnect(obj);
odbc_obj_connect(obj);
res = SQLExecute(stmt);
-*/
+#endif
}
return res;
}
-int odbc_smart_direct_execute(odbc_obj *obj, SQLHSTMT stmt, char *sql)
-{
- int res = 0;
-
- res = SQLExecDirect (stmt, (unsigned char *)sql, SQL_NTS);
- if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
- ast_log(LOG_WARNING, "SQL Execute error! Attempting a reconnect...\n");
- obj->up = 0;
- odbc_obj_disconnect(obj);
- odbc_obj_connect(obj);
- res = SQLExecDirect (stmt, (unsigned char *)sql, SQL_NTS);
- }
-
- return res;
-}
-
-int odbc_sanity_check(odbc_obj *obj)
+int odbc_sanity_check(struct odbc_obj *obj)
{
char *test_sql = "select 1";
SQLHSTMT stmt;
@@ -252,11 +239,10 @@
struct ast_config *config;
struct ast_variable *v;
char *cat, *dsn, *username, *password;
- int enabled, pooling = 0, limit = 1023;
- int connect = 0;
- char *env_var;
-
- odbc_obj *obj;
+ int enabled, pooling, limit;
+ int connect = 0, res = 0;
+
+ struct odbc_class *new;
config = ast_config_load(cfg);
if (config) {
@@ -267,17 +253,24 @@
ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
}
} else {
+ /* Reset all to defaults for each class of odbc connections */
dsn = username = password = NULL;
enabled = 1;
connect = 0;
+ pooling = 0;
+ limit = 0;
for (v = ast_variable_browse(config, cat); v; v = v->next) {
if (!strcasecmp(v->name, "pooling")) {
pooling = 1;
} else if (!strcasecmp(v->name, "limit")) {
sscanf(v->value, "%d", &limit);
if (ast_true(v->value) && !limit) {
- ast_log(LOG_WARNING, "Limit is a number, not a boolean: '%s'. Setting limit to MAX.\n", v->value);
+ 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);
limit = 1023;
+ } else if (ast_false(v->value)) {
+ ast_log(LOG_WARNING, "Limit should be a number, not a boolean: '%s'. Disabling ODBC class '%s'.\n", v->value, cat);
+ enabled = 0;
+ break;
}
} else if (!strcasecmp(v->name, "enabled")) {
enabled = ast_true(v->value);
@@ -292,87 +285,99 @@
}
}
- if (enabled && dsn) {
- obj = new_odbc_obj(cat, dsn, username, password);
- if (obj) {
- register_odbc_obj(cat, obj);
- ast_log(LOG_NOTICE, "registered database handle '%s' dsn->[%s]\n", cat, obj->dsn);
- if (connect) {
- odbc_obj_connect(obj);
+ if (enabled && !ast_strlen_zero(dsn)) {
+ new = ast_calloc(1, sizeof(*new));
+
+ if (!new) {
+ ast_log(LOG_ERROR, "Memory error while loading configuration.\n");
+ res = -1;
+ break;
+ }
+
+ ast_copy_string(new->name, cat, sizeof(new->name));
+ ast_copy_string(new->dsn, dsn, sizeof(new->dsn));
+ ast_copy_string(new->username, username, sizeof(new->username));
+ ast_copy_string(new->password, password, sizeof(new->password));
+ if (pooling) {
+ new->haspool = pooling;
+ if (limit) {
+ new->limit = limit;
+ } else {
+ ast_log(LOG_WARNING, "Pooling without also setting a limit is pointless. Changing limit from 0 to 5.\n");
+ new->limit = 5;
}
- } else {
- ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
}
+ register_odbc_class(new, connect);
+ ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
}
}
}
ast_config_destroy(config);
}
- return 0;
-}
-
-static void odbc_dump_fd(int fd, struct odbc_list *list)
-{
- int count = 0;
- ast_mutex_lock(&list->lock);
- ast_cli(fd, "Name: %s\nDSN: %s\n", list->name, list->dsn);
-
- if (list->haspool) {
- ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections: %d\n", list->limit, list->count);
-
- for (obj = list->head; obj; obj = obj->next) {
- ast_cli(fd, " Connection %d: %s", ++count, obj->up && odbc_sanity_check(obj) ? "connected" : "disconnected");
- }
- } else {
- ast_cli(fd, "Pooled: no\nConnected: %s\n", list->head && list->head->up && odbc_sanity_check(list->head) ? "yes" : "no");
- }
-
- ast_cli(fd, "\n");
-
- ast_mutex_unlock(&list->lock);
+ return res;
}
static int odbc_show_command(int fd, int argc, char **argv)
{
- odbc_obj *obj;
- int i;
+ struct odbc_class *class;
+ struct odbc_obj *current;
+ int count=0;
if (!strcmp(argv[1], "show")) {
- if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all"))) {
- for (i = 0; i < MAX_ODBC_HANDLES; i++) {
- odbc_dump_fd(fd, &ODBC_REGISTRY[i]);
+ AST_LIST_LOCK(&odbc_list);
+ AST_LIST_TRAVERSE(&odbc_list, class, list) {
+ if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all")) || (!strcmp(argv[2], class->name))) {
+ ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
+
+ if (class->haspool) {
+ ast_cli(fd, "Pooled: yes\nLimit: %d\nConnections: %d\n", class->limit, class->count);
+
+ AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
+ ast_cli(fd, " Connection %d: %s", ++count, current->up && odbc_sanity_check(current) ? "connected" : "disconnected");
+ }
+ } else {
+ /* Should only ever be one of these */
+ AST_LIST_TRAVERSE(&(class->odbc_obj), current, list) {
+ ast_cli(fd, "Pooled: no\nConnected: %s\n", current->up && odbc_sanity_check(current) ? "yes" : "no");
+ }
+ }
+
+ ast_cli(fd, "\n");
+
}
- } else {
- obj = odbc_read(ODBC_REGISTRY, argv[2]);
- if (obj)
- odbc_dump_fd(fd, obj->parent);
- }
+ }
+ AST_LIST_UNLOCK(&odbc_list);
}
return 0;
}
static char show_usage[] =
-"Usage: odbc show {DSN}\n"
-" Show ODBC {DSN}\n"
-" Specifying DSN will show that DSN else, all DSNs are shown\n";
+"Usage: odbc show [<class>]\n"
+" List settings of a particular ODBC class.\n"
+" or, if not specified, all classes.\n";
static struct ast_cli_entry odbc_show_struct =
{ { "odbc", "show", NULL }, odbc_show_command, "Show ODBC DSN(s)", show_usage };
/* api calls */
-static int register_odbc_obj(char *name, odbc_obj *obj)
-{
- if (obj != NULL)
- return odbc_write(ODBC_REGISTRY, name, obj);
+static int odbc_register_class(char *name, struct odbc_class *class)
+{
+ if (class) {
+ AST_LIST_LOCK(&odbc_list);
+ AST_LIST_INSERT_HEAD(&odbc_list, class, list);
+ AST_LIST_UNLOCK(&odbc_list);
+ } else {
+ ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
+ }
return 0;
}
-odbc_obj *fetch_odbc_obj(const char *name, int check)
-{
- odbc_obj *obj = NULL;
- if ((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
+struct odbc_obj *odbc_request_obj(const char *name, int check)
+{
+ struct odbc_obj *obj = NULL;
+ if ((obj = (struct odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
if (check) {
odbc_sanity_check(obj);
}
@@ -380,68 +385,15 @@
return obj;
}
-static odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
-{
- static odbc_obj *new;
-
- if (!(new = calloc(1, sizeof(*new))) ||
- !(new->name = malloc(strlen(name) + 1)) ||
- !(new->dsn = malloc(strlen(dsn) + 1)))
- goto cleanup;
-
- if (username) {
- if (!(new->username = malloc(strlen(username) + 1)))
- goto cleanup;
- strcpy(new->username, username);
- }
-
- if (password) {
- if (!(new->password = malloc(strlen(password) + 1)))
- goto cleanup;
- strcpy(new->password, password);
- }
-
- strcpy(new->name, name);
- strcpy(new->dsn, dsn);
- new->env = SQL_NULL_HANDLE;
- new->up = 0;
- ast_mutex_init(&new->lock);
- return new;
-
-cleanup:
- if (new) {
- free(new->name);
- free(new->dsn);
- free(new->username);
- free(new->password);
-
- free(new);
- }
-
- return NULL;
-}
-
-static void destroy_odbc_obj(odbc_obj **obj)
+static void destroy_odbc_obj(struct odbc_obj **obj)
{
odbc_obj_disconnect(*obj);
- ast_mutex_lock(&(*obj)->lock);
- SQLFreeHandle(SQL_HANDLE_STMT, (*obj)->stmt);
- SQLFreeHandle(SQL_HANDLE_DBC, (*obj)->con);
- SQLFreeHandle(SQL_HANDLE_ENV, (*obj)->env);
-
- free((*obj)->name);
- free((*obj)->dsn);
- if ((*obj)->username)
- free((*obj)->username);
- if ((*obj)->password)
- free((*obj)->password);
- ast_mutex_unlock(&(*obj)->lock);
ast_mutex_destroy(&(*obj)->lock);
free(*obj);
}
-static odbc_status odbc_obj_disconnect(odbc_obj *obj)
+static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
{
int res;
ast_mutex_lock(&obj->lock);
@@ -459,7 +411,7 @@
return ODBC_SUCCESS;
}
-static odbc_status odbc_obj_connect(odbc_obj *obj)
+static odbc_status odbc_obj_connect(struct odbc_obj *obj)
{
int res;
SQLINTEGER err;
@@ -468,45 +420,25 @@
ast_mutex_lock(&obj->lock);
- if (obj->env == SQL_NULL_HANDLE) {
- res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &obj->env);
-
- if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
- if (option_verbose > 3)
- ast_log(LOG_WARNING, "res_odbc: Error AllocHandle\n");
- ast_mutex_unlock(&obj->lock);
- return ODBC_FAIL;
- }
-
- res = SQLSetEnvAttr(obj->env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
-
- if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
- if (option_verbose > 3)
- ast_log(LOG_WARNING, "res_odbc: Error SetEnv\n");
- SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
- ast_mutex_unlock(&obj->lock);
- return ODBC_FAIL;
- }
-
- res = SQLAllocHandle(SQL_HANDLE_DBC, obj->env, &obj->con);
-
- if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-
- if (option_verbose > 3)
- ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
- SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
-
- ast_mutex_unlock(&obj->lock);
- return ODBC_FAIL;
- }
- SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
- }
+ res = SQLAllocHandle(SQL_HANDLE_DBC, obj->parent->env, &obj->con);
+
+ if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+
+ if (option_verbose > 3)
+ ast_log(LOG_WARNING, "res_odbc: Error AllocHDB %d\n", res);
+ SQLFreeHandle(SQL_HANDLE_ENV, obj->parent->env);
+
+ ast_mutex_unlock(&obj->lock);
+ return ODBC_FAIL;
+ }
+ SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
+
if (obj->up) {
odbc_obj_disconnect(obj);
- ast_log(LOG_NOTICE,"Re-connecting %s\n", obj->name);
- }
-
- ast_log(LOG_NOTICE, "Connecting %s\n", obj->name);
+ ast_log(LOG_NOTICE, "Re-connecting %s\n", obj->parent->name);
+ }
+
+ ast_log(LOG_NOTICE, "Connecting %s\n", obj->parent->name);
res = SQLConnect(obj->con,
(SQLCHAR *) obj->parent->dsn, SQL_NTS,
@@ -519,7 +451,7 @@
ast_log(LOG_WARNING, "res_odbc: Error SQLConnect=%d errno=%d %s\n", res, (int)err, msg);
return ODBC_FAIL;
} else {
- ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->name, obj->dsn);
+ ast_log(LOG_NOTICE, "res_odbc: Connected to %s [%s]\n", obj->parent->name, obj->parent->dsn);
obj->up = 1;
}
@@ -544,7 +476,6 @@
int load_module(void)
{
- odbc_init();
load_odbc_config();
ast_cli_register(&odbc_disconnect_struct);
ast_cli_register(&odbc_connect_struct);
More information about the asterisk-commits
mailing list