1
#include <grass/config.h>
2
#if defined(HAVE_LIBPQ_FE_H)
9
char *runPg(char *SQL_stmt)
12
char chunk[QRY_LENGTH];
13
static char long_str[2 * QRY_LENGTH];
15
char sqlcmd[QRY_LENGTH];
16
int i, j, nrows, nfields;
23
memset(long_str, '\0', sizeof(long_str));
24
memset(sqlcmd, '\0', sizeof(sqlcmd));
25
memset(buf, '\0', sizeof(buf));
26
memset(chunk, '\0', sizeof(chunk));
28
sprintf(sqlcmd, "%s", SQL_stmt);
31
fprintf(stderr, "\n\nExecuting\n%s\n---------------------\n", sqlcmd);
33
pghost = G__getenv("PG_HOST");
34
pg_conn = PQsetdb(pghost, NULL, NULL, NULL, G_getenv("PG_DBASE"));
36
if (PQstatus(pg_conn) == CONNECTION_BAD) {
37
fprintf(stderr, "Error: connect Postgres:%s\n",
38
PQerrorMessage(pg_conn));
43
res = PQexec(pg_conn, sqlcmd);
44
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
45
fprintf(stderr, "Error: select Postgres:%s\n",
46
PQerrorMessage(pg_conn));
52
nfields = PQnfields(res);
53
nrows = PQntuples(res);
55
if (nrows == 1 && vrbs) {
56
for (j = 0; j < nfields; j++) {
57
strncpy(buf, PQgetvalue(res, 0, j), QRY_LENGTH);
58
sprintf(chunk, "%10s I %s\n", PQfname(res, j), buf);
59
strncat(long_str, chunk, QRY_LENGTH);
64
fprintf(stderr, "%s", PQfname(res, 0));
65
for (j = 1; j < nfields; j++) {
66
fprintf(stderr, ",%s", PQfname(res, j));
68
fprintf(stderr, "\n");
70
for (i = 0; i < nrows; i++) {
71
for (j = 0; j < nfields; j++) {
72
strncpy(buf, PQgetvalue(res, i, j), QRY_LENGTH);
73
fprintf(stderr, "%s,", buf);
75
fprintf(stderr, "\n");
77
fprintf(stderr, "\n%d rows selected\n", nrows);
81
sprintf(chunk, "\n%d rows selected\n", nrows);
82
strncat(long_str, chunk, QRY_LENGTH);
87
/* explicitly close select result to avoid memory leaks */
90
/* close connection to database */
95
char *do_query(char *SQL_stmt, struct Sql *pts)
98
char chunk[QRY_LENGTH];
99
static char long_str[2 * QRY_LENGTH];
101
char sqlcmd[QRY_LENGTH];
102
int i, j, nrows, nfields;
108
memset(long_str, '\0', sizeof(long_str));
109
memset(sqlcmd, '\0', sizeof(sqlcmd));
110
memset(buf, '\0', sizeof(buf));
111
memset(chunk, '\0', sizeof(chunk));
115
"%s @ '(%f,%f,%f,%f)'::box", SQL_stmt,
116
pts->minX, pts->minY, pts->maxX, pts->maxY);
119
"\n\nExecuting\n%s;\n clause @ '( )'::box addded autonmatically.\n\n",
121
pghost = G__getenv("PG_HOST");
122
pg_conn = PQsetdb(pghost, NULL, NULL, NULL, G_getenv("PG_DBASE"));
124
if (PQstatus(pg_conn) == CONNECTION_BAD) {
125
fprintf(stderr, "Error: connect Postgres:%s\n",
126
PQerrorMessage(pg_conn));
131
res = PQexec(pg_conn, sqlcmd);
132
if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) {
133
fprintf(stderr, "Error: select Postgres:%s\n",
134
PQerrorMessage(pg_conn));
140
nfields = PQnfields(res);
141
nrows = PQntuples(res);
144
for (j = 0; j < nfields; j++) {
145
strncpy(buf, PQgetvalue(res, 0, j), QRY_LENGTH);
146
sprintf(chunk, "%10s I %s\n", PQfname(res, j), buf);
147
strncat(long_str, chunk, QRY_LENGTH);
152
fprintf(stderr, "%s", PQfname(res, 0));
153
for (j = 1; j < nfields; j++) {
154
fprintf(stderr, ",%s", PQfname(res, j));
156
fprintf(stderr, "\n");
158
for (i = 0; i < nrows; i++) {
159
for (j = 0; j < nfields; j++) {
160
strncpy(buf, PQgetvalue(res, i, j), QRY_LENGTH);
161
fprintf(stderr, "%s,", buf);
163
fprintf(stderr, "\n");
165
fprintf(stderr, "\n%d rows selected\n", nrows);
169
sprintf(chunk, "\n%d rows selected\n", nrows);
170
strncat(long_str, chunk, QRY_LENGTH);
174
/* explicitly close select result to avoid memory leaks */
177
/* close connection to database */