6 * Test out-of-line parameters and binary I/O.
8 * Before running this, populate a database with the following commands
9 * (provided in src/test/examples/testlibpq3.sql):
11 * CREATE TABLE test1 (i int4, t text, b bytea);
13 * INSERT INTO test1 values (1, 'joe''s place', '\\000\\001\\002\\003\\004');
14 * INSERT INTO test1 values (2, 'ho there', '\\004\\003\\002\\001\\000');
16 * The expected output is:
20 * t = (11 bytes) 'joe's place'
21 * b = (5 bytes) \000\001\002\003\004
25 * t = (8 bytes) 'ho there'
26 * b = (5 bytes) \004\003\002\001\000
31 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
40 exit_nicely(PGconn
*conn
)
47 * This function prints a query result that is a binary-format fetch from
48 * a table defined as in the comment above. We split it out because the
49 * main() function uses it twice.
52 show_binary_results(PGresult
*res
)
60 /* Use PQfnumber to avoid assumptions about field order in result */
61 i_fnum
= PQfnumber(res
, "i");
62 t_fnum
= PQfnumber(res
, "t");
63 b_fnum
= PQfnumber(res
, "b");
65 for (i
= 0; i
< PQntuples(res
); i
++)
73 /* Get the field values (we ignore possibility they are null!) */
74 iptr
= PQgetvalue(res
, i
, i_fnum
);
75 tptr
= PQgetvalue(res
, i
, t_fnum
);
76 bptr
= PQgetvalue(res
, i
, b_fnum
);
79 * The binary representation of INT4 is in network byte order, which
80 * we'd better coerce to the local byte order.
82 ival
= ntohl(*((uint32_t *) iptr
));
85 * The binary representation of TEXT is, well, text, and since libpq
86 * was nice enough to append a zero byte to it, it'll work just fine
89 * The binary representation of BYTEA is a bunch of bytes, which could
90 * include embedded nulls so we have to pay attention to field length.
92 blen
= PQgetlength(res
, i
, b_fnum
);
94 printf("tuple %d: got\n", i
);
95 printf(" i = (%d bytes) %d\n",
96 PQgetlength(res
, i
, i_fnum
), ival
);
97 printf(" t = (%d bytes) '%s'\n",
98 PQgetlength(res
, i
, t_fnum
), tptr
);
99 printf(" b = (%d bytes) ", blen
);
100 for (j
= 0; j
< blen
; j
++)
101 printf("\\%03o", bptr
[j
]);
107 main(int argc
, char **argv
)
109 const char *conninfo
;
112 const char *paramValues
[1];
115 uint32_t binaryIntVal
;
118 * If the user supplies a parameter on the command line, use it as the
119 * conninfo string; otherwise default to setting dbname=postgres and using
120 * environment variables or defaults for all other connection parameters.
125 conninfo
= "dbname = postgres";
127 /* Make a connection to the database */
128 conn
= PQconnectdb(conninfo
);
130 /* Check to see that the backend connection was successfully made */
131 if (PQstatus(conn
) != CONNECTION_OK
)
133 fprintf(stderr
, "Connection to database failed: %s",
134 PQerrorMessage(conn
));
139 * The point of this program is to illustrate use of PQexecParams() with
140 * out-of-line parameters, as well as binary transmission of data.
142 * This first example transmits the parameters as text, but receives the
143 * results in binary format. By using out-of-line parameters we can avoid
144 * a lot of tedious mucking about with quoting and escaping, even though
145 * the data is text. Notice how we don't have to do anything special with
146 * the quote mark in the parameter value.
149 /* Here is our out-of-line parameter value */
150 paramValues
[0] = "joe's place";
152 res
= PQexecParams(conn
,
153 "SELECT * FROM test1 WHERE t = $1",
155 NULL
, /* let the backend deduce param type */
157 NULL
, /* don't need param lengths since text */
158 NULL
, /* default to all text params */
159 1); /* ask for binary results */
161 if (PQresultStatus(res
) != PGRES_TUPLES_OK
)
163 fprintf(stderr
, "SELECT failed: %s", PQerrorMessage(conn
));
168 show_binary_results(res
);
173 * In this second example we transmit an integer parameter in binary form,
174 * and again retrieve the results in binary form.
176 * Although we tell PQexecParams we are letting the backend deduce
177 * parameter type, we really force the decision by casting the parameter
178 * symbol in the query text. This is a good safety measure when sending
182 /* Convert integer value "2" to network byte order */
183 binaryIntVal
= htonl((uint32_t) 2);
185 /* Set up parameter arrays for PQexecParams */
186 paramValues
[0] = (char *) &binaryIntVal
;
187 paramLengths
[0] = sizeof(binaryIntVal
);
188 paramFormats
[0] = 1; /* binary */
190 res
= PQexecParams(conn
,
191 "SELECT * FROM test1 WHERE i = $1::int4",
193 NULL
, /* let the backend deduce param type */
197 1); /* ask for binary results */
199 if (PQresultStatus(res
) != PGRES_TUPLES_OK
)
201 fprintf(stderr
, "SELECT failed: %s", PQerrorMessage(conn
));
206 show_binary_results(res
);
210 /* close the connection to the database and cleanup */