~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to visualization/nviz/src/runPg.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <grass/config.h>
2
 
#if defined(HAVE_LIBPQ_FE_H)
3
 
#include <grass/gis.h>
4
 
#include "pg.h"
5
 
#include <libpq-fe.h>
6
 
#include <stdio.h>
7
 
#include <string.h>
8
 
 
9
 
char *runPg(char *SQL_stmt)
10
 
{
11
 
    char buf[QRY_LENGTH];
12
 
    char chunk[QRY_LENGTH];
13
 
    static char long_str[2 * QRY_LENGTH];
14
 
 
15
 
    char sqlcmd[QRY_LENGTH];
16
 
    int i, j, nrows, nfields;
17
 
 
18
 
    PGconn *pg_conn;
19
 
    PGresult *res;
20
 
    char *pghost;
21
 
    int vrbs = 1;
22
 
 
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));
27
 
 
28
 
    sprintf(sqlcmd, "%s", SQL_stmt);
29
 
 
30
 
    if (vrbs)
31
 
        fprintf(stderr, "\n\nExecuting\n%s\n---------------------\n", sqlcmd);
32
 
 
33
 
    pghost = G__getenv("PG_HOST");
34
 
    pg_conn = PQsetdb(pghost, NULL, NULL, NULL, G_getenv("PG_DBASE"));
35
 
 
36
 
    if (PQstatus(pg_conn) == CONNECTION_BAD) {
37
 
        fprintf(stderr, "Error: connect Postgres:%s\n",
38
 
                PQerrorMessage(pg_conn));
39
 
        PQfinish(pg_conn);
40
 
        return NULL;
41
 
    }
42
 
 
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));
47
 
        PQclear(res);
48
 
        PQfinish(pg_conn);
49
 
        return NULL;
50
 
    }
51
 
 
52
 
    nfields = PQnfields(res);
53
 
    nrows = PQntuples(res);
54
 
 
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);
60
 
        }
61
 
    }
62
 
    else if (nrows) {
63
 
 
64
 
        fprintf(stderr, "%s", PQfname(res, 0));
65
 
        for (j = 1; j < nfields; j++) {
66
 
            fprintf(stderr, ",%s", PQfname(res, j));
67
 
        }
68
 
        fprintf(stderr, "\n");
69
 
 
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);
74
 
            }
75
 
            fprintf(stderr, "\n");
76
 
        }
77
 
        fprintf(stderr, "\n%d rows selected\n", nrows);
78
 
    }
79
 
 
80
 
    if (vrbs) {
81
 
        sprintf(chunk, "\n%d rows selected\n", nrows);
82
 
        strncat(long_str, chunk, QRY_LENGTH);
83
 
    }
84
 
 
85
 
 
86
 
    PQclear(res);
87
 
    /* explicitly close select result to avoid memory leaks  */
88
 
 
89
 
    PQfinish(pg_conn);
90
 
    /* close connection to database */
91
 
 
92
 
    return long_str;
93
 
}
94
 
 
95
 
char *do_query(char *SQL_stmt, struct Sql *pts)
96
 
{
97
 
    char buf[QRY_LENGTH];
98
 
    char chunk[QRY_LENGTH];
99
 
    static char long_str[2 * QRY_LENGTH];
100
 
 
101
 
    char sqlcmd[QRY_LENGTH];
102
 
    int i, j, nrows, nfields;
103
 
 
104
 
    PGconn *pg_conn;
105
 
    PGresult *res;
106
 
    char *pghost;
107
 
 
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));
112
 
 
113
 
 
114
 
    sprintf(sqlcmd,
115
 
            "%s @ '(%f,%f,%f,%f)'::box", SQL_stmt,
116
 
            pts->minX, pts->minY, pts->maxX, pts->maxY);
117
 
 
118
 
    fprintf(stderr,
119
 
            "\n\nExecuting\n%s;\n clause  @ '( )'::box addded autonmatically.\n\n",
120
 
            sqlcmd);
121
 
    pghost = G__getenv("PG_HOST");
122
 
    pg_conn = PQsetdb(pghost, NULL, NULL, NULL, G_getenv("PG_DBASE"));
123
 
 
124
 
    if (PQstatus(pg_conn) == CONNECTION_BAD) {
125
 
        fprintf(stderr, "Error: connect Postgres:%s\n",
126
 
                PQerrorMessage(pg_conn));
127
 
        PQfinish(pg_conn);
128
 
        return NULL;
129
 
    }
130
 
 
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));
135
 
        PQclear(res);
136
 
        PQfinish(pg_conn);
137
 
        return NULL;
138
 
    }
139
 
 
140
 
    nfields = PQnfields(res);
141
 
    nrows = PQntuples(res);
142
 
 
143
 
    if (nrows == 1) {
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);
148
 
        }
149
 
    }
150
 
    else if (nrows) {
151
 
 
152
 
        fprintf(stderr, "%s", PQfname(res, 0));
153
 
        for (j = 1; j < nfields; j++) {
154
 
            fprintf(stderr, ",%s", PQfname(res, j));
155
 
        }
156
 
        fprintf(stderr, "\n");
157
 
 
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);
162
 
            }
163
 
            fprintf(stderr, "\n");
164
 
        }
165
 
        fprintf(stderr, "\n%d rows selected\n", nrows);
166
 
    }
167
 
 
168
 
 
169
 
    sprintf(chunk, "\n%d rows selected\n", nrows);
170
 
    strncat(long_str, chunk, QRY_LENGTH);
171
 
 
172
 
 
173
 
    PQclear(res);
174
 
    /* explicitly close select result to avoid memory leaks  */
175
 
 
176
 
    PQfinish(pg_conn);
177
 
    /* close connection to database */
178
 
 
179
 
    return long_str;
180
 
}
181
 
#endif