File: postgresql.c

package info (click to toggle)
echoping 6.0.2-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,596 kB
  • ctags: 380
  • sloc: sh: 8,966; ansic: 3,350; makefile: 162
file content (147 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * PostgreSQL plugin. 
 * $Id: postgresql.c 377 2007-03-12 20:48:05Z bortz $
 */

#define IN_PLUGIN
#include "../../echoping.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef POSTGRESQL_PREFIX
#include <postgresql/libpq-fe.h>
#else
#include <libpq-fe.h>
#endif

const char     *request = NULL;
int             readall = FALSE;
int             connect_each_time = FALSE;
poptContext     postgresql_poptcon;
PGconn         *conn;
PGresult       *res;
char           *conninfo;
echoping_options global_options;

void
postgresql_usage(const char *msg)
{
	if (msg) {
		printf("PostgreSQL plugin error: %s\n", msg);
	}
	poptPrintUsage(postgresql_poptcon, stdout, 0);
	fprintf(stderr, "  [SQL-request]\n");
	exit(1);
}

char           *
init(const int argc, const char **argv,
     const echoping_options global_external_options)
{
	int             value;
	char           *msg = malloc(256);
	char           *rest;
	/* popt variables */
	struct poptOption options[] = {
		{"conninfo", 'c', POPT_ARG_STRING, &conninfo, 0,
		 "Connection information for the Postgresql server. Something like 'host=foo dbname=bar''",
		 ""},
		{"readall", 'a', POPT_ARG_NONE, &readall, 0,
		 "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
	};
	global_options = global_external_options;
	if (global_options.udp)
		err_quit("UDP makes no sense for a PostgreSQL connection");
	postgresql_poptcon = poptGetContext(NULL, argc,
					    argv, options,
					    POPT_CONTEXT_POSIXMEHARDER);
	while ((value = poptGetNextOpt(postgresql_poptcon)) > 0) {
	}
	if (value < -1) {
		sprintf(msg, "%s: %s",
			poptBadOption(postgresql_poptcon, POPT_BADOPTION_NOALIAS),
			poptStrerror(value));
		postgresql_usage(msg);
	}
	request = poptGetArg(postgresql_poptcon);
	if (request == NULL)
		request = "SELECT now()";
	rest = poptGetArg(postgresql_poptcon);
	if (rest != NULL)
		postgresql_usage("Erroneous additional arguments");
	if (conninfo == NULL)
		conninfo = "";
	return NULL;		/* We only use the conninfo, echoping does not see
				 * our hostname or port */
}

void
start_raw()
{
	if (!connect_each_time) {
		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));
		}
	}
}

int
execute()
{
	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);
	if (PQresultStatus(res) != PGRES_TUPLES_OK) {
		err_ret("Cannot run \"%s\": %s\n", request,
			PQresultErrorMessage(res));
		return -1;
	}
	if (global_options.verbose)
		printf("%d tuples returned\n", PQntuples(res));
	if (readall) {
		for (row = 0; row < PQntuples(res); row++) {
			for (column = 0; column < PQnfields(res); column++) {
				result = PQgetvalue(res, row, column);
				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;
}

void
terminate()
{
	if (!connect_each_time)
		PQfinish(conn);
}