PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
testlibpq.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
Include dependency graph for testlibpq.c:

Go to the source code of this file.

Functions

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

Function Documentation

static void exit_nicely ( PGconn conn)
static

Definition at line 14 of file testlibpq.c.

References PQfinish().

Referenced by main().

15 {
16  PQfinish(conn);
17  exit(1);
18 }
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int main ( int  argc,
char **  argv 
)

Definition at line 21 of file testlibpq.c.

References conn, CONNECTION_OK, exit_nicely(), i, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQconnectdb(), PQerrorMessage(), PQexec(), PQfinish(), PQfname(), PQgetvalue(), PQnfields(), PQntuples(), PQresultStatus(), and PQstatus().

22 {
23  const char *conninfo;
24  PGconn *conn;
25  PGresult *res;
26  int nFields;
27  int i,
28  j;
29 
30  /*
31  * If the user supplies a parameter on the command line, use it as the
32  * conninfo string; otherwise default to setting dbname=postgres and using
33  * environment variables or defaults for all other connection parameters.
34  */
35  if (argc > 1)
36  conninfo = argv[1];
37  else
38  conninfo = "dbname = postgres";
39 
40  /* Make a connection to the database */
41  conn = PQconnectdb(conninfo);
42 
43  /* Check to see that the backend connection was successfully made */
44  if (PQstatus(conn) != CONNECTION_OK)
45  {
46  fprintf(stderr, "Connection to database failed: %s",
47  PQerrorMessage(conn));
48  exit_nicely(conn);
49  }
50 
51  /*
52  * Our test case here involves using a cursor, for which we must be inside
53  * a transaction block. We could do the whole thing with a single
54  * PQexec() of "select * from pg_database", but that's too trivial to make
55  * a good example.
56  */
57 
58  /* Start a transaction block */
59  res = PQexec(conn, "BEGIN");
60  if (PQresultStatus(res) != PGRES_COMMAND_OK)
61  {
62  fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
63  PQclear(res);
64  exit_nicely(conn);
65  }
66 
67  /*
68  * Should PQclear PGresult whenever it is no longer needed to avoid memory
69  * leaks
70  */
71  PQclear(res);
72 
73  /*
74  * Fetch rows from pg_database, the system catalog of databases
75  */
76  res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
77  if (PQresultStatus(res) != PGRES_COMMAND_OK)
78  {
79  fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
80  PQclear(res);
81  exit_nicely(conn);
82  }
83  PQclear(res);
84 
85  res = PQexec(conn, "FETCH ALL in myportal");
86  if (PQresultStatus(res) != PGRES_TUPLES_OK)
87  {
88  fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
89  PQclear(res);
90  exit_nicely(conn);
91  }
92 
93  /* first, print out the attribute names */
94  nFields = PQnfields(res);
95  for (i = 0; i < nFields; i++)
96  printf("%-15s", PQfname(res, i));
97  printf("\n\n");
98 
99  /* next, print out the rows */
100  for (i = 0; i < PQntuples(res); i++)
101  {
102  for (j = 0; j < nFields; j++)
103  printf("%-15s", PQgetvalue(res, i, j));
104  printf("\n");
105  }
106 
107  PQclear(res);
108 
109  /* close the portal ... we don't bother to check for errors ... */
110  res = PQexec(conn, "CLOSE myportal");
111  PQclear(res);
112 
113  /* end the transaction */
114  res = PQexec(conn, "END");
115  PQclear(res);
116 
117  /* close the connection to the database and cleanup */
118  PQfinish(conn);
119 
120  return 0;
121 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2681
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3067
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2759
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
PGconn * conn
Definition: streamutil.c:43
static void exit_nicely(PGconn *conn)
Definition: testlibpq.c:14
void PQclear(PGresult *res)
Definition: fe-exec.c:650
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:585