[asterisk-commits] seanbright: branch seanbright/cdr-tds-conversion r126224 - /team/seanbright/c...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sat Jun 28 15:46:38 CDT 2008


Author: seanbright
Date: Sat Jun 28 15:46:38 2008
New Revision: 126224

URL: http://svn.digium.com/view/asterisk?view=rev&rev=126224
Log:
Last round of changes for this (finally).  Change the name of the
backend to more accurately reflect what it is.  Move the allocation
of the settings struct to the load function to avoid some spurious
checks for NULL, and make things a little more tidy.

Modified:
    team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c

Modified: team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c
URL: http://svn.digium.com/view/asterisk/team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c?view=diff&rev=126224&r1=126223&r2=126224
==============================================================================
--- team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c (original)
+++ team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c Sat Jun 28 15:46:38 2008
@@ -76,7 +76,7 @@
 
 #define DATE_FORMAT "%Y/%m/%d %T"
 
-static char *name = "mssql";
+static char *name = "FreeTDS (MSSQL)";
 static char *config = "cdr_tds.conf";
 
 struct cdr_tds_config {
@@ -102,8 +102,6 @@
 
 static int mssql_connect(void);
 static int mssql_disconnect(void);
-
-static void tds_config_free(void);
 
 static int tds_log(struct ast_cdr *cdr)
 {
@@ -283,10 +281,6 @@
 
 static int mssql_disconnect(void)
 {
-	if (!settings) {
-		return 0;
-	}
-
 	if (settings->dbproc) {
 		dbclose(settings->dbproc);
 		settings->dbproc = NULL;
@@ -314,11 +308,15 @@
 
 	if ((settings->dbproc = dbopen(login, (char *) settings->hostname)) == NULL) {
 		ast_log(LOG_ERROR, "Unable to connect to %s\n", settings->hostname);
+		dbloginfree(login);
 		return -1;
 	}
 
 	if (dbuse(settings->dbproc, (char *) settings->database) == FAIL) {
 		ast_log(LOG_ERROR, "Unable to select database %s\n", settings->database);
+		dbclose(settings->dbproc);
+		settings->dbproc = NULL;
+		dbloginfree(login);
 		return -1;
 	}
 
@@ -327,16 +325,6 @@
 	settings->connected = 1;
 
 	return 0;
-}
-
-static void tds_config_free(void)
-{
-	if (!settings) {
-		return;
-	}
-
-	ast_string_field_free_memory(settings);
-	ast_free(settings);
 }
 
 static int tds_unload_module(void)
@@ -345,9 +333,11 @@
 		ast_mutex_lock(&tds_lock);
 		mssql_disconnect();
 		ast_mutex_unlock(&tds_lock);
-	}
-
-	tds_config_free();
+
+		ast_string_field_free_memory(settings);
+		ast_free(settings);
+	}
+
 	ast_cdr_unregister(name);
 
 	dbexit();
@@ -368,7 +358,7 @@
 
 static int tds_message_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
 {
-	ast_log(LOG_NOTICE, "Msg %d, Level %d, State %d, Line %d\n", msgno, severity, msgstate, line);
+	ast_debug(1, "Msg %d, Level %d, State %d, Line %d\n", msgno, severity, msgstate, line);
 	ast_log(LOG_NOTICE, "%s\n", msgtext);
 
 	return 0;
@@ -395,33 +385,8 @@
 
 	ast_mutex_lock(&tds_lock);
 
-	if (reload) {
-		/* This will clear out all of our existing settings */
-		ast_string_field_init(settings, 0);
-	} else {
-		if (dbinit() == FAIL) {
-			ast_log(LOG_ERROR, "Failed to initialize db-lib\n");
-			ast_config_destroy(cfg);
-			ast_mutex_unlock(&tds_lock);
-			return 0;
-		}
-
-		dberrhandle(tds_error_handler);
-		dbmsghandle(tds_message_handler);
-
-		settings = ast_calloc(1, sizeof(*settings));
-
-		if (!settings || ast_string_field_init(settings, 256)) {
-			if (settings) {
-				ast_free(settings);
-				settings = NULL;
-			}
-			dbexit();
-			ast_config_destroy(cfg);
-			ast_mutex_unlock(&tds_lock);
-			return 0;
-		}
-	}
+	/* Clear out any existing settings */
+	ast_string_field_init(settings, 0);
 
 	ptr = ast_variable_retrieve(cfg, "global", "hostname");
 	if (ptr) {
@@ -476,8 +441,6 @@
 		ast_log(LOG_NOTICE, "Table name not specified, using 'cdr' by default.\n");
 		ast_string_field_set(settings, table, "cdr");
 	}
-
-	ast_config_destroy(cfg);
 
 	mssql_disconnect();
 
@@ -487,31 +450,53 @@
 	}
 
 	ast_mutex_unlock(&tds_lock);
+	ast_config_destroy(cfg);
 
 	return 1;
 
 failed:
 	ast_mutex_unlock(&tds_lock);
-	tds_config_free();
 	ast_config_destroy(cfg);
-	if (!reload) {
+
+	return 0;
+}
+
+static int reload(void)
+{
+	return tds_load_module(1);
+}
+
+static int load_module(void)
+{
+	if (dbinit() == FAIL) {
+		ast_log(LOG_ERROR, "Failed to initialize FreeTDS db-lib\n");
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
+	dberrhandle(tds_error_handler);
+	dbmsghandle(tds_message_handler);
+
+	settings = ast_calloc(1, sizeof(*settings));
+
+	if (!settings || ast_string_field_init(settings, 256)) {
+		if (settings) {
+			ast_free(settings);
+			settings = NULL;
+		}
 		dbexit();
-	}
-
-	return 0;
-}
-
-static int reload(void)
-{
-	return tds_load_module(1);
-}
-
-static int load_module(void)
-{
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	if (!tds_load_module(0)) {
+		ast_string_field_free_memory(settings);
+		ast_free(settings);
+		settings = NULL;
+		dbexit();
 		return AST_MODULE_LOAD_DECLINE;
 	}
+
 	ast_cdr_register(name, ast_module_info->description, tds_log);
+
 	return AST_MODULE_LOAD_SUCCESS;
 }
 




More information about the asterisk-commits mailing list