PostgreSQL Source Code git master
Loading...
Searching...
No Matches
testlibpq3.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include "libpq-fe.h"
#include <netinet/in.h>
#include <arpa/inet.h>
Include dependency graph for testlibpq3.c:

Go to the source code of this file.

Functions

static void exit_nicely (PGconn *conn)
 
static void show_binary_results (PGresult *res)
 
int main (int argc, char **argv)
 

Function Documentation

◆ exit_nicely()

static void exit_nicely ( PGconn conn)
static

Definition at line 47 of file testlibpq3.c.

48{
50 exit(1);
51}
void PQfinish(PGconn *conn)
static int fb(int x)
PGconn * conn
Definition streamutil.c:52

References conn, fb(), and PQfinish().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 114 of file testlibpq3.c.

115{
116 const char *conninfo;
117 PGconn *conn;
118 PGresult *res;
119 const char *paramValues[1];
120 int paramLengths[1];
121 int paramFormats[1];
123
124 /*
125 * If the user supplies a parameter on the command line, use it as the
126 * conninfo string; otherwise default to setting dbname=postgres and using
127 * environment variables or defaults for all other connection parameters.
128 */
129 if (argc > 1)
130 conninfo = argv[1];
131 else
132 conninfo = "dbname = postgres";
133
134 /* Make a connection to the database */
135 conn = PQconnectdb(conninfo);
136
137 /* Check to see that the backend connection was successfully made */
139 {
142 }
143
144 /* Set always-secure search path, so malicious users can't take control. */
145 res = PQexec(conn, "SET search_path = testlibpq3");
147 {
148 fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
149 PQclear(res);
151 }
152 PQclear(res);
153
154 /*
155 * The point of this program is to illustrate use of PQexecParams() with
156 * out-of-line parameters, as well as binary transmission of data.
157 *
158 * This first example transmits the parameters as text, but receives the
159 * results in binary format. By using out-of-line parameters we can avoid
160 * a lot of tedious mucking about with quoting and escaping, even though
161 * the data is text. Notice how we don't have to do anything special with
162 * the quote mark in the parameter value.
163 */
164
165 /* Here is our out-of-line parameter value */
166 paramValues[0] = "joe's place";
167
168 res = PQexecParams(conn,
169 "SELECT * FROM test1 WHERE t = $1",
170 1, /* one param */
171 NULL, /* let the backend deduce param type */
173 NULL, /* don't need param lengths since text */
174 NULL, /* default to all text params */
175 1); /* ask for binary results */
176
178 {
179 fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
180 PQclear(res);
182 }
183
185
186 PQclear(res);
187
188 /*
189 * In this second example we transmit an integer parameter in binary form,
190 * and again retrieve the results in binary form.
191 *
192 * Although we tell PQexecParams we are letting the backend deduce
193 * parameter type, we really force the decision by casting the parameter
194 * symbol in the query text. This is a good safety measure when sending
195 * binary parameters.
196 */
197
198 /* Convert integer value "2" to network byte order */
200
201 /* Set up parameter arrays for PQexecParams */
202 paramValues[0] = (char *) &binaryIntVal;
203 paramLengths[0] = sizeof(binaryIntVal);
204 paramFormats[0] = 1; /* binary */
205
206 res = PQexecParams(conn,
207 "SELECT * FROM test1 WHERE i = $1::int4",
208 1, /* one param */
209 NULL, /* let the backend deduce param type */
213 1); /* ask for binary results */
214
216 {
217 fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
218 PQclear(res);
220 }
221
223
224 PQclear(res);
225
226 /* close the connection to the database and cleanup */
227 PQfinish(conn);
228
229 return 0;
230}
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
PGconn * PQconnectdb(const char *conninfo)
Definition fe-connect.c:825
ConnStatusType PQstatus(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
Definition fe-exec.c:2293
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
#define PQclear
#define PQresultStatus
@ CONNECTION_OK
Definition libpq-fe.h:84
@ PGRES_COMMAND_OK
Definition libpq-fe.h:125
@ PGRES_TUPLES_OK
Definition libpq-fe.h:128
static void exit_nicely(PGconn *conn)
Definition testlibpq3.c:47
static void show_binary_results(PGresult *res)
Definition testlibpq3.c:59

References conn, CONNECTION_OK, exit_nicely(), fb(), fprintf, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear, PQconnectdb(), PQerrorMessage(), PQexec(), PQexecParams(), PQfinish(), PQresultStatus, PQstatus(), and show_binary_results().

◆ show_binary_results()

static void show_binary_results ( PGresult res)
static

Definition at line 59 of file testlibpq3.c.

60{
61 int i,
62 j;
63 int i_fnum,
64 t_fnum,
65 b_fnum;
66
67 /* Use PQfnumber to avoid assumptions about field order in result */
68 i_fnum = PQfnumber(res, "i");
69 t_fnum = PQfnumber(res, "t");
70 b_fnum = PQfnumber(res, "b");
71
72 for (i = 0; i < PQntuples(res); i++)
73 {
74 char *iptr;
75 char *tptr;
76 char *bptr;
77 int blen;
78 int ival;
79
80 /* Get the field values (we ignore possibility they are null!) */
81 iptr = PQgetvalue(res, i, i_fnum);
82 tptr = PQgetvalue(res, i, t_fnum);
83 bptr = PQgetvalue(res, i, b_fnum);
84
85 /*
86 * The binary representation of INT4 is in network byte order, which
87 * we'd better coerce to the local byte order.
88 */
89 ival = ntohl(*((uint32_t *) iptr));
90
91 /*
92 * The binary representation of TEXT is, well, text, and since libpq
93 * was nice enough to append a zero byte to it, it'll work just fine
94 * as a C string.
95 *
96 * The binary representation of BYTEA is a bunch of bytes, which could
97 * include embedded nulls so we have to pay attention to field length.
98 */
99 blen = PQgetlength(res, i, b_fnum);
100
101 printf("tuple %d: got\n", i);
102 printf(" i = (%d bytes) %d\n",
103 PQgetlength(res, i, i_fnum), ival);
104 printf(" t = (%d bytes) '%s'\n",
105 PQgetlength(res, i, t_fnum), tptr);
106 printf(" b = (%d bytes) ", blen);
107 for (j = 0; j < blen; j++)
108 printf("\\%03o", bptr[j]);
109 printf("\n\n");
110 }
111}
int PQfnumber(const PGresult *res, const char *field_name)
Definition fe-exec.c:3606
int j
Definition isn.c:78
int i
Definition isn.c:77
#define PQgetvalue
#define PQgetlength
#define PQntuples
#define printf(...)
Definition port.h:266

References fb(), i, j, PQfnumber(), PQgetlength, PQgetvalue, PQntuples, and printf.

Referenced by main().