PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

static void exit_nicely ( PGconn conn)
static

Definition at line 46 of file testlibpq3.c.

References PQfinish().

Referenced by main().

47 {
48  PQfinish(conn);
49  exit(1);
50 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3568
int main ( int  argc,
char **  argv 
)

Definition at line 113 of file testlibpq3.c.

References conn, CONNECTION_OK, exit_nicely(), NULL, PGRES_TUPLES_OK, PQclear(), PQconnectdb(), PQerrorMessage(), PQexecParams(), PQfinish(), PQresultStatus(), PQstatus(), and show_binary_results().

114 {
115  const char *conninfo;
116  PGconn *conn;
117  PGresult *res;
118  const char *paramValues[1];
119  int paramLengths[1];
120  int paramFormats[1];
121  uint32_t binaryIntVal;
122 
123  /*
124  * If the user supplies a parameter on the command line, use it as the
125  * conninfo string; otherwise default to setting dbname=postgres and using
126  * environment variables or defaults for all other connection parameters.
127  */
128  if (argc > 1)
129  conninfo = argv[1];
130  else
131  conninfo = "dbname = postgres";
132 
133  /* Make a connection to the database */
134  conn = PQconnectdb(conninfo);
135 
136  /* Check to see that the backend connection was successfully made */
137  if (PQstatus(conn) != CONNECTION_OK)
138  {
139  fprintf(stderr, "Connection to database failed: %s",
140  PQerrorMessage(conn));
141  exit_nicely(conn);
142  }
143 
144  /*
145  * The point of this program is to illustrate use of PQexecParams() with
146  * out-of-line parameters, as well as binary transmission of data.
147  *
148  * This first example transmits the parameters as text, but receives the
149  * results in binary format. By using out-of-line parameters we can avoid
150  * a lot of tedious mucking about with quoting and escaping, even though
151  * the data is text. Notice how we don't have to do anything special with
152  * the quote mark in the parameter value.
153  */
154 
155  /* Here is our out-of-line parameter value */
156  paramValues[0] = "joe's place";
157 
158  res = PQexecParams(conn,
159  "SELECT * FROM test1 WHERE t = $1",
160  1, /* one param */
161  NULL, /* let the backend deduce param type */
162  paramValues,
163  NULL, /* don't need param lengths since text */
164  NULL, /* default to all text params */
165  1); /* ask for binary results */
166 
167  if (PQresultStatus(res) != PGRES_TUPLES_OK)
168  {
169  fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
170  PQclear(res);
171  exit_nicely(conn);
172  }
173 
174  show_binary_results(res);
175 
176  PQclear(res);
177 
178  /*
179  * In this second example we transmit an integer parameter in binary form,
180  * and again retrieve the results in binary form.
181  *
182  * Although we tell PQexecParams we are letting the backend deduce
183  * parameter type, we really force the decision by casting the parameter
184  * symbol in the query text. This is a good safety measure when sending
185  * binary parameters.
186  */
187 
188  /* Convert integer value "2" to network byte order */
189  binaryIntVal = htonl((uint32_t) 2);
190 
191  /* Set up parameter arrays for PQexecParams */
192  paramValues[0] = (char *) &binaryIntVal;
193  paramLengths[0] = sizeof(binaryIntVal);
194  paramFormats[0] = 1; /* binary */
195 
196  res = PQexecParams(conn,
197  "SELECT * FROM test1 WHERE i = $1::int4",
198  1, /* one param */
199  NULL, /* let the backend deduce param type */
200  paramValues,
201  paramLengths,
202  paramFormats,
203  1); /* ask for binary results */
204 
205  if (PQresultStatus(res) != PGRES_TUPLES_OK)
206  {
207  fprintf(stderr, "SELECT failed: %s", PQerrorMessage(conn));
208  PQclear(res);
209  exit_nicely(conn);
210  }
211 
212  show_binary_results(res);
213 
214  PQclear(res);
215 
216  /* close the connection to the database and cleanup */
217  PQfinish(conn);
218 
219  return 0;
220 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
static void exit_nicely(PGconn *conn)
Definition: testlibpq3.c:46
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3568
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
PGconn * conn
Definition: streamutil.c:42
static void show_binary_results(PGresult *res)
Definition: testlibpq3.c:58
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define NULL
Definition: c.h:229
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:1860
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5958
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:526
static void show_binary_results ( PGresult res)
static

Definition at line 58 of file testlibpq3.c.

References i, PQfnumber(), PQgetlength(), PQgetvalue(), and PQntuples().

Referenced by main().

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