From 5acf50fa9b04f66a3aed9290a64e9d366be13e90 Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Sat, 1 Feb 2003 21:26:33 +0000 Subject: [PATCH] * src/backend/postgres/PostgresBackend.c (pgendEnable): Added ENTER/LEAVE statements * src/backend/postgres/escape.c: Adde gnc-engine-util.h, and "module = MOD_BACKEND" - (sqlEscape_destroy): Added ENTER/LEAVE statemtents. * src/backend/postgres/gncquery.c (sql_Query_destroy): Added ENTER/LEAVE statements. * src/backend/postgres/putil.h: Cleaned up the formatting on the SEND/FINISH/EXEC/GET_RESULT macros. Changed them to use PQresultErrorMessage where possible. Made them all set the Backend error message string. Removed the PQfinish() calls, and setting be->connection to NULL. Reasoning: if a malformed query fails, there is no reason to close the connection. If an update/insert fails, again, there is no need to close the connection. The user should be presented an error message and respond appropriately. If a update/insert/delete fails, then generally the transaction is rolled back to the last BEGIN, thereby preventing data corruption. * src/engine/QueryNew.c (gncQueryPrint): Check that q is not NULL prior to processing it. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@7905 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 26 ++++++ src/backend/postgres/PostgresBackend.c | 3 + src/backend/postgres/escape.c | 7 +- src/backend/postgres/gncquery.c | 6 +- src/backend/postgres/putil.h | 108 +++++++++++++------------ src/engine/QueryNew.c | 8 +- 6 files changed, 103 insertions(+), 55 deletions(-) diff --git a/ChangeLog b/ChangeLog index 41b50d4a28..b03958498c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2003-02-01 Matthew Vanecek + + * src/backend/postgres/PostgresBackend.c (pgendEnable): + Added ENTER/LEAVE statements + + * src/backend/postgres/escape.c: Adde gnc-engine-util.h, + and "module = MOD_BACKEND" + - (sqlEscape_destroy): Added ENTER/LEAVE statemtents. + + * src/backend/postgres/gncquery.c (sql_Query_destroy): Added + ENTER/LEAVE statements. + + * src/backend/postgres/putil.h: Cleaned up the formatting on the + SEND/FINISH/EXEC/GET_RESULT macros. Changed them to use + PQresultErrorMessage where possible. Made them all set the Backend + error message string. Removed the PQfinish() calls, and setting + be->connection to NULL. Reasoning: if a malformed query fails, there is + no reason to close the connection. If an update/insert fails, + again, there is no need to close the connection. The user should + be presented an error message and respond appropriately. If + a update/insert/delete fails, then generally the transaction is + rolled back to the last BEGIN, thereby preventing data corruption. + + * src/engine/QueryNew.c (gncQueryPrint): Check that q is not NULL + prior to processing it. + 2003-02-01 Derek Atkins * src/gnome/window-register.c: fix the query code to use the correct diff --git a/src/backend/postgres/PostgresBackend.c b/src/backend/postgres/PostgresBackend.c index 4472d776dd..9d51ee1c37 100644 --- a/src/backend/postgres/PostgresBackend.c +++ b/src/backend/postgres/PostgresBackend.c @@ -2405,6 +2405,7 @@ pgendDisable (PGBackend *be) void pgendEnable (PGBackend *be) { + ENTER(" "); if (0 >= be->nest_count) { PERR ("too many nested disables"); @@ -2428,6 +2429,8 @@ pgendEnable (PGBackend *be) be->be.percentage = be->snr.percentage; be->be.events_pending = be->snr.events_pending; be->be.process_events = be->snr.process_events; + + LEAVE(" "); } /* ============================================================= */ diff --git a/src/backend/postgres/escape.c b/src/backend/postgres/escape.c index 8a4b7ed782..3b9d3c3619 100644 --- a/src/backend/postgres/escape.c +++ b/src/backend/postgres/escape.c @@ -32,8 +32,11 @@ #include #include +#include "gnc-engine-util.h" #include "escape.h" +static short module = MOD_BACKEND; + /* ================================================ */ struct _escape { @@ -129,9 +132,11 @@ sqlEscape_new (void) void sqlEscape_destroy (sqlEscape *b) { - if (!b) return; + ENTER(" "); + if (!b) { LEAVE("b is (null)"); return; } g_free (b->escape); b->escape = NULL; g_free (b); + LEAVE(" "); } /* ================ END OF FILE ==================== */ diff --git a/src/backend/postgres/gncquery.c b/src/backend/postgres/gncquery.c index 779995b688..500abc1ba8 100644 --- a/src/backend/postgres/gncquery.c +++ b/src/backend/postgres/gncquery.c @@ -83,11 +83,15 @@ sqlQuery_new(void) void sql_Query_destroy(sqlQuery * sq) { - if (!sq) + ENTER(" "); + if (!sq) { + LEAVE("sq = (null)"); return; + } g_free(sq->q_base); sqlEscape_destroy(sq->escape); g_free(sq); + LEAVE(" "); } /* diff --git a/src/backend/postgres/putil.h b/src/backend/postgres/putil.h index 444fe3d4eb..b7f57d8f66 100644 --- a/src/backend/postgres/putil.h +++ b/src/backend/postgres/putil.h @@ -84,22 +84,22 @@ int finishQuery(PGBackend *be); * It performs a minimal check to see that the send succeeded. */ -#define SEND_QUERY(be,buff,retval) \ -{ \ - int rc; \ - if (NULL == be->connection) return retval; \ - PINFO ("sending query %s", buff); \ - rc = PQsendQuery (be->connection, buff); \ - if (!rc) \ - { \ +#define SEND_QUERY(be,buff,retval) \ +{ \ + int rc; \ + if (NULL == be->connection) return retval; \ + PINFO ("sending query %s", buff); \ + rc = PQsendQuery (be->connection, buff); \ + if (!rc) \ + { \ gchar * msg = (gchar *)PQerrorMessage(be->connection); \ /* hack alert -- we need kinder, gentler error handling */\ - PERR("send query failed:\n\t%s", msg); \ - xaccBackendSetMessage (&be->be, msg); \ - xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR); \ - return retval; \ - } \ -} + PERR("send query failed:\n\t%s", msg); \ + xaccBackendSetMessage (&be->be, msg); \ + xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR); \ + return retval; \ + } \ +} \ /* --------------------------------------------------------------- */ /* The FINISH_QUERY macro makes sure that the previously sent @@ -138,50 +138,54 @@ int finishQuery(PGBackend *be); * wire, and makes sure that no errors occured. Results are left * in the result buffer. */ -#define GET_RESULTS(conn,result) \ -{ \ - ExecStatusType status; \ - result = PQgetResult (conn); \ - if (!result) break; \ - status = PQresultStatus(result); \ - if ((PGRES_COMMAND_OK != status) && \ - (PGRES_TUPLES_OK != status)) \ - { \ - PERR("failed to get result to query:\n" \ - "\t%s", PQerrorMessage((conn))); \ - PQclear (result); \ - PQfinish (conn); \ - be->connection = NULL; \ - xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR); \ - break; \ - } \ -} +#define GET_RESULTS(conn,result) \ +{ \ + gchar *msg = NULL; \ + ExecStatusType status; \ + result = PQgetResult (conn); \ + if (!result) break; \ + status = PQresultStatus(result); \ + msg = PQresultErrorMessage(result); \ + if ((PGRES_COMMAND_OK != status) && \ + (PGRES_TUPLES_OK != status)) \ + { \ + PERR("failed to get result to query:\n\t%s", msg); \ + PQclear (result); \ + xaccBackendSetMessage (&be->be, msg); \ + xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR);\ + break; \ + } \ +} \ /* --------------------------------------------------------------- */ /* The EXEC_QUERY macro executes a query and returns the results * and makes sure that no errors occured. Results are left * in the result buffer. */ -#define EXEC_QUERY(conn,buff,result) \ -{ \ - ExecStatusType status = 0; \ - result = PQexec (conn, buff); \ - if (result) \ - status = PQresultStatus(result); \ - if (!result || \ - ((PGRES_COMMAND_OK != status) && \ - (PGRES_TUPLES_OK != status))) \ - { \ - PERR("failed to get result to query:\n" \ - "\t%s", PQerrorMessage((conn))); \ - if (result) \ - PQclear (result); \ - result = NULL; \ - PQfinish (conn); \ - be->connection = NULL; \ - xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR); \ - } \ -} +#define EXEC_QUERY(conn,buff,result) \ +{ \ + gchar *msg = NULL; \ + ExecStatusType status = 0; \ + result = PQexec (conn, buff); \ + if (result) { \ + status = PQresultStatus(result); \ + msg = PQresultErrorMessage(result); \ + } else { \ + msg = PQerrorMessage(conn); \ + } \ + if (!result || \ + ((PGRES_COMMAND_OK != status) && \ + (PGRES_TUPLES_OK != status))) \ + { \ + PERR("failed to get result to query:\n" \ + "\t%s", msg); \ + if (result) \ + PQclear (result); \ + result = NULL; \ + xaccBackendSetMessage (&be->be, msg); \ + xaccBackendSetError (&be->be, ERR_BACKEND_SERVER_ERR);\ + } \ +} \ /* --------------------------------------------------------------- */ /* The IF_ONE_ROW macro counts the number of rows returned by diff --git a/src/engine/QueryNew.c b/src/engine/QueryNew.c index 7d57f713b8..37fb83c297 100644 --- a/src/engine/QueryNew.c +++ b/src/engine/QueryNew.c @@ -1322,11 +1322,17 @@ gncQueryPrint (QueryNew * query) QueryNewSort_t s[3]; gint maxResults = 0, numSorts = 3; + ENTER (" "); + + if (!query) { + LEAVE("query is (null)"); + return; + } + output = NULL; str = NULL; maxResults = gncQueryGetMaxResults (query); - ENTER (" "); output = gncQueryPrintSearchFor (query, output); output = gncQueryPrintTerms (query, output);