minor simplification of a conditional statement
[asterisk/asterisk.git] / cdr / cdr_pgsql.c
index 09df1bf..4ccd4aa 100644 (file)
@@ -71,15 +71,15 @@ static PGconn       *conn = NULL;
 
 static int pgsql_log(struct ast_cdr *cdr)
 {
-       struct tm tm;
+       struct ast_tm tm;
        char sqlcmd[2048] = "", timestr[128];
        char *pgerror;
        PGresult *result;
 
        ast_mutex_lock(&pgsql_lock);
 
-       ast_localtime(&cdr->start.tv_sec, &tm, NULL);
-       strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
+       ast_localtime(&cdr->start, &tm, NULL);
+       ast_strftime(timestr, sizeof(timestr), DATE_FORMAT, &tm);
 
        if ((!connected) && pghostname && pgdbuser && pgpassword && pgdbname) {
                conn = PQsetdbLogin(pghostname, pgdbport, NULL, NULL, pgdbname, pgdbuser, pgpassword);
@@ -88,7 +88,9 @@ static int pgsql_log(struct ast_cdr *cdr)
                } else {
                        pgerror = PQerrorMessage(conn);
                        ast_log(LOG_ERROR, "cdr_pgsql: Unable to connect to database server %s.  Calls will not be logged!\n", pghostname);
-                        ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                       ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                       PQfinish(conn);
+                       conn = NULL;
                }
        }
 
@@ -146,6 +148,8 @@ static int pgsql_log(struct ast_cdr *cdr)
                                pgerror = PQerrorMessage(conn);
                                ast_log(LOG_ERROR, "cdr_pgsql: Unable to reconnect to database server %s. Calls will not be logged!\n", pghostname);
                                ast_log(LOG_ERROR, "cdr_pgsql: Reason: %s\n", pgerror);
+                               PQfinish(conn);
+                               conn = NULL;
                                connected = 0;
                                ast_mutex_unlock(&pgsql_lock);
                                return -1;
@@ -161,6 +165,7 @@ static int pgsql_log(struct ast_cdr *cdr)
                        if (PQstatus(conn) == CONNECTION_OK) {
                                ast_log(LOG_ERROR, "cdr_pgsql: Connection reestablished.\n");
                                connected = 1;
+                               PQclear(result);
                                result = PQexec(conn, sqlcmd);
                                if (PQresultStatus(result) != PGRES_COMMAND_OK) {
                                        pgerror = PQresultErrorMessage(result);
@@ -180,8 +185,7 @@ static int pgsql_log(struct ast_cdr *cdr)
 
 static int my_unload_module(void)
 { 
-       if (conn)
-               PQfinish(conn);
+       PQfinish(conn);
        if (pghostname)
                ast_free(pghostname);
        if (pgdbname)
@@ -309,8 +313,12 @@ static int unload_module(void)
 
 static int reload(void)
 {
+       int res;
+       ast_mutex_lock(&pgsql_lock);
        my_unload_module();
-       return my_load_module();
+       res = my_load_module();
+       ast_mutex_unlock(&pgsql_lock);
+       return res;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "PostgreSQL CDR Backend",