2 * src/test/examples/testlibpq3.c
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 SCHEMA testlibpq3;
12 * SET search_path = testlibpq3;
13 * SET standard_conforming_strings = ON;
14 * CREATE TABLE test1 (i int4, t text, b bytea);
15 * INSERT INTO test1 values (1, 'joe''s place', '\000\001\002\003\004');
16 * INSERT INTO test1 values (2, 'ho there', '\004\003\002\001\000');
18 * The expected output is:
22 * t = (11 bytes) 'joe's place'
23 * b = (5 bytes) \000\001\002\003\004
27 * t = (8 bytes) 'ho there'
28 * b = (5 bytes) \004\003\002\001\000
39 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
48 exit_nicely(PGconn
*conn
)
55 * This function prints a query result that is a binary-format fetch from
56 * a table defined as in the comment above. We split it out because the
57 * main() function uses it twice.
60 show_binary_results(PGresult
*res
)
68 /* Use PQfnumber to avoid assumptions about field order in result */
69 i_fnum
= PQfnumber(res
, "i");
70 t_fnum
= PQfnumber(res
, "t");
71 b_fnum
= PQfnumber(res
, "b");
73 for (i
= 0; i
< PQntuples(res
); i
++)
81 /* Get the field values (we ignore possibility they are null!) */
82 iptr
= PQgetvalue(res
, i
, i_fnum
);
83 tptr
= PQgetvalue(res
, i
, t_fnum
);
84 bptr
= PQgetvalue(res
, i
, b_fnum
);
87 * The binary representation of INT4 is in network byte order, which
88 * we'd better coerce to the local byte order.
90 ival
= ntohl(*((uint32_t *) iptr
));
93 * The binary representation of TEXT is, well, text, and since libpq
94 * was nice enough to append a zero byte to it, it'll work just fine
97 * The binary representation of BYTEA is a bunch of bytes, which could
98 * include embedded nulls so we have to pay attention to field length.
100 blen
= PQgetlength(res
, i
, b_fnum
);
102 printf("tuple %d: got\n", i
);
103 printf(" i = (%d bytes) %d\n",
104 PQgetlength(res
, i
, i_fnum
), ival
);
105 printf(" t = (%d bytes) '%s'\n",
106 PQgetlength(res
, i
, t_fnum
), tptr
);
107 printf(" b = (%d bytes) ", blen
);
108 for (j
= 0; j
< blen
; j
++)
109 printf("\\%03o", bptr
[j
]);
115 main(int argc
, char **argv
)
117 const char *conninfo
;
120 const char *paramValues
[1];
123 uint32_t binaryIntVal
;
126 * If the user supplies a parameter on the command line, use it as the
127 * conninfo string; otherwise default to setting dbname=postgres and using
128 * environment variables or defaults for all other connection parameters.
133 conninfo
= "dbname = postgres";
135 /* Make a connection to the database */
136 conn
= PQconnectdb(conninfo
);
138 /* Check to see that the backend connection was successfully made */
139 if (PQstatus(conn
) != CONNECTION_OK
)
141 fprintf(stderr
, "%s", PQerrorMessage(conn
));
145 /* Set always-secure search path, so malicious users can't take control. */
146 res
= PQexec(conn
, "SET search_path = testlibpq3");
147 if (PQresultStatus(res
) != PGRES_COMMAND_OK
)
149 fprintf(stderr
, "SET failed: %s", PQerrorMessage(conn
));
156 * The point of this program is to illustrate use of PQexecParams() with
157 * out-of-line parameters, as well as binary transmission of data.
159 * This first example transmits the parameters as text, but receives the
160 * results in binary format. By using out-of-line parameters we can avoid
161 * a lot of tedious mucking about with quoting and escaping, even though
162 * the data is text. Notice how we don't have to do anything special with
163 * the quote mark in the parameter value.
166 /* Here is our out-of-line parameter value */
167 paramValues
[0] = "joe's place";
169 res
= PQexecParams(conn
,
170 "SELECT * FROM test1 WHERE t = $1",
172 NULL
, /* let the backend deduce param type */
174 NULL
, /* don't need param lengths since text */
175 NULL
, /* default to all text params */
176 1); /* ask for binary results */
178 if (PQresultStatus(res
) != PGRES_TUPLES_OK
)
180 fprintf(stderr
, "SELECT failed: %s", PQerrorMessage(conn
));
185 show_binary_results(res
);
190 * In this second example we transmit an integer parameter in binary form,
191 * and again retrieve the results in binary form.
193 * Although we tell PQexecParams we are letting the backend deduce
194 * parameter type, we really force the decision by casting the parameter
195 * symbol in the query text. This is a good safety measure when sending
199 /* Convert integer value "2" to network byte order */
200 binaryIntVal
= htonl((uint32_t) 2);
202 /* Set up parameter arrays for PQexecParams */
203 paramValues
[0] = (char *) &binaryIntVal
;
204 paramLengths
[0] = sizeof(binaryIntVal
);
205 paramFormats
[0] = 1; /* binary */
207 res
= PQexecParams(conn
,
208 "SELECT * FROM test1 WHERE i = $1::int4",
210 NULL
, /* let the backend deduce param type */
214 1); /* ask for binary results */
216 if (PQresultStatus(res
) != PGRES_TUPLES_OK
)
218 fprintf(stderr
, "SELECT failed: %s", PQerrorMessage(conn
));
223 show_binary_results(res
);
227 /* close the connection to the database and cleanup */