Add count of found rows
[ftsbench.git] / pgdriver.c
index a8e922a..ed15a57 100644 (file)
@@ -1,3 +1,32 @@
+/*
+ * Copyright (c) 2006 Teodor Sigaev <teodor@sigaev.ru>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *        notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *        notice, this list of conditions and the following disclaimer in the
+ *        documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the names of any co-contributors
+ *        may be used to endorse or promote products derived from this software
+ *        without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -85,7 +114,7 @@ pgflush(ftsPG *db) {
                /* success write, waits for read */
                db->state = SS_READ;
        } else {
-               fprintf(stderr, "PQflush failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "PQflush failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
 }
@@ -132,7 +161,7 @@ waitResult(ftsPG *db) {
                if ( pfd.events ) {
                        int ret = poll( &pfd, 1, INFTIM);
                        if ( ret<0 ) {
-                               fprintf(stderr,"poll failed: %s", strerror(errno));
+                               fprintf(stderr,"poll failed: %s\n", strerror(errno));
                                exit(1);
                        }
 
@@ -149,7 +178,7 @@ waitResult(ftsPG *db) {
 
        while ( (res = PQgetResult(db->conn))!= NULL ) {
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "Execution of prepared statement failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
@@ -182,15 +211,13 @@ execQuery(ftsDB* adb, char ** words, int flags) {
 
                db->flags = flags;
 
-               if ( flags & FLG_FUNC )
-                       sprintf(buf, "SELECT count(*) FROM ftsbench WHERE to_tsvector(body) @@ to_tsquery( $1 ::text );");
-               else
-                       sprintf(buf, "SELECT count(*) FROM ftsbench WHERE fts @@ to_tsquery( $1 ::text );");
+               sprintf(buf, "SELECT count(*) FROM ftsbench WHERE %s @@ to_tsquery( $1 ::text );",
+                       ( flags & FLG_FUNC ) ? "to_tsvector(body)" : "fts" );
 
                res = PQprepare( db->conn, "search_ftsbench", buf, 1, NULL );
                
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE SELECT command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
@@ -233,7 +260,13 @@ execQuery(ftsDB* adb, char ** words, int flags) {
                        fprintf(stderr, "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
+       } else if ( PQntuples(res) == 1 ) {
+               db->db.nres += atoi( PQgetvalue(res,0,0) );
+       } else {
+               fprintf(stderr,"Bad PQntuples %d\n", PQntuples(res));
+               exit(1);
        }
+       
        PQclear(res);
 
        db->emptyquery = 0;
@@ -252,7 +285,7 @@ startCreateScheme(ftsDB* adb, int flags) {
 
        res = PQexec(db->conn, "DROP TABLE IF EXISTS ftsbench CASCADE;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "DROP TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "DROP TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -266,14 +299,14 @@ startCreateScheme(ftsDB* adb, int flags) {
 
        res = PQexec(db->conn, buf);
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
 
        res = PQexec(db->conn, "BEGIN;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -290,14 +323,14 @@ finishCreateScheme(ftsDB* adb) {
                waitResult(db);
                
                if ( PQsetnonblocking(db->conn, 0) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
        }
 
        res = PQexec(db->conn, "COMMIT;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "CREATE TABLE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
        }
        PQclear(res);
@@ -317,7 +350,7 @@ finishCreateScheme(ftsDB* adb) {
 
                res = PQexec(db->conn, buf);
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "CREATE INDEX command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
                                exit(1);
                }
                PQclear(res);
@@ -331,7 +364,7 @@ finishCreateScheme(ftsDB* adb) {
 
        res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-               fprintf(stderr, "VACUUM ANALYZE command failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
        PQclear(res);
@@ -359,13 +392,13 @@ InsertRow(ftsDB* adb, int id, char *txt) {
                                                 2, NULL );
                
                if (PQresultStatus(res) != PGRES_COMMAND_OK) {
-                       fprintf(stderr, "PREPARE INSERT command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PREPARE INSERT command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
                PQclear(res);
 
                if ( PQsetnonblocking(db->conn, 1) != 0 ) {
-                       fprintf(stderr, "PQsetnonblocking command failed: %s", PQerrorMessage(db->conn));
+                       fprintf(stderr, "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
                        exit(1);
                }
        } else {
@@ -379,7 +412,7 @@ InsertRow(ftsDB* adb, int id, char *txt) {
        if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
                                                 2, paramValues,
                                                 paramLengths, paramFormats, 0) == 0 ) {
-                fprintf(stderr, "PQsendQueryPrepared failed: %s", PQerrorMessage(db->conn));
+                fprintf(stderr, "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
                 exit(1);
        }
 
@@ -388,6 +421,13 @@ InsertRow(ftsDB* adb, int id, char *txt) {
        db->db.nquery++;
 }
 
+static void 
+Close(ftsDB* adb) {
+       ftsPG *db = (ftsPG*)adb;
+
+       PQfinish(db->conn);
+}
+
 ftsDB* 
 PGInit(char * connstr) {
        ftsPG   *db = (ftsPG*)malloc(sizeof(ftsPG));
@@ -399,7 +439,7 @@ PGInit(char * connstr) {
        db->conn = PQconnectdb(conninfo);
 
        if (PQstatus(db->conn) != CONNECTION_OK) {
-               fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(db->conn));
+               fprintf(stderr, "Connection to database failed: %s\n", PQerrorMessage(db->conn));
                exit(1);
        }
 
@@ -409,6 +449,7 @@ PGInit(char * connstr) {
        db->db.startCreateScheme = startCreateScheme;
        db->db.finishCreateScheme = finishCreateScheme;
        db->db.InsertRow = InsertRow;
+       db->db.Close = Close;
        db->socket = PQsocket(db->conn);
        if ( db->socket < 0 ) {
                fprintf(stderr,"Socket error\n");