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

15{
17 exit(1);
18}
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224
PGconn * conn
Definition: streamutil.c:52

References conn, and PQfinish().

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 21 of file testlibpq.c.

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

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