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

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Tue Mar 28 09:39:39 MST 2006


Author: tilghman
Date: Tue Mar 28 10:39:37 2006
New Revision: 15612

URL: http://svn.digium.com/view/asterisk?rev=15612&view=rev
Log:
Add documentation, modifications to the realtime engine to work with the new API; also, add back in a missing initialization

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=15612&r1=15611&r2=15612&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h (original)
+++ team/tilghman/res_odbc_rewrite/include/asterisk/res_odbc.h Tue Mar 28 10:39:37 2006
@@ -3,9 +3,11 @@
  *
  * Copyright (C) 1999 - 2005, Digium, Inc.
  * Copyright (C) 2004 - 2005, Anthony Minessale II
+ * Copyright (C) 2006, Tilghman Lesher
  *
  * Mark Spencer <markster at digium.com>
  * Anthony Minessale <anthmct at yahoo.com>
+ * Tilghman Lesher <res_odbc_200603 at the-tilghman.com>
  *
  * See http://www.asterisk.org for more information about
  * the Asterisk project. Please do not directly contact
@@ -42,19 +44,53 @@
 
 /* functions */
 
-#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
+/*! \brief Executes a prepared statement handle
+ * \param obj The non-NULL result of odbc_request_obj()
+ * \param stmt The prepared statement handle
+ * \return Returns 0 on success or -1 on failure
+ *
+ * This function was originally designed simply to execute a prepared
+ * statement handle and to retry if the initial execution failed.
+ * Unfortunately, it did this by disconnecting and reconnecting the database
+ * handle which on most databases causes the statement handle to become
+ * invalid.  Therefore, this method has been deprecated in favor of
+ * odbc_prepare_and_execute() which allows the statement to be prepared
+ * multiple times, if necessary, in case of a loss of connection.
+ *
+ * This function really only ever worked with MySQL, where the statement handle is
+ * not prepared on the server.  If you are not using MySQL, you should avoid it.
+ */
 int odbc_smart_execute(struct odbc_obj *obj, SQLHSTMT stmt); /* DEPRECATED */
 
-#define fetch_odbc_obj odbc_request_obj
+/*! \brief Retrieves a connected ODBC object
+ * \param name The name of the ODBC class for which a connection is needed.
+ * \param check Whether to ensure that a connection is valid before returning the handle.  Usually unnecessary.
+ * \return Returns an ODBC object or NULL if there is no connection available with the requested name.
+ *
+ * Connection classes may, in fact, contain multiple connection handles.  If
+ * the connection is pooled, then each connection will be dedicated to the
+ * thread which requests it.  Note that all connections should be released
+ * when the thread is done by calling odbc_release_obj(), below.
+ */
 struct odbc_obj *odbc_request_obj(const char *name, int check);
+
+/*! \brief Releases an ODBC object previously allocated by odbc_request_obj()
+ * \param obj The ODBC object
+ */
 void odbc_release_obj(struct odbc_obj *obj);
+
+/*! \brief Checks an ODBC object to ensure it is still connected
+ * \param obj The ODBC object
+ * \return Returns 0 if connected, -1 otherwise.
+ */
 int odbc_sanity_check(struct odbc_obj *obj);
+
+/*! \brief Prepares, executes, and returns the resulting statement handle.
+ * \param obj The ODBC object
+ * \param prepare_cb A function callback, which, when called, should return a statement handle prepared, with any necessary parameters or result columns bound.
+ * \param data A parameter to be passed to the prepare_cb parameter function, indicating which statement handle is to be prepared.
+ * \return Returns a statement handle or NULL on error.
+ */
 SQLHSTMT odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT (*prepare_cb)(struct odbc_obj *obj, void *data), void *data);
 
 #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=15612&r1=15611&r2=15612&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_config_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_config_odbc.c Tue Mar 28 10:39:37 2006
@@ -81,19 +81,21 @@
 	if (!table)
 		return NULL;
 
-	obj = fetch_odbc_obj(database, 0);
+	obj = odbc_request_obj(database, 0);
 	if (!obj)
 		return NULL;
 
 	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	newparam = va_arg(aq, const char *);
 	if (!newparam)  {
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 	newval = va_arg(aq, const char *);
@@ -109,6 +111,7 @@
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 	
@@ -125,6 +128,7 @@
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
@@ -132,20 +136,23 @@
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	res = SQLFetch(stmt);
 	if (res == SQL_NO_DATA) {
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
                 return NULL;
 	}
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Fetch error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-		return NULL;
-	}
-	for (x=0;x<colcount;x++) {
+		odbc_release_obj(obj);
+		return NULL;
+	}
+	for (x = 0; x < colcount; x++) {
 		rowdata[0] = '\0';
 		collen = sizeof(coltitle);
 		res = SQLDescribeCol(stmt, x + 1, (unsigned char *)coltitle, sizeof(coltitle), &collen, 
@@ -154,6 +161,7 @@
 			ast_log(LOG_WARNING, "SQL Describe Column error!\n[%s]\n\n", sql);
 			if (var)
 				ast_variables_destroy(var);
+			odbc_release_obj(obj);
 			return NULL;
 		}
 
@@ -166,6 +174,7 @@
 			ast_log(LOG_WARNING, "SQL Get Data error!\n[%s]\n\n", sql);
 			if (var)
 				ast_variables_destroy(var);
+			odbc_release_obj(obj);
 			return NULL;
 		}
 		stringp = rowdata;
