From 3108dd1c76ab57f1e4462c691cd6d015eb65f4eb Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 7 Dec 2011 19:00:19 +0100 Subject: freeing PGresult just after executing a query do not save PGresult anymore, just calculate the maybe needed values (modified row count, last inserted id) and freeing PGresult afterwards --- ts3db.c | 58 ++++++++++++++++++++++------------------------------------ 1 file changed, 22 insertions(+), 36 deletions(-) (limited to 'ts3db.c') diff --git a/ts3db.c b/ts3db.c index 70eb152..b264b8a 100644 --- a/ts3db.c +++ b/ts3db.c @@ -8,7 +8,8 @@ typedef struct { PGconn *conn; - PGresult *result; + unsigned long long modified_rows; + unsigned long long last_inserted_id; } pg_connection; static log_callback log = 0; @@ -37,7 +38,6 @@ int ts3dbplugin_disconnect() { unsigned int i; for (i = 0; i < connection_count; i++) { - PQclear(connections[i].result); PQfinish(connections[i].conn); } @@ -98,7 +98,8 @@ int ts3dbplugin_connect(unsigned int *connection_nr) *connection_nr = connection_count; connections[connection_count].conn = conn; - connections[connection_count].result = NULL; + connections[connection_count].modified_rows = 0; + connections[connection_count].last_inserted_id = 0; connection_count++; return 0; @@ -114,28 +115,28 @@ PGconn *get_connection(unsigned int connection_nr) return connections[connection_nr].conn; } -PGresult *get_last_result(unsigned int connection_nr) -{ - if (connection_nr >= connection_count) { - log("Connection not found.", LOG_ERROR); - return NULL; - } - - return connections[connection_nr].result; -} - void save_last_result(unsigned int connection_nr, PGresult *result) { + const char *rows, *last_id; if (connection_nr >= connection_count) { log("Connection not found.", LOG_ERROR); return; } - if (connections[connection_nr].result != NULL) { - PQclear(connections[connection_nr].result); - } + connections[connection_nr].modified_rows = 0; + connections[connection_nr].last_inserted_id = 0; + + rows = PQcmdTuples(result); + sscanf(rows, "%llu", &connections[connection_nr].modified_rows); + + if (PQnfields(result) == 1) { + if (strcmp(PQfname(result, 0), "last_inserted_id") == 0) { + last_id = PQgetvalue(result, 0, 0); - connections[connection_nr].result = result; + log(last_id, LOG_ERROR); + sscanf(last_id, "%llu", &connections[connection_nr].last_inserted_id); + } + } } char *ts3dbplugin_getlasterror(unsigned int connection_nr) @@ -173,30 +174,13 @@ unsigned int ts3dbplugin_tableexists(unsigned int connection_nr, const char* tab unsigned long long ts3dbplugin_getmodifiedrowcount(unsigned int connection_nr) { log("getmodifiedrowcount", LOG_DEVELOP); - PGresult *result = get_last_result(connection_nr); - const char *count = PQcmdTuples(result); - unsigned long long number; - - sscanf(count, "%llu", &number); - return number; + return connections[connection_nr].modified_rows; } unsigned long long ts3dbplugin_getlastinsertid(unsigned int connection_nr) { log("getlastinsertid", LOG_DEVELOP); - const PGresult *result = get_last_result(connection_nr); - - if (PQnfields(result) == 1) { - if (strcmp(PQfname(result, 0), "last_inserted_id") == 0) { - const char *count = PQgetvalue(result, 0, 0); - unsigned long long number; - - sscanf(count, "%llu", &number); - return number; - } - } - - return 0; + return connections[connection_nr].last_inserted_id; } unsigned int ts3dbplugin_open(unsigned int connection_nr, const char* query, @@ -239,6 +223,7 @@ unsigned int ts3dbplugin_open(unsigned int connection_nr, const char* query, } } + PQclear(result); return 0; } @@ -261,5 +246,6 @@ unsigned int ts3dbplugin_exec(unsigned int connection_nr, const char* query) log(PQresultErrorMessage(result), LOG_WARNING); } + PQclear(result); return 0; } -- cgit v1.2.3