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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Sun Jun 22 09:43:56 CDT 2008


Author: seanbright
Date: Sun Jun 22 09:43:55 2008
New Revision: 124598

URL: http://svn.digium.com/view/asterisk?view=rev&rev=124598
Log:
Actually fail to load when any of our required configuration settings aren't present.

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=124598&r1=124597&r2=124598
==============================================================================
--- team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c (original)
+++ team/seanbright/cdr-tds-conversion/cdr/cdr_tds.c Sun Jun 22 09:43:55 2008
@@ -109,10 +109,6 @@
 {
 	char sqlcmd[2048], start[80], answer[80], end[80];
 	char *accountcode, *src, *dst, *dcontext, *clid, *channel, *dstchannel, *lastapp, *lastdata, *uniqueid;
-/*
-	int res = 0;
-	int retried = 0;
-*/
 	RETCODE erc;
 
 	ast_mutex_lock(&settings->lock);
@@ -133,6 +129,15 @@
 	get_date(start, cdr->start);
 	get_date(answer, cdr->answer);
 	get_date(end, cdr->end);
+
+	/* Ensure that we are connected */
+	if (!settings->connected) {
+		if (mssql_connect()) {
+			/* Connect failed */
+			ast_mutex_unlock(&settings->lock);
+			return -1;
+		}
+	}
 
 	erc = dbfcmd(settings->dbproc,
 		"INSERT INTO %s "
@@ -209,6 +214,9 @@
 		return -1;
 	}
 
+	/* Consume any results we might get back (this is more of a sanity check than
+	 * anything else, since an INSERT shouldn't return results).
+	 */
 	while ((erc = dbresults(settings->dbproc)) != NO_MORE_RESULTS) {
 		while (dbnextrow(settings->dbproc) != NO_MORE_ROWS);
 	}
@@ -320,7 +328,7 @@
 	if (settings) {
 		ast_mutex_destroy(&settings->lock);
 		ast_string_field_free_memory(settings);
-		ast_free(settings);	
+		ast_free(settings);
 	}
 }
 
@@ -392,32 +400,46 @@
 		ast_mutex_init(&settings->lock);
 	}
 
+	ast_mutex_lock(&settings->lock);
+
 	ptr = ast_variable_retrieve(cfg, "global", "hostname");
 	if (ptr) {
 		ast_string_field_set(settings, hostname, ptr);
 	} else {
-		ast_log(LOG_ERROR, "Database server hostname not specified.\n");
+		ast_log(LOG_ERROR, "Failed to connect to MSSQL: Database server hostname not specified.\n");
+		ast_mutex_unlock(&settings->lock);
+		cdr_tds_config_destroy();
+		return 0;
 	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "dbname");
 	if (ptr) {
 		ast_string_field_set(settings, database, ptr);
 	} else {
-		ast_log(LOG_ERROR, "Database dbname not specified.\n");
+		ast_log(LOG_ERROR, "Failed to connect to MSSQL: Database dbname not specified.\n");
+		ast_mutex_unlock(&settings->lock);
+		cdr_tds_config_destroy();
+		return 0;
 	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "user");
 	if (ptr) {
 		ast_string_field_set(settings, username, ptr);
 	} else {
-		ast_log(LOG_ERROR, "Database dbuser not specified.\n");
+		ast_log(LOG_ERROR, "Failed to connect to MSSQL: Database dbuser not specified.\n");
+		ast_mutex_unlock(&settings->lock);
+		cdr_tds_config_destroy();
+		return 0;
 	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "password");
 	if (ptr) {
 		ast_string_field_set(settings, password, ptr);
 	} else {
-		ast_log(LOG_ERROR, "Database password not specified.\n");
+		ast_log(LOG_ERROR, "Failed to connect to MSSQL: Database password not specified.\n");
+		ast_mutex_unlock(&settings->lock);
+		cdr_tds_config_destroy();
+		return 0;
 	}
 
 	ptr = ast_variable_retrieve(cfg, "global", "charset");
@@ -437,16 +459,16 @@
 	ptr = ast_variable_retrieve(cfg, "global", "table");
 	if (ptr) {
 		ast_string_field_set(settings, table, ptr);
-	} else {	
-		ast_debug(1, "Table not specified.  Assuming 'cdr'\n");
+	} else {
+		ast_log(LOG_NOTICE, "Table name not specified, using 'cdr' by default.\n");
 		ast_string_field_set(settings, table, "cdr");
 	}
 
 	ast_config_destroy(cfg);
 
-	ast_mutex_lock(&settings->lock);
 	mssql_disconnect();
 	mssql_connect();
+
 	ast_mutex_unlock(&settings->lock);
 
 	return 1;
@@ -459,8 +481,9 @@
 
 static int load_module(void)
 {
-	if (!tds_load_module(0))
-		return AST_MODULE_LOAD_DECLINE;
+	if (!tds_load_module(0)) {
+		return AST_MODULE_LOAD_FAILURE;
+	}
 	ast_cdr_register(name, ast_module_info->description, tds_log);
 	return AST_MODULE_LOAD_SUCCESS;
 }




More information about the asterisk-commits mailing list