[asterisk-commits] branch tilghman/res_odbc_rewrite - r7403 in /team/tilghman/res_odbc_rewrite: ...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Dec 8 17:23:49 CST 2005


Author: tilghman
Date: Thu Dec  8 17:23:47 2005
New Revision: 7403

URL: http://svn.digium.com/view/asterisk?rev=7403&view=rev
Log:
Not ready, yet, just committing beginning of changes

Modified:
    team/tilghman/res_odbc_rewrite/configs/res_odbc.conf.sample
    team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h
    team/tilghman/res_odbc_rewrite/res/res_odbc.c

Modified: team/tilghman/res_odbc_rewrite/configs/res_odbc.conf.sample
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/configs/res_odbc.conf.sample?rev=7403&r1=7402&r2=7403&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/configs/res_odbc.conf.sample (original)
+++ team/tilghman/res_odbc_rewrite/configs/res_odbc.conf.sample Thu Dec  8 17:23:47 2005
@@ -1,17 +1,27 @@
 ;;; odbc setup file 
 
+; ENV is a global set of environmental variables that will get set.
+; Note that all environmental variables can be seen by all connections,
+; so you can't have different values for different connections.
+[ENV]
+FOO => BAR
+SERVERNAME => localhost
+INFORMIXDIR => /opt/informix
+
 [asterisk]
+enabled => no
 dsn => asterisk
 ;username => myuser
 ;password => mypass
 pre-connect => yes
 
 
-;[mysql2]
-;dsn => MySQL-asterisk
-;username => myuser
-;password => mypass
-;pre-connect => yes
+[mysql2]
+enabled => no             ; Simple definition to turn off a set of database connections
+dsn => MySQL-asterisk
+username => myuser
+password => mypass
+pre-connect => yes
 
 
 

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=7403&r1=7402&r2=7403&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h (original)
+++ team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h Thu Dec  8 17:23:47 2005
@@ -31,32 +31,32 @@
 
 typedef struct odbc_obj odbc_obj;
 
-typedef enum { ODBC_SUCCESS=0,ODBC_FAIL=-1} odbc_status;
+typedef enum { ODBC_SUCCESS=0, ODBC_FAIL=-1} odbc_status;
 
 struct odbc_obj {
-	char *name;
-	char *dsn;
-	char *username;
-	char *password;
-	SQLHENV  env;                   /* ODBC Environment */
 	SQLHDBC  con;                   /* ODBC Connection Handle */
-	SQLHSTMT stmt;                  /* ODBC Statement Handle */
-	ast_mutex_t lock;
-	int up;
-
+	struct odbc_list *parent;       /* Information about the connection is protected */
+	struct odbc_obj *next;
+	unsigned int used:1;
+	unsigned int up:1;
 };
 
 /* functions */
-odbc_obj *new_odbc_obj(char *name,char *dsn,char *username, char *password);
-odbc_status odbc_obj_connect(odbc_obj *obj);
-odbc_status odbc_obj_disconnect(odbc_obj *obj);
-void destroy_odbc_obj(odbc_obj **obj);
-int register_odbc_obj(char *name,odbc_obj *obj);
+
+#if 0
+odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password); /* STATIC */
+odbc_status odbc_obj_connect(odbc_obj *obj); /* STATIC */
+odbc_status odbc_obj_disconnect(odbc_obj *obj); /* STATIC */
+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 */
+
 odbc_obj *fetch_odbc_obj(const char *name, int check);
-int odbc_dump_fd(int fd,odbc_obj *obj);
+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_execute(odbc_obj *obj, SQLHSTMT stmt);
 int odbc_smart_direct_execute(odbc_obj *obj, SQLHSTMT stmt, char *sql);
 
 #endif /* _ASTERISK_RES_ODBC_H */

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=7403&r1=7402&r2=7403&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_odbc.c Thu Dec  8 17:23:47 2005
@@ -52,8 +52,16 @@
 struct odbc_list
 {
 	char name[80];
-	odbc_obj *obj;
-	int used;
+	char dsn[80];
+	char username[80];
+	char password[80];
+	SQLHENV env;
+	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;
 };
 
 static struct odbc_list ODBC_REGISTRY[MAX_ODBC_HANDLES];
