+/*
+ * 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>
#endif /* HAVE_SYS_POLL_H */
#endif /* HAVE_POLL */
+#ifndef INFTIM
+#define INFTIM (-1)
+#endif
+
typedef enum SocketState {
SS_NONE = 0,
SS_READ,
/* success write, waits for read */
db->state = SS_READ;
} else {
- fprintf(stderr, "PQflush failed: %s", PQerrorMessage(db->conn));
- exit(1);
+ fatal( "PQflush failed: %s\n", PQerrorMessage(db->conn));
}
}
break;
case SS_NONE:
default:
- fprintf(stderr,"Should not be here!\n");
- exit(1);
+ fatal( "Should not be here!\n");
break;
}
if ( pfd.events ) {
int ret = poll( &pfd, 1, INFTIM);
- if ( ret<0 ) {
- fprintf(stderr,"poll failed: %s", 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 ) {
} 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", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "Execution of prepared statement failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
}
}
static void
execQuery(ftsDB* adb, char ** words, int flags) {
ftsPG *db = (ftsPG*)adb;
- const char *paramValues[1];
int i = 0;
PGresult *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));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "PREPARE SELECT command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
db->prepared = 1;
if ( i!= 0 )
sb_add(&db->b, (db->flags & FLG_OR) ? " | '" : " & '", 4);
else
- sb_add(&db->b, "'", 1);
+ sb_addchar(&db->b, '\'');
while( *ptr ) {
if (*ptr == '\'')
- sb_add(&db->b, "'", 1);
- sb_add(&db->b, ptr, 1);
+ sb_addchar(&db->b, '\'');
+ sb_addchar(&db->b, *ptr);
ptr++;
}
- sb_add(&db->b, "'", 1);
+ sb_addchar(&db->b, '\'');
i++;
words++;
}
- paramValues[0] = db->b.str;
+ i = 1;
res = PQexecPrepared( db->conn, "search_ftsbench",
- 1, paramValues,
- NULL, NULL, 0);
+ 1, (const char**)&(db->b.str),
+ &(db->b.strlen), &i, 0);
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 {
+ fatal("Bad PQntuples %d\n", PQntuples(res));
}
+
PQclear(res);
db->emptyquery = 0;
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", 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 )
"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", 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", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE TABLE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
return;
if ( db->db.nquery > 0 ) {
waitResult(db);
- if ( PQsetnonblocking(db->conn, 0) != 0 ) {
- fprintf(stderr, "PQsetnonblocking command failed: %s", 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", 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 ) {
sprintf(buf,"CREATE INDEX ftsindex ON ftsbench USING %s ( fts );",
(db->flags & FLG_GIST) ? "GiST" : "GIN" );
- printf("(create index, ");
- fflush(stdout);
+ report("(create index, ");
res = PQexec(db->conn, buf);
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "CREATE INDEX command failed: %s", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "CREATE INDEX command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
- } else {
- printf("(");
- fflush(stdout);
- }
+ } else
+ report("(");
- printf("vacuum");
- fflush(stdout);
+ report("vacuum");
res = PQexec(db->conn, "VACUUM ANALYZE ftsbench;");
- if (PQresultStatus(res) != PGRES_COMMAND_OK) {
- fprintf(stderr, "VACUUM ANALYZE command failed: %s", PQerrorMessage(db->conn));
- exit(1);
- }
+ if (PQresultStatus(res) != PGRES_COMMAND_OK)
+ fatal( "VACUUM ANALYZE command failed: %s\n", PQerrorMessage(db->conn));
PQclear(res);
- printf(") ");
- fflush(stdout);
+
+ report(") ");
return;
}
"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", 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", PQerrorMessage(db->conn));
- exit(1);
- }
+ if ( PQsetnonblocking(db->conn, 1) != 0 )
+ fatal( "PQsetnonblocking command failed: %s\n", PQerrorMessage(db->conn));
} else {
waitResult(db);
}
if ( PQsendQueryPrepared( db->conn, "insert_ftsbench",
2, paramValues,
- paramLengths, paramFormats, 0) == 0 ) {
- fprintf(stderr, "PQsendQueryPrepared failed: %s", PQerrorMessage(db->conn));
- exit(1);
- }
+ paramLengths, paramFormats, 0) == 0 )
+ fatal( "PQsendQueryPrepared failed: %s\n", PQerrorMessage(db->conn));
pgflush(db);
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));
sprintf(conninfo, "dbname=%s", connstr);
db->conn = PQconnectdb(conninfo);
- if (PQstatus(db->conn) != CONNECTION_OK) {
- fprintf(stderr, "Connection to database failed: %s", 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);
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");
- exit(1);
- }
+ if ( db->socket < 0 )
+ fatal("Socket error\n");
return (ftsDB*)db;
}