PostgreSQL Source Code  git master
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 48 of file testlibpq3.c.

49 {
50  PQfinish(conn);
51  exit(1);
52 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4879
exit(1)
PGconn * conn
Definition: streamutil.c:53

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

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 115 of file testlibpq3.c.

116 {
117  const char *conninfo;
118  PGconn *conn;
119  PGresult *res;
120  const char *paramValues[1];
121  int paramLengths[1];
122  int paramFormats[1];
123  uint32_t binaryIntVal;
124 
125  /*
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.
129  */
130  if (argc > 1)
131  conninfo = argv[1];
132  else
133  conninfo = "dbname = postgres";
134 
135  /* Make a connection to the database */
136  conn = PQconnectdb(conninfo);
137 
138  /* Check to see that the backend connection was successfully made */
139  if (PQstatus(conn) != CONNECTION_OK)
140  {
141  fprintf(stderr, "%s", PQerrorMessage(conn));
142  exit_nicely(conn);
143  }
144 
145  /* Set always-secure search path, so malicious users can't take control. */
146  res = PQexec(conn, "SET search_path = testlibpq3");
148  {
149  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
150  PQclear(res);
151  exit_nicely(conn);
152  }
153  PQclear(res);
154 
155  /*
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.
158  *
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.
164  */
165 
166  /* Here is our out-of-line parameter value */
167  paramValues[0] = "joe's place";
168 
170  "SELECT * FROM test1 WHERE t = $1",
171  1, /* one param */
172  NULL, /* let the backend deduce param type */
173  paramValues,
174  NULL, /* don't need param lengths since text */
175  NULL, /* default to all text params */
176  1); /* ask for binary results */
177 
179  {
180  fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
181  PQclear(res);
182  exit_nicely(conn);
183  }
184 
186 
187  PQclear(res);
188 
189  /*
190  * In this second example we transmit an integer parameter in binary form,
191  * and again retrieve the results in binary form.
192  *
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
196  * binary parameters.
197  */
198 
199  /* Convert integer value "2" to network byte order */
200  binaryIntVal = htonl((uint32_t) 2);
201 
202  /* Set up parameter arrays for PQexecParams */
203  paramValues[0] = (char *) &binaryIntVal;
204  paramLengths[0] = sizeof(binaryIntVal);
205  paramFormats[0] = 1; /* binary */
206 
208  "SELECT * FROM test1 WHERE i = $1::int4",
209  1, /* one param */
210  NULL, /* let the backend deduce param type */
211  paramValues,
212  paramLengths,
213  paramFormats,
214  1); /* ask for binary results */
215 
217  {
218  fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
219  PQclear(res);
220  exit_nicely(conn);
221  }
222 
224 
225  PQclear(res);
226 
227  /* close the connection to the database and cleanup */
228  PQfinish(conn);
229 
230  return 0;
231 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7200
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7137
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:745
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:2276
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
@ CONNECTION_OK
Definition: libpq-fe.h:81
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:120
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:123
#define fprintf
Definition: port.h:242
static void exit_nicely(PGconn *conn)
Definition: testlibpq3.c:48
static void show_binary_results(PGresult *res)
Definition: testlibpq3.c:60

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

◆ show_binary_results()

static void show_binary_results ( PGresult res)
static

Definition at line 60 of file testlibpq3.c.

61 {
62  int i,
63  j;
64  int i_fnum,
65  t_fnum,
66  b_fnum;
67 
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");
72 
73  for (i = 0; i < PQntuples(res); i++)
74  {
75  char *iptr;
76  char *tptr;
77  char *bptr;
78  int blen;
79  int ival;
80 
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);
85 
86  /*
87  * The binary representation of INT4 is in network byte order, which
88  * we'd better coerce to the local byte order.
89  */
90  ival = ntohl(*((uint32_t *) iptr));
91 
92  /*
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
95  * as a C string.
96  *
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.
99  */
100  blen = PQgetlength(res, i, b_fnum);
101 
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]);
110  printf("\n\n");
111  }
112 }
int PQgetlength(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3887
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3481
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3876
int PQfnumber(const PGresult *res, const char *field_name)
Definition: fe-exec.c:3589
int j
Definition: isn.c:73
int i
Definition: isn.c:72
#define printf(...)
Definition: port.h:244

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

Referenced by main().