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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Feb 7 12:08:58 MST 2006


Author: tilghman
Date: Tue Feb  7 13:08:56 2006
New Revision: 9201

URL: http://svn.digium.com/view/asterisk?rev=9201&view=rev
Log:
Woohoo, it compiles.  Ship it!

Modified:
    team/tilghman/res_odbc_rewrite/funcs/func_odbc.c
    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/funcs/func_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/funcs/func_odbc.c?rev=9201&r1=9200&r2=9201&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/funcs/func_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/funcs/func_odbc.c Tue Feb  7 13:08:56 2006
@@ -65,7 +65,7 @@
  */
 static void acf_odbc_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
 {
-	odbc_obj *obj;
+	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
 	char *s, *t, *arg, buf[512]="", varname[15];
 	int res, argcount=0, valcount=0, i, retry=0;
@@ -92,10 +92,10 @@
 		return;
 	}
 
-	obj = fetch_odbc_obj(query->dsn, 0);
+	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
-		ast_log(LOG_ERROR, "No such DSN registered: %s (check res_odbc.conf)\n", query->dsn);
+		ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
 		ast_mutex_unlock(&query_lock);
 		return;
 	}
@@ -226,7 +226,7 @@
 
 static char *acf_odbc_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
 {
-	odbc_obj *obj;
+	struct odbc_obj *obj;
 	struct acf_odbc_query *query;
 	char *s, *arg, sql[512] = "", varname[15];
 	int count=0, res, x;
@@ -251,10 +251,10 @@
 		return "";
 	}
 
-	obj = fetch_odbc_obj(query->dsn, 0);
+	obj = odbc_request_obj(query->dsn, 0);
 
 	if (!obj) {
-		ast_log(LOG_ERROR, "No such DSN registered: %s (check res_odbc.conf)\n", query->dsn);
+		ast_log(LOG_ERROR, "No such DSN registered (or out of connections): %s (check res_odbc.conf)\n", query->dsn);
 		ast_mutex_unlock(&query_lock);
 		return "";
 	}
@@ -328,7 +328,7 @@
 		goto acf_out;
 	}
 
-	for (x=0; x<colcount; x++) {
+	for (x = 0; x < colcount; x++) {
 		int buflen, coldatalen;
 		char coldata[256];
 

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=9201&r1=9200&r2=9201&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h (original)
+++ team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h Tue Feb  7 13:08:56 2006
@@ -49,13 +49,12 @@
 void destroy_odbc_obj(odbc_obj **obj); /* STATIC */
 int register_odbc_obj(char *name,odbc_obj *obj); /* STATIC */
 #endif
-int odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt); /* DEPRECATE */
+int odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt); /* DEPRECATED */
 
 #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);
+void odbc_release_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_odbc.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/res_odbc_rewrite/res/res_odbc.c?rev=9201&r1=9200&r2=9201&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_odbc.c Tue Feb  7 13:08:56 2006
@@ -68,46 +68,9 @@
 
 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;
-
-	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 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_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));
-			registry[x].obj = obj;
-			registry[x].used = 1;
-			return 1;
-		}
-	} */
-	return 0;
-}
+static int odbc_register_class(struct odbc_class *class, int connect);
 
 static char *tdesc = "ODBC Resource";
-/* internal stuff */
 
 SQLHSTMT odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
 {
@@ -148,6 +111,7 @@
 				 * While this isn't the best way to try to correct an error, this won't automatically
 				 * fail when the statement handle invalidates.
 				 */
+				/* XXX Actually, it might, if we're using a non-pooled connection. Possible race here. XXX */
 				odbc_obj_disconnect(obj);
 				odbc_obj_connect(obj);
 				continue;
@@ -308,7 +272,7 @@
 						}
 					}
 
-					register_odbc_class(new, connect);
+					odbc_register_class(new, connect);
 					ast_log(LOG_NOTICE, "Registered ODBC class '%s' dsn->[%s]\n", cat, dsn);
 				}
 			}
@@ -322,12 +286,12 @@
 {
 	struct odbc_class *class;
 	struct odbc_obj *current;
-	int count=0;
-	
+
 	if (!strcmp(argv[1], "show")) {
 		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))) {
+				int count = 0;
 				ast_cli(fd, "Name: %s\nDSN: %s\n", class->name, class->dsn);
 
 				if (class->haspool) {
@@ -344,7 +308,6 @@
 				}
 
 				ast_cli(fd, "\n");
-
 			}
 		}
 		AST_LIST_UNLOCK(&odbc_list);
