Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 951ec87

Browse files
committed
Fix psql to cope with autocommit off, at least during startup.
Behavior of backslash commands (especially for large objects) may still require some thought.
1 parent e258a2b commit 951ec87

File tree

6 files changed

+78
-54
lines changed

6 files changed

+78
-54
lines changed

src/bin/psql/command.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.82 2002/10/03 17:09:41 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.83 2002/10/15 02:24:15 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -1464,13 +1464,17 @@ test_superuser(const char *username)
14641464
if (!username)
14651465
return false;
14661466

1467+
/*
1468+
* Use begin/commit to avoid starting a transaction block if server
1469+
* has autocommit off by default.
1470+
*/
14671471
initPQExpBuffer(&buf);
1468-
printfPQExpBuffer(&buf, "SELECT usesuper FROM pg_catalog.pg_user WHERE usename = '%s'", username);
1469-
res = PSQLexec(buf.data);
1472+
printfPQExpBuffer(&buf, "BEGIN; SELECT usesuper FROM pg_catalog.pg_user WHERE usename = '%s'; COMMIT", username);
1473+
res = PSQLexec(buf.data, true);
14701474
termPQExpBuffer(&buf);
14711475

14721476
answer =
1473-
(PQntuples(res) > 0 && PQnfields(res) > 0
1477+
(res && PQntuples(res) > 0 && PQnfields(res) > 0
14741478
&& !PQgetisnull(res, 0, 0)
14751479
&& PQgetvalue(res, 0, 0)
14761480
&& strcmp(PQgetvalue(res, 0, 0), "t") == 0);

src/bin/psql/common.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.46 2002/10/03 17:09:41 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.47 2002/10/15 02:24:16 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99

@@ -204,12 +204,19 @@ handle_sigint(SIGNAL_ARGS)
204204
*
205205
* This is the way to send "backdoor" queries (those not directly entered
206206
* by the user). It is subject to -E but not -e.
207+
*
208+
* If the given querystring generates multiple PGresults, normally the last
209+
* one is returned to the caller. However, if ignore_command_ok is TRUE,
210+
* then PGresults with status PGRES_COMMAND_OK are ignored. This is intended
211+
* mainly to allow locutions such as "begin; select ...; commit".
207212
*/
208213
PGresult *
209-
PSQLexec(const char *query)
214+
PSQLexec(const char *query, bool ignore_command_ok)
210215
{
211-
PGresult *res;
216+
PGresult *res = NULL;
217+
PGresult *newres;
212218
const char *var;
219+
ExecStatusType rstatus;
213220

214221
if (!pset.db)
215222
{
@@ -230,18 +237,31 @@ PSQLexec(const char *query)
230237
return NULL;
231238

232239
cancelConn = pset.db;
233-
res = PQexec(pset.db, query);
234-
if (PQresultStatus(res) == PGRES_COPY_IN)
235-
copy_in_state = true;
240+
if (PQsendQuery(pset.db, query))
241+
{
242+
while ((newres = PQgetResult(pset.db)) != NULL)
243+
{
244+
if (ignore_command_ok &&
245+
PQresultStatus(newres) == PGRES_COMMAND_OK)
246+
{
247+
PQclear(newres);
248+
continue;
249+
}
250+
PQclear(res);
251+
res = newres;
252+
}
253+
}
254+
rstatus = PQresultStatus(res);
236255
/* keep cancel connection for copy out state */
237-
if (PQresultStatus(res) != PGRES_COPY_OUT)
256+
if (rstatus != PGRES_COPY_OUT)
238257
cancelConn = NULL;
258+
if (rstatus == PGRES_COPY_IN)
259+
copy_in_state = true;
239260

240-
if (res && (PQresultStatus(res) == PGRES_COMMAND_OK ||
241-
PQresultStatus(res) == PGRES_TUPLES_OK ||
242-
PQresultStatus(res) == PGRES_COPY_IN ||
243-
PQresultStatus(res) == PGRES_COPY_OUT)
244-
)
261+
if (res && (rstatus == PGRES_COMMAND_OK ||
262+
rstatus == PGRES_TUPLES_OK ||
263+
rstatus == PGRES_COPY_IN ||
264+
rstatus == PGRES_COPY_OUT))
245265
return res;
246266
else
247267
{

src/bin/psql/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.18 2002/07/06 20:12:30 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.19 2002/10/15 02:24:16 tgl Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
@@ -33,7 +33,7 @@ extern PGconn *cancelConn;
3333
extern void handle_sigint(SIGNAL_ARGS);
3434
#endif /* not WIN32 */
3535

36-
extern PGresult *PSQLexec(const char *query);
36+
extern PGresult *PSQLexec(const char *query, bool ignore_command_ok);
3737

3838
extern bool SendQuery(const char *query);
3939

src/bin/psql/copy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.26 2002/10/03 17:09:41 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/copy.c,v 1.27 2002/10/15 02:24:16 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -324,7 +324,7 @@ do_copy(const char *args)
324324
return false;
325325
}
326326

327-
result = PSQLexec(query.data);
327+
result = PSQLexec(query.data, false);
328328
termPQExpBuffer(&query);
329329

330330
switch (PQresultStatus(result))

src/bin/psql/describe.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000-2002 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.69 2002/09/22 20:44:22 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/describe.c,v 1.70 2002/10/15 02:24:16 tgl Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -91,7 +91,7 @@ describeAggregates(const char *pattern, bool verbose)
9191

9292
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
9393

94-
res = PSQLexec(buf.data);
94+
res = PSQLexec(buf.data, false);
9595
termPQExpBuffer(&buf);
9696
if (!res)
9797
return false;
@@ -161,7 +161,7 @@ describeFunctions(const char *pattern, bool verbose)
161161

162162
appendPQExpBuffer(&buf, "ORDER BY 2, 3, 1, 4;");
163163

164-
res = PSQLexec(buf.data);
164+
res = PSQLexec(buf.data, false);
165165
termPQExpBuffer(&buf);
166166
if (!res)
167167
return false;
@@ -229,7 +229,7 @@ describeTypes(const char *pattern, bool verbose)
229229

230230
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
231231

232-
res = PSQLexec(buf.data);
232+
res = PSQLexec(buf.data, false);
233233
termPQExpBuffer(&buf);
234234
if (!res)
235235
return false;
@@ -276,7 +276,7 @@ describeOperators(const char *pattern)
276276

277277
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3, 4;");
278278

279-
res = PSQLexec(buf.data);
279+
res = PSQLexec(buf.data, false);
280280
termPQExpBuffer(&buf);
281281
if (!res)
282282
return false;
@@ -321,7 +321,7 @@ listAllDbs(bool verbose)
321321
"\n LEFT JOIN pg_catalog.pg_user u ON d.datdba = u.usesysid\n"
322322
"ORDER BY 1;");
323323

324-
res = PSQLexec(buf.data);
324+
res = PSQLexec(buf.data, false);
325325
termPQExpBuffer(&buf);
326326
if (!res)
327327
return false;
@@ -374,7 +374,7 @@ permissionsList(const char *pattern)
374374

375375
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
376376

377-
res = PSQLexec(buf.data);
377+
res = PSQLexec(buf.data, false);
378378
if (!res)
379379
{
380380
termPQExpBuffer(&buf);
@@ -532,7 +532,7 @@ objectDescription(const char *pattern)
532532

533533
appendPQExpBuffer(&buf, "ORDER BY 1, 2, 3;");
534534

535-
res = PSQLexec(buf.data);
535+
res = PSQLexec(buf.data, false);
536536
termPQExpBuffer(&buf);
537537
if (!res)
538538
return false;
@@ -576,7 +576,7 @@ describeTableDetails(const char *pattern, bool verbose)
576576

577577
appendPQExpBuffer(&buf, "ORDER BY 2, 3;");
578578

579-
res = PSQLexec(buf.data);
579+
res = PSQLexec(buf.data, false);
580580
termPQExpBuffer(&buf);
581581
if (!res)
582582
return false;
@@ -655,7 +655,7 @@ describeOneTableDetails(const char *schemaname,
655655
"SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules\n"
656656
"FROM pg_catalog.pg_class WHERE oid = '%s'",
657657
oid);
658-
res = PSQLexec(buf.data);
658+
res = PSQLexec(buf.data, false);
659659
if (!res)
660660
goto error_return;
661661

@@ -711,7 +711,7 @@ describeOneTableDetails(const char *schemaname,
711711
appendPQExpBuffer(&buf, " AND a.attrelid = i.indexrelid");
712712
appendPQExpBuffer(&buf, "\nORDER BY a.attnum");
713713

714-
res = PSQLexec(buf.data);
714+
res = PSQLexec(buf.data, false);
715715
if (!res)
716716
goto error_return;
717717

@@ -721,7 +721,7 @@ describeOneTableDetails(const char *schemaname,
721721
PGresult *result;
722722

723723
printfPQExpBuffer(&buf, "SELECT pg_catalog.pg_get_viewdef('%s'::pg_catalog.oid)", oid);
724-
result = PSQLexec(buf.data);
724+
result = PSQLexec(buf.data, false);
725725
if (!result)
726726
goto error_return;
727727

@@ -763,7 +763,7 @@ describeOneTableDetails(const char *schemaname,
763763
"WHERE d.adrelid = '%s' AND d.adnum = %s",
764764
oid, PQgetvalue(res, i, 4));
765765

766-
result = PSQLexec(buf.data);
766+
result = PSQLexec(buf.data, false);
767767

768768
if (cells[i * cols + 2][0])
769769
strcat(cells[i * cols + 2], " ");
@@ -830,7 +830,7 @@ describeOneTableDetails(const char *schemaname,
830830
"AND i.indrelid = c2.oid",
831831
oid);
832832

833-
result = PSQLexec(buf.data);
833+
result = PSQLexec(buf.data, false);
834834
if (!result)
835835
goto error_return;
836836
else if (PQntuples(result) != 1)
@@ -886,7 +886,7 @@ describeOneTableDetails(const char *schemaname,
886886
"FROM pg_catalog.pg_rewrite r\n"
887887
"WHERE r.ev_class = '%s' AND r.rulename != '_RETURN'",
888888
oid);
889-
result = PSQLexec(buf.data);
889+
result = PSQLexec(buf.data, false);
890890
if (!result)
891891
goto error_return;
892892
else
@@ -944,7 +944,7 @@ describeOneTableDetails(const char *schemaname,
944944
"WHERE c.oid = '%s' AND c.oid = i.indrelid AND i.indexrelid = c2.oid\n"
945945
"ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname",
946946
oid);
947-
result1 = PSQLexec(buf.data);
947+
result1 = PSQLexec(buf.data, false);
948948
if (!result1)
949949
goto error_return;
950950
else
@@ -959,7 +959,7 @@ describeOneTableDetails(const char *schemaname,
959959
"FROM pg_catalog.pg_constraint r\n"
960960
"WHERE r.conrelid = '%s' AND r.contype = 'c'",
961961
oid);
962-
result2 = PSQLexec(buf.data);
962+
result2 = PSQLexec(buf.data, false);
963963
if (!result2)
964964
goto error_return;
965965
else
@@ -974,7 +974,7 @@ describeOneTableDetails(const char *schemaname,
974974
"FROM pg_catalog.pg_rewrite r\n"
975975
"WHERE r.ev_class = '%s'",
976976
oid);
977-
result3 = PSQLexec(buf.data);
977+
result3 = PSQLexec(buf.data, false);
978978
if (!result3)
979979
goto error_return;
980980
else
@@ -994,7 +994,7 @@ describeOneTableDetails(const char *schemaname,
994994
" JOIN pg_catalog.pg_constraint c ON (d.refclassid = c.tableoid AND d.refobjid = c.oid) "
995995
" WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'))",
996996
oid);
997-
result4 = PSQLexec(buf.data);
997+
result4 = PSQLexec(buf.data, false);
998998
if (!result4)
999999
goto error_return;
10001000
else
@@ -1010,7 +1010,7 @@ describeOneTableDetails(const char *schemaname,
10101010
"FROM pg_catalog.pg_constraint r\n"
10111011
"WHERE r.conrelid = '%s' AND r.contype = 'f'",
10121012
oid);
1013-
result5 = PSQLexec(buf.data);
1013+
result5 = PSQLexec(buf.data, false);
10141014
if (!result5)
10151015
goto error_return;
10161016
else
@@ -1206,7 +1206,7 @@ describeUsers(const char *pattern)
12061206

12071207
appendPQExpBuffer(&buf, "ORDER BY 1;");
12081208

1209-
res = PSQLexec(buf.data);
1209+
res = PSQLexec(buf.data, false);
12101210
termPQExpBuffer(&buf);
12111211
if (!res)
12121212
return false;
@@ -1312,7 +1312,7 @@ listTables(const char *tabtypes, const char *pattern, bool verbose)
13121312

13131313
appendPQExpBuffer(&buf, "ORDER BY 1,2;");
13141314

1315-
res = PSQLexec(buf.data);
1315+
res = PSQLexec(buf.data, false);
13161316
termPQExpBuffer(&buf);
13171317
if (!res)
13181318
return false;
@@ -1374,7 +1374,7 @@ listDomains(const char *pattern)
13741374

13751375
appendPQExpBuffer(&buf, "ORDER BY 1, 2;");
13761376

1377-
res = PSQLexec(buf.data);
1377+
res = PSQLexec(buf.data, false);
13781378
termPQExpBuffer(&buf);
13791379
if (!res)
13801380
return false;

0 commit comments

Comments
 (0)