PostgreSQL Source Code  git master
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

◆ exit_nicely()

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:3638

◆ main()

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  /* Set always-secure search path, so malicous users can't take control. */
52  res = PQexec(conn,
53  "SELECT pg_catalog.set_config('search_path', '', false)");
54  if (PQresultStatus(res) != PGRES_COMMAND_OK)
55  {
56  fprintf(stderr, "SET failed: %s", PQerrorMessage(conn));
57  PQclear(res);
58  exit_nicely(conn);
59  }
60 
61  /*
62  * Should PQclear PGresult whenever it is no longer needed to avoid memory
63  * leaks
64  */
65  PQclear(res);
66 
67  /*
68  * Our test case here involves using a cursor, for which we must be inside
69  * a transaction block. We could do the whole thing with a single
70  * PQexec() of "select * from pg_database", but that's too trivial to make
71  * a good example.
72  */
73 
74  /* Start a transaction block */
75  res = PQexec(conn, "BEGIN");
76  if (PQresultStatus(res) != PGRES_COMMAND_OK)
77  {
78  fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
79  PQclear(res);
80  exit_nicely(conn);
81  }
82  PQclear(res);
83 
84  /*
85  * Fetch rows from pg_database, the system catalog of databases
86  */
87  res = PQexec(conn, "DECLARE myportal CURSOR FOR select * from pg_database");
88  if (PQresultStatus(res) != PGRES_COMMAND_OK)
89  {
90  fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
91  PQclear(res);
92  exit_nicely(conn);
93  }
94  PQclear(res);
95 
96  res = PQexec(conn, "FETCH ALL in myportal");
97  if (PQresultStatus(res) != PGRES_TUPLES_OK)
98  {
99  fprintf(stderr, "FETCH ALL failed: %s", PQerrorMessage(conn));
100  PQclear(res);
101  exit_nicely(conn);
102  }
103 
104  /* first, print out the attribute names */
105  nFields = PQnfields(res);
106  for (i = 0; i < nFields; i++)
107  printf("%-15s", PQfname(res, i));
108  printf("\n\n");
109 
110  /* next, print out the rows */
111  for (i = 0; i < PQntuples(res); i++)
112  {
113  for (j = 0; j < nFields; j++)
114  printf("%-15s", PQgetvalue(res, i, j));
115  printf("\n");
116  }
117 
118  PQclear(res);
119 
120  /* close the portal ... we don't bother to check for errors ... */
121  res = PQexec(conn, "CLOSE myportal");
122  PQclear(res);
123 
124  /* end the transaction */
125  res = PQexec(conn, "END");
126  PQclear(res);
127 
128  /* close the connection to the database and cleanup */
129  PQfinish(conn);
130 
131  return 0;
132 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2732
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6116
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3118
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2810
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3638
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
PGconn * conn
Definition: streamutil.c:55
static void exit_nicely(PGconn *conn)
Definition: testlibpq.c:14
void PQclear(PGresult *res)
Definition: fe-exec.c:671
int i
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6063
PGconn * PQconnectdb(const char *conninfo)
Definition: fe-connect.c:592