+/*
+ * 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>
/* 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);
}
}
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);
}
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);
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);
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;
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);
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);
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);
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);
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);
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 {
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);
}
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));
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);
}
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");