@@ -360,37 +323,99 @@
 static struct ast_cli_entry odbc_show_struct =
         { { "odbc", "show", NULL }, odbc_show_command, "Show ODBC DSN(s)", show_usage };
 
-/* api calls */
-
-static int odbc_register_class(char *name, struct odbc_class *class)
-{
+static int odbc_register_class(struct odbc_class *class, int connect)
+{
+	struct odbc_obj *obj;
 	if (class) {
 		AST_LIST_LOCK(&odbc_list);
 		AST_LIST_INSERT_HEAD(&odbc_list, class, list);
 		AST_LIST_UNLOCK(&odbc_list);
+
+		if (connect) {
+			/* Request and release builds a connection */
+			obj = odbc_request_obj(class->name, 0);
+			odbc_release_obj(obj);
+		}
+
+		return 0;
 	} else {
 		ast_log(LOG_WARNING, "Attempted to register a NULL class?\n");
-	}
-	return 0;
+		return -1;
+	}
+}
+
+void odbc_release_obj(struct odbc_obj *obj)
+{
+	/* For pooled connections, this frees the connection to be
+	 * reused.  For non-pooled connections, it does nothing. */
+	obj->used = 0;
 }
 
 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);
-		}
+	struct odbc_class *class;
+
+	AST_LIST_LOCK(&odbc_list);
+	AST_LIST_TRAVERSE(&odbc_list, class, list) {
+		if (!strcmp(class->name, name))
+			break;
+	}
+	AST_LIST_UNLOCK(&odbc_list);
+
+	if (!class)
+		return NULL;
+
+	AST_LIST_LOCK(&class->odbc_obj);
+	if (class->haspool) {
+		/* Recycle connections before building another */
+		AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
+			if (! obj->used) {
+				obj->used = 1;
+				break;
+			}
+		}
+
+		if (!obj && (class->count < class->limit)) {
+			class->count++;
+			obj = ast_calloc(1, sizeof(*obj));
+			if (!obj) {
+				ast_log(LOG_ERROR, "Out of memory\n");
+				AST_LIST_UNLOCK(&class->odbc_obj);
+				return NULL;
+			}
+			ast_mutex_init(&obj->lock);
+			obj->parent = class;
+			odbc_obj_connect(obj);
+			AST_LIST_INSERT_TAIL(&class->odbc_obj, obj, list);
+		}
+	} else {
+		/* Non-pooled connection: multiple modules can use the same connection. */
+		AST_LIST_TRAVERSE(&class->odbc_obj, obj, list) {
+			/* Non-pooled connection: if there is an entry, return it */
+			break;
+		}
+
+		if (!obj) {
+			/* No entry: build one */
+			obj = ast_calloc(1, sizeof(*obj));
+			if (!obj) {
+				ast_log(LOG_ERROR, "Out of memory\n");
+				AST_LIST_UNLOCK(&class->odbc_obj);
+				return NULL;
+			}
+			ast_mutex_init(&obj->lock);
+			obj->parent = class;
+			odbc_obj_connect(obj);
+			AST_LIST_INSERT_HEAD(&class->odbc_obj, obj, list);
+		}
+	}
+	AST_LIST_UNLOCK(&class->odbc_obj);
+
+	if (obj && check) {
+		odbc_sanity_check(obj);
 	}
 	return obj;
-}
-
-static void destroy_odbc_obj(struct odbc_obj **obj)
-{
-	odbc_obj_disconnect(*obj);
-
-	ast_mutex_destroy(&(*obj)->lock);
-	free(*obj);
 }
 
 static odbc_status odbc_obj_disconnect(struct odbc_obj *obj)
@@ -465,20 +490,13 @@
 
 int unload_module(void)
 {
-	STANDARD_HANGUP_LOCALUSERS;
-	odbc_destroy();
-	ast_cli_unregister(&odbc_disconnect_struct);
-	ast_cli_unregister(&odbc_connect_struct);
-	ast_cli_unregister(&odbc_show_struct);
-	ast_log(LOG_NOTICE, "res_odbc unloaded.\n");
-	return 0;
+	/* Prohibit unloading */
+	return -1;
 }
 
 int load_module(void)
 {
 	load_odbc_config();
-	ast_cli_register(&odbc_disconnect_struct);
-	ast_cli_register(&odbc_connect_struct);
 	ast_cli_register(&odbc_show_struct);
 	ast_log(LOG_NOTICE, "res_odbc loaded.\n");
 	return 0;



More information about the asterisk-commits mailing list