* Now can connect at each iteration

* A few bugs fixed
Initial
Stephane Bortzmeyer 20 years ago
parent de253aab54
commit 20260fd88f

@ -23,7 +23,9 @@ echoping_postgresql \- echoping plugin which connects to a PostgreSQL RDBMS serv
.RI -m\ postgresql .RI -m\ postgresql
.B hostname .B hostname
.RI [-c conninfo] .RI [-c conninfo]
.RI [request] .RI [-e]
.RI [-r]
.RI [SQL-request]
.SH DESCRIPTION .SH DESCRIPTION
.PP .PP
.\" TeX users may be more comfortable with the \fB<whatever>\fP and .\" TeX users may be more comfortable with the \fB<whatever>\fP and
@ -42,6 +44,9 @@ Connection information for the Postgresql server. Something like 'host=foo dbnam
.TP .TP
.B \-r, \-\-readall .B \-r, \-\-readall
Read all the data sent by the Postgresql server Read all the data sent by the Postgresql server
.TP
.B \-e, \-\-connect-each-time
(Re)connect to the Postgresql server at each iteration
.SH SEE ALSO .SH SEE ALSO
.BR echoping (1), .BR echoping (1),
.SH AUTHOR .SH AUTHOR

@ -1,6 +1,5 @@
/* /*
* PostgreSQL plugin. * PostgreSQL plugin.
* TODO: loops with and without opening the connection each time?
* $Id$ * $Id$
*/ */
@ -19,6 +18,7 @@
const char *request = NULL; const char *request = NULL;
int readall = FALSE; int readall = FALSE;
int connect_each_time = FALSE;
poptContext postgresql_poptcon; poptContext postgresql_poptcon;
PGconn *conn; PGconn *conn;
PGresult *res; PGresult *res;
@ -44,7 +44,6 @@ init (const int argc, const char **argv,
int value; int value;
char *msg = malloc (256); char *msg = malloc (256);
char *rest; char *rest;
char *hostname;
/* popt variables */ /* popt variables */
struct poptOption options[] = { struct poptOption options[] = {
{"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0, {"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0,
@ -53,6 +52,9 @@ init (const int argc, const char **argv,
{"readall", 'a', POPT_ARG_NONE, &readall, 0, {"readall", 'a', POPT_ARG_NONE, &readall, 0,
"Read all the data sent by the Postgresql server", "Read all the data sent by the Postgresql server",
""}, ""},
{"connect-each-time", 'e', POPT_ARG_NONE, &connect_each_time, 0,
"(Re)Connect to the Postgresql server at each iteration",
""},
POPT_AUTOHELP POPT_TABLEEND POPT_AUTOHELP POPT_TABLEEND
}; };
global_options = global_external_options; global_options = global_external_options;
@ -71,7 +73,6 @@ init (const int argc, const char **argv,
poptStrerror (value)); poptStrerror (value));
postgresql_usage (msg); postgresql_usage (msg);
} }
/* hostname = poptGetArg (postgresql_poptcon); /* Not used */
request = poptGetArg (postgresql_poptcon); request = poptGetArg (postgresql_poptcon);
if (request == NULL) if (request == NULL)
request = "SELECT now()"; request = "SELECT now()";
@ -86,14 +87,17 @@ init (const int argc, const char **argv,
void void
start_raw () start_raw ()
{ {
conn = PQconnectdb (conninfo); if (!connect_each_time)
if (conn == NULL)
{
err_quit ("Cannot create connection\n");
}
if (PQstatus (conn) == CONNECTION_BAD)
{ {
err_quit ("Connection failed: %s\n", PQerrorMessage (conn)); conn = PQconnectdb (conninfo);
if (conn == NULL)
{
err_quit ("Cannot create connection\n");
}
if (PQstatus (conn) == CONNECTION_BAD)
{
err_quit ("Connection failed: %s\n", PQerrorMessage (conn));
}
} }
} }
@ -101,30 +105,56 @@ int
execute () execute ()
{ {
unsigned int row, column; unsigned int row, column;
char *result;
if (connect_each_time)
{
conn = PQconnectdb (conninfo);
if (conn == NULL)
{
err_ret ("Cannot create connection\n");
return -1;
}
if (PQstatus (conn) == CONNECTION_BAD)
{
err_ret ("Connection failed: %s\n", PQerrorMessage (conn));
return -1;
}
}
res = PQexec (conn, request); res = PQexec (conn, request);
if (PQresultStatus (res) != PGRES_TUPLES_OK) if (PQresultStatus (res) != PGRES_TUPLES_OK)
{ {
printf ("Cannot run \"%s\": %s\n", request, PQresultErrorMessage (res)); err_ret ("Cannot run \"%s\": %s\n", request,
PQresultErrorMessage (res));
return -1; return -1;
} }
if (global_options.verbose) if (global_options.verbose)
printf ("%d tuples returned\n", PQntuples (res)); printf ("%d tuples returned\n", PQntuples (res));
if (readall) if (readall)
{ {
for (row = 0; row++; row < PQntuples (res)) for (row = 0; row < PQntuples (res); row++)
{ {
for (column = 0; column++; column < PQnfields (res)) for (column = 0; column < PQnfields (res); column++)
{ {
PQgetvalue (res, row, column); result = PQgetvalue (res, row, column);
/* TODO: test the return code */ if (result == NULL)
{
err_ret ("Cannot retrieve value [%d,%d]\n", row, column);
return -1;
}
/* else {
printf ("DEBUG: [%d,%d] %s\n", row, column, result);
} */
} }
} }
} }
if (connect_each_time)
PQfinish (conn);
return 0; return 0;
} }
void void
terminate () terminate ()
{ {
PQfinish (conn); if (!connect_each_time)
PQfinish (conn);
} }

Loading…
Cancel
Save