1996-09-20 04:19:05 +08:00
|
|
|
/*
|
|
|
|
* pginterface.c
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
#include "halt.h"
|
|
|
|
#include "pginterface.h"
|
|
|
|
|
|
|
|
#define NUL '\0'
|
|
|
|
|
|
|
|
/* GLOBAL VARIABLES */
|
1997-09-08 10:41:22 +08:00
|
|
|
static PGconn *conn;
|
1997-09-07 13:04:48 +08:00
|
|
|
static PGresult *res = NULL;
|
1996-09-20 04:19:05 +08:00
|
|
|
|
|
|
|
#define ON_ERROR_STOP 0
|
1997-09-07 13:04:48 +08:00
|
|
|
#define ON_ERROR_CONTINUE 1
|
1996-09-20 04:19:05 +08:00
|
|
|
|
1997-09-08 10:41:22 +08:00
|
|
|
static int on_error_state = ON_ERROR_STOP;
|
1996-09-20 04:19:05 +08:00
|
|
|
|
|
|
|
/* LOCAL VARIABLES */
|
1997-09-08 10:41:22 +08:00
|
|
|
static int tuple;
|
1996-09-20 04:19:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** connectdb - returns PGconn structure
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-08 10:41:22 +08:00
|
|
|
PGconn *
|
1997-09-07 13:04:48 +08:00
|
|
|
connectdb(char *dbName,
|
|
|
|
char *pghost,
|
|
|
|
char *pgport,
|
|
|
|
char *pgoptions,
|
|
|
|
char *pgtty)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
/* make a connection to the database */
|
|
|
|
conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
|
|
|
halt("Connection to database '%s' failed.\n%s\n", dbName,
|
1997-09-07 13:04:48 +08:00
|
|
|
PQerrorMessage(conn));
|
1996-09-20 04:19:05 +08:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** disconnectdb
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-07 13:04:48 +08:00
|
|
|
void
|
|
|
|
disconnectdb()
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
PQfinish(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** doquery - returns PGresult structure
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-08 10:41:22 +08:00
|
|
|
PGresult *
|
1997-09-07 13:04:48 +08:00
|
|
|
doquery(char *query)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
if (res != NULL)
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
res = PQexec(conn, query);
|
|
|
|
|
|
|
|
if (on_error_state == ON_ERROR_STOP &&
|
|
|
|
(res == NULL ||
|
1997-09-07 13:04:48 +08:00
|
|
|
PQresultStatus(res) == PGRES_BAD_RESPONSE ||
|
|
|
|
PQresultStatus(res) == PGRES_NONFATAL_ERROR ||
|
|
|
|
PQresultStatus(res) == PGRES_FATAL_ERROR))
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
if (res != NULL)
|
1997-09-07 13:04:48 +08:00
|
|
|
fprintf(stderr, "query error: %s\n", PQcmdStatus(res));
|
|
|
|
else
|
|
|
|
fprintf(stderr, "connection error: %s\n", PQerrorMessage(conn));
|
1996-09-20 04:19:05 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
halt("failed request: %s\n", query);
|
|
|
|
}
|
|
|
|
tuple = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** fetch - returns tuple number (starts at 0), or the value END_OF_TUPLES
|
|
|
|
** NULL pointers are skipped
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-07 13:04:48 +08:00
|
|
|
int
|
|
|
|
fetch(void *param,...)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
1997-09-08 10:41:22 +08:00
|
|
|
va_list ap;
|
|
|
|
int arg,
|
|
|
|
num_fields;
|
1996-09-20 04:19:05 +08:00
|
|
|
|
1996-09-26 08:48:48 +08:00
|
|
|
num_fields = PQnfields(res);
|
1996-09-20 04:19:05 +08:00
|
|
|
|
|
|
|
if (tuple >= PQntuples(res))
|
|
|
|
return END_OF_TUPLES;
|
|
|
|
|
|
|
|
va_start(ap, param);
|
1996-09-26 08:48:48 +08:00
|
|
|
for (arg = 0; arg < num_fields; arg++)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
if (param != NULL)
|
|
|
|
{
|
|
|
|
if (PQfsize(res, arg) == -1)
|
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
|
|
|
|
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
|
1996-09-20 04:19:05 +08:00
|
|
|
}
|
|
|
|
else
|
1997-09-07 13:04:48 +08:00
|
|
|
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
|
1996-09-20 04:19:05 +08:00
|
|
|
}
|
|
|
|
param = va_arg(ap, char *);
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
return tuple++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** fetchwithnulls - returns tuple number (starts at 0),
|
|
|
|
** or the value END_OF_TUPLES
|
|
|
|
** Returns true or false into null indicator variables
|
|
|
|
** NULL pointers are skipped
|
1996-09-20 04:19:05 +08:00
|
|
|
*/
|
1997-09-07 13:04:48 +08:00
|
|
|
int
|
|
|
|
fetchwithnulls(void *param,...)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
1997-09-08 10:41:22 +08:00
|
|
|
va_list ap;
|
|
|
|
int arg,
|
|
|
|
num_fields;
|
1996-09-20 04:19:05 +08:00
|
|
|
|
1996-09-26 08:48:48 +08:00
|
|
|
num_fields = PQnfields(res);
|
1996-09-20 04:19:05 +08:00
|
|
|
|
1996-09-26 08:48:48 +08:00
|
|
|
if (tuple >= PQntuples(res))
|
1996-09-20 04:19:05 +08:00
|
|
|
return END_OF_TUPLES;
|
1996-09-26 08:48:48 +08:00
|
|
|
|
1996-09-20 04:19:05 +08:00
|
|
|
va_start(ap, param);
|
1996-09-26 08:48:48 +08:00
|
|
|
for (arg = 0; arg < num_fields; arg++)
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
if (param != NULL)
|
|
|
|
{
|
1996-09-26 08:48:48 +08:00
|
|
|
if (PQfsize(res, arg) == -1)
|
|
|
|
{
|
1997-09-07 13:04:48 +08:00
|
|
|
memcpy(param, PQgetvalue(res, tuple, arg), PQgetlength(res, tuple, arg));
|
|
|
|
((char *) param)[PQgetlength(res, tuple, arg)] = NUL;
|
1996-09-26 08:48:48 +08:00
|
|
|
}
|
1996-09-20 04:19:05 +08:00
|
|
|
else
|
1997-09-07 13:04:48 +08:00
|
|
|
memcpy(param, PQgetvalue(res, tuple, arg), PQfsize(res, arg));
|
1996-09-20 04:19:05 +08:00
|
|
|
}
|
|
|
|
param = va_arg(ap, char *);
|
1997-09-07 13:04:48 +08:00
|
|
|
if (PQgetisnull(res, tuple, arg) != 0)
|
|
|
|
*(int *) param = 1;
|
1996-09-26 08:48:48 +08:00
|
|
|
else
|
1997-09-07 13:04:48 +08:00
|
|
|
*(int *) param = 0;
|
1996-09-26 08:48:48 +08:00
|
|
|
param = va_arg(ap, char *);
|
1996-09-20 04:19:05 +08:00
|
|
|
}
|
|
|
|
va_end(ap);
|
1996-09-26 08:48:48 +08:00
|
|
|
return tuple++;
|
1996-09-20 04:19:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** on_error_stop
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-07 13:04:48 +08:00
|
|
|
void
|
|
|
|
on_error_stop()
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
on_error_state = ON_ERROR_STOP;
|
|
|
|
}
|
1997-09-07 13:04:48 +08:00
|
|
|
|
1996-09-20 04:19:05 +08:00
|
|
|
/*
|
|
|
|
**
|
1997-09-07 13:04:48 +08:00
|
|
|
** on_error_continue
|
1996-09-20 04:19:05 +08:00
|
|
|
**
|
|
|
|
*/
|
1997-09-07 13:04:48 +08:00
|
|
|
void
|
|
|
|
on_error_continue()
|
1996-09-20 04:19:05 +08:00
|
|
|
{
|
|
|
|
on_error_state = ON_ERROR_CONTINUE;
|
|
|
|
}
|