3
* Test out-of-line parameters and binary I/O.
5
* Before running this, populate a database with the following commands
6
* (provided in src/test/examples/testlibpq3.sql):
8
* CREATE TABLE test1 (i int4, t text, b bytea);
10
* INSERT INTO test1 values (1, 'joe''s place', '\\000\\001\\002\\003\\004');
11
* INSERT INTO test1 values (2, 'ho there', '\\004\\003\\002\\001\\000');
13
* The expected output is:
17
* t = (11 bytes) 'joe's place'
18
* b = (5 bytes) \000\001\002\003\004
24
#include <sys/types.h>
28
#include <netinet/in.h>
29
#include <arpa/inet.h>
33
exit_nicely(PGconn *conn)
40
main(int argc, char **argv)
45
const char *paramValues[1];
53
* If the user supplies a parameter on the command line, use it as the
54
* conninfo string; otherwise default to setting dbname=template1 and
55
* using environment variables or defaults for all other connection
61
conninfo = "dbname = template1";
63
/* Make a connection to the database */
64
conn = PQconnectdb(conninfo);
66
/* Check to see that the backend connection was successfully made */
67
if (PQstatus(conn) != CONNECTION_OK)
69
fprintf(stderr, "Connection to database failed: %s",
70
PQerrorMessage(conn));
75
* The point of this program is to illustrate use of PQexecParams()
76
* with out-of-line parameters, as well as binary transmission of
77
* results. By using out-of-line parameters we can avoid a lot of
78
* tedious mucking about with quoting and escaping. Notice how we
79
* don't have to do anything special with the quote mark in the
83
/* Here is our out-of-line parameter value */
84
paramValues[0] = "joe's place";
86
res = PQexecParams(conn,
87
"SELECT * FROM test1 WHERE t = $1",
89
NULL, /* let the backend deduce param type */
91
NULL, /* don't need param lengths since text */
92
NULL, /* default to all text params */
93
1); /* ask for binary results */
95
if (PQresultStatus(res) != PGRES_TUPLES_OK)
97
fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
102
/* Use PQfnumber to avoid assumptions about field order in result */
103
i_fnum = PQfnumber(res, "i");
104
t_fnum = PQfnumber(res, "t");
105
b_fnum = PQfnumber(res, "b");
107
for (i = 0; i < PQntuples(res); i++)
115
/* Get the field values (we ignore possibility they are null!) */
116
iptr = PQgetvalue(res, i, i_fnum);
117
tptr = PQgetvalue(res, i, t_fnum);
118
bptr = PQgetvalue(res, i, b_fnum);
121
* The binary representation of INT4 is in network byte order,
122
* which we'd better coerce to the local byte order.
124
ival = ntohl(*((uint32_t *) iptr));
127
* The binary representation of TEXT is, well, text, and since
128
* libpq was nice enough to append a zero byte to it, it'll work
129
* just fine as a C string.
131
* The binary representation of BYTEA is a bunch of bytes, which
132
* could include embedded nulls so we have to pay attention to
135
blen = PQgetlength(res, i, b_fnum);
137
printf("tuple %d: got\n", i);
138
printf(" i = (%d bytes) %d\n",
139
PQgetlength(res, i, i_fnum), ival);
140
printf(" t = (%d bytes) '%s'\n",
141
PQgetlength(res, i, t_fnum), tptr);
142
printf(" b = (%d bytes) ", blen);
143
for (j = 0; j < blen; j++)
144
printf("\\%03o", bptr[j]);
150
/* close the connection to the database and cleanup */