changeset 772:c6969f1af9f3

Fix app_acct and associated test
author Sebastien Decugis <sdecugis@nict.go.jp>
date Tue, 01 Nov 2011 10:12:18 +0100
parents eac79a449c06
children f4f20a182406
files extensions/app_acct/acct_db.c tests/testappacct.c
diffstat 2 files changed, 28 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/app_acct/acct_db.c	Sun Oct 30 20:57:59 2011 +0100
+++ b/extensions/app_acct/acct_db.c	Tue Nov 01 10:12:18 2011 +0100
@@ -58,6 +58,8 @@
 static 
 #endif /* TEST_DEBUG */
 pthread_key_t connk;
+static char * sql = NULL;   /* The buffer that will contain the SQL query */
+static int nbrecords = 0;
 
 
 /* Initialize the database context: connection to the DB, prepared statement to insert new records */
@@ -65,7 +67,6 @@
 {
 	struct acct_record_list emptyrecords;
 	struct fd_list * li;
-	char * sql=NULL;   /* The buffer that will contain the SQL query */
 	size_t sql_allocd = 0; /* The malloc'd size of the buffer */
 	size_t sql_offset = 0; /* The actual data already written in this buffer */
 	int idx = 0;
@@ -196,8 +197,8 @@
 		return EINVAL;
         }
 	PQclear(res);
+	nbrecords = emptyrecords.nball;
 	
-	free(sql);
 	acct_rec_empty(&emptyrecords);
 	
 	CHECK_POSIX( pthread_key_create(&connk, (void (*)(void*))PQfinish) );
@@ -211,6 +212,7 @@
 void acct_db_free(void)
 {	
 	CHECK_POSIX_DO(pthread_key_delete(connk) , );
+	free(sql);
 }
 
 /* When a new message has been received, insert the content of the parsed mapping into the DB (using prepared statement) */
@@ -224,6 +226,7 @@
 	PGresult *res;
 	struct fd_list *li;
 	PGconn *conn;
+	int new = 0;
 	
 	TRACE_ENTRY("%p", records);
 	CHECK_PARAMS( records );
@@ -232,6 +235,8 @@
 	if (!conn) {
 		conn = PQconnectdb(acct_config->conninfo);
 		CHECK_POSIX( pthread_setspecific(connk, conn) );
+		
+		new = 1;
 	}
 	
 	/* First, check if the connection with the DB has not staled, and eventually try to fix it */
@@ -245,6 +250,18 @@
 		}
 	}
 	
+	if (new) {
+		/* Create the prepared statement for this ocnnection, it is not shared */
+		res = PQprepare(conn, stmt, sql, nbrecords, NULL);
+		if (PQresultStatus(res) != PGRES_COMMAND_OK) {
+			TRACE_DEBUG(INFO, "Preparing statement '%s' failed: %s",
+				sql, PQerrorMessage(conn));
+			PQclear(res);
+			return EINVAL;
+        	}
+		PQclear(res);
+	}
+	
 	size = 	acct_config->tsfield ? records->nball + 1 : records->nball;
 	
 	/* Alloc the arrays of parameters */
--- a/tests/testappacct.c	Sun Oct 30 20:57:59 2011 +0100
+++ b/tests/testappacct.c	Tue Nov 01 10:12:18 2011 +0100
@@ -252,15 +252,21 @@
 	/* Now, check the record was actually registered properly */
 	{
 		PGresult * res;
-		char * s;
+		uint8_t * bs;
+		char * es;
+		size_t l;
+		int i;
 		res = PQexec(conn, "SELECT \"Session-Id\" from " TABLE ";");
 		CHECK( PGRES_TUPLES_OK, PQresultStatus(res) );
 		
 		/* We also check that the Session-Id we retrieve is the same as what we generated earlier (not trashed in the process) */
-		s = PQgetvalue(res, 0, 0);
-		CHECK( 0, strcmp(s, (char *)sess_bkp) );
+		es = PQgetvalue(res, 0, 0);
+		bs = PQunescapeBytea((uint8_t *)es, &l);
+		
+		CHECK( 0, fd_os_cmp(bs, l, sess_bkp, sess_bkp_len) );
 		
 		PQclear(res);
+		PQfreemem(bs);
 	}  
 
 	/* That's all for the tests yet */
"Welcome to our mercurial repository"