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

Commit 5dbc5da

Browse files
committed
Fix behavior of psql's \p to agree with \g, \w, etc.
In commit e984ef5 I (tgl) simplified the behavior of \p to just print the current query buffer; but Daniel Vérité points out that this made it inconsistent with the behavior of \g and \w. It should print the same thing \g would execute. Fix that, and improve related comments. Daniel Vérité Discussion: https://postgr.es/m/9b4ea968-753f-4b5f-b46c-d7d3bf7c8f90@manitou-mail.org
1 parent 130ae4a commit 5dbc5da

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

src/bin/psql/command.c

+19-6
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_bra
106106
const char *cmd);
107107
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch);
108108
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch,
109-
PQExpBuffer query_buf);
109+
PQExpBuffer query_buf, PQExpBuffer previous_buf);
110110
static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch);
111111
static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch,
112112
const char *cmd);
@@ -362,7 +362,8 @@ exec_command(const char *cmd,
362362
else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
363363
status = exec_command_out(scan_state, active_branch);
364364
else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
365-
status = exec_command_print(scan_state, active_branch, query_buf);
365+
status = exec_command_print(scan_state, active_branch,
366+
query_buf, previous_buf);
366367
else if (strcmp(cmd, "password") == 0)
367368
status = exec_command_password(scan_state, active_branch);
368369
else if (strcmp(cmd, "prompt") == 0)
@@ -955,7 +956,7 @@ exec_command_edit(PsqlScanState scan_state, bool active_branch,
955956
if (fname)
956957
canonicalize_path(fname);
957958

958-
/* Applies to previous query if current buffer is empty */
959+
/* If query_buf is empty, recall previous query for editing */
959960
copy_previous_query(query_buf, previous_buf);
960961

961962
if (do_edit(fname, query_buf, lineno, NULL))
@@ -1827,12 +1828,19 @@ exec_command_out(PsqlScanState scan_state, bool active_branch)
18271828
*/
18281829
static backslashResult
18291830
exec_command_print(PsqlScanState scan_state, bool active_branch,
1830-
PQExpBuffer query_buf)
1831+
PQExpBuffer query_buf, PQExpBuffer previous_buf)
18311832
{
18321833
if (active_branch)
18331834
{
1835+
/*
1836+
* We want to print the same thing \g would execute, but not to change
1837+
* the query buffer state; so we can't use copy_previous_query().
1838+
* Also, beware of possibility that buffer pointers are NULL.
1839+
*/
18341840
if (query_buf && query_buf->len > 0)
18351841
puts(query_buf->data);
1842+
else if (previous_buf && previous_buf->len > 0)
1843+
puts(previous_buf->data);
18361844
else if (!pset.quiet)
18371845
puts(_("Query buffer is empty."));
18381846
fflush(stdout);
@@ -2549,9 +2557,14 @@ exec_command_write(PsqlScanState scan_state, bool active_branch,
25492557
{
25502558
int result;
25512559

2560+
/*
2561+
* We want to print the same thing \g would execute, but not to
2562+
* change the query buffer state; so we can't use
2563+
* copy_previous_query(). Also, beware of possibility that buffer
2564+
* pointers are NULL.
2565+
*/
25522566
if (query_buf && query_buf->len > 0)
25532567
fprintf(fd, "%s\n", query_buf->data);
2554-
/* Applies to previous query if current buffer is empty */
25552568
else if (previous_buf && previous_buf->len > 0)
25562569
fprintf(fd, "%s\n", previous_buf->data);
25572570

@@ -2602,7 +2615,7 @@ exec_command_watch(PsqlScanState scan_state, bool active_branch,
26022615
free(opt);
26032616
}
26042617

2605-
/* Applies to previous query if current buffer is empty */
2618+
/* If query_buf is empty, recall and execute previous query */
26062619
copy_previous_query(query_buf, previous_buf);
26072620

26082621
success = do_watch(query_buf, sleep);

src/test/regress/expected/psql.out

+32
Original file line numberDiff line numberDiff line change
@@ -2932,3 +2932,35 @@ NOTICE: foo
29322932
CONTEXT: PL/pgSQL function inline_code_block line 3 at RAISE
29332933
ERROR: bar
29342934
CONTEXT: PL/pgSQL function inline_code_block line 4 at RAISE
2935+
-- test printing and clearing the query buffer
2936+
SELECT 1;
2937+
?column?
2938+
----------
2939+
1
2940+
(1 row)
2941+
2942+
\p
2943+
SELECT 1;
2944+
SELECT 2 \r
2945+
\p
2946+
SELECT 1;
2947+
SELECT 3 \p
2948+
SELECT 3
2949+
UNION SELECT 4 \p
2950+
SELECT 3
2951+
UNION SELECT 4
2952+
UNION SELECT 5
2953+
ORDER BY 1;
2954+
?column?
2955+
----------
2956+
3
2957+
4
2958+
5
2959+
(3 rows)
2960+
2961+
\r
2962+
\p
2963+
SELECT 3
2964+
UNION SELECT 4
2965+
UNION SELECT 5
2966+
ORDER BY 1;

src/test/regress/sql/psql.sql

+12
Original file line numberDiff line numberDiff line change
@@ -548,3 +548,15 @@ begin
548548
raise notice 'foo';
549549
raise exception 'bar';
550550
end $$;
551+
552+
-- test printing and clearing the query buffer
553+
SELECT 1;
554+
\p
555+
SELECT 2 \r
556+
\p
557+
SELECT 3 \p
558+
UNION SELECT 4 \p
559+
UNION SELECT 5
560+
ORDER BY 1;
561+
\r
562+
\p

0 commit comments

Comments
 (0)