1 /* -*-pgsql-c-*- */
2 /*
3 * $Header$
4 *
5 * pgpool: a language independent connection pool server for PostgreSQL
6 * written by Tatsuo Ishii
7 *
8 * Copyright (c) 2003-2010 PgPool Global Development Group
9 *
10 * Permission to use, copy, modify, and distribute this software and
11 * its documentation for any purpose and without fee is hereby
12 * granted, provided that the above copyright notice appear in all
13 * copies and that both that copyright notice and this permission
14 * notice appear in supporting documentation, and that the name of the
15 * author not be used in advertising or publicity pertaining to
16 * distribution of the software without specific, written prior
17 * permission. The author makes no representations about the
18 * suitability of this software for any purpose. It is provided "as
19 * is" without express or implied warranty.
20 *
21 * Extended protocol test program.
22 * Features are:
23 * libq trace on/off
24 * explicit transaction on/off
25 * multiple SQL statements
26 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35
36 #include "libpq-fe.h"
37 #include "libpq/libpq-fs.h"
38
39 int
main(int argc,char ** argv)40 main(int argc, char **argv)
41 {
42 char *connect_string = "user=t-ishii dbname=test port=5432";
43 int doTrace = 1; /* set 1 to start libpq trace */
44 int doTransaction = 1; /* set 1 to start explicit transaction */
45 int doSleep = 0; /* set non 0 seconds to sleep after connection
46 * and beforfe starting command */
47
48 /* SQL commands to be executed */
49 static char *commands[] = {
50 "SAVEPOINT S1",
51 "UPDATE t1 SET k = 1",
52 "ROLLBACK TO S1",
53 "SELECT 1",
54 "RELEASE SAVEPOINT S1"
55 };
56
57 PGconn *conn;
58 PGresult *res;
59 int i;
60
61 conn = PQconnectdb(connect_string);
62 if (PQstatus(conn) == CONNECTION_BAD)
63 {
64 printf("Unable to connect to db\n");
65 PQfinish(conn);
66 return 1;
67 }
68
69 if (doSleep)
70 sleep(doSleep);
71
72 if (doTrace == 1)
73 PQtrace(conn, stdout);
74
75 if (doTransaction)
76 PQexec(conn, "BEGIN;");
77
78 for (i = 0; i < sizeof(commands) / sizeof(char *); i++)
79 {
80 char *command = commands[i];
81
82 res = PQexecParams(conn, command, 0, NULL, NULL, NULL, NULL, 0);
83 switch (PQresultStatus(res))
84 {
85 case PGRES_COMMAND_OK:
86 case PGRES_TUPLES_OK:
87 fprintf(stderr, "\"%s\" : succeeded\n", command);
88 break;
89 default:
90 fprintf(stderr, "\"%s\" failed: %s\n", command, PQresultErrorMessage(res));
91 break;
92 }
93 PQclear(res);
94
95 }
96
97 if (doTransaction == 1)
98 {
99 PQexec(conn, "COMMIT;");
100 }
101
102 PQfinish(conn);
103 return 0;
104 }
105