@@ -185,6 +194,7 @@
 
 
 	SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	odbc_release_obj(obj);
 	return var;
 }
 
@@ -222,19 +232,21 @@
 		return NULL;
 	memset(&ra, 0, sizeof(ra));
 
-	obj = fetch_odbc_obj(database, 0);
+	obj = odbc_request_obj(database, 0);
 	if (!obj)
 		return NULL;
 
 	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	newparam = va_arg(aq, const char *);
 	if (!newparam)  {
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 	initfield = ast_strdupa(newparam);
@@ -254,7 +266,8 @@
 	res = SQLPrepare(stmt, (unsigned char *)sql, SQL_NTS);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 	
@@ -270,21 +283,24 @@
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	res = SQLNumResultCols(stmt, &colcount);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Column Count error!\n[%s]\n\n", sql);
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	cfg = ast_config_new();
 	if (!cfg) {
 		ast_log(LOG_WARNING, "Out of memory!\n");
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
@@ -334,7 +350,8 @@
 		ast_category_append(cfg, cat);
 	}
 
-	SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+	odbc_release_obj(obj);
 	return cfg;
 }
 
@@ -354,19 +371,21 @@
 	if (!table)
 		return -1;
 
-	obj = fetch_odbc_obj (database, 0);
+	obj = odbc_request_obj(database, 0);
 	if (!obj)
 		return -1;
 
 	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
+		odbc_release_obj(obj);
 		return -1;
 	}
 
 	newparam = va_arg(aq, const char *);
 	if (!newparam)  {
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return -1;
 	}
 	newval = va_arg(aq, const char *);
@@ -382,6 +401,7 @@
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return -1;
 	}
 	
@@ -400,21 +420,69 @@
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Execute error!\n[%s]\n\n", sql);
 		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return -1;
 	}
 
 	res = SQLRowCount(stmt, &rowcount);
 	SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	odbc_release_obj(obj);
 
 	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
 		ast_log(LOG_WARNING, "SQL Row Count error!\n[%s]\n\n", sql);
 		return -1;
 	}
 
-       if (rowcount >= 0)
-               return (int)rowcount;
+	if (rowcount >= 0)
+		return (int)rowcount;
 
 	return -1;
+}
+
+struct config_odbc_obj {
+	char *sql;
+	unsigned long id;
+	unsigned long cat_metric;
+	unsigned long var_metric;
+	unsigned long commented;
+	char filename[128];
+	char category[128];
+	char var_name[128];
+	char var_val[128];
+	SQLINTEGER err;
+};
+
+static SQLHSTMT config_odbc_prepare(struct odbc_obj *obj, void *data)
+{
+	struct config_odbc_obj *q = data;
+	SQLHSTMT sth;
+	int res;
+
+	res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &sth);
+	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+		if (option_verbose > 3)
+			ast_verbose( VERBOSE_PREFIX_4 "Failure in AllocStatement %d\n", res);
+		return NULL;
+	}
+
+	res = SQLPrepare(sth, (unsigned char *)q->sql, SQL_NTS);
+	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+		if (option_verbose > 3)
+			ast_verbose( VERBOSE_PREFIX_4 "Error in PREPARE %d\n", res);
+		SQLFreeHandle(SQL_HANDLE_STMT, sth);
+		return NULL;
+	}
+
+	SQLBindCol(sth, 1, SQL_C_ULONG, &q->id, sizeof(q->id), &q->err);
+	SQLBindCol(sth, 2, SQL_C_ULONG, &q->cat_metric, sizeof(q->cat_metric), &q->err);
+	SQLBindCol(sth, 3, SQL_C_ULONG, &q->var_metric, sizeof(q->var_metric), &q->err);
+	SQLBindCol(sth, 4, SQL_C_ULONG, &q->commented, sizeof(q->commented), &q->err);
+	SQLBindCol(sth, 5, SQL_C_CHAR, q->filename, sizeof(q->filename), &q->err);
+	SQLBindCol(sth, 6, SQL_C_CHAR, q->category, sizeof(q->category), &q->err);
+	SQLBindCol(sth, 7, SQL_C_CHAR, q->var_name, sizeof(q->var_name), &q->err);
+	SQLBindCol(sth, 8, SQL_C_CHAR, q->var_val, sizeof(q->var_val), &q->err);
+
+	return sth;
 }
 
 static struct ast_config *config_odbc(const char *database, const char *table, const char *file, struct ast_config *cfg)