@@ -141,9 +149,7 @@
 				ast_log(LOG_WARNING, "SQL Execute error %d! Attempting a reconnect...\n", res);
 				SQLFreeHandle(SQL_HANDLE_STMT, stmt);
 
-				ast_mutex_lock(&obj->lock);
 				obj->up = 0;
-				ast_mutex_unlock(&obj->lock);
 				odbc_obj_disconnect(obj);
 				odbc_obj_connect(obj);
 				continue;
@@ -197,9 +203,7 @@
 	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");
-		ast_mutex_lock(&obj->lock);
 		obj->up = 0;
-		ast_mutex_unlock(&obj->lock);
 		odbc_obj_disconnect(obj);
 		odbc_obj_connect(obj);
 		res = SQLExecDirect (stmt, (unsigned char *)sql, SQL_NTS);
@@ -214,27 +218,25 @@
 	SQLHSTMT stmt;
 	int res = 0;
 
-	ast_mutex_lock(&obj->lock);
-	if(obj->up) { /* so you say... let's make sure */
-		res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
+	if (obj->up) {
+		res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
 		if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-			obj->up = 0; /* Liar!*/
+			obj->up = 0;
 		} else {
 			res = SQLPrepare(stmt, (unsigned char *)test_sql, SQL_NTS);
 			if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-				obj->up = 0; /* Liar!*/
+				obj->up = 0;
 			} else {
 				res = SQLExecute(stmt);
 				if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-					obj->up = 0; /* Liar!*/
+					obj->up = 0;
 				}
 			}
 		}
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
 	}
-	ast_mutex_unlock(&obj->lock);
-
-	if(!obj->up) { /* Try to reconnect! */
+
+	if (!obj->up) { /* Try to reconnect! */
 		ast_log(LOG_WARNING, "Connection is down attempting to reconnect...\n");
 		odbc_obj_disconnect(obj);
 		odbc_obj_connect(obj);
@@ -248,7 +250,7 @@
 	struct ast_config *config;
 	struct ast_variable *v;
 	char *cat, *dsn, *username, *password;
-	int enabled;
+	int enabled, pooling = 0, limit = 1023;
 	int connect = 0;
 	char *env_var;
 
@@ -257,157 +259,108 @@
 	config = ast_config_load(cfg);
 	if (config) {
 		for (cat = ast_category_browse(config, NULL); cat; cat=ast_category_browse(config, cat)) {
-			if (!strcmp(cat, "ENV")) {
+			if (!strcasecmp(cat, "ENV")) {
 				for (v = ast_variable_browse(config, cat); v; v = v->next) {
-					env_var = malloc(strlen(v->name) + strlen(v->value) + 2);
-					if (env_var) {
-						sprintf(env_var, "%s=%s", v->name, v->value);
-						ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
-						putenv(env_var);
-						free(env_var);
+					setenv(v->name, v->value, 1);
+					ast_log(LOG_NOTICE, "Adding ENV var: %s=%s\n", v->name, v->value);
+				}
+			} else {
+				dsn = username = password = NULL;
+				enabled = 1;
+				connect = 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);
+							limit = 1023;
+						}
+					} else if (!strcasecmp(v->name, "enabled")) {
+						enabled = ast_true(v->value);
+					} else if (!strcasecmp(v->name, "pre-connect")) {
+						connect = ast_true(v->value);
+					} else if (!strcasecmp(v->name, "dsn")) {
+						dsn = v->value;
+					} else if (!strcasecmp(v->name, "username")) {
+						username = v->value;
+					} else if (!strcasecmp(v->name, "password")) {
+						password = v->value;
 					}
 				}
 
-			cat = ast_category_browse(config, cat);
+				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);
+						}
+					} else {
+						ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
+					}
+
+				}
 			}
