Clean up: use separate function for fatal error
[ftsbench.git] / pgdriver.c
index adf329f..0c4c59d 100644 (file)
@@ -114,8 +114,7 @@ pgflush(ftsPG *db) {
                /* success write, waits for read */
                db->state = SS_READ;
        } else {
-               fprintf(stderr, "PQflush failed: %s\n", PQerrorMessage(db->conn));
-               exit(1);
+               fatal( "PQflush failed: %s\n", PQerrorMessage(db->conn));
        }
 }
 
@@ -136,8 +135,7 @@ checkStatus(ftsPG* db) {
                        break;
                case SS_NONE:
                default:
-                       fprintf(stderr,"Should not be here!\n");
-                       exit(1);
+                       fatal( "Should not be here!\n");
                        break;
        }
 
@@ -160,14 +158,11 @@ waitResult(ftsPG *db) {
 
                if ( pfd.events ) {
                        int ret = poll( &pfd, 1, INFTIM);
-                       if ( ret<0 ) {
-                               fprintf(stderr,"poll failed: %s\n", strerror(errno));
-                               exit(1);
-                       }
+                       if ( ret<0 ) 
+                               fatal("poll failed: %s\n", strerror(errno));
 
-                       if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) {
-                               fprintf(stderr,"Poll report about socket error\n");
-                               exit(1);
+                       if ( pfd.revents & (POLLHUP | POLLNVAL | POLLERR) ) { 
+                               fatal("Poll report about socket error\n");
                        } else if ( pfd.revents & POLLIN ) {
                                db->state = SS_READYREAD;
                        } else if ( pfd.revents & POLLOUT ) {
@@ -177,10 +172,8 @@ waitResult(ftsPG *db) {
        } while( checkStatus(db) != RS_OK );
 
        while ( (res = PQgetResult(db->conn))!= NULL ) {
-               if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+                       fatal( "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
                PQclear(res);
        }
 }
@@ -216,10 +209,8 @@ execQuery(ftsDB* adb, char ** words, int flags) {
 
                res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
                
-               if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+                       fatal( "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
                PQclear(res);
 
                db->prepared = 1;
@@ -256,15 +247,12 @@ execQuery(ftsDB* adb, char ** words, int flags) {
        if (PQresultStatus(res) != PGRES_TUPLES_OK) {
                /* skip error ' all words are a stop word' for GIN index -
                   result is empty, in any case */
-               if ( checkEmptyQuery(db, res) == 0 ) { 
-                       fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if ( checkEmptyQuery(db, res) == 0 ) 
+                       fatal( "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
        } else if ( PQntuples(res) == 1 ) {
                db->db.nres += atoi( PQgetvalue(res,0,0) );
        } else {
-               fprintf(stderr,"Bad PQntuples %d\n", PQntuples(res));
-               exit(1);
+               fatal("Bad PQntuples %d\n", PQntuples(res));
        }
        
        PQclear(res);
@@ -284,10 +272,8 @@ startCreateScheme(ftsDB* adb, int flags) {
        db->flags = flags;
 
        res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
-       if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-       }
+       if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+               fatal( "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
        PQclear(res);
 
        if ( flags & FLG_FUNC ) 
@@ -298,17 +284,13 @@ startCreateScheme(ftsDB* adb, int flags) {
                                        "FOR EACH ROW EXECUTE PROCEDURE tsearch2(fts, body);" );
 
        res = PQexec(db->conn, buf);
-       if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-       }
+       if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+               fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
        PQclear(res);
 
        res = PQexec(db->conn, "BEGIN;");
-       if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-       }
+       if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+               fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
        PQclear(res);
 
        return;
@@ -322,17 +304,13 @@ finishCreateScheme(ftsDB* adb) {
        if ( db->db.nquery > 0 ) {
                waitResult(db);
                
-               if ( PQsetnonblocking(db->conn, 0) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if ( PQsetnonblocking(db->conn, 0) != 0 ) 
+                       fatal( "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
        }
 
        res = PQexec(db->conn, "COMMIT;");
-       if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-       }
+       if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+               fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
        PQclear(res);
 
        if ( (db->flags & (FLG_GIST | FLG_GIN)) != 0 ) {
@@ -348,10 +326,8 @@ finishCreateScheme(ftsDB* adb) {
                report("(create index, ");
 
                res = PQexec(db->conn, buf);
-               if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
-                               exit(1);
-               }
+               if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+                       fatal( "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
                PQclear(res);
        } else 
                report("(");
@@ -359,10 +335,8 @@ finishCreateScheme(ftsDB* adb) {
        report("vacuum");
 
        res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
-       if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
-               exit(1);
-       }
+       if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+               fatal( "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
        PQclear(res);
 
        report(") ");
@@ -387,16 +361,12 @@ InsertRow(ftsDB* adb, int id, char *txt) {
                                                 "INSERT INTO ftsbench (id, body) VALUES ( $1 ::int4, $2 ::text);",
                                                 2, NULL );
                
-               if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if (PQresultStatus(res) != PGRES_COMMAND_OK) 
+                       fatal( "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
                PQclear(res);
 
-               if ( PQsetnonblocking(db->conn, 1) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
-                       exit(1);
-               }
+               if ( PQsetnonblocking(db->conn, 1) != 0 ) 
+                       fatal( "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
        } else {
                waitResult(db);
        }
@@ -407,10 +377,8 @@ InsertRow(ftsDB* adb, int id, char *txt) {
 
        if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
                                                 2, paramValues,
-                                                paramLengths, paramFormats, 0) == 0 ) {
-                fprintf(stderr, "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
-                exit(1);
-       }
+                                                paramLengths, paramFormats, 0) == 0 ) 
+                fatal( "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
 
        pgflush(db);
 
@@ -434,10 +402,8 @@ PGInit(char * connstr) {
        sprintf(conninfo, "dbname=%s", connstr);
        db->conn = PQconnectdb(conninfo);
 
-       if (PQstatus(db->conn) != CONNECTION_OK) {
-               fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(db->conn));
-               exit(1);
-       }
+       if (PQstatus(db->conn) != CONNECTION_OK) 
+               fatal( "Connection to database failed: %s\n", PQerrorMessage(db->conn));
 
        db->origreceiver = PQsetNoticeReceiver(db->conn, NoticeReceiver, (void *)db);
 
@@ -447,10 +413,8 @@ PGInit(char * connstr) {
        db->db.InsertRow = InsertRow;
        db->db.Close = Close;
        db->socket = PQsocket(db->conn);
-       if ( db->socket < 0 ) {
-               fprintf(stderr,"Socket error\n");
-               exit(1);
-       }
+       if ( db->socket < 0 ) 
+               fatal("Socket error\n");
        
        return (ftsDB*)db;
 }