Jan-Henrik Haukeland wrote:
- I can't use Connection_PreparedStatement on a postgresql
connection until I've issued a Connection_executeQuery. Doing so segfaults in libzdb. I've reported this one already, and I can easily work around this.
Its on my TODO list and I hope to have it moved over to Martin's TODO list :)
Hi,
the problem should be fixed ... postgresql returned PGRES_EMPTY_QUERY for PQprepare() whereas libzdb expected PGRES_COMMAND_OK or PGRES_TUPLES_OK. I have checked the postgresql code and it seems that PGRES_EMPTY_QUERY is not error in this context - the prepared statement is processed fine. I have modified the code to check for result (C->res) of PQprepare() and accept the PGRES_EMPTY_QUERY.
Note that to prevent the uncaught exception like this:
--8<-- Uncaught exception SQLException raised in Connection_prepareStatement at src/db/Connection.c:294 --8<--
you should modify your code using TRY-CATCH-END_TRY this way:
--8<-- TRY { PreparedStatement_T p = Connection_prepareStatement(con, "SELECT * FROM dbmail_users WHERE userid=?;"); if (! p) printf("[%s]", Connection_getLastError(con)); assert(p); } CATCH(SQLException) { printf("\tResult: prepare statement failed -- %s\n", Connection_getLastError(con)); } END_TRY; --8<--
This way you will get the the error message printed out and the program won't crash.
The patch is in the cvs and attachment as well.
Cheers, Martin
Index: src/db/postgresql/PostgresqlConnection.c =================================================================== RCS file: /cvsroot/libzdb/src/db/postgresql/PostgresqlConnection.c,v retrieving revision 1.15 retrieving revision 1.17 diff -u -r1.15 -r1.17 --- src/db/postgresql/PostgresqlConnection.c 23 Feb 2008 19:57:28 -0000 1.15 +++ src/db/postgresql/PostgresqlConnection.c 3 Mar 2008 22:34:30 -0000 1.17 @@ -40,7 +40,7 @@ /** * Implementation of the Connection/Strategy interface for postgresql. * - * @version $Id: PostgresqlConnection.c,v 1.15 2008/02/23 19:57:28 hauk Exp $ + * @version $Id: PostgresqlConnection.c,v 1.17 2008/03/03 22:34:30 martinp Exp $ * @file */
@@ -249,9 +249,12 @@ /* Get the unique prepared statement ID */ name = Util_getString("%d", rand()); PQclear(C->res); - C->res = PQprepare(C->db, name, stmt, prm1, NULL); + C->res = PQprepare(C->db, name, stmt, 0, NULL); FREE(stmt); - if (C->lastError == PGRES_COMMAND_OK || C->lastError == PGRES_TUPLES_OK) { + if (C->res && + (C->lastError == PGRES_EMPTY_QUERY || + C->lastError == PGRES_COMMAND_OK || + C->lastError == PGRES_TUPLES_OK)) { return PreparedStatement_new(PostgresqlPreparedStatement_new(C->db, C->maxRows, name,