@@ -423,80 +491,76 @@
 	struct ast_category *cur_cat;
 	int res = 0;
 	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];
+	char sql[255] = "";
+	unsigned int last_cat_metric = 0;
 	SQLSMALLINT rowcount=0;
 	SQLHSTMT stmt;
 	char last[128] = "";
+	struct config_odbc_obj q;
+
+	memset(&q, 0, sizeof(q));
 
 	if (!file || !strcmp (file, "res_config_odbc.conf"))
 		return NULL;		/* cant configure myself with myself ! */
 
-	obj = fetch_odbc_obj(database, 0);
+	obj = odbc_request_obj(database, 0);
 	if (!obj)
 		return NULL;
 
-	res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
-
-	SQLBindCol (stmt, 1, SQL_C_ULONG, &id, sizeof (id), &err);
-	SQLBindCol (stmt, 2, SQL_C_ULONG, &cat_metric, sizeof (cat_metric), &err);
-	SQLBindCol (stmt, 3, SQL_C_ULONG, &var_metric, sizeof (var_metric), &err);
-	SQLBindCol (stmt, 4, SQL_C_ULONG, &commented, sizeof (commented), &err);
-	SQLBindCol (stmt, 5, SQL_C_CHAR, &filename, sizeof (filename), &err);
-	SQLBindCol (stmt, 6, SQL_C_CHAR, &category, sizeof (category), &err);
-	SQLBindCol (stmt, 7, SQL_C_CHAR, &var_name, sizeof (var_name), &err);
-	SQLBindCol (stmt, 8, SQL_C_CHAR, &var_val, sizeof (var_val), &err);
-	
 	snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE filename='%s' and commented=0 ORDER BY filename,cat_metric desc,var_metric asc,category,var_name,var_val,id", table, file);
-
-	res = odbc_smart_direct_execute(obj, stmt, sql);
-	
-	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-		ast_log (LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
-		return NULL;
-	}
-
-	res = SQLNumResultCols (stmt, &rowcount);
-
-	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
-		ast_log (LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
-		SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	q.sql = sql;
+
+	stmt = odbc_prepare_and_execute(obj, config_odbc_prepare, &q);
+
+	if (!stmt) {
+		ast_log(LOG_WARNING, "SQL select error!\n[%s]\n\n", sql);
+		odbc_release_obj(obj);
+		return NULL;
+	}
+
+	res = SQLNumResultCols(stmt, &rowcount);
+
+	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
+		ast_log(LOG_WARNING, "SQL NumResultCols error!\n[%s]\n\n", sql);
+		SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+		odbc_release_obj(obj);
 		return NULL;
 	}
 
 	if (!rowcount) {
-		ast_log (LOG_NOTICE, "found nothing\n");
+		ast_log(LOG_NOTICE, "found nothing\n");
+		odbc_release_obj(obj);
 		return cfg;
 	}
 
 	cur_cat = ast_config_get_current_category(cfg);
 
 	while ((res = SQLFetch(stmt)) != SQL_NO_DATA) {
-		if (!strcmp (var_name, "#include")) {
-			if (!ast_config_internal_load(var_val, cfg)) {
-				SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+		if (!strcmp (q.var_name, "#include")) {
+			if (!ast_config_internal_load(q.var_val, cfg)) {
+				SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+				odbc_release_obj(obj);
 				return NULL;
 			}
 			continue;
 		} 
-		if (strcmp(last, category) || last_cat_metric != cat_metric) {
-			cur_cat = ast_category_new(category);
+		if (strcmp(last, q.category) || last_cat_metric != q.cat_metric) {
+			cur_cat = ast_category_new(q.category);
 			if (!cur_cat) {
 				ast_log(LOG_WARNING, "Out of memory!\n");
 				break;
 			}
-			strcpy(last, category);
-			last_cat_metric	= cat_metric;
+			strcpy(last, q.category);
+			last_cat_metric	= q.cat_metric;
 			ast_category_append(cfg, cur_cat);
 		}
 
-		new_v = ast_variable_new(var_name, var_val);
+		new_v = ast_variable_new(q.var_name, q.var_val);
 		ast_variable_append(cur_cat, new_v);
 	}
 
-	SQLFreeHandle (SQL_HANDLE_STMT, stmt);
+	SQLFreeHandle(SQL_HANDLE_STMT, stmt);
+	odbc_release_obj(obj);
 	return cfg;
 }
 

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=15612&r1=15611&r2=15612&view=diff
==============================================================================
--- team/tilghman/res_odbc_rewrite/res/res_odbc.c (original)
+++ team/tilghman/res_odbc_rewrite/res/res_odbc.c Tue Mar 28 10:39:37 2006
@@ -262,6 +262,7 @@
 					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));
+					SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &new->env);
 					if (pooling) {
 						new->haspool = pooling;
 						if (limit) {



More information about the asterisk-commits mailing list