-
-			dsn = username = password = NULL;
-			enabled = 1;
-			connect = 0;
-			for (v = ast_variable_browse(config, cat); v; v = v->next) {
-				if (!strcmp(v->name, "enabled"))
-					enabled = ast_true(v->value);
-				if (!strcmp(v->name, "pre-connect"))
-					connect = ast_true(v->value);
-				if (!strcmp(v->name, "dsn"))
-					dsn = v->value;
-				if (!strcmp(v->name, "username"))
-					username = v->value;
-				if (!strcmp(v->name, "password"))
-					password = v->value;
-			}
-
-			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);
-					}
-				} else {
-					ast_log(LOG_WARNING, "Addition of obj %s failed.\n", cat);
-				}
-
-			}
 		}
 		ast_config_destroy(config);
 	}
 	return 0;
 }
 
-int odbc_dump_fd(int fd, odbc_obj *obj)
-{
-	/* make sure the connection is up before we lie to our master.*/
-	odbc_sanity_check(obj);
-	ast_cli(fd, "Name: %s\nDSN: %s\nConnected: %s\n\n", obj->name, obj->dsn, obj->up ? "yes" : "no");
-	return 0;
-}
-
-static int odbc_connect_usage(int fd)
-{
-	ast_cli(fd, "usage odbc connect <DSN>\n");
-	return 0;
-}
-
-static int odbc_disconnect_usage(int fd)
-{
-	ast_cli(fd, "usage odbc disconnect <DSN>\n");
-	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);
 }
 
 static int odbc_show_command(int fd, int argc, char **argv)
 {
 	odbc_obj *obj;
-	int x = 0;
+	int i;
 	
 	if (!strcmp(argv[1], "show")) {
-		if (!argv[2] || (argv[2] && !strcmp(argv[2], "all"))) {
-			for (x = 0; x < MAX_ODBC_HANDLES; x++) {
-				if (!ODBC_REGISTRY[x].used)
-					break;
-				if (ODBC_REGISTRY[x].obj)
-					odbc_dump_fd(fd, ODBC_REGISTRY[x].obj);
+		if ((argc == 2) || (argc == 3 && !strcmp(argv[2], "all"))) {
+			for (i = 0; i < MAX_ODBC_HANDLES; i++) {
+				odbc_dump_fd(fd, &ODBC_REGISTRY[i]);
 			}
 		} else {
 			obj = odbc_read(ODBC_REGISTRY, argv[2]);
 			if (obj)
-				odbc_dump_fd(fd, obj);
+				odbc_dump_fd(fd, obj->parent);
 		}
 	}
 	return 0;
 }
-
-static int odbc_disconnect_command(int fd, int argc, char **argv)
-{
-	odbc_obj *obj;
-	if (!strcmp(argv[1], "disconnect")) {
-		if (!argv[2])
-			return odbc_disconnect_usage(fd);
-
-		obj = odbc_read(ODBC_REGISTRY, argv[2]);
-		if (obj) {
-			odbc_obj_disconnect(obj);
-		}
-	} 
-	return 0;
-}
-
-static int odbc_connect_command(int fd, int argc, char **argv)
-{
-	odbc_obj *obj;
-	if (!argv[1])
-		return odbc_connect_usage(fd);
-
-	if (!strcmp(argv[1], "connect") || !strcmp(argv[1], "disconnect")) {
-		if (!argv[2])
-			return odbc_connect_usage(fd);
-
-		obj = odbc_read(ODBC_REGISTRY, argv[2]);
-		if (obj) {
-			odbc_obj_connect(obj);
-		}
-	}
-	return 0;
-}
-
-
-static char connect_usage[] =
-"Usage: odbc connect <DSN>\n"
-"       Connect to ODBC DSN\n";
-
-static char disconnect_usage[] =
-"Usage: odbc connect <DSN>\n"
-"       Disconnect from ODBC DSN\n";
 
 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";
 
-static struct ast_cli_entry odbc_connect_struct =
-        { { "odbc", "connect", NULL }, odbc_connect_command, "Connect to ODBC DSN", connect_usage };
-
-
-static struct ast_cli_entry odbc_disconnect_struct =
-        { { "odbc", "disconnect", NULL }, odbc_disconnect_command, "Disconnect from ODBC DSN", disconnect_usage };
-
 static struct ast_cli_entry odbc_show_struct =
         { { "odbc", "show", NULL }, odbc_show_command, "Show ODBC DSN(s)", show_usage };
 
 /* api calls */
 
-int register_odbc_obj(char *name, odbc_obj *obj)
+static int register_odbc_obj(char *name, odbc_obj *obj)
 {
 	if (obj != NULL)
 		return odbc_write(ODBC_REGISTRY, name, obj);
@@ -417,14 +370,15 @@
 odbc_obj *fetch_odbc_obj(const char *name, int check)
 {
 	odbc_obj *obj = NULL;
-	if((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
-		if(check)
+	if ((obj = (odbc_obj *) odbc_read(ODBC_REGISTRY, name))) {
+		if (check) {
 			odbc_sanity_check(obj);
+		}
 	}
 	return obj;
 }
 
-odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
+static odbc_obj *new_odbc_obj(char *name, char *dsn, char *username, char *password)
 {
 	static odbc_obj *new;
 
@@ -463,7 +417,7 @@
 	return new;
 }
 
-void destroy_odbc_obj(odbc_obj **obj)
+static void destroy_odbc_obj(odbc_obj **obj)
 {
 	odbc_obj_disconnect(*obj);
 
@@ -483,26 +437,25 @@
 	free(*obj);
 }
 
-odbc_status odbc_obj_disconnect(odbc_obj *obj)
+static odbc_status odbc_obj_disconnect(odbc_obj *obj)
 {
 	int res;
 	ast_mutex_lock(&obj->lock);
 
 	res = SQLDisconnect(obj->con);
 
-
 	if (res == ODBC_SUCCESS) {
-		ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->name, obj->dsn);
+		ast_log(LOG_WARNING, "res_odbc: disconnected %d from %s [%s]\n", res, obj->parent->name, obj->parent->dsn);
 	} else {
 		ast_log(LOG_WARNING, "res_odbc: %s [%s] already disconnected\n",
-		obj->name, obj->dsn);
+		obj->parent->name, obj->parent->dsn);
 	}
 	obj->up = 0;
 	ast_mutex_unlock(&obj->lock);
 	return ODBC_SUCCESS;
 }
 
-odbc_status odbc_obj_connect(odbc_obj *obj)
+static odbc_status odbc_obj_connect(odbc_obj *obj)
 {
 	int res;
 	SQLINTEGER err;
@@ -544,7 +497,7 @@
 		}
 		SQLSetConnectAttr(obj->con, SQL_LOGIN_TIMEOUT, (SQLPOINTER *) 10, 0);
 	}
-	if(obj->up) {
+	if (obj->up) {
 		odbc_obj_disconnect(obj);
 		ast_log(LOG_NOTICE,"Re-connecting %s\n", obj->name);
 	}
@@ -552,18 +505,16 @@
 	ast_log(LOG_NOTICE, "Connecting %s\n", obj->name);
 
 	res = SQLConnect(obj->con,
-		   (SQLCHAR *) obj->dsn, SQL_NTS,
-		   (SQLCHAR *) obj->username, SQL_NTS,
-		   (SQLCHAR *) obj->password, SQL_NTS);
+		   (SQLCHAR *) obj->parent->dsn, SQL_NTS,
+		   (SQLCHAR *) obj->parent->username, SQL_NTS,
+		   (SQLCHAR *) obj->parent->password, SQL_NTS);
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		SQLGetDiagRec(SQL_HANDLE_DBC, obj->con, 1, stat, &err, msg, 100, &mlen);
-		SQLFreeHandle(SQL_HANDLE_ENV, obj->env);
 		ast_mutex_unlock(&obj->lock);
 		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);
 		obj->up = 1;
 	}



More information about the asterisk-commits mailing list